Implement custom rules editor + guest rule suggestions
Server: - RuleSuggestionService — in-memory pending suggestion store (one per lobby) - GameSuggestRulesHandler (game.custom.suggest_rules) — guest proposes rules - GameAcceptRuleSuggestionHandler (game.custom.suggest_rules.accept) — host applies suggestion - GameRejectRuleSuggestionHandler (game.custom.suggest_rules.reject) — host dismisses - New message types: SuggestRulesRequest/Response, AcceptRuleSuggestionRequest/Response, RejectRuleSuggestionRequest/Response, LobbyRuleSuggestionPush, LobbyRuleSuggestionRejectedPush - ServiceRegistration: register RuleSuggestionService + 3 new handlers Client: - CustomLobby.gd: rule_suggestion_received/rejected signals + suggest/accept/reject methods - LobbyRoomScreen.tscn: add SuggestionBanner (host accept/reject UI); EditRulesBtn always visible - lobby_room_screen.gd: replace OS.alert stub with full scrollable rules editor popup (SpinBoxes + CheckBoxes for all 17 rule fields, grouped by category); host sees "Apply", guest sees "Suggest"; SuggestionBanner shows proposed rules summary to host 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:
@@ -176,6 +176,17 @@ public sealed record UpdateLobbyRulesRequest(Guid LobbyId, CustomRulesOverride R
|
||||
/// <summary>List all in-progress custom games (Status == Started). Supports paging.</summary>
|
||||
public sealed record ListActiveLobbiesRequest(int Page = 1, int PageSize = 20);
|
||||
|
||||
// ── Rule suggestion requests (guest → host) ───────────────────────────────────
|
||||
|
||||
/// <summary>Guest proposes rule overrides to the host. Host can accept or reject.</summary>
|
||||
public sealed record SuggestRulesRequest(Guid LobbyId, CustomRulesOverride Rules);
|
||||
|
||||
/// <summary>Host accepts the guest's pending rule suggestion; applies it as the new overrides.</summary>
|
||||
public sealed record AcceptRuleSuggestionRequest(Guid LobbyId);
|
||||
|
||||
/// <summary>Host rejects the guest's pending rule suggestion.</summary>
|
||||
public sealed record RejectRuleSuggestionRequest(Guid LobbyId);
|
||||
|
||||
// ── Responses ─────────────────────────────────────────────────────────────────
|
||||
|
||||
public sealed record GameReadyResponse(Guid SessionId, string Status);
|
||||
@@ -265,6 +276,10 @@ public sealed record ChangeLobbyDeckResponse(Guid LobbyId);
|
||||
|
||||
public sealed record UpdateLobbyRulesResponse(Guid LobbyId, CustomRulesDto Rules);
|
||||
|
||||
public sealed record SuggestRulesResponse(Guid LobbyId);
|
||||
public sealed record AcceptRuleSuggestionResponse(Guid LobbyId);
|
||||
public sealed record RejectRuleSuggestionResponse(Guid LobbyId);
|
||||
|
||||
public sealed record ListActiveLobbiesResponse(
|
||||
IEnumerable<ActiveLobbyDto> Games,
|
||||
int TotalCount,
|
||||
@@ -354,6 +369,18 @@ public sealed record LobbyDeckChangedPush(Guid LobbyId, Guid AccountId, Guid Dec
|
||||
/// </summary>
|
||||
public sealed record LobbyPartnerLeftPush(Guid LobbyId, string Reason);
|
||||
|
||||
/// <summary>
|
||||
/// Pushed to the host when the guest submits a rule suggestion.
|
||||
/// The host can accept (game.custom.suggest_rules.accept) or reject (game.custom.suggest_rules.reject).
|
||||
/// </summary>
|
||||
public sealed record LobbyRuleSuggestionPush(
|
||||
Guid LobbyId,
|
||||
Guid SuggesterAccountId,
|
||||
CustomRulesOverride SuggestedRules);
|
||||
|
||||
/// <summary>Pushed to the guest when the host rejects their rule suggestion.</summary>
|
||||
public sealed record LobbyRuleSuggestionRejectedPush(Guid LobbyId);
|
||||
|
||||
// ── DTOs ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using SOR.Database.Contexts;
|
||||
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.suggest_rules.accept — host accepts the guest's pending rule suggestion.
|
||||
///
|
||||
/// Applies the suggested overrides to the lobby entity (same logic as GameUpdateLobbyRulesHandler),
|
||||
/// clears the pending suggestion, and pushes LobbyUpdatedPush to both participants.
|
||||
/// </summary>
|
||||
internal sealed class GameAcceptRuleSuggestionHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly RuleSuggestionService _suggestions;
|
||||
|
||||
public GameAcceptRuleSuggestionHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush,
|
||||
RuleSuggestionService suggestions)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_clientPush = clientPush;
|
||||
_suggestions = suggestions;
|
||||
}
|
||||
|
||||
public string MessageType => "game.custom.suggest_rules.accept";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<AcceptRuleSuggestionRequest>(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");
|
||||
|
||||
if (lobby.HostAccountId != accountId)
|
||||
throw new GameException("HOST_ONLY");
|
||||
|
||||
if (lobby.HostReady || lobby.GuestReady)
|
||||
throw new GameException("CANNOT_CHANGE_RULES_WHILE_READY");
|
||||
|
||||
var suggestion = _suggestions.Get(lobby.Id)
|
||||
?? throw new GameException("NO_PENDING_SUGGESTION");
|
||||
|
||||
// Apply overrides (same mapping as GameUpdateLobbyRulesHandler).
|
||||
var r = suggestion.Rules;
|
||||
lobby.OpeningHandSizeOverride = r.OpeningHandSize;
|
||||
lobby.FirstPlayerSkipsDrawOverride = r.FirstPlayerSkipsDraw;
|
||||
lobby.SpAccumulationPerTurnOverride = r.SpAccumulationPerTurn;
|
||||
lobby.DrawPerTurnOverride = r.DrawPerTurn;
|
||||
lobby.SpHasCapOverride = r.SpHasCap;
|
||||
lobby.SpCapOverride = r.SpCap;
|
||||
lobby.BoardWidthOverride = r.BoardWidth;
|
||||
lobby.BoardHeightOverride = r.BoardHeight;
|
||||
lobby.HandSizeCapOverride = r.HandSizeCap;
|
||||
lobby.DiscardOnMaxHandOverride = r.DiscardOnMaxHand;
|
||||
lobby.MaxDeckSizeOverride = r.MaxDeckSize;
|
||||
lobby.MinDeckSizeOverride = r.MinDeckSize;
|
||||
lobby.MaxArchTier1Override = r.MaxArchTier1;
|
||||
lobby.TurnTimerSecondsOverride = r.TurnTimerSeconds;
|
||||
lobby.TimeLimitMinutesOverride = r.TimeLimitMinutes;
|
||||
lobby.TurnLimitOverride = r.TurnLimit;
|
||||
lobby.MpBuffOverride = r.MpBuff;
|
||||
|
||||
await _gameDb.SaveChangesAsync(ct);
|
||||
_suggestions.Remove(lobby.Id);
|
||||
|
||||
var effective = GameCreateCustomLobbyHandler.ToDto(new GameRules
|
||||
{
|
||||
OpeningHandSize = r.OpeningHandSize ?? GameRules.Default.OpeningHandSize,
|
||||
FirstPlayerSkipsDraw = r.FirstPlayerSkipsDraw ?? GameRules.Default.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn = r.SpAccumulationPerTurn ?? GameRules.Default.SpAccumulationPerTurn,
|
||||
DrawPerTurn = r.DrawPerTurn ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap = r.SpHasCap ?? GameRules.Default.SpHasCap,
|
||||
SpCap = r.SpCap ?? GameRules.Default.SpCap,
|
||||
BoardWidth = r.BoardWidth ?? GameRules.Default.BoardWidth,
|
||||
BoardHeight = r.BoardHeight ?? GameRules.Default.BoardHeight,
|
||||
HandSizeCap = r.HandSizeCap ?? GameRules.Default.HandSizeCap,
|
||||
DiscardOnMaxHand = r.DiscardOnMaxHand ?? GameRules.Default.DiscardOnMaxHand,
|
||||
MaxDeckSize = r.MaxDeckSize ?? GameRules.Default.MaxDeckSize,
|
||||
MinDeckSize = r.MinDeckSize ?? GameRules.Default.MinDeckSize,
|
||||
MaxArchTier1 = r.MaxArchTier1 ?? GameRules.Default.MaxArchTier1,
|
||||
TurnTimerSeconds = r.TurnTimerSeconds ?? GameRules.Default.TurnTimerSeconds,
|
||||
TimeLimitMinutes = r.TimeLimitMinutes ?? GameRules.Default.TimeLimitMinutes,
|
||||
TurnLimit = r.TurnLimit ?? GameRules.Default.TurnLimit,
|
||||
MpBuff = r.MpBuff ?? GameRules.Default.MpBuff,
|
||||
}, lobby.Password != null);
|
||||
|
||||
var updatedPush = ServerMessage.Push("game.custom.lobby_updated",
|
||||
new LobbyUpdatedPush(lobby.Id, effective));
|
||||
|
||||
await Task.WhenAll(
|
||||
_clientPush.PushAsync(lobby.HostAccountId.ToString(), updatedPush, ct),
|
||||
_clientPush.PushAsync(lobby.GuestAccountId!.Value.ToString(), updatedPush, ct));
|
||||
|
||||
return ServerMessage.Reply("game.custom.suggest_rules.accept.ack", message.Seq,
|
||||
new AcceptRuleSuggestionResponse(lobby.Id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using SOR.Database.Contexts;
|
||||
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.suggest_rules.reject — host rejects the guest's pending rule suggestion.
|
||||
///
|
||||
/// Clears the pending suggestion and pushes LobbyRuleSuggestionRejectedPush to the guest.
|
||||
/// </summary>
|
||||
internal sealed class GameRejectRuleSuggestionHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly RuleSuggestionService _suggestions;
|
||||
|
||||
public GameRejectRuleSuggestionHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush,
|
||||
RuleSuggestionService suggestions)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_clientPush = clientPush;
|
||||
_suggestions = suggestions;
|
||||
}
|
||||
|
||||
public string MessageType => "game.custom.suggest_rules.reject";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<RejectRuleSuggestionRequest>(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.HostAccountId != accountId)
|
||||
throw new GameException("HOST_ONLY");
|
||||
|
||||
_suggestions.Remove(lobby.Id);
|
||||
|
||||
if (lobby.GuestAccountId.HasValue)
|
||||
await _clientPush.PushAsync(
|
||||
lobby.GuestAccountId.Value.ToString(),
|
||||
ServerMessage.Push("game.custom.lobby.rule_suggestion.rejected",
|
||||
new LobbyRuleSuggestionRejectedPush(lobby.Id)),
|
||||
ct);
|
||||
|
||||
return ServerMessage.Reply("game.custom.suggest_rules.reject.ack", message.Seq,
|
||||
new RejectRuleSuggestionResponse(lobby.Id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using SOR.Database.Contexts;
|
||||
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.suggest_rules — guest proposes rule overrides to the host.
|
||||
///
|
||||
/// Only allowed when the lobby is Full and neither player is ready.
|
||||
/// The host receives a LobbyRuleSuggestionPush and can accept or reject via
|
||||
/// game.custom.suggest_rules.accept / .reject.
|
||||
/// Any previous pending suggestion for the same lobby is replaced.
|
||||
/// </summary>
|
||||
internal sealed class GameSuggestRulesHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly RuleSuggestionService _suggestions;
|
||||
|
||||
public GameSuggestRulesHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush,
|
||||
RuleSuggestionService suggestions)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_clientPush = clientPush;
|
||||
_suggestions = suggestions;
|
||||
}
|
||||
|
||||
public string MessageType => "game.custom.suggest_rules";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<SuggestRulesRequest>(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");
|
||||
|
||||
if (lobby.GuestAccountId != accountId)
|
||||
throw new GameException("GUEST_ONLY");
|
||||
|
||||
if (lobby.HostReady || lobby.GuestReady)
|
||||
throw new GameException("CANNOT_SUGGEST_WHILE_READY");
|
||||
|
||||
_suggestions.Upsert(lobby.Id, new PendingSuggestion(lobby.Id, accountId, req.Rules));
|
||||
|
||||
await _clientPush.PushAsync(
|
||||
lobby.HostAccountId.ToString(),
|
||||
ServerMessage.Push("game.custom.lobby.rule_suggestion",
|
||||
new LobbyRuleSuggestionPush(lobby.Id, accountId, req.Rules)),
|
||||
ct);
|
||||
|
||||
return ServerMessage.Reply("game.custom.suggest_rules.ack", message.Seq,
|
||||
new SuggestRulesResponse(lobby.Id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace SOR.Game;
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
/// <summary>
|
||||
/// In-memory store for pending guest rule suggestions, one active suggestion per lobby.
|
||||
/// Thread-safe; suggestions are ephemeral and lost on restart (acceptable — they are
|
||||
/// short-lived lobby interactions).
|
||||
/// </summary>
|
||||
internal sealed record PendingSuggestion(
|
||||
Guid LobbyId,
|
||||
Guid SuggesterAccountId,
|
||||
CustomRulesOverride Rules);
|
||||
|
||||
internal sealed class RuleSuggestionService
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, PendingSuggestion> _pending = new();
|
||||
|
||||
/// <summary>Adds or replaces the pending suggestion for a lobby.</summary>
|
||||
public void Upsert(Guid lobbyId, PendingSuggestion suggestion) =>
|
||||
_pending[lobbyId] = suggestion;
|
||||
|
||||
/// <summary>Returns the pending suggestion for a lobby, or null if none.</summary>
|
||||
public PendingSuggestion? Get(Guid lobbyId) =>
|
||||
_pending.TryGetValue(lobbyId, out var s) ? s : null;
|
||||
|
||||
/// <summary>Removes the pending suggestion for a lobby.</summary>
|
||||
public void Remove(Guid lobbyId) => _pending.TryRemove(lobbyId, out _);
|
||||
}
|
||||
@@ -79,6 +79,14 @@ public static class ServiceRegistration
|
||||
services.AddTransient<IMessageHandler, GameProposePresetHandler>();
|
||||
services.AddTransient<IMessageHandler, GameVotePresetHandler>();
|
||||
|
||||
// Rule suggestion service (in-memory, one pending suggestion per lobby at a time).
|
||||
services.AddSingleton<RuleSuggestionService>();
|
||||
|
||||
// Rule suggestion handlers (guest proposes → host accepts or rejects)
|
||||
services.AddTransient<IMessageHandler, GameSuggestRulesHandler>();
|
||||
services.AddTransient<IMessageHandler, GameAcceptRuleSuggestionHandler>();
|
||||
services.AddTransient<IMessageHandler, GameRejectRuleSuggestionHandler>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user