Update 0.1.8.2 - Correctif UI, Correctif Nearby CompactUI, Add changelog
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MareSynchronos.WebAPI.AutoDetect;
|
||||
using MareSynchronos.MareConfiguration;
|
||||
@@ -15,6 +17,11 @@ public class AutoDetectRequestService
|
||||
private readonly MareConfigService _configService;
|
||||
private readonly DalamudUtilService _dalamud;
|
||||
private readonly MareMediator _mediator;
|
||||
private readonly object _syncRoot = new();
|
||||
private readonly Dictionary<string, DateTime> _activeCooldowns = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, RefusalTracker> _refusalTrackers = new(StringComparer.Ordinal);
|
||||
private static readonly TimeSpan RequestCooldown = TimeSpan.FromMinutes(5);
|
||||
private static readonly TimeSpan RefusalLockDuration = TimeSpan.FromMinutes(15);
|
||||
|
||||
public AutoDetectRequestService(ILogger<AutoDetectRequestService> logger, DiscoveryConfigProvider configProvider, DiscoveryApiClient client, MareConfigService configService, MareMediator mediator, DalamudUtilService dalamudUtilService)
|
||||
{
|
||||
@@ -26,7 +33,7 @@ public class AutoDetectRequestService
|
||||
_dalamud = dalamudUtilService;
|
||||
}
|
||||
|
||||
public async Task<bool> SendRequestAsync(string token, CancellationToken ct = default)
|
||||
public async Task<bool> SendRequestAsync(string token, string? uid = null, string? targetDisplayName = null, CancellationToken ct = default)
|
||||
{
|
||||
if (!_configService.Current.AllowAutoDetectPairRequests)
|
||||
{
|
||||
@@ -34,6 +41,47 @@ public class AutoDetectRequestService
|
||||
_mediator.Publish(new NotificationMessage("Nearby request blocked", "Enable 'Allow pair requests' in Settings to send requests.", NotificationType.Info));
|
||||
return false;
|
||||
}
|
||||
var targetKey = BuildTargetKey(uid, token, targetDisplayName);
|
||||
if (!string.IsNullOrEmpty(targetKey))
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
lock (_syncRoot)
|
||||
{
|
||||
if (_refusalTrackers.TryGetValue(targetKey, out var tracker))
|
||||
{
|
||||
if (tracker.LockUntil.HasValue && tracker.LockUntil.Value > now)
|
||||
{
|
||||
PublishLockNotification(tracker.LockUntil.Value - now);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tracker.LockUntil.HasValue && tracker.LockUntil.Value <= now)
|
||||
{
|
||||
tracker.LockUntil = null;
|
||||
tracker.Count = 0;
|
||||
if (tracker.Count == 0 && tracker.LockUntil == null)
|
||||
{
|
||||
_refusalTrackers.Remove(targetKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_activeCooldowns.TryGetValue(targetKey, out var lastSent))
|
||||
{
|
||||
var elapsed = now - lastSent;
|
||||
if (elapsed < RequestCooldown)
|
||||
{
|
||||
PublishCooldownNotification(RequestCooldown - elapsed);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (elapsed >= RequestCooldown)
|
||||
{
|
||||
_activeCooldowns.Remove(targetKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var endpoint = _configProvider.RequestEndpoint;
|
||||
if (string.IsNullOrEmpty(endpoint))
|
||||
{
|
||||
@@ -53,10 +101,51 @@ public class AutoDetectRequestService
|
||||
var ok = await _client.SendRequestAsync(endpoint!, token, displayName, ct).ConfigureAwait(false);
|
||||
if (ok)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(targetKey))
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
_activeCooldowns[targetKey] = DateTime.UtcNow;
|
||||
if (_refusalTrackers.TryGetValue(targetKey, out var tracker))
|
||||
{
|
||||
tracker.Count = 0;
|
||||
tracker.LockUntil = null;
|
||||
if (tracker.Count == 0 && tracker.LockUntil == null)
|
||||
{
|
||||
_refusalTrackers.Remove(targetKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_mediator.Publish(new NotificationMessage("Nearby request sent", "The other user will receive a request notification.", NotificationType.Info));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!string.IsNullOrEmpty(targetKey))
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
lock (_syncRoot)
|
||||
{
|
||||
_activeCooldowns.Remove(targetKey);
|
||||
if (!_refusalTrackers.TryGetValue(targetKey, out var tracker))
|
||||
{
|
||||
tracker = new RefusalTracker();
|
||||
_refusalTrackers[targetKey] = tracker;
|
||||
}
|
||||
|
||||
if (tracker.LockUntil.HasValue && tracker.LockUntil.Value <= now)
|
||||
{
|
||||
tracker.LockUntil = null;
|
||||
tracker.Count = 0;
|
||||
}
|
||||
|
||||
tracker.Count++;
|
||||
if (tracker.Count >= 3)
|
||||
{
|
||||
tracker.LockUntil = now.Add(RefusalLockDuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
_mediator.Publish(new NotificationMessage("Nearby request failed", "The server rejected the request. Try again soon.", NotificationType.Warning));
|
||||
}
|
||||
return ok;
|
||||
@@ -80,4 +169,42 @@ public class AutoDetectRequestService
|
||||
_logger.LogInformation("Nearby: sending accept notify via {endpoint}", endpoint);
|
||||
return await _client.SendAcceptAsync(endpoint!, targetUid, displayName, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static string? BuildTargetKey(string? uid, string? token, string? displayName)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(uid)) return "uid:" + uid;
|
||||
if (!string.IsNullOrEmpty(token)) return "token:" + token;
|
||||
if (!string.IsNullOrEmpty(displayName)) return "name:" + displayName;
|
||||
return null;
|
||||
}
|
||||
|
||||
private void PublishCooldownNotification(TimeSpan remaining)
|
||||
{
|
||||
var durationText = FormatDuration(remaining);
|
||||
_mediator.Publish(new NotificationMessage("Nearby request en attente", $"Nearby request déjà envoyée. Merci d'attendre environ {durationText} avant de réessayer.", NotificationType.Info, TimeSpan.FromSeconds(5)));
|
||||
}
|
||||
|
||||
private void PublishLockNotification(TimeSpan remaining)
|
||||
{
|
||||
var durationText = FormatDuration(remaining);
|
||||
_mediator.Publish(new NotificationMessage("Nearby request bloquée", $"Nearby request bloquée après plusieurs refus. Réessayez dans {durationText}.", NotificationType.Warning, TimeSpan.FromSeconds(5)));
|
||||
}
|
||||
|
||||
private static string FormatDuration(TimeSpan remaining)
|
||||
{
|
||||
if (remaining.TotalMinutes >= 1)
|
||||
{
|
||||
var minutes = Math.Max(1, (int)Math.Ceiling(remaining.TotalMinutes));
|
||||
return minutes == 1 ? "1 minute" : minutes + " minutes";
|
||||
}
|
||||
|
||||
var seconds = Math.Max(1, (int)Math.Ceiling(remaining.TotalSeconds));
|
||||
return seconds == 1 ? "1 seconde" : seconds + " secondes";
|
||||
}
|
||||
|
||||
private sealed class RefusalTracker
|
||||
{
|
||||
public int Count;
|
||||
public DateTime? LockUntil;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user