Phase 8: Free Play ready-up system + client scenes
Server (Phase A): - CustomLobby: +HostReady/GuestReady/HostFirstTurnPref/GuestFirstTurnPref/MaxPlayers - Migration: AddReadyUpSystem — applied to prod DB - GameJoinCustomLobbyHandler: no longer auto-creates session; sets Full status, resets readiness, pushes CustomLobbyGuestJoinedPush to host - GameLobbyReadyHandler (NEW): game.custom.lobby.ready — marks caller ready, resolves first-turn, creates session + pushes game.custom.started when all ready - GameUnreadyHandler (NEW): game.custom.lobby.unready - GameChangeLobbyDeckHandler (NEW): game.custom.lobby.change_deck - GameUpdateLobbyRulesHandler (NEW): game.custom.update_rules (host only) - GameListActiveLobbiesHandler (NEW): game.custom.list_active (Status==Started) - GameListCustomLobbiesHandler: now batch-loads host/guest display names - GameMessages: +LobbyReadyRequest/Response, UnreadyInLobbyRequest/Response, ChangeLobbyDeckRequest/Response, UpdateLobbyRulesRequest/Response, ListActiveLobbiesRequest/Response, LobbyReadyStatusPush, LobbyDeckChangedPush, ActiveLobbyDto; CustomLobbyDto +HostDisplayName/GuestDisplayName - ServiceRegistration: register 5 new handlers Client (Phase B): - CustomLobby.gd: new autoload for Free Play lobby state + all server calls - project.godot: register CustomLobby autoload - MainScreen: +Free Play button → FreePlayScreen.tscn - FreePlayScreen (NEW): Open Lobbies tab + Active Games tab + Create dialog - LobbyRoomScreen (NEW): ready-up UI, first-turn selector, preset voting - Game.gd: +is_spectating, +start_spectating(), +stop_spectating() - game_screen.gd: spectator mode — SPECTATING label, hidden action buttons, neutral HUD labels, stop_spectating() on exit 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:
@@ -1,45 +1,33 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
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.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.
|
||||
/// When the guest joins, the lobby transitions to Full and both players enter
|
||||
/// the lobby room. The game does NOT start here — both players must mark themselves
|
||||
/// ready via game.custom.lobby.ready before the GameSession is created.
|
||||
/// </summary>
|
||||
internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly ILogger<GameJoinCustomLobbyHandler> _logger;
|
||||
|
||||
public GameJoinCustomLobbyHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush,
|
||||
IEventBus eventBus,
|
||||
ILogger<GameJoinCustomLobbyHandler> logger)
|
||||
IClientPush clientPush)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_clientPush = clientPush;
|
||||
_eventBus = eventBus;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string MessageType => "game.custom.join";
|
||||
@@ -65,81 +53,31 @@ internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHand
|
||||
if (lobby.Password != null && lobby.Password != req.Password)
|
||||
throw new GameException("WRONG_PASSWORD");
|
||||
|
||||
// Seat the guest and transition to Full.
|
||||
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,
|
||||
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.
|
||||
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;
|
||||
// Reset readiness — joining always starts a fresh ready-up cycle.
|
||||
lobby.HostReady = false;
|
||||
lobby.GuestReady = false;
|
||||
lobby.HostFirstTurnPref = null;
|
||||
lobby.GuestFirstTurnPref = null;
|
||||
|
||||
await _gameDb.SaveChangesAsync(ct);
|
||||
|
||||
try
|
||||
{
|
||||
await _eventBus.PublishAsync(
|
||||
NatsSubjects.GameStarted,
|
||||
new GameStartedEvent(
|
||||
session.Id,
|
||||
new[] { lobby.HostAccountId, accountId },
|
||||
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(lobby.HostAccountId.ToString(),
|
||||
ServerMessage.Push("game.custom.started",
|
||||
new CustomLobbyGameStartedPush(session.Id, accountId)), ct),
|
||||
_clientPush.PushAsync(accountId.ToString(),
|
||||
ServerMessage.Push("game.custom.started",
|
||||
new CustomLobbyGameStartedPush(session.Id, lobby.HostAccountId)), ct));
|
||||
// Notify the host that their guest has arrived.
|
||||
await _clientPush.PushAsync(lobby.HostAccountId.ToString(),
|
||||
ServerMessage.Push("game.custom.lobby.guest_joined",
|
||||
new CustomLobbyGuestJoinedPush(
|
||||
LobbyId : lobby.Id,
|
||||
GuestAccountId: accountId,
|
||||
LobbyName : lobby.Name)), ct);
|
||||
|
||||
return ServerMessage.Reply("game.custom.join.ack", message.Seq,
|
||||
new JoinCustomLobbyResponse(
|
||||
LobbyId : lobby.Id,
|
||||
Status : "GameCreated",
|
||||
GameSessionId : session.Id));
|
||||
LobbyId : lobby.Id,
|
||||
Status : "WaitingForReady",
|
||||
GameSessionId: null));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user