- GameRules: add StartingRandomTilePct (int, 0–100, default 0)
- Board.Create(): accept startingRandomTilePct param; each tile
independently seeded from {Lake, Hill, Forest, Field} on Base1
- GameEngine: pass rules.StartingRandomTilePct to Board.Create()
- GameMessages: add StartingRandomTilePct to CustomRulesOverride + CustomRulesDto
- CustomLobby entity: add StartingRandomTilePctOverride column
- GameRuleSet entity: add StartingRandomTilePct column
- Migration AddStartingTileRule: new columns on custom_lobbies + game_rule_sets
- All 6 handlers wired: Create, LobbyReady, UpdateRules, AcceptSuggestion,
ListCustomLobbies, ListActiveLobbies
- Client lobby_room_screen.gd: "Starting Tile Affinity %" SpinBox (0–100)
in BOARD section; displayed in rules label and suggestion summary
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>
188 lines
8.2 KiB
C#
188 lines
8.2 KiB
C#
namespace SOR.Game.Handlers;
|
|
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Logging;
|
|
using SOR.Database.Contexts;
|
|
using SOR.Database.Entities.Game;
|
|
using SOR.Database.Enums;
|
|
using SOR.Networking.Abstractions.EventBus;
|
|
using SOR.Networking.Abstractions.Messaging;
|
|
using SOR.Networking.Abstractions.Push;
|
|
using SOR.Networking.Abstractions.Transport;
|
|
using SOR.Shared.Nats;
|
|
using SOR.Shared.Networking;
|
|
|
|
/// <summary>
|
|
/// game.custom.lobby.ready — mark the calling player as ready inside a Full lobby.
|
|
///
|
|
/// Updates the caller's deck choice and first-turn preference, then sets their
|
|
/// ready flag. When both players are ready the handler resolves who goes first,
|
|
/// creates the GameSession, and pushes CustomLobbyGameStartedPush to both.
|
|
/// </summary>
|
|
internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
|
{
|
|
private readonly IMessageSerializer _serializer;
|
|
private readonly GameDbContext _gameDb;
|
|
private readonly IClientPush _clientPush;
|
|
private readonly IEventBus _eventBus;
|
|
private readonly ILogger<GameLobbyReadyHandler> _logger;
|
|
|
|
public GameLobbyReadyHandler(
|
|
IMessageSerializer serializer,
|
|
GameDbContext gameDb,
|
|
IClientPush clientPush,
|
|
IEventBus eventBus,
|
|
ILogger<GameLobbyReadyHandler> logger)
|
|
{
|
|
_serializer = serializer;
|
|
_gameDb = gameDb;
|
|
_clientPush = clientPush;
|
|
_eventBus = eventBus;
|
|
_logger = logger;
|
|
}
|
|
|
|
public string MessageType => "game.custom.lobby.ready";
|
|
|
|
public async Task<IServerMessage?> HandleAsync(
|
|
IClientMessage message,
|
|
AuthenticatedConnection connection,
|
|
CancellationToken ct)
|
|
{
|
|
var req = _serializer.DeserializePayload<LobbyReadyRequest>(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.Full)
|
|
throw new GameException("LOBBY_NOT_FULL");
|
|
|
|
var isHost = lobby.HostAccountId == accountId;
|
|
var isGuest = lobby.GuestAccountId == accountId;
|
|
|
|
if (!isHost && !isGuest)
|
|
throw new GameException("NOT_IN_LOBBY");
|
|
|
|
// Update deck and first-turn pref, then mark ready.
|
|
if (isHost)
|
|
{
|
|
lobby.HostDeckId = req.DeckId;
|
|
lobby.HostFirstTurnPref = req.FirstTurnPref;
|
|
lobby.HostReady = true;
|
|
}
|
|
else
|
|
{
|
|
lobby.GuestDeckId = req.DeckId;
|
|
lobby.GuestFirstTurnPref = req.FirstTurnPref;
|
|
lobby.GuestReady = true;
|
|
}
|
|
|
|
await _gameDb.SaveChangesAsync(ct);
|
|
|
|
// Push updated ready status to both participants.
|
|
var readyPush = ServerMessage.Push("game.custom.lobby.ready_status",
|
|
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady));
|
|
|
|
await Task.WhenAll(
|
|
_clientPush.PushAsync(lobby.HostAccountId.ToString(), readyPush, ct),
|
|
_clientPush.PushAsync(lobby.GuestAccountId!.Value.ToString(), readyPush, ct));
|
|
|
|
// All-ready check.
|
|
bool allReady = lobby.GuestAccountId.HasValue && lobby.HostReady && lobby.GuestReady;
|
|
|
|
if (allReady)
|
|
await StartGameAsync(lobby, accountId, ct);
|
|
|
|
return ServerMessage.Reply("game.custom.lobby.ready.ack", message.Seq,
|
|
new LobbyReadyResponse(LobbyId: lobby.Id, IsReady: true));
|
|
}
|
|
|
|
private async Task StartGameAsync(
|
|
SOR.Database.Entities.Game.CustomLobby lobby,
|
|
Guid triggeringAccountId,
|
|
CancellationToken ct)
|
|
{
|
|
var guestId = lobby.GuestAccountId!.Value;
|
|
|
|
// Resolve first-turn: matching preferences → that player goes first; otherwise random.
|
|
Guid playerOneId;
|
|
if (lobby.HostFirstTurnPref == "host" && lobby.GuestFirstTurnPref == "host")
|
|
playerOneId = lobby.HostAccountId;
|
|
else if (lobby.HostFirstTurnPref == "guest" && lobby.GuestFirstTurnPref == "guest")
|
|
playerOneId = guestId;
|
|
else
|
|
playerOneId = Random.Shared.Next(2) == 0 ? lobby.HostAccountId : guestId;
|
|
|
|
var playerTwoId = playerOneId == lobby.HostAccountId ? guestId : lobby.HostAccountId;
|
|
var p1DeckId = playerOneId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value;
|
|
var p2DeckId = playerTwoId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value;
|
|
|
|
// Build effective rules from stored overrides.
|
|
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 = 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,
|
|
StartingRandomTilePct = lobby.StartingRandomTilePctOverride ?? GameRules.Default.StartingRandomTilePct,
|
|
};
|
|
|
|
var session = new GameSession
|
|
{
|
|
PlayerOneAccountId = playerOneId,
|
|
PlayerOneDeckId = p1DeckId,
|
|
PlayerTwoAccountId = playerTwoId,
|
|
PlayerTwoDeckId = p2DeckId,
|
|
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);
|
|
|
|
try
|
|
{
|
|
await _eventBus.PublishAsync(
|
|
NatsSubjects.GameStarted,
|
|
new GameStartedEvent(
|
|
session.Id,
|
|
new[] { playerOneId, playerTwoId },
|
|
DateTimeOffset.UtcNow),
|
|
ct);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex,
|
|
"Failed publishing game-started event for custom lobby session {SessionId}",
|
|
session.Id);
|
|
}
|
|
|
|
// Push game-started notification to both players.
|
|
await Task.WhenAll(
|
|
_clientPush.PushAsync(playerOneId.ToString(),
|
|
ServerMessage.Push("game.custom.started",
|
|
new CustomLobbyGameStartedPush(session.Id, playerTwoId)), ct),
|
|
_clientPush.PushAsync(playerTwoId.ToString(),
|
|
ServerMessage.Push("game.custom.started",
|
|
new CustomLobbyGameStartedPush(session.Id, playerOneId)), ct));
|
|
}
|
|
}
|