Cut over game.state_delta to account-scoped multiplayer schema

This commit is contained in:
Shard
2026-03-27 12:42:57 +00:00
parent f07ea4135a
commit cb024c70e2
5 changed files with 260 additions and 257 deletions
+22 -45
View File
@@ -155,53 +155,35 @@ internal abstract class GameHandlerBase
CancellationToken ct,
string? prevStateJson = null)
{
var p1Id = session.PlayerOneAccountId.ToString();
var p2Id = session.PlayerTwoAccountId.ToString();
if (prevStateJson != null)
{
var prevState = GameEngine.Deserialize(prevStateJson);
var allIds = GetAllPlayerIds(session);
var d1 = GameStateDiffer.ComputeDelta(prevState, state, p1Id);
var d2 = GameStateDiffer.ComputeDelta(prevState, state, p2Id);
// 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;
d.MetaDelta == null &&
d.ChangedTiles == null &&
d.UnitsAdded == null &&
d.UnitsRemoved == null &&
d.UnitsChanged == null &&
d.ArchSpiritByAccount == null &&
d.PlayerStatsByAccount == null &&
d.PublicCountsByAccount == null &&
d.PrivateByViewer == null;
var deltas = allIds.ToDictionary(
pid => pid,
pid => GameStateDiffer.ComputeDelta(prevState, state, pid));
var tasks = new List<Task>();
// Additional players (seats 3+): always receive full-state updates
// (the delta system only computes P1/P2 perspective deltas).
var addlIds = GetAllPlayerIds(session).Skip(2).ToList();
if (IsEmpty(d1) || IsEmpty(d2))
if (deltas.Values.Any(IsEmpty))
{
// 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));
foreach (var pid in allIds)
tasks.Add(clientPush.PushAsync(pid,
ServerMessage.Push("game.state_update",
new GameStateUpdatePush(GameEngine.ToDto(state, pid))), ct));
// Spectators also get a full push when the delta is suspicious.
var regFull = SpectatorRegistryService.Instance;
if (regFull != null)
{
@@ -218,10 +200,9 @@ internal abstract class GameHandlerBase
}
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));
foreach (var (pid, delta) in deltas)
tasks.Add(clientPush.PushAsync(pid,
ServerMessage.Push("game.state_delta", new GameStateDeltaPush(delta)), ct));
var registry = SpectatorRegistryService.Instance;
if (registry != null)
@@ -236,10 +217,6 @@ internal abstract class GameHandlerBase
}
}
}
foreach (var aid in addlIds)
tasks.Add(clientPush.PushAsync(aid,
ServerMessage.Push("game.state_update",
new GameStateUpdatePush(GameEngine.ToDto(state, aid))), ct));
await Task.WhenAll(tasks);
}