Files
sor/Server/src/SOR.Game/Handlers/GameConcedeHandler.cs
T
d27c7f67c3 Debug delta diffing: add diagnostics + empty-delta fallback
- All game action handlers: move ToDto() call to after SaveStateAsync()
  so the ACK's StateVersion matches the pushed delta (stale-version fix)
- GameHandlerBase.PushStateToPlayersAsync: add empty-delta safety fallback
  — when ComputeDelta() returns all-null content fields, fall back to a
  full game.state_update push so clients always see the correct state
- Add Console.WriteLine diagnostics to PushStateToPlayersAsync to expose
  prev/next phase, turn, active player, and whether prevStateJson matches
  the post-mutation session.StateJson (root cause investigation)
- Game.gd: wire game.activity push to game_activity signal

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-03-21 21:52:45 -04:00

79 lines
2.9 KiB
C#

namespace SOR.Game.Handlers;
using System.Text.Json;
using SOR.Database.Contexts;
using SOR.Database.Enums;
using SOR.Networking.Abstractions.EventBus;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Push;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
/// <summary>
/// game.concede — the calling player forfeits the match.
/// </summary>
internal sealed class GameConcedeHandler : GameHandlerBase, IMessageHandler
{
private readonly IMessageSerializer _serializer;
private readonly GameDbContext _gameDb;
private readonly SorIdentityDbContext _identityDb;
private readonly IClientPush _clientPush;
private readonly IEventBus _eventBus;
public GameConcedeHandler(
IMessageSerializer serializer,
GameDbContext gameDb,
SorIdentityDbContext identityDb,
IClientPush clientPush,
IEventBus eventBus)
{
_serializer = serializer;
_gameDb = gameDb;
_identityDb = identityDb;
_clientPush = clientPush;
_eventBus = eventBus;
}
public string MessageType => "game.concede";
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken ct)
{
var req = _serializer.DeserializePayload<GameConcedeRequest>(message.Payload);
var actorId = connection.UserId;
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
var state = GameEngine.Deserialize(session.StateJson!);
var newState = GameEngine.Concede(state, actorId);
var winnerId = session.PlayerOneAccountId.ToString() == actorId
? session.PlayerTwoAccountId
: session.PlayerOneAccountId;
// Set completion fields BEFORE SaveStateAsync so they are committed atomically
// with the StateJson update. This prevents the race window where StateJson has
// Phase=GameOver but Status is still Active (causing ghost-match on next login).
session.Status = GameSessionStatus.Completed;
session.WinnerAccountId = winnerId;
session.WinCondition = "concede";
session.EndedAt = DateTimeOffset.UtcNow;
await SaveStateAsync(_gameDb, session, newState, actorId, "game.concede",
JsonSerializer.Serialize(req), ct);
var dto = GameEngine.ToDto(newState, actorId);
await FinalizeGameAsync(
_gameDb, _identityDb, _clientPush, _eventBus,
session,
winnerAccountId: winnerId,
loserAccountId: Guid.Parse(actorId),
winCondition: "concede",
ct);
return ServerMessage.Reply("game.concede.ack", message.Seq,
new GameConcedeResponse(dto));
}
}