Cut over game.state_delta to account-scoped multiplayer schema
This commit is contained in:
@@ -15,23 +15,24 @@ internal static class GameStateDiffer
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Compute a viewer-specific delta (own hand visible; opponent hand = count only).
|
||||
/// Compute a viewer-specific delta (private hand/discard only for the viewer;
|
||||
/// all public data keyed by account ID).
|
||||
/// </summary>
|
||||
public static GameStateDelta ComputeDelta(GameState prev, GameState next, string viewerAccountId)
|
||||
{
|
||||
var changedTiles = DiffTiles(prev, next);
|
||||
var (addedUnits, removedIds, changed) = DiffAllFieldUnits(prev, next);
|
||||
var changedTiles = DiffTiles(prev, next);
|
||||
var (unitsAdded, unitsRemoved, unitsChanged) = DiffAllFieldUnits(prev, next);
|
||||
|
||||
// Viewer's zones
|
||||
prev.Players.TryGetValue(viewerAccountId, out var prevViewer);
|
||||
next.Players.TryGetValue(viewerAccountId, out var nextViewer);
|
||||
|
||||
List<CardInstanceDto>? handAdded = null;
|
||||
List<string>? handRemovedIds = null;
|
||||
List<CardInstanceDto>? discardAdded = null;
|
||||
|
||||
PrivateViewerDelta? privateByViewer = null;
|
||||
if (prevViewer != null && nextViewer != null)
|
||||
{
|
||||
List<CardInstanceDto>? handAdded = null;
|
||||
List<string>? handRemovedIds = null;
|
||||
List<CardInstanceDto>? discardAdded = null;
|
||||
|
||||
var prevHandIds = new HashSet<string>(prevViewer.Hand.Select(c => c.InstanceId));
|
||||
var nextHandIds = new HashSet<string>(nextViewer.Hand.Select(c => c.InstanceId));
|
||||
|
||||
@@ -46,112 +47,58 @@ internal static class GameStateDiffer
|
||||
if (removed.Count > 0)
|
||||
handRemovedIds = removed;
|
||||
|
||||
// Discard: only appended; emit new tail entries
|
||||
if (nextViewer.Discard.Count > prevViewer.Discard.Count)
|
||||
discardAdded = nextViewer.Discard
|
||||
.Skip(prevViewer.Discard.Count)
|
||||
.Select(GameEngine.ToCardDto)
|
||||
.ToList();
|
||||
|
||||
if (handAdded != null || handRemovedIds != null || discardAdded != null)
|
||||
privateByViewer = new PrivateViewerDelta(handAdded, handRemovedIds, discardAdded);
|
||||
}
|
||||
|
||||
// Opponent's counts
|
||||
var prevOpp = prev.Players.Values.FirstOrDefault(p => p.AccountId != viewerAccountId);
|
||||
var nextOpp = next.Players.Values.FirstOrDefault(p => p.AccountId != viewerAccountId);
|
||||
|
||||
int? oppHandCount = null;
|
||||
int? oppDeckCount = null;
|
||||
|
||||
if (prevOpp != null && nextOpp != null)
|
||||
{
|
||||
if (prevOpp.Hand.Count != nextOpp.Hand.Count) oppHandCount = nextOpp.Hand.Count;
|
||||
if (prevOpp.Deck.Count != nextOpp.Deck.Count) oppDeckCount = nextOpp.Deck.Count;
|
||||
}
|
||||
|
||||
// ArchSpirits
|
||||
UnitDelta? ownArchDelta = null;
|
||||
UnitDelta? oppArchDelta = null;
|
||||
|
||||
if (prevViewer?.ArchSpirit != null && nextViewer?.ArchSpirit != null)
|
||||
ownArchDelta = DiffUnit(prevViewer.ArchSpirit, nextViewer.ArchSpirit);
|
||||
if (prevOpp?.ArchSpirit != null && nextOpp?.ArchSpirit != null)
|
||||
oppArchDelta = DiffUnit(prevOpp.ArchSpirit, nextOpp.ArchSpirit);
|
||||
|
||||
// Player stats
|
||||
PlayerStatsDelta? ownStats = null;
|
||||
PlayerStatsDelta? oppStats = null;
|
||||
|
||||
if (prevViewer != null && nextViewer != null)
|
||||
ownStats = DiffPlayerStats(prevViewer, nextViewer);
|
||||
if (prevOpp != null && nextOpp != null)
|
||||
oppStats = DiffPlayerStats(prevOpp, nextOpp);
|
||||
|
||||
var archByAccount = DiffArchByAccount(prev, next);
|
||||
var statsByAccount = DiffStatsByAccount(prev, next);
|
||||
var countsByAccount = DiffPublicCountsByAccount(prev, next, viewerAccountId);
|
||||
var metaDelta = DiffMeta(prev, next);
|
||||
|
||||
return new GameStateDelta(
|
||||
StateVersion : next.NextActionSequence,
|
||||
ChangedTiles : changedTiles,
|
||||
AddedUnits : addedUnits,
|
||||
RemovedUnitIds : removedIds,
|
||||
ChangedUnits : changed,
|
||||
HandAdded : handAdded,
|
||||
HandRemovedIds : handRemovedIds,
|
||||
DiscardAdded : discardAdded,
|
||||
OpponentHandCount : oppHandCount,
|
||||
OpponentDeckCount : oppDeckCount,
|
||||
OwnArchSpiritDelta : ownArchDelta,
|
||||
OpponentArchSpiritDelta : oppArchDelta,
|
||||
OwnStatsDelta : ownStats,
|
||||
OpponentStatsDelta : oppStats,
|
||||
MetaDelta : metaDelta);
|
||||
StateVersion: next.NextActionSequence,
|
||||
ChangedTiles: changedTiles,
|
||||
UnitsAdded: unitsAdded,
|
||||
UnitsRemoved: unitsRemoved,
|
||||
UnitsChanged: unitsChanged,
|
||||
ArchSpiritByAccount: archByAccount,
|
||||
PlayerStatsByAccount: statsByAccount,
|
||||
PublicCountsByAccount: countsByAccount,
|
||||
PrivateByViewer: privateByViewer,
|
||||
MetaDelta: metaDelta);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute a spectator delta: tile/unit/archspirit/meta changes visible to all;
|
||||
/// hand card details suppressed. P1 mapped to "own", P2 to "opponent" in the result.
|
||||
/// Compute spectator delta (public data only).
|
||||
/// </summary>
|
||||
public static GameStateDelta ComputeSpectatorDelta(GameState prev, GameState next)
|
||||
{
|
||||
var changedTiles = DiffTiles(prev, next);
|
||||
var (addedUnits, removedIds, changed) = DiffAllFieldUnits(prev, next);
|
||||
|
||||
// Map p1 → "own", p2 → "opponent" for archspirit and stats fields
|
||||
prev.Players.TryGetValue(prev.PlayerOneAccountId, out var prevP1);
|
||||
next.Players.TryGetValue(next.PlayerOneAccountId, out var nextP1);
|
||||
var prevP2 = prev.Players.Values.FirstOrDefault(p => p.AccountId != prev.PlayerOneAccountId);
|
||||
var nextP2 = next.Players.Values.FirstOrDefault(p => p.AccountId != next.PlayerOneAccountId);
|
||||
|
||||
UnitDelta? ownArchDelta = null;
|
||||
UnitDelta? oppArchDelta = null;
|
||||
|
||||
if (prevP1?.ArchSpirit != null && nextP1?.ArchSpirit != null)
|
||||
ownArchDelta = DiffUnit(prevP1.ArchSpirit, nextP1.ArchSpirit);
|
||||
if (prevP2?.ArchSpirit != null && nextP2?.ArchSpirit != null)
|
||||
oppArchDelta = DiffUnit(prevP2.ArchSpirit, nextP2.ArchSpirit);
|
||||
|
||||
PlayerStatsDelta? ownStats = null;
|
||||
PlayerStatsDelta? oppStats = null;
|
||||
|
||||
if (prevP1 != null && nextP1 != null) ownStats = DiffPlayerStats(prevP1, nextP1);
|
||||
if (prevP2 != null && nextP2 != null) oppStats = DiffPlayerStats(prevP2, nextP2);
|
||||
var changedTiles = DiffTiles(prev, next);
|
||||
var (unitsAdded, unitsRemoved, unitsChanged) = DiffAllFieldUnits(prev, next);
|
||||
|
||||
var archByAccount = DiffArchByAccount(prev, next);
|
||||
var statsByAccount = DiffStatsByAccount(prev, next);
|
||||
var countsByAccount = DiffPublicCountsByAccount(prev, next, null);
|
||||
var metaDelta = DiffMeta(prev, next);
|
||||
|
||||
return new GameStateDelta(
|
||||
StateVersion : next.NextActionSequence,
|
||||
ChangedTiles : changedTiles,
|
||||
AddedUnits : addedUnits,
|
||||
RemovedUnitIds : removedIds,
|
||||
ChangedUnits : changed,
|
||||
HandAdded : null,
|
||||
HandRemovedIds : null,
|
||||
DiscardAdded : null,
|
||||
OpponentHandCount : null,
|
||||
OpponentDeckCount : null,
|
||||
OwnArchSpiritDelta : ownArchDelta,
|
||||
OpponentArchSpiritDelta : oppArchDelta,
|
||||
OwnStatsDelta : ownStats,
|
||||
OpponentStatsDelta : oppStats,
|
||||
MetaDelta : metaDelta);
|
||||
StateVersion: next.NextActionSequence,
|
||||
ChangedTiles: changedTiles,
|
||||
UnitsAdded: unitsAdded,
|
||||
UnitsRemoved: unitsRemoved,
|
||||
UnitsChanged: unitsChanged,
|
||||
ArchSpiritByAccount: archByAccount,
|
||||
PlayerStatsByAccount: statsByAccount,
|
||||
PublicCountsByAccount: countsByAccount,
|
||||
PrivateByViewer: null,
|
||||
MetaDelta: metaDelta);
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
@@ -216,34 +163,34 @@ internal static class GameStateDiffer
|
||||
return result;
|
||||
}
|
||||
|
||||
private static (List<CardInstanceDto>? added, List<string>? removed, List<UnitDelta>? changed)
|
||||
private static (List<UnitSnapshot>? added, List<string>? removed, List<UnitDelta>? changed)
|
||||
DiffAllFieldUnits(GameState prev, GameState next)
|
||||
{
|
||||
var prevById = new Dictionary<string, CardInstance>();
|
||||
var nextById = new Dictionary<string, CardInstance>();
|
||||
var prevById = new Dictionary<string, (CardInstance unit, string ownerId)>();
|
||||
var nextById = new Dictionary<string, (CardInstance unit, string ownerId)>();
|
||||
|
||||
foreach (var ps in prev.Players.Values)
|
||||
foreach (var u in ps.Field)
|
||||
prevById[u.InstanceId] = u;
|
||||
prevById[u.InstanceId] = (u, ps.AccountId);
|
||||
|
||||
foreach (var ps in next.Players.Values)
|
||||
foreach (var u in ps.Field)
|
||||
nextById[u.InstanceId] = u;
|
||||
nextById[u.InstanceId] = (u, ps.AccountId);
|
||||
|
||||
List<CardInstanceDto>? added = null;
|
||||
List<string>? removed = null;
|
||||
List<UnitDelta>? changed = null;
|
||||
List<UnitSnapshot>? added = null;
|
||||
List<string>? removed = null;
|
||||
List<UnitDelta>? changed = null;
|
||||
|
||||
foreach (var (id, nextUnit) in nextById)
|
||||
foreach (var (id, nextEntry) in nextById)
|
||||
{
|
||||
if (!prevById.TryGetValue(id, out var prevUnit))
|
||||
if (!prevById.TryGetValue(id, out var prevEntry))
|
||||
{
|
||||
added ??= new List<CardInstanceDto>();
|
||||
added.Add(GameEngine.ToCardDto(nextUnit));
|
||||
added ??= new List<UnitSnapshot>();
|
||||
added.Add(new UnitSnapshot(nextEntry.ownerId, GameEngine.ToCardDto(nextEntry.unit)));
|
||||
}
|
||||
else
|
||||
{
|
||||
var delta = DiffUnit(prevUnit, nextUnit);
|
||||
var delta = DiffUnit(prevEntry.unit, nextEntry.unit);
|
||||
if (delta != null)
|
||||
{
|
||||
changed ??= new List<UnitDelta>();
|
||||
@@ -340,6 +287,78 @@ internal static class GameStateDiffer
|
||||
DiscardCount : discardChanged ? next.Discard.Count : null);
|
||||
}
|
||||
|
||||
private static Dictionary<string, UnitDelta>? DiffArchByAccount(GameState prev, GameState next)
|
||||
{
|
||||
Dictionary<string, UnitDelta>? result = null;
|
||||
|
||||
foreach (var (accountId, nextPlayer) in next.Players)
|
||||
{
|
||||
if (!prev.Players.TryGetValue(accountId, out var prevPlayer))
|
||||
continue;
|
||||
|
||||
if (prevPlayer.ArchSpirit != null && nextPlayer.ArchSpirit != null)
|
||||
{
|
||||
var d = DiffUnit(prevPlayer.ArchSpirit, nextPlayer.ArchSpirit);
|
||||
if (d != null)
|
||||
{
|
||||
result ??= new Dictionary<string, UnitDelta>();
|
||||
result[accountId] = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Dictionary<string, PlayerStatsDelta>? DiffStatsByAccount(GameState prev, GameState next)
|
||||
{
|
||||
Dictionary<string, PlayerStatsDelta>? result = null;
|
||||
|
||||
foreach (var (accountId, nextPlayer) in next.Players)
|
||||
{
|
||||
if (!prev.Players.TryGetValue(accountId, out var prevPlayer))
|
||||
continue;
|
||||
|
||||
var d = DiffPlayerStats(prevPlayer, nextPlayer);
|
||||
if (d != null)
|
||||
{
|
||||
result ??= new Dictionary<string, PlayerStatsDelta>();
|
||||
result[accountId] = d;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Dictionary<string, PublicCountsDelta>? DiffPublicCountsByAccount(
|
||||
GameState prev,
|
||||
GameState next,
|
||||
string? viewerAccountId)
|
||||
{
|
||||
Dictionary<string, PublicCountsDelta>? result = null;
|
||||
|
||||
foreach (var (accountId, nextPlayer) in next.Players)
|
||||
{
|
||||
if (viewerAccountId != null && accountId == viewerAccountId)
|
||||
continue;
|
||||
|
||||
if (!prev.Players.TryGetValue(accountId, out var prevPlayer))
|
||||
continue;
|
||||
|
||||
var deck = prevPlayer.Deck.Count != nextPlayer.Deck.Count ? nextPlayer.Deck.Count : (int?)null;
|
||||
var hand = prevPlayer.Hand.Count != nextPlayer.Hand.Count ? nextPlayer.Hand.Count : (int?)null;
|
||||
var discard = prevPlayer.Discard.Count != nextPlayer.Discard.Count ? nextPlayer.Discard.Count : (int?)null;
|
||||
|
||||
if (deck != null || hand != null || discard != null)
|
||||
{
|
||||
result ??= new Dictionary<string, PublicCountsDelta>();
|
||||
result[accountId] = new PublicCountsDelta(deck, hand, discard);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static GameMetaDelta? DiffMeta(GameState prev, GameState next)
|
||||
{
|
||||
bool phaseChanged = prev.Phase != next.Phase;
|
||||
|
||||
Reference in New Issue
Block a user