554 lines
19 KiB
C#
554 lines
19 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using LightlessSync.API.Data;
|
|
using LightlessSync.API.Data.Enum;
|
|
using LightlessSync.API.Data.Extensions;
|
|
using LightlessSync.API.Dto.CharaData;
|
|
using LightlessSync.API.Dto.Group;
|
|
using LightlessSync.API.Dto.User;
|
|
using LightlessSync.LightlessConfiguration;
|
|
using LightlessSync.LightlessConfiguration.Models;
|
|
using LightlessSync.Services.Mediator;
|
|
using LightlessSync.Services.Events;
|
|
using LightlessSync.Services.ServerConfiguration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LightlessSync.PlayerData.Pairs;
|
|
|
|
public sealed class PairCoordinator : MediatorSubscriberBase
|
|
{
|
|
private readonly ILogger<PairCoordinator> _logger;
|
|
private readonly LightlessConfigService _configService;
|
|
private readonly LightlessMediator _mediator;
|
|
private readonly PairHandlerRegistry _handlerRegistry;
|
|
private readonly PairManager _pairManager;
|
|
private readonly PairLedger _pairLedger;
|
|
private readonly ServerConfigurationManager _serverConfigurationManager;
|
|
private readonly ConcurrentDictionary<string, OnlineUserCharaDataDto> _pendingCharacterData = new(StringComparer.Ordinal);
|
|
|
|
public PairCoordinator(
|
|
ILogger<PairCoordinator> logger,
|
|
LightlessConfigService configService,
|
|
LightlessMediator mediator,
|
|
PairHandlerRegistry handlerRegistry,
|
|
PairManager pairManager,
|
|
PairLedger pairLedger,
|
|
ServerConfigurationManager serverConfigurationManager)
|
|
: base(logger, mediator)
|
|
{
|
|
_logger = logger;
|
|
_configService = configService;
|
|
_mediator = mediator;
|
|
_handlerRegistry = handlerRegistry;
|
|
_pairManager = pairManager;
|
|
_pairLedger = pairLedger;
|
|
_serverConfigurationManager = serverConfigurationManager;
|
|
|
|
mediator.Subscribe<ActiveServerChangedMessage>(this, msg => HandleActiveServerChange(msg.ServerUrl));
|
|
mediator.Subscribe<DisconnectedMessage>(this, _ => HandleDisconnected());
|
|
}
|
|
|
|
internal PairLedger Ledger => _pairLedger;
|
|
|
|
private void PublishPairDataChanged(bool groupChanged = false)
|
|
{
|
|
_mediator.Publish(new RefreshUiMessage());
|
|
_mediator.Publish(new PairDataChangedMessage());
|
|
if (groupChanged)
|
|
{
|
|
_mediator.Publish(new GroupCollectionChangedMessage());
|
|
}
|
|
}
|
|
|
|
private void NotifyUserOnline(PairConnection? connection, bool sendNotification)
|
|
{
|
|
if (connection is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var config = _configService.Current;
|
|
if (config.ShowOnlineNotifications && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Pair {Uid} marked online", connection.User.UID);
|
|
}
|
|
|
|
if (!sendNotification || !config.ShowOnlineNotifications)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (config.ShowOnlineNotificationsOnlyForIndividualPairs &&
|
|
(!connection.IsDirectlyPaired || connection.IsOneSided))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var note = _serverConfigurationManager.GetNoteForUid(connection.User.UID);
|
|
if (config.ShowOnlineNotificationsOnlyForNamedPairs &&
|
|
string.IsNullOrEmpty(note))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var message = !string.IsNullOrEmpty(note)
|
|
? $"{note} ({connection.User.AliasOrUID}) is now online"
|
|
: $"{connection.User.AliasOrUID} is now online";
|
|
|
|
_mediator.Publish(new NotificationMessage("User online", message, NotificationType.Info, TimeSpan.FromSeconds(5)));
|
|
}
|
|
|
|
private void ReapplyLastKnownData(string userId, string ident, bool forced = false)
|
|
{
|
|
var result = _handlerRegistry.ApplyLastReceivedData(new PairUniqueIdentifier(userId), ident, forced);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to reapply cached data for {Uid}: {Error}", userId, result.Error);
|
|
}
|
|
}
|
|
|
|
public void HandleGroupChangePermissions(GroupPermissionDto dto)
|
|
{
|
|
var result = _pairManager.UpdateGroupPermissions(dto);
|
|
if (!result.Success)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update permissions for group {GroupId}: {Error}", dto.Group.GID, result.Error);
|
|
}
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
|
|
public void HandleGroupFullInfo(GroupFullInfoDto dto)
|
|
{
|
|
var result = _pairManager.AddGroup(dto);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to add group {GroupId}: {Error}", dto.Group.GID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
|
|
public void HandleGroupPairJoined(GroupPairFullInfoDto dto)
|
|
{
|
|
var result = _pairManager.AddOrUpdateGroupPair(dto);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to add group pair {Uid}/{Group}: {Error}", dto.User.UID, dto.Group.GID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
|
|
private void HandleActiveServerChange(string serverUrl)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Active server changed to {Server}", serverUrl);
|
|
}
|
|
|
|
ResetPairState();
|
|
}
|
|
|
|
private void HandleDisconnected()
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Lightless disconnected, clearing pair state");
|
|
}
|
|
|
|
ResetPairState();
|
|
}
|
|
|
|
private void ResetPairState()
|
|
{
|
|
_handlerRegistry.ResetAllHandlers();
|
|
_pairManager.ClearAll();
|
|
_pendingCharacterData.Clear();
|
|
_mediator.Publish(new ClearProfileUserDataMessage());
|
|
_mediator.Publish(new ClearProfileGroupDataMessage());
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
|
|
public void HandleGroupPairLeft(GroupPairDto dto)
|
|
{
|
|
var deregistration = _pairManager.RemoveGroupPair(dto);
|
|
if (deregistration.Success && deregistration.Value is { } registration && registration.CharacterIdent is not null)
|
|
{
|
|
_ = _handlerRegistry.DeregisterOfflinePair(registration, forceDisposal: true);
|
|
}
|
|
else if (!deregistration.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("RemoveGroupPair failed for {Uid}: {Error}", dto.User.UID, deregistration.Error);
|
|
}
|
|
|
|
if (deregistration.Success)
|
|
{
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
}
|
|
|
|
public void HandleGroupRemoved(GroupDto dto)
|
|
{
|
|
var removalResult = _pairManager.RemoveGroup(dto.Group.GID);
|
|
if (removalResult.Success)
|
|
{
|
|
foreach (var registration in removalResult.Value)
|
|
{
|
|
if (registration.CharacterIdent is not null)
|
|
{
|
|
_ = _handlerRegistry.DeregisterOfflinePair(registration, forceDisposal: true);
|
|
}
|
|
}
|
|
}
|
|
else if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to remove group {Group}: {Error}", dto.Group.GID, removalResult.Error);
|
|
}
|
|
|
|
if (removalResult.Success)
|
|
{
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
}
|
|
|
|
public void HandleGroupInfoUpdate(GroupInfoDto dto)
|
|
{
|
|
var result = _pairManager.UpdateGroupInfo(dto);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update group info for {Group}: {Error}", dto.Group.GID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
|
|
public void HandleGroupPairPermissions(GroupPairUserPermissionDto dto)
|
|
{
|
|
var result = _pairManager.UpdateGroupPairPermissions(dto);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update group pair permissions for {Group}: {Error}", dto.Group.GID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
|
|
public void HandleGroupPairStatus(GroupPairUserInfoDto dto, bool isSelf)
|
|
{
|
|
PairOperationResult result;
|
|
if (isSelf)
|
|
{
|
|
result = _pairManager.UpdateGroupStatus(dto);
|
|
}
|
|
else
|
|
{
|
|
result = _pairManager.UpdateGroupPairStatus(dto);
|
|
}
|
|
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update group status for {Group}:{Uid}: {Error}", dto.GID, dto.UID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged(groupChanged: true);
|
|
}
|
|
|
|
public void HandleUserAddPair(UserPairDto dto, bool addToLastAddedUser = true)
|
|
{
|
|
var result = _pairManager.AddOrUpdateIndividual(dto, addToLastAddedUser);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to add/update pair {Uid}: {Error}", dto.User.UID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged();
|
|
}
|
|
|
|
public void HandleUserAddPair(UserFullPairDto dto)
|
|
{
|
|
var result = _pairManager.AddOrUpdateIndividual(dto);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to add/update full pair {Uid}: {Error}", dto.User.UID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged();
|
|
}
|
|
|
|
public void HandleUserRemovePair(UserDto dto)
|
|
{
|
|
var removal = _pairManager.RemoveIndividual(dto);
|
|
if (removal.Success && removal.Value is { } registration && registration.CharacterIdent is not null)
|
|
{
|
|
_ = _handlerRegistry.DeregisterOfflinePair(registration, forceDisposal: true);
|
|
}
|
|
else if (!removal.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("RemoveIndividual failed for {Uid}: {Error}", dto.User.UID, removal.Error);
|
|
}
|
|
|
|
if (removal.Success)
|
|
{
|
|
_pendingCharacterData.TryRemove(dto.User.UID, out _);
|
|
PublishPairDataChanged();
|
|
}
|
|
}
|
|
|
|
public void HandleUserStatus(UserIndividualPairStatusDto dto)
|
|
{
|
|
var result = _pairManager.SetIndividualStatus(dto);
|
|
if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update individual pair status for {Uid}: {Error}", dto.User.UID, result.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged();
|
|
}
|
|
|
|
public void HandleUserOnline(OnlineUserIdentDto dto, bool sendNotification)
|
|
{
|
|
var wasOnline = false;
|
|
PairConnection? previousConnection = null;
|
|
if (_pairManager.TryGetPair(dto.User.UID, out var existingConnection))
|
|
{
|
|
previousConnection = existingConnection;
|
|
wasOnline = existingConnection.IsOnline;
|
|
}
|
|
|
|
var registrationResult = _pairManager.MarkOnline(dto);
|
|
if (!registrationResult.Success)
|
|
{
|
|
_logger.LogDebug("MarkOnline failed for {Uid}: {Error}", dto.User.UID, registrationResult.Error);
|
|
return;
|
|
}
|
|
|
|
var registration = registrationResult.Value;
|
|
if (registration.CharacterIdent is null)
|
|
{
|
|
_logger.LogDebug("Online registration for {Uid} missing ident.", dto.User.UID);
|
|
}
|
|
else
|
|
{
|
|
var handlerResult = _handlerRegistry.RegisterOnlinePair(registration);
|
|
if (!handlerResult.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("RegisterOnlinePair failed for {Uid}: {Error}", dto.User.UID, handlerResult.Error);
|
|
}
|
|
}
|
|
|
|
var connectionResult = _pairManager.GetPair(dto.User.UID);
|
|
var connection = connectionResult.Success ? connectionResult.Value : previousConnection;
|
|
if (connection is not null)
|
|
{
|
|
_mediator.Publish(new ClearProfileUserDataMessage(connection.User));
|
|
}
|
|
else
|
|
{
|
|
_mediator.Publish(new ClearProfileUserDataMessage(dto.User));
|
|
}
|
|
|
|
if (!wasOnline)
|
|
{
|
|
NotifyUserOnline(connection, sendNotification);
|
|
}
|
|
|
|
if (registration.CharacterIdent is not null &&
|
|
_pendingCharacterData.TryRemove(dto.User.UID, out var pendingData))
|
|
{
|
|
var pendingRegistration = new PairRegistration(new PairUniqueIdentifier(dto.User.UID), registration.CharacterIdent);
|
|
var pendingApply = _handlerRegistry.ApplyCharacterData(pendingRegistration, pendingData);
|
|
if (!pendingApply.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Applying pending character data for {Uid} failed: {Error}", dto.User.UID, pendingApply.Error);
|
|
}
|
|
}
|
|
|
|
PublishPairDataChanged();
|
|
}
|
|
|
|
public void HandleUserOffline(UserData user)
|
|
{
|
|
var registrationResult = _pairManager.MarkOffline(user);
|
|
if (registrationResult.Success)
|
|
{
|
|
_pendingCharacterData.TryRemove(user.UID, out _);
|
|
if (registrationResult.Value.CharacterIdent is not null)
|
|
{
|
|
_ = _handlerRegistry.DeregisterOfflinePair(registrationResult.Value);
|
|
}
|
|
|
|
_mediator.Publish(new ClearProfileUserDataMessage(user));
|
|
PublishPairDataChanged();
|
|
}
|
|
else if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("MarkOffline failed for {Uid}: {Error}", user.UID, registrationResult.Error);
|
|
}
|
|
}
|
|
|
|
public void HandleUserPermissions(UserPermissionsDto dto)
|
|
{
|
|
var pairResult = _pairManager.GetPair(dto.User.UID);
|
|
if (!pairResult.Success)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Permission update received for unknown pair {Uid}", dto.User.UID);
|
|
}
|
|
return;
|
|
}
|
|
|
|
var connection = pairResult.Value;
|
|
var previous = connection.OtherToSelfPermissions;
|
|
|
|
var updateResult = _pairManager.UpdateOtherPermissions(dto);
|
|
if (!updateResult.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update permissions for {Uid}: {Error}", dto.User.UID, updateResult.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged();
|
|
|
|
if (previous.IsPaused() != dto.Permissions.IsPaused())
|
|
{
|
|
_mediator.Publish(new ClearProfileUserDataMessage(dto.User));
|
|
|
|
if (connection.Ident is not null)
|
|
{
|
|
var pauseResult = _handlerRegistry.SetPausedState(new PairUniqueIdentifier(dto.User.UID), connection.Ident, dto.Permissions.IsPaused());
|
|
if (!pauseResult.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update pause state for {Uid}: {Error}", dto.User.UID, pauseResult.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!connection.IsPaused && connection.Ident is not null)
|
|
{
|
|
ReapplyLastKnownData(dto.User.UID, connection.Ident);
|
|
}
|
|
}
|
|
|
|
public void HandleSelfPermissions(UserPermissionsDto dto)
|
|
{
|
|
var pairResult = _pairManager.GetPair(dto.User.UID);
|
|
if (!pairResult.Success)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Self permission update received for unknown pair {Uid}", dto.User.UID);
|
|
}
|
|
return;
|
|
}
|
|
|
|
var connection = pairResult.Value;
|
|
var previous = connection.SelfToOtherPermissions;
|
|
|
|
var updateResult = _pairManager.UpdateSelfPermissions(dto);
|
|
if (!updateResult.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update self permissions for {Uid}: {Error}", dto.User.UID, updateResult.Error);
|
|
return;
|
|
}
|
|
|
|
PublishPairDataChanged();
|
|
|
|
if (previous.IsPaused() != dto.Permissions.IsPaused())
|
|
{
|
|
_mediator.Publish(new ClearProfileUserDataMessage(dto.User));
|
|
|
|
if (connection.Ident is not null)
|
|
{
|
|
var pauseResult = _handlerRegistry.SetPausedState(new PairUniqueIdentifier(dto.User.UID), connection.Ident, dto.Permissions.IsPaused());
|
|
if (!pauseResult.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to update pause state for {Uid}: {Error}", dto.User.UID, pauseResult.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!connection.IsPaused && connection.Ident is not null)
|
|
{
|
|
ReapplyLastKnownData(dto.User.UID, connection.Ident);
|
|
}
|
|
}
|
|
|
|
public void HandleUploadStatus(UserDto dto)
|
|
{
|
|
var pairResult = _pairManager.GetPair(dto.User.UID);
|
|
if (!pairResult.Success)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Upload status received for unknown pair {Uid}", dto.User.UID);
|
|
}
|
|
return;
|
|
}
|
|
|
|
var connection = pairResult.Value;
|
|
if (connection.Ident is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var setResult = _handlerRegistry.SetUploading(new PairUniqueIdentifier(dto.User.UID), connection.Ident, true);
|
|
if (!setResult.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Failed to set uploading for {Uid}: {Error}", dto.User.UID, setResult.Error);
|
|
}
|
|
}
|
|
|
|
public void HandleCharacterData(OnlineUserCharaDataDto dto)
|
|
{
|
|
var pairResult = _pairManager.GetPair(dto.User.UID);
|
|
if (!pairResult.Success)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Character data received for unknown pair {Uid}, queued for later.", dto.User.UID);
|
|
}
|
|
_pendingCharacterData[dto.User.UID] = dto;
|
|
return;
|
|
}
|
|
|
|
var connection = pairResult.Value;
|
|
_mediator.Publish(new EventMessage(new Event(connection.User, nameof(PairCoordinator), EventSeverity.Informational, "Received Character Data")));
|
|
if (connection.Ident is null)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("Character data received for {Uid} without ident, queued for later.", dto.User.UID);
|
|
}
|
|
_pendingCharacterData[dto.User.UID] = dto;
|
|
return;
|
|
}
|
|
|
|
_pendingCharacterData.TryRemove(dto.User.UID, out _);
|
|
var registration = new PairRegistration(new PairUniqueIdentifier(dto.User.UID), connection.Ident);
|
|
var applyResult = _handlerRegistry.ApplyCharacterData(registration, dto);
|
|
if (!applyResult.Success && _logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
_logger.LogDebug("ApplyCharacterData queued for {Uid}: {Error}", dto.User.UID, applyResult.Error);
|
|
}
|
|
}
|
|
|
|
public void HandleProfile(UserDto dto)
|
|
{
|
|
_mediator.Publish(new ClearProfileUserDataMessage(dto.User));
|
|
}
|
|
}
|