- ChatChannel: add ArchivedAt (nullable DateTimeOffset) - ChatChannelMembership.EndMatchMembershipsAsync: set channel.ArchivedAt so match channels are archived when games end - GameHandlerBase.FinalizeGameAsync: add IEventBus param; compute OriginPoints rewards scaled by rank diff; publish GameEndedEvent so ChatLifecycleHostedService triggers archival - GameRuleSet: add WinnerOpReward (50), LoserOpReward (10), RankDiffOpMultiplierPct (0) with NOT NULL + defaults - GameMessages.GameOverPush: add WinnerOpDelta / LoserOpDelta fields - GameConcedeHandler + GameAttackHandler: inject IEventBus, pass to FinalizeGameAsync - Migrations: 20260309200000_AddArchivedAtToChatChannel (ChatDbContext), 20260309210000_AddOpRewardFields (GameDbContext) - Admin GameSessionsPage: inject ChatDbContext; purge Match channels before deleting sessions in PurgeAllAsync - Admin RankedRulesPage: new "Origin Points Rewards" card with three number inputs (WinnerOpReward, LoserOpReward, RankDiffOpMultiplierPct) - Admin AccountDetail: display RankScore + OriginPoints in Profile card; new "Award OP" card that credits OP directly to the account balance 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>
80 lines
2.7 KiB
C#
80 lines
2.7 KiB
C#
namespace SOR.Game.Handlers;
|
|
|
|
using System.Text.Json;
|
|
using SOR.Database.Contexts;
|
|
using SOR.Networking.Abstractions.EventBus;
|
|
using SOR.Networking.Abstractions.Messaging;
|
|
using SOR.Networking.Abstractions.Push;
|
|
using SOR.Networking.Abstractions.Transport;
|
|
using SOR.Shared.Networking;
|
|
|
|
/// <summary>
|
|
/// game.attack — active player attacks with one of their field cards.
|
|
/// </summary>
|
|
internal sealed class GameAttackHandler : GameHandlerBase, IMessageHandler
|
|
{
|
|
private readonly IMessageSerializer _serializer;
|
|
private readonly GameDbContext _gameDb;
|
|
private readonly SorIdentityDbContext _identityDb;
|
|
private readonly IClientPush _clientPush;
|
|
private readonly IEventBus _eventBus;
|
|
|
|
public GameAttackHandler(
|
|
IMessageSerializer serializer,
|
|
GameDbContext gameDb,
|
|
SorIdentityDbContext identityDb,
|
|
IClientPush clientPush,
|
|
IEventBus eventBus)
|
|
{
|
|
_serializer = serializer;
|
|
_gameDb = gameDb;
|
|
_identityDb = identityDb;
|
|
_clientPush = clientPush;
|
|
_eventBus = eventBus;
|
|
}
|
|
|
|
public string MessageType => "game.attack";
|
|
|
|
public async Task<IServerMessage?> HandleAsync(
|
|
IClientMessage message,
|
|
AuthenticatedConnection connection,
|
|
CancellationToken ct)
|
|
{
|
|
var req = _serializer.DeserializePayload<GameAttackRequest>(message.Payload);
|
|
var actorId = connection.UserId;
|
|
|
|
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
|
var state = GameEngine.Deserialize(session.StateJson!);
|
|
|
|
var (newState, gameOver) = GameEngine.Attack(
|
|
state, actorId, req.AttackerInstanceId, req.TargetInstanceId);
|
|
|
|
var dto = GameEngine.ToDto(newState, actorId);
|
|
await SaveStateAsync(_gameDb, session, newState, actorId, "game.attack",
|
|
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
|
|
|
|
if (gameOver)
|
|
{
|
|
// The attacking player destroyed the opponent's ArchSpirit.
|
|
var loserId = session.PlayerOneAccountId.ToString() == actorId
|
|
? session.PlayerTwoAccountId
|
|
: session.PlayerOneAccountId;
|
|
|
|
await FinalizeGameAsync(
|
|
_gameDb, _identityDb, _clientPush, _eventBus,
|
|
session,
|
|
winnerAccountId: Guid.Parse(actorId),
|
|
loserAccountId: loserId,
|
|
winCondition: "health",
|
|
ct);
|
|
}
|
|
else
|
|
{
|
|
await PushStateToPlayersAsync(_clientPush, session, newState, ct);
|
|
}
|
|
|
|
return ServerMessage.Reply("game.attack.ack", message.Seq,
|
|
new GameAttackResponse(dto));
|
|
}
|
|
}
|