Fix FFA last-man-standing and nil attack-target crash
- GameEngine.Concede: use EliminatePlayer instead of unconditionally setting GamePhase.GameOver — FFA game continues until one team remains. Return value changed to (GameState, bool gameOver) tuple. - GameConcedeHandler / GameForceConcedeHandler: check gameOver flag; when game continues push BroadcastPlayerEliminatedAsync + state update instead of calling FinalizeMultiPlayerGameAsync prematurely. - game_screen.gd _valid_attack_target_coords: use `not (_tpos is Dictionary)` guard instead of typed assignment — prevents nil crash when eliminated players' units still appear in get_all_enemy_units() with position:null. 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:
@@ -2215,7 +2215,9 @@ func _valid_attack_target_coords(unit: Dictionary) -> Array:
|
||||
var to_check : Array = Game.get_all_enemy_units()
|
||||
for t: Dictionary in to_check:
|
||||
if not t.get("canBeAttacked", true): continue
|
||||
var tp : Dictionary = t.get("position", {})
|
||||
var _tpos = t.get("position")
|
||||
if not (_tpos is Dictionary): continue # null when unit removed from board
|
||||
var tp : Dictionary = _tpos
|
||||
if tp.is_empty(): continue
|
||||
var dist : int = abs(tp.get("x", 0) - ux) + abs(tp.get("y", 0) - uy)
|
||||
if dist >= min_r and dist <= max_r:
|
||||
|
||||
@@ -985,11 +985,19 @@ public static class GameEngine
|
||||
return (winners, loserSet);
|
||||
}
|
||||
|
||||
/// <summary>Mark the game as over with the conceding player as the loser.</summary>
|
||||
public static GameState Concede(GameState state, string actorId)
|
||||
/// <summary>
|
||||
/// Mark the conceding player as eliminated.
|
||||
/// In FFA / multi-team games the game only ends when a single team remains;
|
||||
/// otherwise the game continues and the caller must push an elimination event.
|
||||
/// </summary>
|
||||
public static (GameState state, bool gameOver) Concede(GameState state, string actorId)
|
||||
{
|
||||
state.Phase = GamePhase.GameOver;
|
||||
return state;
|
||||
var player = state.Players.GetValueOrDefault(actorId);
|
||||
if (player != null)
|
||||
EliminatePlayer(state, player, out bool over);
|
||||
else
|
||||
state.Phase = GamePhase.GameOver; // fallback: unknown player
|
||||
return (state, state.Phase == GamePhase.GameOver);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -46,24 +46,37 @@ internal sealed class GameConcedeHandler : GameHandlerBase, IMessageHandler
|
||||
|
||||
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
var newState = GameEngine.Concede(state, actorId);
|
||||
var (newState, gameOver) = GameEngine.Concede(state, actorId);
|
||||
|
||||
session.Status = GameSessionStatus.Completed;
|
||||
session.WinCondition = "concede";
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
if (gameOver)
|
||||
{
|
||||
session.Status = GameSessionStatus.Completed;
|
||||
session.WinCondition = "concede";
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.concede",
|
||||
JsonSerializer.Serialize(req), ct);
|
||||
|
||||
var dto = GameEngine.ToDto(newState, actorId);
|
||||
|
||||
if (!gameOver)
|
||||
{
|
||||
// Player eliminated but game continues — notify all players then push updated state.
|
||||
await BroadcastPlayerEliminatedAsync(_clientPush, session, newState, actorId, ct);
|
||||
await PushStateToPlayersAsync(_clientPush, session, newState, ct);
|
||||
return ServerMessage.Reply("game.concede.ack", message.Seq,
|
||||
new GameConcedeResponse(dto));
|
||||
}
|
||||
|
||||
// Game is over — finalize rankings and push game.over to all participants.
|
||||
bool isMultiPlayer = session.PlayerCount > 2 ||
|
||||
!string.IsNullOrEmpty(session.AdditionalPlayersJson);
|
||||
|
||||
if (isMultiPlayer)
|
||||
{
|
||||
// In N-player games the conceding player is a loser; all other non-eliminated players win.
|
||||
var loserId = Guid.Parse(actorId);
|
||||
var loserId = Guid.Parse(actorId);
|
||||
var winnerIds = newState.Players.Values
|
||||
.Where(p => !p.IsEliminated && p.AccountId != actorId)
|
||||
.Select(p => Guid.Parse(p.AccountId))
|
||||
|
||||
@@ -67,17 +67,29 @@ internal sealed class GameForceConcedeHandler : GameHandlerBase, IMessageHandler
|
||||
|
||||
// Treat as the inactive player conceding.
|
||||
var inactivePlayerId = state.ActivePlayerAccountId;
|
||||
var newState = GameEngine.Concede(state, inactivePlayerId);
|
||||
var (newState, gameOver) = GameEngine.Concede(state, inactivePlayerId);
|
||||
|
||||
session.Status = GameSessionStatus.Completed;
|
||||
session.WinCondition = "inactive";
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
if (gameOver)
|
||||
{
|
||||
session.Status = GameSessionStatus.Completed;
|
||||
session.WinCondition = "inactive";
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.force_concede",
|
||||
JsonSerializer.Serialize(req), ct);
|
||||
|
||||
var dto = GameEngine.ToDto(newState, actorId);
|
||||
|
||||
if (!gameOver)
|
||||
{
|
||||
// Inactive player eliminated but game continues (FFA with remaining players).
|
||||
await BroadcastPlayerEliminatedAsync(_clientPush, session, newState, inactivePlayerId, ct);
|
||||
await PushStateToPlayersAsync(_clientPush, session, newState, ct);
|
||||
return ServerMessage.Reply("game.force_concede.ack", message.Seq,
|
||||
new GameForceConcedeResponse(dto));
|
||||
}
|
||||
|
||||
bool isMultiPlayer = session.PlayerCount > 2 ||
|
||||
!string.IsNullOrEmpty(session.AdditionalPlayersJson);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user