Feat: chat archiving + ranked OP rewards + admin award OP
- 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>
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
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.EventBus;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Nats;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
@@ -106,14 +109,21 @@ internal abstract class GameHandlerBase
|
||||
new GameStateUpdatePush(GameEngine.ToDto(state, p2Id))), ct));
|
||||
}
|
||||
|
||||
// OP config snapshot — read from RuleSnapshotJson at finalization time.
|
||||
private sealed record OpSnapshot(
|
||||
int WinnerOpReward = 50,
|
||||
int LoserOpReward = 10,
|
||||
int RankDiffOpMultiplierPct = 0);
|
||||
|
||||
/// <summary>
|
||||
/// Finalize a completed game: update session status, adjust ELO ranks,
|
||||
/// push game-over to both players.
|
||||
/// award OriginPoints, push game-over to both players, publish GameEndedEvent.
|
||||
/// </summary>
|
||||
protected static async Task FinalizeGameAsync(
|
||||
GameDbContext gameDb,
|
||||
SorIdentityDbContext identityDb,
|
||||
IClientPush clientPush,
|
||||
IEventBus eventBus,
|
||||
GameSession session,
|
||||
Guid winnerAccountId,
|
||||
Guid loserAccountId,
|
||||
@@ -125,7 +135,7 @@ internal abstract class GameHandlerBase
|
||||
session.WinCondition = winCondition;
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
// Load both accounts for ELO adjustment.
|
||||
// Load both accounts for ELO + OP adjustment.
|
||||
var accounts = await identityDb.Users
|
||||
.Where(a => a.Id == winnerAccountId || a.Id == loserAccountId)
|
||||
.ToListAsync(ct);
|
||||
@@ -133,11 +143,28 @@ internal abstract class GameHandlerBase
|
||||
var winner = accounts.First(a => a.Id == winnerAccountId);
|
||||
var loser = accounts.First(a => a.Id == loserAccountId);
|
||||
|
||||
// Capture pre-ELO ranks for OP scaling.
|
||||
var winnerPreRank = winner.RankScore;
|
||||
var loserPreRank = loser.RankScore;
|
||||
|
||||
var (winnerDelta, loserDelta) = GameEngine.ComputeEloDelta(winner.RankScore, loser.RankScore);
|
||||
|
||||
winner.RankScore += winnerDelta;
|
||||
loser.RankScore = Math.Max(0, loser.RankScore - loserDelta);
|
||||
|
||||
// Compute OriginPoints rewards from the rule snapshot.
|
||||
var snap = JsonSerializer.Deserialize<OpSnapshot>(
|
||||
session.RuleSnapshotJson ?? "{}",
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new OpSnapshot();
|
||||
|
||||
var rankDiff = loserPreRank - winnerPreRank; // positive = winner was underdog
|
||||
var adjustment = rankDiff * snap.RankDiffOpMultiplierPct / 1000;
|
||||
var winnerOp = Math.Max(1, snap.WinnerOpReward + adjustment);
|
||||
var loserOp = Math.Max(0, snap.LoserOpReward - adjustment);
|
||||
|
||||
winner.OriginPoints += winnerOp;
|
||||
loser.OriginPoints += loserOp;
|
||||
|
||||
await identityDb.SaveChangesAsync(ct);
|
||||
await gameDb.SaveChangesAsync(ct);
|
||||
|
||||
@@ -147,12 +174,24 @@ internal abstract class GameHandlerBase
|
||||
LoserAccountId : loserAccountId,
|
||||
WinCondition : winCondition,
|
||||
WinnerRankDelta : winnerDelta,
|
||||
LoserRankDelta : loserDelta);
|
||||
LoserRankDelta : loserDelta,
|
||||
WinnerOpDelta : winnerOp,
|
||||
LoserOpDelta : loserOp);
|
||||
|
||||
await Task.WhenAll(
|
||||
clientPush.PushAsync(winnerAccountId.ToString(),
|
||||
ServerMessage.Push("game.over", push), ct),
|
||||
clientPush.PushAsync(loserAccountId.ToString(),
|
||||
ServerMessage.Push("game.over", push), ct));
|
||||
|
||||
// Publish inter-service event so ChatLifecycleHostedService archives the match channel.
|
||||
await eventBus.PublishAsync(NatsSubjects.GameEnded,
|
||||
new GameEndedEvent(
|
||||
GameId : session.Id,
|
||||
WinnerId : winnerAccountId,
|
||||
LoserId : loserAccountId,
|
||||
DurationSeconds : (int)(session.EndedAt!.Value - session.CreatedAt).TotalSeconds,
|
||||
EndedAt : session.EndedAt.Value),
|
||||
ct);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user