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)
|
||||
|
||||
Reference in New Issue
Block a user