Phase 2+7: GameEngine rule enforcement + extended lobby handlers
Phase 2 — GameEngine: - Draw(): now draws DrawPerTurn+DrawPerTurnBonus cards per call (was always 1); enforces HandSizeCap with block-draw or auto-discard (DiscardOnMaxHand) - EndTurn(): returns (GameState, bool gameOver); applies MpBuff to all next-player units at turn start; checks TurnLimit and sets GameOver phase if exceeded - GetTurnLimitWinner(): helper returning winner/loser by ArchSpirit HP (P1 on tie) - ValidateDeck(): new public method checking MinDeckSize, MaxDeckSize, MaxArchTier1 - GameRules note: new fields are already in place from Phase 1 Phase 7 — Handlers + Messages: - CustomRulesOverride: +9 new nullable fields (HandSizeCap, DiscardOnMaxHand, MaxDeckSize, MinDeckSize, MaxArchTier1, TurnTimerSeconds, TimeLimitMinutes, TurnLimit, MpBuff) - CustomRulesDto: +9 matching fields + HasPassword bool - CreateCustomLobbyRequest: +Password field - JoinCustomLobbyRequest: +Password field; handler now validates password - ListCustomLobbiesRequest: +NameSearch, ShowPasswordProtected filters - GameCreateCustomLobbyHandler: full rule merge + ToDto covers all new fields - GameJoinCustomLobbyHandler: password check + full rules construction incl. new fields - GameListCustomLobbiesHandler: full DTO mapping + NameSearch/password filters - GameEndTurnHandler: injected identityDb+eventBus; handles TurnLimit game-over using same pattern as GameAttackHandler (set session fields before SaveStateAsync) - GameDrawHandler: update deconstruct to discard unused drawn list (_) - CustomLobbyGuestJoinedPush: new push type (used in Phase 6) 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:
@@ -148,26 +148,50 @@ public static class GameEngine
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draw the top card from the active player's deck into hand.
|
||||
/// Returns null if the deck is empty (no mill).
|
||||
/// Draw cards from the active player's deck into hand.
|
||||
/// Draws Rules.DrawPerTurn + player.DrawPerTurnBonus cards in one call.
|
||||
/// Respects HandSizeCap: if DiscardOnMaxHand, excess cards are auto-discarded;
|
||||
/// otherwise drawing stops when the cap is reached.
|
||||
/// </summary>
|
||||
public static (GameState state, CardInstance? drawn) Draw(GameState state, string actorId)
|
||||
public static (GameState state, IReadOnlyList<CardInstance> drawn) Draw(GameState state, string actorId)
|
||||
{
|
||||
ValidateActivePlayer(state, actorId);
|
||||
ValidatePhase(state, GamePhase.Draw, GamePhase.Main);
|
||||
|
||||
var player = state.Players[actorId];
|
||||
if (player.Deck.Count == 0)
|
||||
return (state, null);
|
||||
var rules = state.Rules;
|
||||
|
||||
var card = player.Deck[0];
|
||||
player.Deck.RemoveAt(0);
|
||||
player.Hand.Add(card);
|
||||
int count = Math.Max(1, rules.DrawPerTurn + player.DrawPerTurnBonus);
|
||||
var drawn = new List<CardInstance>(count);
|
||||
|
||||
for (int i = 0; i < count && player.Deck.Count > 0; i++)
|
||||
{
|
||||
// If capped and not auto-discarding, stop drawing once at cap.
|
||||
if (rules.HandSizeCap > 0 && !rules.DiscardOnMaxHand && player.Hand.Count >= rules.HandSizeCap)
|
||||
break;
|
||||
|
||||
var card = player.Deck[0];
|
||||
player.Deck.RemoveAt(0);
|
||||
player.Hand.Add(card);
|
||||
drawn.Add(card);
|
||||
}
|
||||
|
||||
// Auto-discard excess from the newest-drawn cards when DiscardOnMaxHand is active.
|
||||
if (rules.HandSizeCap > 0 && rules.DiscardOnMaxHand)
|
||||
{
|
||||
while (player.Hand.Count > rules.HandSizeCap)
|
||||
{
|
||||
var last = player.Hand[^1];
|
||||
player.Hand.RemoveAt(player.Hand.Count - 1);
|
||||
player.Discard.Add(last);
|
||||
drawn.Remove(last);
|
||||
}
|
||||
}
|
||||
|
||||
if (state.Phase == GamePhase.Draw)
|
||||
state.Phase = GamePhase.Main;
|
||||
|
||||
return (state, card);
|
||||
return (state, drawn);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -776,7 +800,11 @@ public static class GameEngine
|
||||
/// IsExhausted resets for the ENDING player's units (they finished acting).
|
||||
/// MP + Defense restore for the STARTING player (at the top of their turn).
|
||||
/// </summary>
|
||||
public static GameState EndTurn(GameState state, string actorId)
|
||||
/// <summary>
|
||||
/// Returns gameOver = true when TurnLimit is exceeded; winner is the player with more
|
||||
/// ArchSpirit HP (P1 wins on tie). Caller must invoke FinalizeGameAsync in that case.
|
||||
/// </summary>
|
||||
public static (GameState state, bool gameOver) EndTurn(GameState state, string actorId)
|
||||
{
|
||||
ValidateActivePlayer(state, actorId);
|
||||
ValidatePhase(state, GamePhase.Main);
|
||||
@@ -806,22 +834,29 @@ public static class GameEngine
|
||||
AbilitySystem.TickActiveEffects(state, actorId);
|
||||
AbilitySystem.ExpireActiveEffects(state, actorId, isEndOfTurn: true);
|
||||
|
||||
// Switch active player.
|
||||
// Switch active player and increment turn counter.
|
||||
state.ActivePlayerAccountId = GetOpponentId(state, actorId);
|
||||
state.TurnNumber++;
|
||||
|
||||
// Start of next player's turn: restore MP, defense, accumulate SP.
|
||||
var next = state.Players[state.ActivePlayerAccountId];
|
||||
// Check turn limit BEFORE setting up the new turn.
|
||||
var rules = state.Rules;
|
||||
if (rules.TurnLimit > 0 && state.TurnNumber > rules.TurnLimit)
|
||||
{
|
||||
state.Phase = GamePhase.GameOver;
|
||||
return (state, true);
|
||||
}
|
||||
|
||||
// Start of next player's turn: restore MP (+ MpBuff), defense, accumulate SP.
|
||||
var next = state.Players[state.ActivePlayerAccountId];
|
||||
|
||||
foreach (var card in next.Field.OfType<FieldCard>())
|
||||
{
|
||||
if (card.MaxMp.HasValue) card.CurrentMp = card.MaxMp.Value;
|
||||
if (card.MaxMp.HasValue) card.CurrentMp = card.MaxMp.Value + rules.MpBuff;
|
||||
if (card.MaxDefense.HasValue) card.CurrentDefense = card.MaxDefense;
|
||||
}
|
||||
if (next.ArchSpirit is not null)
|
||||
{
|
||||
if (next.ArchSpirit.MaxMp.HasValue) next.ArchSpirit.CurrentMp = next.ArchSpirit.MaxMp.Value;
|
||||
if (next.ArchSpirit.MaxMp.HasValue) next.ArchSpirit.CurrentMp = next.ArchSpirit.MaxMp.Value + rules.MpBuff;
|
||||
if (next.ArchSpirit.MaxDefense.HasValue) next.ArchSpirit.CurrentDefense = next.ArchSpirit.MaxDefense;
|
||||
}
|
||||
|
||||
@@ -842,7 +877,22 @@ public static class GameEngine
|
||||
|
||||
AbilitySystem.FireRuinAbilities(state);
|
||||
|
||||
return state;
|
||||
return (state, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper to determine the turn-limit winner: player with more ArchSpirit HP.
|
||||
/// P1 wins on tie. Returns (winnerId, loserId).
|
||||
/// </summary>
|
||||
public static (Guid winnerId, Guid loserId) GetTurnLimitWinner(GameState state)
|
||||
{
|
||||
var p1Id = Guid.Parse(state.PlayerOneAccountId);
|
||||
var p2Id = Guid.Parse(GetOpponentId(state, state.PlayerOneAccountId));
|
||||
|
||||
var p1Hp = state.Players[state.PlayerOneAccountId].ArchSpirit?.CurrentHealth ?? 0;
|
||||
var p2Hp = state.Players[state.Players.Keys.First(k => k != state.PlayerOneAccountId)].ArchSpirit?.CurrentHealth ?? 0;
|
||||
|
||||
return p1Hp >= p2Hp ? (p1Id, p2Id) : (p2Id, p1Id);
|
||||
}
|
||||
|
||||
/// <summary>Mark the game as over with the conceding player as the loser.</summary>
|
||||
@@ -917,6 +967,39 @@ public static class GameEngine
|
||||
Board : boardDto);
|
||||
}
|
||||
|
||||
// ── Deck validation ───────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Validate a deck against custom lobby rules.
|
||||
/// Returns a list of error codes (empty = valid). Only checks rules with non-zero limits.
|
||||
/// Does not check the arch spirit (it is validated separately at card-edit time).
|
||||
/// </summary>
|
||||
public static IReadOnlyList<string> ValidateDeck(
|
||||
IEnumerable<(CardDefinition def, int qty)> deck,
|
||||
GameRules rules)
|
||||
{
|
||||
var errors = new List<string>();
|
||||
var deckList = deck.ToList();
|
||||
int totalCards = deckList.Sum(d => d.qty);
|
||||
|
||||
if (rules.MinDeckSize > 0 && totalCards < rules.MinDeckSize)
|
||||
errors.Add($"DECK_TOO_SMALL:{totalCards}<{rules.MinDeckSize}");
|
||||
|
||||
if (rules.MaxDeckSize > 0 && totalCards > rules.MaxDeckSize)
|
||||
errors.Add($"DECK_TOO_LARGE:{totalCards}>{rules.MaxDeckSize}");
|
||||
|
||||
if (rules.MaxArchTier1 > 0)
|
||||
{
|
||||
var tier1Count = deckList
|
||||
.Where(d => d.def.CardType == CardType.ArchSpirit && (d.def.Tier ?? 1) == 1)
|
||||
.Sum(d => d.qty);
|
||||
if (tier1Count > rules.MaxArchTier1)
|
||||
errors.Add($"TOO_MANY_ARCH_TIER1:{tier1Count}>{rules.MaxArchTier1}");
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
// ── ELO ───────────────────────────────────────────────────────────────────
|
||||
|
||||
public static (int winnerDelta, int loserDelta) ComputeEloDelta(int winnerScore, int loserScore)
|
||||
|
||||
@@ -90,9 +90,10 @@ public sealed record GameForceConcedeRequest(Guid SessionId);
|
||||
|
||||
/// <summary>Create a new custom lobby. Host provides their deck and optional rule overrides.</summary>
|
||||
public sealed record CreateCustomLobbyRequest(
|
||||
string LobbyName,
|
||||
Guid DeckId,
|
||||
CustomRulesOverride? Rules = null);
|
||||
string LobbyName,
|
||||
Guid DeckId,
|
||||
string? Password = null,
|
||||
CustomRulesOverride? Rules = null);
|
||||
|
||||
/// <summary>
|
||||
/// Per-field rule overrides for a custom lobby. Null fields inherit from the
|
||||
@@ -106,16 +107,29 @@ public sealed record CustomRulesOverride(
|
||||
bool? SpHasCap = null,
|
||||
int? SpCap = null,
|
||||
int? BoardWidth = null,
|
||||
int? BoardHeight = null);
|
||||
int? BoardHeight = null,
|
||||
int? HandSizeCap = null,
|
||||
bool? DiscardOnMaxHand = null,
|
||||
int? MaxDeckSize = null,
|
||||
int? MinDeckSize = null,
|
||||
int? MaxArchTier1 = null,
|
||||
int? TurnTimerSeconds = null,
|
||||
int? TimeLimitMinutes = null,
|
||||
int? TurnLimit = null,
|
||||
int? MpBuff = null);
|
||||
|
||||
/// <summary>Join an open custom lobby with the specified deck.</summary>
|
||||
public sealed record JoinCustomLobbyRequest(Guid LobbyId, Guid DeckId);
|
||||
public sealed record JoinCustomLobbyRequest(Guid LobbyId, Guid DeckId, string? Password = null);
|
||||
|
||||
/// <summary>Leave or cancel a custom lobby.</summary>
|
||||
public sealed record LeaveCustomLobbyRequest(Guid LobbyId);
|
||||
|
||||
/// <summary>List open custom lobbies.</summary>
|
||||
public sealed record ListCustomLobbiesRequest(int Page = 1, int PageSize = 20);
|
||||
/// <summary>List open custom lobbies. NameSearch filters by lobby name (case-insensitive contains).</summary>
|
||||
public sealed record ListCustomLobbiesRequest(
|
||||
int Page = 1,
|
||||
int PageSize = 20,
|
||||
string? NameSearch = null,
|
||||
bool ShowPasswordProtected = true);
|
||||
|
||||
// ── Responses ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -221,6 +235,12 @@ public sealed record CustomLobbyGameStartedPush(
|
||||
Guid SessionId,
|
||||
Guid OpponentAccountId);
|
||||
|
||||
/// <summary>Pushed to the host when a guest joins their open lobby.</summary>
|
||||
public sealed record CustomLobbyGuestJoinedPush(
|
||||
Guid LobbyId,
|
||||
Guid GuestAccountId,
|
||||
string LobbyName);
|
||||
|
||||
// ── DTOs ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
@@ -320,7 +340,17 @@ public sealed record CustomRulesDto(
|
||||
bool SpHasCap,
|
||||
int SpCap,
|
||||
int BoardWidth,
|
||||
int BoardHeight);
|
||||
int BoardHeight,
|
||||
int HandSizeCap,
|
||||
bool DiscardOnMaxHand,
|
||||
int MaxDeckSize,
|
||||
int MinDeckSize,
|
||||
int MaxArchTier1,
|
||||
int TurnTimerSeconds,
|
||||
int TimeLimitMinutes,
|
||||
int TurnLimit,
|
||||
int MpBuff,
|
||||
bool HasPassword);
|
||||
|
||||
/// <summary>
|
||||
/// A single client-provided target for one action in an ability.
|
||||
|
||||
@@ -52,21 +52,44 @@ internal sealed class GameCreateCustomLobbyHandler : GameHandlerBase, IMessageHa
|
||||
DrawPerTurn = req.Rules?.DrawPerTurn ?? defaultRules?.DrawPerTurn ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap = req.Rules?.SpHasCap ?? defaultRules?.SpHasCap ?? GameRules.Default.SpHasCap,
|
||||
SpCap = req.Rules?.SpCap ?? defaultRules?.SpCap ?? GameRules.Default.SpCap,
|
||||
BoardWidth = req.Rules?.BoardWidth ?? defaultRules?.BoardWidth ?? GameRules.Default.BoardWidth,
|
||||
BoardHeight = req.Rules?.BoardHeight ?? defaultRules?.BoardHeight ?? GameRules.Default.BoardHeight,
|
||||
HandSizeCap = req.Rules?.HandSizeCap ?? defaultRules?.HandSizeCap ?? GameRules.Default.HandSizeCap,
|
||||
DiscardOnMaxHand = req.Rules?.DiscardOnMaxHand ?? defaultRules?.DiscardOnMaxHand ?? GameRules.Default.DiscardOnMaxHand,
|
||||
MaxDeckSize = req.Rules?.MaxDeckSize ?? defaultRules?.MaxDeckSize ?? GameRules.Default.MaxDeckSize,
|
||||
MinDeckSize = req.Rules?.MinDeckSize ?? defaultRules?.MinDeckSize ?? GameRules.Default.MinDeckSize,
|
||||
MaxArchTier1 = req.Rules?.MaxArchTier1 ?? defaultRules?.MaxArchTier1 ?? GameRules.Default.MaxArchTier1,
|
||||
TurnTimerSeconds = req.Rules?.TurnTimerSeconds ?? defaultRules?.TurnTimerSeconds ?? GameRules.Default.TurnTimerSeconds,
|
||||
TimeLimitMinutes = req.Rules?.TimeLimitMinutes ?? defaultRules?.TimeLimitMinutes ?? GameRules.Default.TimeLimitMinutes,
|
||||
TurnLimit = req.Rules?.TurnLimit ?? defaultRules?.TurnLimit ?? GameRules.Default.TurnLimit,
|
||||
MpBuff = req.Rules?.MpBuff ?? defaultRules?.MpBuff ?? GameRules.Default.MpBuff,
|
||||
};
|
||||
|
||||
var lobby = new CustomLobby
|
||||
{
|
||||
Name = req.LobbyName,
|
||||
HostAccountId = accountId,
|
||||
HostDeckId = req.DeckId,
|
||||
Status = CustomLobbyStatus.Open,
|
||||
OpeningHandSizeOverride = req.Rules?.OpeningHandSize,
|
||||
FirstPlayerSkipsDrawOverride = req.Rules?.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurnOverride = req.Rules?.SpAccumulationPerTurn,
|
||||
DrawPerTurnOverride = req.Rules?.DrawPerTurn,
|
||||
SpHasCapOverride = req.Rules?.SpHasCap,
|
||||
SpCapOverride = req.Rules?.SpCap,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
Name = req.LobbyName,
|
||||
HostAccountId = accountId,
|
||||
HostDeckId = req.DeckId,
|
||||
Status = CustomLobbyStatus.Open,
|
||||
Password = req.Password,
|
||||
OpeningHandSizeOverride = req.Rules?.OpeningHandSize,
|
||||
FirstPlayerSkipsDrawOverride = req.Rules?.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurnOverride = req.Rules?.SpAccumulationPerTurn,
|
||||
DrawPerTurnOverride = req.Rules?.DrawPerTurn,
|
||||
SpHasCapOverride = req.Rules?.SpHasCap,
|
||||
SpCapOverride = req.Rules?.SpCap,
|
||||
BoardWidthOverride = req.Rules?.BoardWidth,
|
||||
BoardHeightOverride = req.Rules?.BoardHeight,
|
||||
HandSizeCapOverride = req.Rules?.HandSizeCap,
|
||||
DiscardOnMaxHandOverride = req.Rules?.DiscardOnMaxHand,
|
||||
MaxDeckSizeOverride = req.Rules?.MaxDeckSize,
|
||||
MinDeckSizeOverride = req.Rules?.MinDeckSize,
|
||||
MaxArchTier1Override = req.Rules?.MaxArchTier1,
|
||||
TurnTimerSecondsOverride = req.Rules?.TurnTimerSeconds,
|
||||
TimeLimitMinutesOverride = req.Rules?.TimeLimitMinutes,
|
||||
TurnLimitOverride = req.Rules?.TurnLimit,
|
||||
MpBuffOverride = req.Rules?.MpBuff,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
_gameDb.CustomLobbies.Add(lobby);
|
||||
@@ -76,10 +99,11 @@ internal sealed class GameCreateCustomLobbyHandler : GameHandlerBase, IMessageHa
|
||||
new CreateCustomLobbyResponse(
|
||||
LobbyId : lobby.Id,
|
||||
LobbyName : lobby.Name,
|
||||
ActiveRules : ToDto(effective)));
|
||||
ActiveRules : ToDto(effective, req.Password != null)));
|
||||
}
|
||||
|
||||
private static CustomRulesDto ToDto(GameRules r) => new(
|
||||
// Shared helper used by list/join handlers as well.
|
||||
internal static CustomRulesDto ToDto(GameRules r, bool hasPassword = false) => new(
|
||||
OpeningHandSize : r.OpeningHandSize,
|
||||
FirstPlayerSkipsDraw : r.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn : r.SpAccumulationPerTurn,
|
||||
@@ -87,5 +111,15 @@ internal sealed class GameCreateCustomLobbyHandler : GameHandlerBase, IMessageHa
|
||||
SpHasCap : r.SpHasCap,
|
||||
SpCap : r.SpCap,
|
||||
BoardWidth : r.BoardWidth,
|
||||
BoardHeight : r.BoardHeight);
|
||||
BoardHeight : r.BoardHeight,
|
||||
HandSizeCap : r.HandSizeCap,
|
||||
DiscardOnMaxHand : r.DiscardOnMaxHand,
|
||||
MaxDeckSize : r.MaxDeckSize,
|
||||
MinDeckSize : r.MinDeckSize,
|
||||
MaxArchTier1 : r.MaxArchTier1,
|
||||
TurnTimerSeconds : r.TurnTimerSeconds,
|
||||
TimeLimitMinutes : r.TimeLimitMinutes,
|
||||
TurnLimit : r.TurnLimit,
|
||||
MpBuff : r.MpBuff,
|
||||
HasPassword : hasPassword);
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ internal sealed class GameDrawHandler : GameHandlerBase, IMessageHandler
|
||||
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
|
||||
var (newState, drawn) = GameEngine.Draw(state, actorId);
|
||||
var (newState, _) = GameEngine.Draw(state, actorId);
|
||||
|
||||
var dto = GameEngine.ToDto(newState, actorId);
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.draw",
|
||||
|
||||
@@ -2,6 +2,8 @@ namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.EventBus;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
@@ -9,18 +11,29 @@ using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// game.end_turn — active player ends their turn.
|
||||
/// If TurnLimit is configured and the new turn number exceeds it, the game ends
|
||||
/// immediately (winner = player with higher ArchSpirit HP; P1 wins on tie).
|
||||
/// </summary>
|
||||
internal sealed class GameEndTurnHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public GameEndTurnHandler(IMessageSerializer serializer, GameDbContext gameDb, IClientPush clientPush)
|
||||
public GameEndTurnHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
SorIdentityDbContext identityDb,
|
||||
IClientPush clientPush,
|
||||
IEventBus eventBus)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_identityDb = identityDb;
|
||||
_clientPush = clientPush;
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
|
||||
public string MessageType => "game.end_turn";
|
||||
@@ -35,15 +48,40 @@ internal sealed class GameEndTurnHandler : GameHandlerBase, IMessageHandler
|
||||
|
||||
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
var newState = GameEngine.EndTurn(state, actorId);
|
||||
var (newState, gameOver) = GameEngine.EndTurn(state, actorId);
|
||||
|
||||
var dto = GameEngine.ToDto(newState, actorId);
|
||||
if (gameOver)
|
||||
{
|
||||
var (winnerId, loserId) = GameEngine.GetTurnLimitWinner(newState);
|
||||
|
||||
session.Status = GameSessionStatus.Completed;
|
||||
session.WinnerAccountId = winnerId;
|
||||
session.WinCondition = "turn_limit";
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
var dto = GameEngine.ToDto(newState, actorId);
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.end_turn",
|
||||
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
|
||||
|
||||
await FinalizeGameAsync(
|
||||
_gameDb, _identityDb, _clientPush, _eventBus,
|
||||
session,
|
||||
winnerAccountId: winnerId,
|
||||
loserAccountId: loserId,
|
||||
winCondition: "turn_limit",
|
||||
ct);
|
||||
|
||||
return ServerMessage.Reply("game.end_turn.ack", message.Seq,
|
||||
new GameEndTurnResponse(dto));
|
||||
}
|
||||
|
||||
var stateDto = GameEngine.ToDto(newState, actorId);
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.end_turn",
|
||||
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
|
||||
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(stateDto));
|
||||
|
||||
await PushStateToPlayersAsync(_clientPush, session, newState, ct);
|
||||
|
||||
return ServerMessage.Reply("game.end_turn.ack", message.Seq,
|
||||
new GameEndTurnResponse(dto));
|
||||
new GameEndTurnResponse(stateDto));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,10 @@ internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHand
|
||||
if (lobby.HostAccountId == accountId)
|
||||
throw new GameException("CANNOT_JOIN_OWN_LOBBY");
|
||||
|
||||
// Password check.
|
||||
if (lobby.Password != null && lobby.Password != req.Password)
|
||||
throw new GameException("WRONG_PASSWORD");
|
||||
|
||||
lobby.GuestAccountId = accountId;
|
||||
lobby.GuestDeckId = req.DeckId;
|
||||
lobby.Status = CustomLobbyStatus.Full;
|
||||
@@ -69,13 +73,22 @@ internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHand
|
||||
var rules = new GameRules
|
||||
{
|
||||
OpeningHandSize = lobby.OpeningHandSizeOverride ?? GameRules.Default.OpeningHandSize,
|
||||
FirstPlayerSkipsDraw = lobby.FirstPlayerSkipsDrawOverride ?? GameRules.Default.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn = lobby.SpAccumulationPerTurnOverride ?? GameRules.Default.SpAccumulationPerTurn,
|
||||
DrawPerTurn = lobby.DrawPerTurnOverride ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap = lobby.SpHasCapOverride ?? GameRules.Default.SpHasCap,
|
||||
SpCap = lobby.SpCapOverride ?? GameRules.Default.SpCap,
|
||||
BoardWidth = GameRules.Default.BoardWidth,
|
||||
BoardHeight = GameRules.Default.BoardHeight,
|
||||
FirstPlayerSkipsDraw = lobby.FirstPlayerSkipsDrawOverride ?? GameRules.Default.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn = lobby.SpAccumulationPerTurnOverride ?? GameRules.Default.SpAccumulationPerTurn,
|
||||
DrawPerTurn = lobby.DrawPerTurnOverride ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap = lobby.SpHasCapOverride ?? GameRules.Default.SpHasCap,
|
||||
SpCap = lobby.SpCapOverride ?? GameRules.Default.SpCap,
|
||||
BoardWidth = lobby.BoardWidthOverride ?? GameRules.Default.BoardWidth,
|
||||
BoardHeight = lobby.BoardHeightOverride ?? GameRules.Default.BoardHeight,
|
||||
HandSizeCap = lobby.HandSizeCapOverride ?? GameRules.Default.HandSizeCap,
|
||||
DiscardOnMaxHand = lobby.DiscardOnMaxHandOverride ?? GameRules.Default.DiscardOnMaxHand,
|
||||
MaxDeckSize = lobby.MaxDeckSizeOverride ?? GameRules.Default.MaxDeckSize,
|
||||
MinDeckSize = lobby.MinDeckSizeOverride ?? GameRules.Default.MinDeckSize,
|
||||
MaxArchTier1 = lobby.MaxArchTier1Override ?? GameRules.Default.MaxArchTier1,
|
||||
TurnTimerSeconds = lobby.TurnTimerSecondsOverride ?? GameRules.Default.TurnTimerSeconds,
|
||||
TimeLimitMinutes = lobby.TimeLimitMinutesOverride ?? GameRules.Default.TimeLimitMinutes,
|
||||
TurnLimit = lobby.TurnLimitOverride ?? GameRules.Default.TurnLimit,
|
||||
MpBuff = lobby.MpBuffOverride ?? GameRules.Default.MpBuff,
|
||||
};
|
||||
|
||||
// Create the game session with the rule snapshot.
|
||||
@@ -115,15 +128,13 @@ internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHand
|
||||
}
|
||||
|
||||
// Push game-started notification to both players.
|
||||
var push = new CustomLobbyGameStartedPush(session.Id, accountId);
|
||||
var pushToHost = new CustomLobbyGameStartedPush(session.Id, accountId);
|
||||
var pushToGuest = new CustomLobbyGameStartedPush(session.Id, lobby.HostAccountId);
|
||||
|
||||
await Task.WhenAll(
|
||||
_clientPush.PushAsync(lobby.HostAccountId.ToString(),
|
||||
ServerMessage.Push("game.custom.started", pushToHost), ct),
|
||||
ServerMessage.Push("game.custom.started",
|
||||
new CustomLobbyGameStartedPush(session.Id, accountId)), ct),
|
||||
_clientPush.PushAsync(accountId.ToString(),
|
||||
ServerMessage.Push("game.custom.started", pushToGuest), ct));
|
||||
ServerMessage.Push("game.custom.started",
|
||||
new CustomLobbyGameStartedPush(session.Id, lobby.HostAccountId)), ct));
|
||||
|
||||
return ServerMessage.Reply("game.custom.join.ack", message.Seq,
|
||||
new JoinCustomLobbyResponse(
|
||||
|
||||
@@ -9,6 +9,7 @@ using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// game.custom.list — returns a paged list of open custom lobbies.
|
||||
/// Supports optional NameSearch filter and ShowPasswordProtected toggle.
|
||||
/// </summary>
|
||||
internal sealed class GameListCustomLobbiesHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
@@ -35,8 +36,15 @@ internal sealed class GameListCustomLobbiesHandler : GameHandlerBase, IMessageHa
|
||||
|
||||
var query = _gameDb.CustomLobbies
|
||||
.AsNoTracking()
|
||||
.Where(l => l.Status == CustomLobbyStatus.Open)
|
||||
.OrderByDescending(l => l.CreatedAt);
|
||||
.Where(l => l.Status == CustomLobbyStatus.Open);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(req.NameSearch))
|
||||
query = query.Where(l => l.Name.ToLower().Contains(req.NameSearch.ToLower()));
|
||||
|
||||
if (!req.ShowPasswordProtected)
|
||||
query = query.Where(l => l.Password == null);
|
||||
|
||||
query = query.OrderByDescending(l => l.CreatedAt);
|
||||
|
||||
var total = await query.CountAsync(ct);
|
||||
var lobbies = await query
|
||||
@@ -45,20 +53,30 @@ internal sealed class GameListCustomLobbiesHandler : GameHandlerBase, IMessageHa
|
||||
.ToListAsync(ct);
|
||||
|
||||
var dtos = lobbies.Select(l => new CustomLobbyDto(
|
||||
LobbyId : l.Id,
|
||||
LobbyName : l.Name,
|
||||
HostAccountId : l.HostAccountId,
|
||||
HasGuest : l.GuestAccountId.HasValue,
|
||||
Rules : new CustomRulesDto(
|
||||
LobbyId : l.Id,
|
||||
LobbyName : l.Name,
|
||||
HostAccountId : l.HostAccountId,
|
||||
HasGuest : l.GuestAccountId.HasValue,
|
||||
Rules : new CustomRulesDto(
|
||||
OpeningHandSize : l.OpeningHandSizeOverride ?? GameRules.Default.OpeningHandSize,
|
||||
FirstPlayerSkipsDraw : l.FirstPlayerSkipsDrawOverride ?? GameRules.Default.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn : l.SpAccumulationPerTurnOverride ?? GameRules.Default.SpAccumulationPerTurn,
|
||||
DrawPerTurn : l.DrawPerTurnOverride ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap : l.SpHasCapOverride ?? GameRules.Default.SpHasCap,
|
||||
SpCap : l.SpCapOverride ?? GameRules.Default.SpCap,
|
||||
BoardWidth : GameRules.Default.BoardWidth,
|
||||
BoardHeight : GameRules.Default.BoardHeight),
|
||||
CreatedAt : l.CreatedAt));
|
||||
FirstPlayerSkipsDraw : l.FirstPlayerSkipsDrawOverride ?? GameRules.Default.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn : l.SpAccumulationPerTurnOverride ?? GameRules.Default.SpAccumulationPerTurn,
|
||||
DrawPerTurn : l.DrawPerTurnOverride ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap : l.SpHasCapOverride ?? GameRules.Default.SpHasCap,
|
||||
SpCap : l.SpCapOverride ?? GameRules.Default.SpCap,
|
||||
BoardWidth : l.BoardWidthOverride ?? GameRules.Default.BoardWidth,
|
||||
BoardHeight : l.BoardHeightOverride ?? GameRules.Default.BoardHeight,
|
||||
HandSizeCap : l.HandSizeCapOverride ?? GameRules.Default.HandSizeCap,
|
||||
DiscardOnMaxHand : l.DiscardOnMaxHandOverride ?? GameRules.Default.DiscardOnMaxHand,
|
||||
MaxDeckSize : l.MaxDeckSizeOverride ?? GameRules.Default.MaxDeckSize,
|
||||
MinDeckSize : l.MinDeckSizeOverride ?? GameRules.Default.MinDeckSize,
|
||||
MaxArchTier1 : l.MaxArchTier1Override ?? GameRules.Default.MaxArchTier1,
|
||||
TurnTimerSeconds : l.TurnTimerSecondsOverride ?? GameRules.Default.TurnTimerSeconds,
|
||||
TimeLimitMinutes : l.TimeLimitMinutesOverride ?? GameRules.Default.TimeLimitMinutes,
|
||||
TurnLimit : l.TurnLimitOverride ?? GameRules.Default.TurnLimit,
|
||||
MpBuff : l.MpBuffOverride ?? GameRules.Default.MpBuff,
|
||||
HasPassword : l.Password != null),
|
||||
CreatedAt : l.CreatedAt));
|
||||
|
||||
return ServerMessage.Reply("game.custom.list.ack", message.Seq,
|
||||
new ListCustomLobbiesResponse(
|
||||
|
||||
Reference in New Issue
Block a user