using AutoMapper; using LANCommander.SDK.Rpc.Client; using LANCommander.SDK.Rpc.Server; using LANCommander.SDK.Services; using LANCommander.Server.Services; using LANCommander.Server.Services.PowerShell; using Microsoft.AspNetCore.SignalR; using ZiggyCreatures.Caching.Fusion; namespace LANCommander.Server.Hubs; public partial class RpcHub( IFusionCache cache, IMapper mapper, ILogger logger, ServerService serverService, GameService gameService, ScriptDebugger scriptDebugger, ScriptClient scriptClient) : Hub, IRpcHub { private string GetConnectionsCacheKey(string userIdentifier) => $"RPC/Connections/{userIdentifier}"; public override async Task OnConnectedAsync() { var cacheKey = GetConnectionsCacheKey(Context.UserIdentifier); var connections = await cache.TryGetAsync>(cacheKey); connections = connections.HasValue ? new List(connections.Value) : new List(); connections.Value.RemoveAll(c => c == Context.ConnectionId); connections.Value.Add(Context.ConnectionId); await cache.SetAsync(cacheKey, connections.Value); await base.OnConnectedAsync(); } public override async Task OnDisconnectedAsync(Exception? exception) { var cacheKey = GetConnectionsCacheKey(Context.UserIdentifier); var connections = await cache.TryGetAsync>(cacheKey); if (connections.HasValue) { connections.Value.RemoveAll(c => c == Context.ConnectionId); await cache.SetAsync(cacheKey, connections.Value); } } }