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>
This commit is contained in:
@@ -127,27 +127,94 @@ internal abstract class GameHandlerBase
|
||||
if (prevStateJson != null)
|
||||
{
|
||||
var prevState = GameEngine.Deserialize(prevStateJson);
|
||||
|
||||
// ── DIAGNOSTIC: log prev/next state to identify why deltas are consistently null ──
|
||||
Console.WriteLine(
|
||||
$"[DELTA] prev: phase={prevState.Phase}({(int)prevState.Phase}) turn={prevState.TurnNumber} " +
|
||||
$"active={prevState.ActivePlayerAccountId} seq={prevState.NextActionSequence}");
|
||||
Console.WriteLine(
|
||||
$"[DELTA] next: phase={state.Phase}({(int)state.Phase}) turn={state.TurnNumber} " +
|
||||
$"active={state.ActivePlayerAccountId} seq={state.NextActionSequence}");
|
||||
Console.WriteLine(
|
||||
$"[DELTA] prevStateJson length={prevStateJson.Length} " +
|
||||
$"sameAsCurrentSession={(prevStateJson == session.StateJson)} " +
|
||||
$"referenceEqual={ReferenceEquals(prevStateJson, session.StateJson)}");
|
||||
// ─────────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
var d1 = GameStateDiffer.ComputeDelta(prevState, state, p1Id);
|
||||
var d2 = GameStateDiffer.ComputeDelta(prevState, state, p2Id);
|
||||
|
||||
var tasks = new List<Task>
|
||||
{
|
||||
clientPush.PushAsync(p1Id,
|
||||
ServerMessage.Push("game.state_delta", new GameStateDeltaPush(d1)), ct),
|
||||
clientPush.PushAsync(p2Id,
|
||||
ServerMessage.Push("game.state_delta", new GameStateDeltaPush(d2)), ct),
|
||||
};
|
||||
Console.WriteLine(
|
||||
$"[DELTA] d1: meta={d1.MetaDelta != null} tiles={d1.ChangedTiles != null} " +
|
||||
$"units+={d1.AddedUnits != null} units-={d1.RemovedUnitIds != null} " +
|
||||
$"hand+={d1.HandAdded != null} ownStats={d1.OwnStatsDelta != null} " +
|
||||
$"oppStats={d1.OpponentStatsDelta != null} ver={d1.StateVersion}");
|
||||
|
||||
var registry = SpectatorRegistryService.Instance;
|
||||
if (registry != null)
|
||||
// Safety fallback: if the differ produces an empty delta (all content fields null),
|
||||
// the client would advance its stateVersion but see no visual changes. Fall back to
|
||||
// a full state_update so clients always reflect the correct post-action state.
|
||||
// This guards against any unknown runtime edge case where ComputeDelta returns nothing.
|
||||
static bool IsEmpty(GameStateDelta d) =>
|
||||
d.MetaDelta == null &&
|
||||
d.ChangedTiles == null &&
|
||||
d.AddedUnits == null &&
|
||||
d.RemovedUnitIds == null &&
|
||||
d.ChangedUnits == null &&
|
||||
d.HandAdded == null &&
|
||||
d.HandRemovedIds == null &&
|
||||
d.DiscardAdded == null &&
|
||||
d.OpponentHandCount == null &&
|
||||
d.OpponentDeckCount == null &&
|
||||
d.OwnArchSpiritDelta == null &&
|
||||
d.OpponentArchSpiritDelta == null &&
|
||||
d.OwnStatsDelta == null &&
|
||||
d.OpponentStatsDelta == null;
|
||||
|
||||
var tasks = new List<Task>();
|
||||
|
||||
if (IsEmpty(d1) || IsEmpty(d2))
|
||||
{
|
||||
var spectators = registry.GetSpectators(session.Id);
|
||||
if (spectators.Count > 0)
|
||||
// At least one player's delta is empty — fall back to full state for both.
|
||||
tasks.Add(clientPush.PushAsync(p1Id,
|
||||
ServerMessage.Push("game.state_update",
|
||||
new GameStateUpdatePush(GameEngine.ToDto(state, p1Id))), ct));
|
||||
tasks.Add(clientPush.PushAsync(p2Id,
|
||||
ServerMessage.Push("game.state_update",
|
||||
new GameStateUpdatePush(GameEngine.ToDto(state, p2Id))), ct));
|
||||
|
||||
// Spectators also get a full push when the delta is suspicious.
|
||||
var regFull = SpectatorRegistryService.Instance;
|
||||
if (regFull != null)
|
||||
{
|
||||
var sd = GameStateDiffer.ComputeSpectatorDelta(prevState, state);
|
||||
foreach (var specId in spectators)
|
||||
tasks.Add(clientPush.PushAsync(specId.ToString(),
|
||||
ServerMessage.Push("game.state_delta", new GameStateDeltaPush(sd)), ct));
|
||||
var spectatorsFull = regFull.GetSpectators(session.Id);
|
||||
if (spectatorsFull.Count > 0)
|
||||
{
|
||||
var spectatorDto = GameEngine.ToDtoSpectator(state);
|
||||
foreach (var specId in spectatorsFull)
|
||||
tasks.Add(clientPush.PushAsync(specId.ToString(),
|
||||
ServerMessage.Push("game.state_update",
|
||||
new GameStateUpdatePush(spectatorDto)), ct));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tasks.Add(clientPush.PushAsync(p1Id,
|
||||
ServerMessage.Push("game.state_delta", new GameStateDeltaPush(d1)), ct));
|
||||
tasks.Add(clientPush.PushAsync(p2Id,
|
||||
ServerMessage.Push("game.state_delta", new GameStateDeltaPush(d2)), ct));
|
||||
|
||||
var registry = SpectatorRegistryService.Instance;
|
||||
if (registry != null)
|
||||
{
|
||||
var spectators = registry.GetSpectators(session.Id);
|
||||
if (spectators.Count > 0)
|
||||
{
|
||||
var sd = GameStateDiffer.ComputeSpectatorDelta(prevState, state);
|
||||
foreach (var specId in spectators)
|
||||
tasks.Add(clientPush.PushAsync(specId.ToString(),
|
||||
ServerMessage.Push("game.state_delta", new GameStateDeltaPush(sd)), ct));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user