Fix gameplay engine rules + add RuleSet/CustomLobby system
Engine corrections (per gameplay.md + user spec): - Opening draw: both players draw 6 cards at game start - P1 skips Draw phase on turn 1 (already has opening hand); P2 draws once at start of their first turn → 7 cards total - SP: starts at 0, accumulates +3/turn (never resets, no cap by default) - ST: starts at 0, earned only through sacrifice (no per-turn grant) - No entry sickness: units can attack/move/ability the turn they are summoned - Defense restores to MaxDefense at start of each turn (§6.9.3) - Rename Graveyard → Discard throughout (GameState, GameEngine, GameMessages) - Remove IsEntrySick flag from CardInstance and CardInstanceDto GameRuleSet / CustomLobby infrastructure: - GameRuleSet DB entity: admin-configurable rules (IsRankedDefault, IsCustomDefault) - GameRules record (SOR.Game): in-memory config embedded in GameState.Rules - GameSession.RuleSnapshotJson: immutable per-session rule snapshot (jsonb) - MatchmakerService: loads ranked ruleset and snapshots it into each GameSession - GameReadyHandler: deserializes rule snapshot and passes rules to GameEngine.Initialize - CustomLobby DB entity: host+guest, rule overrides, GameSessionId link - CustomLobbyStatus enum: Open / Full / Started / Cancelled Custom game handlers (4): - game.custom.create — host creates lobby with optional rule overrides - game.custom.join — guest joins; when full, creates GameSession + pushes both players - game.custom.leave — host cancels or guest leaves - game.custom.list — paged list of Open lobbies Admin portal: - /game/ranked-rules — edit the ranked play ruleset (auto-seeds default on first visit) - /game/custom-lobbies — read-only view of active and recent custom lobbies - NavMenu: added Ranked Rules + Custom Lobbies links under Game Migration: AddGameRulesAndCustomLobby (game_rule_sets, custom_lobbies, rule_snapshot column on game_sessions) 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:
@@ -0,0 +1,106 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Game;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// game.custom.join — guest joins an open custom lobby with their chosen deck.
|
||||
///
|
||||
/// When the lobby is full (both host and guest present), a GameSession is created
|
||||
/// immediately and CustomLobbyGameStartedPush is sent to both players. Both players
|
||||
/// then send game.ready to start the actual game.
|
||||
/// </summary>
|
||||
internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
|
||||
public GameJoinCustomLobbyHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_clientPush = clientPush;
|
||||
}
|
||||
|
||||
public string MessageType => "game.custom.join";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<JoinCustomLobbyRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
var lobby = await _gameDb.CustomLobbies.FindAsync([req.LobbyId], ct)
|
||||
?? throw new GameException("LOBBY_NOT_FOUND");
|
||||
|
||||
if (lobby.Status != CustomLobbyStatus.Open)
|
||||
throw new GameException("LOBBY_NOT_OPEN");
|
||||
|
||||
if (lobby.HostAccountId == accountId)
|
||||
throw new GameException("CANNOT_JOIN_OWN_LOBBY");
|
||||
|
||||
lobby.GuestAccountId = accountId;
|
||||
lobby.GuestDeckId = req.DeckId;
|
||||
lobby.Status = CustomLobbyStatus.Full;
|
||||
|
||||
// Build the effective rules from stored overrides (falling back to Default).
|
||||
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,
|
||||
};
|
||||
|
||||
// Create the game session with the rule snapshot.
|
||||
var session = new GameSession
|
||||
{
|
||||
PlayerOneAccountId = lobby.HostAccountId,
|
||||
PlayerOneDeckId = lobby.HostDeckId,
|
||||
PlayerTwoAccountId = accountId,
|
||||
PlayerTwoDeckId = req.DeckId,
|
||||
Status = GameSessionStatus.Initializing,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
RuleSnapshotJson = JsonSerializer.Serialize(rules),
|
||||
};
|
||||
|
||||
_gameDb.GameSessions.Add(session);
|
||||
|
||||
lobby.GameSessionId = session.Id;
|
||||
lobby.Status = CustomLobbyStatus.Started;
|
||||
|
||||
await _gameDb.SaveChangesAsync(ct);
|
||||
|
||||
// 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),
|
||||
_clientPush.PushAsync(accountId.ToString(),
|
||||
ServerMessage.Push("game.custom.started", pushToGuest), ct));
|
||||
|
||||
return ServerMessage.Reply("game.custom.join.ack", message.Seq,
|
||||
new JoinCustomLobbyResponse(
|
||||
LobbyId : lobby.Id,
|
||||
Status : "GameCreated",
|
||||
GameSessionId : session.Id));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user