mirror of
https://github.com/DorkSquadRR/DorkNet.git
synced 2026-10-26 15:38:45 -07:00
Refactor presence tracking: replace OnlinePresenceService with PlayerPresenceService for improved activity management and streamline message handling logic
This commit is contained in:
@@ -402,7 +402,7 @@ public class GoToController(
|
||||
var invite = await db.Messages.FirstOrDefaultAsync(m =>
|
||||
m.Id == inviteId
|
||||
&& m.RecipientPlayerId == callerId.Value
|
||||
&& (m.Type == 6 || m.Type == 7));
|
||||
&& (m.Type == 6 || m.Type == 7 || m.Type == -6 || m.Type == -7));
|
||||
|
||||
long? roomInstanceId = null;
|
||||
if (invite is not null && TryParseRoomInstanceId(invite.Body, out var parsed))
|
||||
@@ -413,12 +413,12 @@ public class GoToController(
|
||||
// parallel — verified in the live server log:
|
||||
// 09:53:47.161Z DELETE messageIds=[60]
|
||||
// 09:53:47.245Z /goto/invite/60 → [goto-invite] invite 60 not found
|
||||
// DELETE usually wins the race, so the message row is gone
|
||||
// by the time we run FirstOrDefaultAsync. The
|
||||
// (instance, player) → LatestInviteMessageId mapping that
|
||||
// SendInvite writes onto the PrivateInstanceInviteeEntity
|
||||
// survives the message deletion and lets us recover
|
||||
// roomInstanceId here.
|
||||
// Current deletes tombstone invite rows so this should only
|
||||
// be needed for invite rows that were hard-deleted by older
|
||||
// builds. The (instance, player) → LatestInviteMessageId
|
||||
// mapping that SendInvite writes onto the
|
||||
// PrivateInstanceInviteeEntity survives those old deletions
|
||||
// and lets us recover roomInstanceId here.
|
||||
if (roomInstanceId is null)
|
||||
{
|
||||
var inviteeRoomInstance = await db.PrivateInstanceInvitees
|
||||
@@ -741,6 +741,7 @@ public class GoToController(
|
||||
}
|
||||
|
||||
presence.SetRoom(playerId, targetRoom);
|
||||
presence.MarkActive(playerId);
|
||||
|
||||
// Real visit tracking: upsert the per-(room, player) row so
|
||||
// we get distinct VisitorCount (unique players ever) AND the
|
||||
|
||||
@@ -56,6 +56,7 @@ public class MatchController(
|
||||
if (completed.HasValue)
|
||||
{
|
||||
presence.SetRoom(pid, completed.Value.TargetRoom);
|
||||
presence.MarkActive(pid);
|
||||
logger.LogInformation(
|
||||
"[joinresult] committed presence player={Player} room={RoomId}/{RoomName} instance={Instance} deferred={Deferred}",
|
||||
pid,
|
||||
@@ -64,6 +65,11 @@ public class MatchController(
|
||||
completed.Value.TargetRoom.RoomInstanceId,
|
||||
completed.Value.DeferPresenceCommit);
|
||||
}
|
||||
else
|
||||
{
|
||||
presence.MarkActive(pid);
|
||||
presence.TouchRoom(pid);
|
||||
}
|
||||
logger.LogInformation(
|
||||
"[joinresult] player={Player} instance={Instance} OK ({Label})",
|
||||
pid, instanceId, label);
|
||||
|
||||
@@ -128,6 +128,11 @@ public class MatchPlayerController(
|
||||
// an HTTP 401 mid-heartbeat.
|
||||
var playerId = TryGetCurrentPlayerId();
|
||||
var room = await GetPresenceRoomForResponseAsync(playerId);
|
||||
if (playerId != 0)
|
||||
{
|
||||
presence.MarkActive(playerId);
|
||||
if (room is not null) presence.TouchRoom(playerId);
|
||||
}
|
||||
return Ok(new PresenceDto
|
||||
{
|
||||
PlayerId = playerId,
|
||||
@@ -273,6 +278,7 @@ public class MatchPlayerController(
|
||||
"[player-heartbeat] synthesised dorm fallback player={PlayerId} room={RoomId} dataBlob={DataBlob} (presence cache miss)",
|
||||
playerId, fallback.RoomId, fallback.DataBlob);
|
||||
presence.SetRoom(playerId, fallback);
|
||||
presence.MarkActive(playerId);
|
||||
return fallback;
|
||||
}
|
||||
log.LogInformation("[player-heartbeat] player={PlayerId} room=null", playerId);
|
||||
@@ -291,6 +297,8 @@ public class MatchPlayerController(
|
||||
presence.SetRoom(playerId, room);
|
||||
}
|
||||
}
|
||||
presence.MarkActive(playerId);
|
||||
presence.TouchRoom(playerId);
|
||||
|
||||
log.LogInformation(
|
||||
"[player-heartbeat] player={PlayerId} room={RoomId} instance={InstanceId} subRoom={SubRoomId} location={Location} photon={PhotonRoomId} dataBlob={DataBlob}",
|
||||
|
||||
@@ -36,8 +36,7 @@ namespace DorkNet.Server.Controllers.Match;
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
public class PlayerPresenceController(
|
||||
PlayerPresenceService presence,
|
||||
OnlinePresenceService onlinePresence) : ControllerBase
|
||||
PlayerPresenceService presence) : ControllerBase
|
||||
{
|
||||
[HttpGet("/player")]
|
||||
public ActionResult<List<PlayerPresenceDto>> GetPresences()
|
||||
@@ -83,19 +82,19 @@ public class PlayerPresenceController(
|
||||
// privacy concern actually applies.
|
||||
var callerId = this.CurrentPlayerId();
|
||||
var callerRoom = callerId is long cid ? presence.GetRoom(cid) : null;
|
||||
var onlineIds = onlinePresence.OnlinePlayerIds().ToHashSet();
|
||||
var result = ids.Select(id =>
|
||||
{
|
||||
var room = presence.GetRoom(id);
|
||||
// Other players need both a room presence and an active
|
||||
// notify connection. Presence keys intentionally live for
|
||||
// minutes to survive long room loads, so using them alone
|
||||
// leaves friends stuck online after a graceful disconnect.
|
||||
// For self, keep using room presence: boot-time bulk probes
|
||||
// can race the SignalR connection, and returning self as
|
||||
// offline there can desync the heartbeat cache.
|
||||
// live signal. Use the recent-heartbeat marker as the
|
||||
// truth source: SignalR can drop while the player is still
|
||||
// heartbeating, or linger after the game is gone. Presence
|
||||
// keys intentionally live for minutes to survive long room
|
||||
// loads, so using them alone leaves friends stuck online
|
||||
// after a graceful disconnect.
|
||||
var isSelf = callerId == id;
|
||||
var isOnline = room is not null && (isSelf || onlineIds.Contains(id));
|
||||
var active = isSelf || presence.IsRecentlyActive(id);
|
||||
var isOnline = room is not null && active;
|
||||
return new PlayerPresenceDto
|
||||
{
|
||||
PlayerId = (int)id,
|
||||
|
||||
Reference in New Issue
Block a user