Phase 5: Preset system — VoteSessionService + 5 handlers
- VoteSessionService singleton: ConcurrentDictionary<Guid, ActiveVote> keyed by lobbyId. TryStart prevents concurrent votes; Cast resolves immediately on any rejection or all approval. - GameSavePresetHandler (game.custom.preset.save): captures lobby override columns into CustomRulesOverride JSON and persists as LobbyPreset; participants only. - GameListPresetsHandler (game.custom.preset.list): returns all presets owned by caller. - GameDeletePresetHandler (game.custom.preset.delete): owner-only delete. - GameProposePresetHandler (game.custom.preset.propose): host-only in Full lobby; starts vote with guest as sole voter; pushes game.custom.preset.vote_requested to guest. - GameVotePresetHandler (game.custom.preset.vote): casts yes/no; Approved → applies preset overrides to lobby DB + pushes game.custom.lobby_updated to both; Rejected → pushes game.custom.preset.vote_rejected to both. - GameMessages.cs: SavePreset/ListPresets/DeletePreset/ProposePreset/VotePreset request/response types; LobbyPresetDto; PresetVoteRequestedPush, LobbyUpdatedPush, PresetVoteRejectedPush. 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:
@@ -124,6 +124,25 @@ public sealed record JoinCustomLobbyRequest(Guid LobbyId, Guid DeckId, string? P
|
|||||||
/// <summary>Leave or cancel a custom lobby.</summary>
|
/// <summary>Leave or cancel a custom lobby.</summary>
|
||||||
public sealed record LeaveCustomLobbyRequest(Guid LobbyId);
|
public sealed record LeaveCustomLobbyRequest(Guid LobbyId);
|
||||||
|
|
||||||
|
// ── Preset requests ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// <summary>Save the current lobby's rule overrides as a named preset (participants only).</summary>
|
||||||
|
public sealed record SavePresetRequest(Guid LobbyId, string PresetName);
|
||||||
|
|
||||||
|
/// <summary>List all presets owned by the calling player.</summary>
|
||||||
|
public sealed record ListPresetsRequest();
|
||||||
|
|
||||||
|
/// <summary>Delete a preset owned by the calling player.</summary>
|
||||||
|
public sealed record DeletePresetRequest(Guid PresetId);
|
||||||
|
|
||||||
|
/// <summary>Host proposes loading a preset; triggers a yes/no vote for the other participant.</summary>
|
||||||
|
public sealed record ProposePresetRequest(Guid LobbyId, Guid PresetId);
|
||||||
|
|
||||||
|
/// <summary>Cast a yes/no vote on the active preset-load proposal for a lobby.</summary>
|
||||||
|
public sealed record VotePresetRequest(Guid LobbyId, bool Accept);
|
||||||
|
|
||||||
|
// ── Spectator requests ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/// <summary>Join a game session as a spectator.</summary>
|
/// <summary>Join a game session as a spectator.</summary>
|
||||||
public sealed record SpectateRequest(Guid SessionId);
|
public sealed record SpectateRequest(Guid SessionId);
|
||||||
|
|
||||||
@@ -188,6 +207,20 @@ public sealed record JoinCustomLobbyResponse(
|
|||||||
|
|
||||||
public sealed record LeaveCustomLobbyResponse(Guid LobbyId);
|
public sealed record LeaveCustomLobbyResponse(Guid LobbyId);
|
||||||
|
|
||||||
|
// ── Preset responses ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
public sealed record SavePresetResponse(Guid PresetId, string Name);
|
||||||
|
|
||||||
|
public sealed record ListPresetsResponse(IEnumerable<LobbyPresetDto> Presets);
|
||||||
|
|
||||||
|
public sealed record DeletePresetResponse(Guid PresetId);
|
||||||
|
|
||||||
|
public sealed record ProposePresetResponse(Guid LobbyId, string Status); // "VoteStarted"
|
||||||
|
|
||||||
|
public sealed record VotePresetResponse(Guid LobbyId, string Outcome); // "Approved" | "Rejected" | "Pending"
|
||||||
|
|
||||||
|
// ── Spectator responses ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
/// <summary>Ack for game.spectate — spectator receives current filtered game state.</summary>
|
/// <summary>Ack for game.spectate — spectator receives current filtered game state.</summary>
|
||||||
public sealed record SpectateResponse(GameStateDto State);
|
public sealed record SpectateResponse(GameStateDto State);
|
||||||
|
|
||||||
@@ -253,6 +286,24 @@ public sealed record CustomLobbyGuestJoinedPush(
|
|||||||
Guid GuestAccountId,
|
Guid GuestAccountId,
|
||||||
string LobbyName);
|
string LobbyName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Pushed to the non-proposing participant when the host proposes loading a preset.
|
||||||
|
/// The recipient should display the proposed rules and prompt Accept/Reject.
|
||||||
|
/// </summary>
|
||||||
|
public sealed record PresetVoteRequestedPush(
|
||||||
|
Guid LobbyId,
|
||||||
|
Guid PresetId,
|
||||||
|
string PresetName,
|
||||||
|
CustomRulesOverride Rules);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Pushed to both participants when a preset-load vote is approved and lobby rules update.
|
||||||
|
/// </summary>
|
||||||
|
public sealed record LobbyUpdatedPush(Guid LobbyId, CustomRulesDto NewRules);
|
||||||
|
|
||||||
|
/// <summary>Pushed to both participants when a preset-load vote is rejected.</summary>
|
||||||
|
public sealed record PresetVoteRejectedPush(Guid LobbyId, Guid PresetId);
|
||||||
|
|
||||||
// ── DTOs ─────────────────────────────────────────────────────────────────────
|
// ── DTOs ─────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -364,6 +415,13 @@ public sealed record CustomRulesDto(
|
|||||||
int MpBuff,
|
int MpBuff,
|
||||||
bool HasPassword);
|
bool HasPassword);
|
||||||
|
|
||||||
|
/// <summary>Client-facing lobby preset summary.</summary>
|
||||||
|
public sealed record LobbyPresetDto(
|
||||||
|
Guid PresetId,
|
||||||
|
string Name,
|
||||||
|
CustomRulesOverride Rules,
|
||||||
|
DateTimeOffset CreatedAt);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A single client-provided target for one action in an ability.
|
/// A single client-provided target for one action in an ability.
|
||||||
/// Exactly one of InstanceId or Coord should be set, matching the action's TargetType.
|
/// Exactly one of InstanceId or Coord should be set, matching the action's TargetType.
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
namespace SOR.Game.Handlers;
|
||||||
|
|
||||||
|
using SOR.Database.Contexts;
|
||||||
|
using SOR.Networking.Abstractions.Messaging;
|
||||||
|
using SOR.Networking.Abstractions.Transport;
|
||||||
|
using SOR.Shared.Networking;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// game.custom.preset.delete — delete a preset owned by the calling player.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class GameDeletePresetHandler : GameHandlerBase, IMessageHandler
|
||||||
|
{
|
||||||
|
private readonly IMessageSerializer _serializer;
|
||||||
|
private readonly GameDbContext _gameDb;
|
||||||
|
|
||||||
|
public GameDeletePresetHandler(IMessageSerializer serializer, GameDbContext gameDb)
|
||||||
|
{
|
||||||
|
_serializer = serializer;
|
||||||
|
_gameDb = gameDb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string MessageType => "game.custom.preset.delete";
|
||||||
|
|
||||||
|
public async Task<IServerMessage?> HandleAsync(
|
||||||
|
IClientMessage message,
|
||||||
|
AuthenticatedConnection connection,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var req = _serializer.DeserializePayload<DeletePresetRequest>(message.Payload);
|
||||||
|
var accountId = Guid.Parse(connection.UserId);
|
||||||
|
|
||||||
|
var preset = await _gameDb.LobbyPresets.FindAsync([req.PresetId], ct)
|
||||||
|
?? throw new GameException("PRESET_NOT_FOUND");
|
||||||
|
|
||||||
|
if (preset.OwnerAccountId != accountId)
|
||||||
|
throw new GameException("NOT_PRESET_OWNER");
|
||||||
|
|
||||||
|
_gameDb.LobbyPresets.Remove(preset);
|
||||||
|
await _gameDb.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
return ServerMessage.Reply("game.custom.preset.delete.ack", message.Seq,
|
||||||
|
new DeletePresetResponse(req.PresetId));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
namespace SOR.Game.Handlers;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SOR.Database.Contexts;
|
||||||
|
using SOR.Networking.Abstractions.Messaging;
|
||||||
|
using SOR.Networking.Abstractions.Transport;
|
||||||
|
using SOR.Shared.Networking;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// game.custom.preset.list — returns all presets owned by the calling player.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class GameListPresetsHandler : GameHandlerBase, IMessageHandler
|
||||||
|
{
|
||||||
|
private readonly IMessageSerializer _serializer;
|
||||||
|
private readonly GameDbContext _gameDb;
|
||||||
|
|
||||||
|
public GameListPresetsHandler(IMessageSerializer serializer, GameDbContext gameDb)
|
||||||
|
{
|
||||||
|
_serializer = serializer;
|
||||||
|
_gameDb = gameDb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string MessageType => "game.custom.preset.list";
|
||||||
|
|
||||||
|
public async Task<IServerMessage?> HandleAsync(
|
||||||
|
IClientMessage message,
|
||||||
|
AuthenticatedConnection connection,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var accountId = Guid.Parse(connection.UserId);
|
||||||
|
|
||||||
|
var presets = await _gameDb.LobbyPresets
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(p => p.OwnerAccountId == accountId)
|
||||||
|
.OrderByDescending(p => p.CreatedAt)
|
||||||
|
.ToListAsync(ct);
|
||||||
|
|
||||||
|
var dtos = presets.Select(p =>
|
||||||
|
{
|
||||||
|
var rules = JsonSerializer.Deserialize<CustomRulesOverride>(p.RulesJson)
|
||||||
|
?? new CustomRulesOverride();
|
||||||
|
return new LobbyPresetDto(p.Id, p.Name, rules, p.CreatedAt);
|
||||||
|
});
|
||||||
|
|
||||||
|
return ServerMessage.Reply("game.custom.preset.list.ack", message.Seq,
|
||||||
|
new ListPresetsResponse(dtos));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
namespace SOR.Game.Handlers;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
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.preset.propose — host proposes loading a preset in a Full lobby.
|
||||||
|
/// Starts a vote; pushes a vote-requested notification to the guest.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class GameProposePresetHandler : GameHandlerBase, IMessageHandler
|
||||||
|
{
|
||||||
|
private readonly IMessageSerializer _serializer;
|
||||||
|
private readonly GameDbContext _gameDb;
|
||||||
|
private readonly IClientPush _clientPush;
|
||||||
|
private readonly VoteSessionService _voteService;
|
||||||
|
|
||||||
|
public GameProposePresetHandler(
|
||||||
|
IMessageSerializer serializer,
|
||||||
|
GameDbContext gameDb,
|
||||||
|
IClientPush clientPush,
|
||||||
|
VoteSessionService voteService)
|
||||||
|
{
|
||||||
|
_serializer = serializer;
|
||||||
|
_gameDb = gameDb;
|
||||||
|
_clientPush = clientPush;
|
||||||
|
_voteService = voteService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string MessageType => "game.custom.preset.propose";
|
||||||
|
|
||||||
|
public async Task<IServerMessage?> HandleAsync(
|
||||||
|
IClientMessage message,
|
||||||
|
AuthenticatedConnection connection,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var req = _serializer.DeserializePayload<ProposePresetRequest>(message.Payload);
|
||||||
|
var accountId = Guid.Parse(connection.UserId);
|
||||||
|
|
||||||
|
var lobby = await _gameDb.CustomLobbies
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(l => l.Id == req.LobbyId)
|
||||||
|
.SingleOrDefaultAsync(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("NOT_LOBBY_HOST");
|
||||||
|
|
||||||
|
if (!lobby.GuestAccountId.HasValue)
|
||||||
|
throw new GameException("NO_GUEST");
|
||||||
|
|
||||||
|
var preset = await _gameDb.LobbyPresets.FindAsync([req.PresetId], ct)
|
||||||
|
?? throw new GameException("PRESET_NOT_FOUND");
|
||||||
|
|
||||||
|
if (preset.OwnerAccountId != accountId)
|
||||||
|
throw new GameException("NOT_PRESET_OWNER");
|
||||||
|
|
||||||
|
var rules = JsonSerializer.Deserialize<CustomRulesOverride>(preset.RulesJson)
|
||||||
|
?? new CustomRulesOverride();
|
||||||
|
|
||||||
|
// Start a vote — the guest is the sole voter.
|
||||||
|
var guestId = lobby.GuestAccountId.Value;
|
||||||
|
if (!_voteService.TryStart(lobby.Id, preset.Id, preset.Name, rules, guestId))
|
||||||
|
throw new GameException("VOTE_ALREADY_IN_PROGRESS");
|
||||||
|
|
||||||
|
await _clientPush.PushAsync(guestId.ToString(),
|
||||||
|
ServerMessage.Push("game.custom.preset.vote_requested",
|
||||||
|
new PresetVoteRequestedPush(lobby.Id, preset.Id, preset.Name, rules)), ct);
|
||||||
|
|
||||||
|
return ServerMessage.Reply("game.custom.preset.propose.ack", message.Seq,
|
||||||
|
new ProposePresetResponse(lobby.Id, "VoteStarted"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
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.Transport;
|
||||||
|
using SOR.Shared.Networking;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// game.custom.preset.save — save the current lobby's rule overrides as a named preset.
|
||||||
|
/// Only the host or guest of an Open/Full lobby may save a preset.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class GameSavePresetHandler : GameHandlerBase, IMessageHandler
|
||||||
|
{
|
||||||
|
private readonly IMessageSerializer _serializer;
|
||||||
|
private readonly GameDbContext _gameDb;
|
||||||
|
|
||||||
|
public GameSavePresetHandler(IMessageSerializer serializer, GameDbContext gameDb)
|
||||||
|
{
|
||||||
|
_serializer = serializer;
|
||||||
|
_gameDb = gameDb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string MessageType => "game.custom.preset.save";
|
||||||
|
|
||||||
|
public async Task<IServerMessage?> HandleAsync(
|
||||||
|
IClientMessage message,
|
||||||
|
AuthenticatedConnection connection,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var req = _serializer.DeserializePayload<SavePresetRequest>(message.Payload);
|
||||||
|
var accountId = Guid.Parse(connection.UserId);
|
||||||
|
|
||||||
|
var lobby = await _gameDb.CustomLobbies
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(l => l.Id == req.LobbyId)
|
||||||
|
.SingleOrDefaultAsync(ct)
|
||||||
|
?? throw new GameException("LOBBY_NOT_FOUND");
|
||||||
|
|
||||||
|
if (lobby.Status == CustomLobbyStatus.Cancelled || lobby.Status == CustomLobbyStatus.Started)
|
||||||
|
throw new GameException("LOBBY_NOT_ACTIVE");
|
||||||
|
|
||||||
|
if (lobby.HostAccountId != accountId && lobby.GuestAccountId != accountId)
|
||||||
|
throw new GameException("NOT_IN_LOBBY");
|
||||||
|
|
||||||
|
// Capture the lobby's current override values into a CustomRulesOverride shape.
|
||||||
|
var overrides = new CustomRulesOverride(
|
||||||
|
OpeningHandSize : lobby.OpeningHandSizeOverride,
|
||||||
|
FirstPlayerSkipsDraw : lobby.FirstPlayerSkipsDrawOverride,
|
||||||
|
SpAccumulationPerTurn : lobby.SpAccumulationPerTurnOverride,
|
||||||
|
DrawPerTurn : lobby.DrawPerTurnOverride,
|
||||||
|
SpHasCap : lobby.SpHasCapOverride,
|
||||||
|
SpCap : lobby.SpCapOverride,
|
||||||
|
BoardWidth : lobby.BoardWidthOverride,
|
||||||
|
BoardHeight : lobby.BoardHeightOverride,
|
||||||
|
HandSizeCap : lobby.HandSizeCapOverride,
|
||||||
|
DiscardOnMaxHand : lobby.DiscardOnMaxHandOverride,
|
||||||
|
MaxDeckSize : lobby.MaxDeckSizeOverride,
|
||||||
|
MinDeckSize : lobby.MinDeckSizeOverride,
|
||||||
|
MaxArchTier1 : lobby.MaxArchTier1Override,
|
||||||
|
TurnTimerSeconds : lobby.TurnTimerSecondsOverride,
|
||||||
|
TimeLimitMinutes : lobby.TimeLimitMinutesOverride,
|
||||||
|
TurnLimit : lobby.TurnLimitOverride,
|
||||||
|
MpBuff : lobby.MpBuffOverride);
|
||||||
|
|
||||||
|
var preset = new LobbyPreset
|
||||||
|
{
|
||||||
|
OwnerAccountId = accountId,
|
||||||
|
Name = req.PresetName.Trim(),
|
||||||
|
RulesJson = JsonSerializer.Serialize(overrides),
|
||||||
|
CreatedAt = DateTimeOffset.UtcNow,
|
||||||
|
};
|
||||||
|
|
||||||
|
_gameDb.LobbyPresets.Add(preset);
|
||||||
|
await _gameDb.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
return ServerMessage.Reply("game.custom.preset.save.ack", message.Seq,
|
||||||
|
new SavePresetResponse(preset.Id, preset.Name));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
namespace SOR.Game.Handlers;
|
||||||
|
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
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.preset.vote — cast a yes/no vote on an active preset-load proposal.
|
||||||
|
///
|
||||||
|
/// On Approved: applies the preset overrides to the lobby entity and pushes
|
||||||
|
/// game.custom.lobby_updated to both participants with the new effective rules.
|
||||||
|
/// On Rejected: pushes game.custom.preset.vote_rejected to both participants.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class GameVotePresetHandler : GameHandlerBase, IMessageHandler
|
||||||
|
{
|
||||||
|
private readonly IMessageSerializer _serializer;
|
||||||
|
private readonly GameDbContext _gameDb;
|
||||||
|
private readonly IClientPush _clientPush;
|
||||||
|
private readonly VoteSessionService _voteService;
|
||||||
|
|
||||||
|
public GameVotePresetHandler(
|
||||||
|
IMessageSerializer serializer,
|
||||||
|
GameDbContext gameDb,
|
||||||
|
IClientPush clientPush,
|
||||||
|
VoteSessionService voteService)
|
||||||
|
{
|
||||||
|
_serializer = serializer;
|
||||||
|
_gameDb = gameDb;
|
||||||
|
_clientPush = clientPush;
|
||||||
|
_voteService = voteService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string MessageType => "game.custom.preset.vote";
|
||||||
|
|
||||||
|
public async Task<IServerMessage?> HandleAsync(
|
||||||
|
IClientMessage message,
|
||||||
|
AuthenticatedConnection connection,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var req = _serializer.DeserializePayload<VotePresetRequest>(message.Payload);
|
||||||
|
var accountId = Guid.Parse(connection.UserId);
|
||||||
|
|
||||||
|
var (outcome, vote) = _voteService.Cast(req.LobbyId, accountId, req.Accept);
|
||||||
|
|
||||||
|
if (vote == null)
|
||||||
|
throw new GameException("NO_ACTIVE_VOTE");
|
||||||
|
|
||||||
|
switch (outcome)
|
||||||
|
{
|
||||||
|
case VoteOutcome.Approved:
|
||||||
|
await ApplyPresetAsync(req.LobbyId, vote.Rules, ct);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case VoteOutcome.Rejected:
|
||||||
|
await RejectVoteAsync(req.LobbyId, vote.PresetId, ct);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case VoteOutcome.Pending:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ServerMessage.Reply("game.custom.preset.vote.ack", message.Seq,
|
||||||
|
new VotePresetResponse(req.LobbyId, outcome.ToString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ApplyPresetAsync(Guid lobbyId, CustomRulesOverride r, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var lobby = await _gameDb.CustomLobbies.FindAsync([lobbyId], ct)
|
||||||
|
?? throw new GameException("LOBBY_NOT_FOUND");
|
||||||
|
|
||||||
|
// Apply all overrides to the lobby entity (null = use default).
|
||||||
|
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);
|
||||||
|
|
||||||
|
// Build effective rules for the push.
|
||||||
|
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);
|
||||||
|
|
||||||
|
await Task.WhenAll(
|
||||||
|
_clientPush.PushAsync(lobby.HostAccountId.ToString(),
|
||||||
|
ServerMessage.Push("game.custom.lobby_updated",
|
||||||
|
new LobbyUpdatedPush(lobbyId, effective)), ct),
|
||||||
|
_clientPush.PushAsync(lobby.GuestAccountId!.Value.ToString(),
|
||||||
|
ServerMessage.Push("game.custom.lobby_updated",
|
||||||
|
new LobbyUpdatedPush(lobbyId, effective)), ct));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task RejectVoteAsync(Guid lobbyId, Guid presetId, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var lobby = await _gameDb.CustomLobbies
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(l => l.Id == lobbyId)
|
||||||
|
.SingleOrDefaultAsync(ct);
|
||||||
|
|
||||||
|
if (lobby is null) return;
|
||||||
|
|
||||||
|
await Task.WhenAll(
|
||||||
|
_clientPush.PushAsync(lobby.HostAccountId.ToString(),
|
||||||
|
ServerMessage.Push("game.custom.preset.vote_rejected",
|
||||||
|
new PresetVoteRejectedPush(lobbyId, presetId)), ct),
|
||||||
|
_clientPush.PushAsync(lobby.GuestAccountId!.Value.ToString(),
|
||||||
|
ServerMessage.Push("game.custom.preset.vote_rejected",
|
||||||
|
new PresetVoteRejectedPush(lobbyId, presetId)), ct));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -61,6 +61,16 @@ public static class ServiceRegistration
|
|||||||
services.AddTransient<IMessageHandler, GameSpectateHandler>();
|
services.AddTransient<IMessageHandler, GameSpectateHandler>();
|
||||||
services.AddTransient<IMessageHandler, GameStopSpectatingHandler>();
|
services.AddTransient<IMessageHandler, GameStopSpectatingHandler>();
|
||||||
|
|
||||||
|
// Preset vote service (in-memory, one active vote per lobby at a time).
|
||||||
|
services.AddSingleton<VoteSessionService>();
|
||||||
|
|
||||||
|
// Preset handlers
|
||||||
|
services.AddTransient<IMessageHandler, GameSavePresetHandler>();
|
||||||
|
services.AddTransient<IMessageHandler, GameListPresetsHandler>();
|
||||||
|
services.AddTransient<IMessageHandler, GameDeletePresetHandler>();
|
||||||
|
services.AddTransient<IMessageHandler, GameProposePresetHandler>();
|
||||||
|
services.AddTransient<IMessageHandler, GameVotePresetHandler>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
namespace SOR.Game;
|
||||||
|
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
|
internal enum VoteOutcome { Pending, Approved, Rejected }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// State for one active preset-load vote in a lobby.
|
||||||
|
/// The Votes dictionary maps voter account IDs → true (accept) / false (reject) / null (not voted yet).
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class ActiveVote
|
||||||
|
{
|
||||||
|
public Guid LobbyId { get; }
|
||||||
|
public Guid PresetId { get; }
|
||||||
|
public string PresetName { get; }
|
||||||
|
public CustomRulesOverride Rules { get; }
|
||||||
|
public Dictionary<Guid, bool?> Votes { get; }
|
||||||
|
|
||||||
|
public ActiveVote(
|
||||||
|
Guid lobbyId, Guid presetId, string presetName,
|
||||||
|
CustomRulesOverride rules, Dictionary<Guid, bool?> votes)
|
||||||
|
{
|
||||||
|
LobbyId = lobbyId;
|
||||||
|
PresetId = presetId;
|
||||||
|
PresetName = presetName;
|
||||||
|
Rules = rules;
|
||||||
|
Votes = votes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// In-memory store for active preset-load votes keyed by lobby ID.
|
||||||
|
/// A lobby can only have one active vote at a time.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class VoteSessionService
|
||||||
|
{
|
||||||
|
private readonly ConcurrentDictionary<Guid, ActiveVote> _votes = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Start a new vote for <paramref name="lobbyId"/>.
|
||||||
|
/// Returns false if a vote is already in progress for that lobby.
|
||||||
|
/// <paramref name="voterId"/> is the single voter (the non-proposing participant).
|
||||||
|
/// </summary>
|
||||||
|
public bool TryStart(
|
||||||
|
Guid lobbyId,
|
||||||
|
Guid presetId,
|
||||||
|
string presetName,
|
||||||
|
CustomRulesOverride rules,
|
||||||
|
Guid voterId)
|
||||||
|
{
|
||||||
|
var vote = new ActiveVote(
|
||||||
|
lobbyId, presetId, presetName, rules,
|
||||||
|
new Dictionary<Guid, bool?> { [voterId] = null });
|
||||||
|
|
||||||
|
return _votes.TryAdd(lobbyId, vote);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Record a vote from <paramref name="accountId"/>.
|
||||||
|
/// Returns the outcome and the vote object (null if no active vote was found).
|
||||||
|
/// Removes the vote from the store when the outcome is Approved or Rejected.
|
||||||
|
/// </summary>
|
||||||
|
public (VoteOutcome outcome, ActiveVote? vote) Cast(Guid lobbyId, Guid accountId, bool accept)
|
||||||
|
{
|
||||||
|
if (!_votes.TryGetValue(lobbyId, out var vote))
|
||||||
|
return (VoteOutcome.Rejected, null);
|
||||||
|
|
||||||
|
if (!vote.Votes.ContainsKey(accountId))
|
||||||
|
return (VoteOutcome.Pending, vote);
|
||||||
|
|
||||||
|
vote.Votes[accountId] = accept;
|
||||||
|
|
||||||
|
// Any rejection resolves immediately.
|
||||||
|
if (vote.Votes.Values.Any(v => v == false))
|
||||||
|
{
|
||||||
|
_votes.TryRemove(lobbyId, out _);
|
||||||
|
return (VoteOutcome.Rejected, vote);
|
||||||
|
}
|
||||||
|
|
||||||
|
// All votes in → approve.
|
||||||
|
if (vote.Votes.Values.All(v => v.HasValue))
|
||||||
|
{
|
||||||
|
_votes.TryRemove(lobbyId, out _);
|
||||||
|
return (VoteOutcome.Approved, vote);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (VoteOutcome.Pending, vote);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActiveVote? Get(Guid lobbyId) => _votes.GetValueOrDefault(lobbyId);
|
||||||
|
|
||||||
|
public void Cancel(Guid lobbyId) => _votes.TryRemove(lobbyId, out _);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user