fix: push presence when a player comes back online

OnDisconnectedAsync told friends a player went offline, but nothing ever
pushed the opposite transition — PlayerCameOnline existed in
NotificationService with zero callers. The pair was asymmetric, so a SignalR
reconnect (network blip, replica restart) greyed the player out on every
friend's list for the rest of the session even though they were still
playing. Only re-entering a room, which pushes presence via /goto, brought
them back.

AddConnectionAsync already computed the first-connection transition to
maintain the online set but returned void; it now reports it, mirroring
RemoveConnectionAsync.

The push is deliberately conditional on a room already being known. The
presence payload derives isOnline from roomInstance being non-null, so
pushing with no room reads as OFFLINE — worse than staying quiet. On a fresh
boot there is no room yet and the /goto that follows sends the real presence
anyway, so this only fires on reconnects, where the cached room is exactly
what friends need to see again.
This commit is contained in:
Alexa
2026-07-27 23:40:11 +03:00
parent 327dcb789c
commit 49df7393dc
3 changed files with 41 additions and 7 deletions
@@ -45,7 +45,11 @@ public class OnlinePresenceService
public OnlinePresenceService(IConnectionMultiplexer? redis = null) => _redis = redis;
public async Task AddConnectionAsync(long playerId, string connectionId)
/// <summary>Register one SignalR connection. Returns true when that was the
/// player's FIRST active connection, i.e. they just came online and friends
/// should be told. Mirrors <see cref="RemoveConnectionAsync"/>, which
/// reports the opposite transition.</summary>
public async Task<bool> AddConnectionAsync(long playerId, string connectionId)
{
if (_redis is { } mux)
{
@@ -59,13 +63,21 @@ public class OnlinePresenceService
if (added)
{
var size = await db.SetLengthAsync(connsKey);
if (size == 1) await db.SetAddAsync(OnlineSetKey, playerId);
if (size == 1)
{
await db.SetAddAsync(OnlineSetKey, playerId);
return true;
}
}
return;
return false;
}
var set = _localConns.GetOrAdd(playerId, _ => new HashSet<string>());
lock (set) set.Add(connectionId);
lock (set)
{
set.Add(connectionId);
return set.Count == 1;
}
}
/// <summary>Remove one SignalR connection. Returns true when that was