Harden multiplayer rejoin and eliminate-concede cleanup semantics
This commit is contained in:
@@ -1279,6 +1279,7 @@ public static class GameEngine
|
||||
private static bool EliminatePlayer(GameState state, PlayerState victim, out bool gameOver)
|
||||
{
|
||||
victim.IsEliminated = true;
|
||||
RemovePlayerBoardPresence(state, victim.AccountId);
|
||||
gameOver = false;
|
||||
|
||||
var survivingTeams = state.Players.Values
|
||||
@@ -1301,6 +1302,31 @@ public static class GameEngine
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void RemovePlayerBoardPresence(GameState state, string accountId)
|
||||
{
|
||||
if (!state.Players.TryGetValue(accountId, out var p))
|
||||
return;
|
||||
|
||||
p.Field.Clear();
|
||||
p.ArchSpirit = null;
|
||||
|
||||
if (state.Board == null) return;
|
||||
|
||||
foreach (var tile in state.Board.Tiles.Values)
|
||||
{
|
||||
if (tile.OccupantOwner == accountId)
|
||||
{
|
||||
tile.OccupantId = null;
|
||||
tile.OccupantOwner = null;
|
||||
}
|
||||
if (tile.RuinOwner == accountId)
|
||||
{
|
||||
tile.RuinId = null;
|
||||
tile.RuinOwner = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find the player who owns <paramref name="targetInstanceId"/> from among non-eliminated
|
||||
/// players other than <paramref name="actorId"/>.
|
||||
|
||||
@@ -235,8 +235,13 @@ public sealed record GameActivateAbilityResponse(GameStateDto State);
|
||||
/// <summary>
|
||||
/// Response for game.get_active_session.
|
||||
/// SessionId is null when the player has no in-progress game.
|
||||
/// CanRejoinAsPlayer=false indicates the caller is in-session but eliminated/conceded.
|
||||
/// </summary>
|
||||
public sealed record GameGetActiveSessionResponse(Guid? SessionId, string? Phase);
|
||||
public sealed record GameGetActiveSessionResponse(
|
||||
Guid? SessionId,
|
||||
string? Phase,
|
||||
bool? CanRejoinAsPlayer,
|
||||
bool? IsEliminated);
|
||||
|
||||
public sealed record GameForceConcedeResponse(GameStateDto State);
|
||||
|
||||
|
||||
@@ -56,6 +56,10 @@ internal sealed class GameForceConcedeHandler : GameHandlerBase, IMessageHandler
|
||||
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
|
||||
// Eliminated players cannot influence outcome via force-concede.
|
||||
if (state.Players.TryGetValue(actorId, out var actorState) && actorState.IsEliminated)
|
||||
throw new GameException("PLAYER_ELIMINATED");
|
||||
|
||||
// The caller must NOT be the active player — they are the waiting player.
|
||||
if (state.ActivePlayerAccountId == actorId)
|
||||
throw new GameException("YOU_ARE_THE_ACTIVE_PLAYER");
|
||||
|
||||
@@ -36,7 +36,7 @@ internal sealed class GameGetActiveSessionHandler : IMessageHandler
|
||||
|
||||
var actorIdStr = actorId.ToString();
|
||||
|
||||
var session = await _gameDb.GameSessions
|
||||
var sessions = await _gameDb.GameSessions
|
||||
.AsNoTracking()
|
||||
.Where(s => (
|
||||
s.PlayerOneAccountId == actorId
|
||||
@@ -46,16 +46,29 @@ internal sealed class GameGetActiveSessionHandler : IMessageHandler
|
||||
&& s.Status == GameSessionStatus.Active
|
||||
&& s.StateJson != null)
|
||||
.OrderByDescending(s => s.CreatedAt)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
.Take(12)
|
||||
.ToListAsync(ct);
|
||||
|
||||
if (session == null)
|
||||
if (sessions.Count == 0)
|
||||
return ServerMessage.Reply("game.get_active_session.ack", message.Seq,
|
||||
new GameGetActiveSessionResponse(null, null));
|
||||
new GameGetActiveSessionResponse(null, null, null, null));
|
||||
|
||||
// Deserialize state to extract the current phase.
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
// Prefer a session where the caller is still an active player (not eliminated).
|
||||
foreach (var s in sessions)
|
||||
{
|
||||
var st = GameEngine.Deserialize(s.StateJson!);
|
||||
var ps = st.Players.TryGetValue(actorIdStr, out var direct)
|
||||
? direct
|
||||
: st.Players.Values.FirstOrDefault(p => p.AccountId.Equals(actorIdStr, StringComparison.OrdinalIgnoreCase));
|
||||
if (ps != null && !ps.IsEliminated)
|
||||
{
|
||||
return ServerMessage.Reply("game.get_active_session.ack", message.Seq,
|
||||
new GameGetActiveSessionResponse(s.Id, st.Phase.ToString(), true, false));
|
||||
}
|
||||
}
|
||||
|
||||
// Caller only appears as eliminated across active sessions: suppress rejoin prompt.
|
||||
return ServerMessage.Reply("game.get_active_session.ack", message.Seq,
|
||||
new GameGetActiveSessionResponse(session.Id, state.Phase.ToString()));
|
||||
new GameGetActiveSessionResponse(null, null, false, true));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user