Fix delta arch-spawn propagation and viewer private lookup
This commit is contained in:
@@ -459,7 +459,7 @@ public sealed record GameStateDelta(
|
||||
IReadOnlyList<UnitSnapshot>? UnitsAdded,
|
||||
IReadOnlyList<string>? UnitsRemoved,
|
||||
IReadOnlyList<UnitDelta>? UnitsChanged,
|
||||
IReadOnlyDictionary<string, UnitDelta>? ArchSpiritByAccount,
|
||||
IReadOnlyDictionary<string, ArchSpiritUpdate>? ArchSpiritByAccount,
|
||||
IReadOnlyDictionary<string, PlayerStatsDelta>? PlayerStatsByAccount,
|
||||
IReadOnlyDictionary<string, PublicCountsDelta>? PublicCountsByAccount,
|
||||
PrivateViewerDelta? PrivateByViewer,
|
||||
@@ -507,6 +507,10 @@ public sealed record PublicCountsDelta(
|
||||
int? HandCount,
|
||||
int? DiscardCount);
|
||||
|
||||
public sealed record ArchSpiritUpdate(
|
||||
CardInstanceDto? Full,
|
||||
UnitDelta? Delta);
|
||||
|
||||
public sealed record PrivateViewerDelta(
|
||||
IReadOnlyList<CardInstanceDto>? HandAdded,
|
||||
IReadOnlyList<string>? HandRemovedIds,
|
||||
|
||||
@@ -23,8 +23,8 @@ internal static class GameStateDiffer
|
||||
var changedTiles = DiffTiles(prev, next);
|
||||
var (unitsAdded, unitsRemoved, unitsChanged) = DiffAllFieldUnits(prev, next);
|
||||
|
||||
prev.Players.TryGetValue(viewerAccountId, out var prevViewer);
|
||||
next.Players.TryGetValue(viewerAccountId, out var nextViewer);
|
||||
var prevViewer = FindPlayer(prev, viewerAccountId);
|
||||
var nextViewer = FindPlayer(next, viewerAccountId);
|
||||
|
||||
PrivateViewerDelta? privateByViewer = null;
|
||||
if (prevViewer != null && nextViewer != null)
|
||||
@@ -287,22 +287,35 @@ internal static class GameStateDiffer
|
||||
DiscardCount : discardChanged ? next.Discard.Count : null);
|
||||
}
|
||||
|
||||
private static Dictionary<string, UnitDelta>? DiffArchByAccount(GameState prev, GameState next)
|
||||
private static Dictionary<string, ArchSpiritUpdate>? DiffArchByAccount(GameState prev, GameState next)
|
||||
{
|
||||
Dictionary<string, UnitDelta>? result = null;
|
||||
Dictionary<string, ArchSpiritUpdate>? result = null;
|
||||
|
||||
foreach (var (accountId, nextPlayer) in next.Players)
|
||||
{
|
||||
if (!prev.Players.TryGetValue(accountId, out var prevPlayer))
|
||||
continue;
|
||||
prev.Players.TryGetValue(accountId, out var prevPlayer);
|
||||
|
||||
if (prevPlayer.ArchSpirit != null && nextPlayer.ArchSpirit != null)
|
||||
var prevArch = prevPlayer?.ArchSpirit;
|
||||
var nextArch = nextPlayer.ArchSpirit;
|
||||
|
||||
if (prevArch == null && nextArch != null)
|
||||
{
|
||||
var d = DiffUnit(prevPlayer.ArchSpirit, nextPlayer.ArchSpirit);
|
||||
result ??= new Dictionary<string, ArchSpiritUpdate>();
|
||||
result[accountId] = new ArchSpiritUpdate(
|
||||
Full: GameEngine.ToCardDto(nextArch),
|
||||
Delta: null);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prevArch != null && nextArch != null)
|
||||
{
|
||||
var d = DiffUnit(prevArch, nextArch);
|
||||
if (d != null)
|
||||
{
|
||||
result ??= new Dictionary<string, UnitDelta>();
|
||||
result[accountId] = d;
|
||||
result ??= new Dictionary<string, ArchSpiritUpdate>();
|
||||
result[accountId] = new ArchSpiritUpdate(
|
||||
Full: null,
|
||||
Delta: d);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -339,7 +352,7 @@ internal static class GameStateDiffer
|
||||
|
||||
foreach (var (accountId, nextPlayer) in next.Players)
|
||||
{
|
||||
if (viewerAccountId != null && accountId == viewerAccountId)
|
||||
if (viewerAccountId != null && string.Equals(accountId, viewerAccountId, StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
if (!prev.Players.TryGetValue(accountId, out var prevPlayer))
|
||||
@@ -359,6 +372,19 @@ internal static class GameStateDiffer
|
||||
return result;
|
||||
}
|
||||
|
||||
private static PlayerState? FindPlayer(GameState state, string accountId)
|
||||
{
|
||||
if (state.Players.TryGetValue(accountId, out var direct))
|
||||
return direct;
|
||||
|
||||
foreach (var (key, ps) in state.Players)
|
||||
if (string.Equals(key, accountId, StringComparison.OrdinalIgnoreCase))
|
||||
return ps;
|
||||
|
||||
return state.Players.Values.FirstOrDefault(p =>
|
||||
string.Equals(p.AccountId, accountId, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static GameMetaDelta? DiffMeta(GameState prev, GameState next)
|
||||
{
|
||||
bool phaseChanged = prev.Phase != next.Phase;
|
||||
|
||||
Reference in New Issue
Block a user