Add matchmaking + gameplay services (SOR.Matchmaking + SOR.Game)
DB layer:
- Account.RankScore (int, default 1000) — ELO rank for matchmaking
- GameEnums: MatchmakingStatus, GameSessionStatus, GamePhase
- Entities: MatchmakingEntry, GameSession, GameActionLog
- GameDbContext (schema: game) + AddGameDatabase() registration
- Migrations: AddRankScore (Identity), InitialCreate (Game)
SOR.Matchmaking:
- MatchmakerService (BackgroundService) — 5s poll, ELO ±100→±500 range
- Handlers: matchmaking.join, matchmaking.leave, matchmaking.status
- Pushes MatchFoundPush to both players via IClientPush on match
SOR.Game:
- GameState / PlayerState / CardInstance / FieldCard JSONB model
- GameEngine (pure static): Init, Draw, PlayCard, Attack, EndTurn, Concede
- ELO delta (K=32) computed and applied on game end
- Handlers: game.ready, game.get_state, game.draw, game.play_card,
game.attack, game.end_turn, game.concede
- State filtered per-viewer (opponent hand/deck hidden)
- GameOverPush with rank deltas to both players
Admin pages:
- /game/matchmaking — live queue stats, active entries, recent activity
- /game/sessions — session stats, filterable by status/account
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,64 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// game.concede — the calling player forfeits the match.
|
||||
/// </summary>
|
||||
internal sealed class GameConcedeHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
|
||||
public GameConcedeHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
SorIdentityDbContext identityDb,
|
||||
IClientPush clientPush)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_identityDb = identityDb;
|
||||
_clientPush = clientPush;
|
||||
}
|
||||
|
||||
public string MessageType => "game.concede";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<GameConcedeRequest>(message.Payload);
|
||||
var actorId = connection.UserId;
|
||||
|
||||
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
var newState = GameEngine.Concede(state, actorId);
|
||||
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.concede",
|
||||
JsonSerializer.Serialize(req), ct);
|
||||
|
||||
var winnerId = session.PlayerOneAccountId.ToString() == actorId
|
||||
? session.PlayerTwoAccountId
|
||||
: session.PlayerOneAccountId;
|
||||
|
||||
await FinalizeGameAsync(
|
||||
_gameDb, _identityDb, _clientPush,
|
||||
session,
|
||||
winnerAccountId: winnerId,
|
||||
loserAccountId: Guid.Parse(actorId),
|
||||
winCondition: "concede",
|
||||
ct);
|
||||
|
||||
return ServerMessage.Reply("game.concede.ack", message.Seq,
|
||||
new GameConcedeResponse(GameEngine.ToDto(newState, actorId)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user