Files
sor/Server/src/SOR.Chat/ChatChannelMembership.cs
T
0600326db7 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>
2026-03-09 16:16:22 -04:00

158 lines
4.5 KiB
C#

namespace SOR.Chat;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Contexts;
using SOR.Database.Entities.Chat;
using SOR.Database.Enums;
internal static class ChatChannelMembership
{
public static async Task EnsureDefaultMembershipsAsync(
ChatDbContext chatDb,
Guid accountId,
CancellationToken ct)
{
var now = DateTimeOffset.UtcNow;
var global = await GetOrCreateSingletonChannelAsync(chatDb, ChatChannelType.Global, "global", now, ct);
await EnsureMemberAsync(chatDb, global.Id, accountId, now, ct);
var lobby = await GetOrCreateSingletonChannelAsync(chatDb, ChatChannelType.Lobby, "lobby", now, ct);
await EnsureMemberAsync(chatDb, lobby.Id, accountId, now, ct);
}
public static async Task EnsureMatchMembershipsAsync(
ChatDbContext chatDb,
Guid matchId,
IReadOnlyCollection<Guid> accountIds,
CancellationToken ct)
{
if (accountIds.Count == 0)
return;
var now = DateTimeOffset.UtcNow;
var channel = await GetOrCreateMatchChannelAsync(chatDb, matchId, now, ct);
foreach (var accountId in accountIds.Distinct())
await EnsureMemberAsync(chatDb, channel.Id, accountId, now, ct);
}
public static async Task EndMatchMembershipsAsync(
ChatDbContext chatDb,
Guid matchId,
CancellationToken ct)
{
var matchKey = matchId.ToString("D");
var channel = await chatDb.Channels
.Where(c => c.ChannelType == ChatChannelType.Match && c.MatchId == matchKey)
.OrderBy(c => c.CreatedAt)
.FirstOrDefaultAsync(ct);
if (channel is null)
return;
var members = await chatDb.Members
.Where(m => m.ChannelId == channel.Id && m.LeftAt == null)
.ToListAsync(ct);
if (members.Count == 0)
return;
var now = DateTimeOffset.UtcNow;
foreach (var member in members)
member.LeftAt = now;
channel.ArchivedAt = now;
await chatDb.SaveChangesAsync(ct);
}
private static async Task<ChatChannel> GetOrCreateSingletonChannelAsync(
ChatDbContext chatDb,
ChatChannelType channelType,
string name,
DateTimeOffset now,
CancellationToken ct)
{
var existing = await chatDb.Channels
.Where(c => c.ChannelType == channelType && c.MatchId == null)
.OrderBy(c => c.CreatedAt)
.FirstOrDefaultAsync(ct);
if (existing is not null)
return existing;
var channel = new ChatChannel
{
Name = name,
ChannelType = channelType,
MatchId = null,
CreatedAt = now,
};
chatDb.Channels.Add(channel);
await chatDb.SaveChangesAsync(ct);
return channel;
}
private static async Task<ChatChannel> GetOrCreateMatchChannelAsync(
ChatDbContext chatDb,
Guid matchId,
DateTimeOffset now,
CancellationToken ct)
{
var matchKey = matchId.ToString("D");
var existing = await chatDb.Channels
.Where(c => c.ChannelType == ChatChannelType.Match && c.MatchId == matchKey)
.OrderBy(c => c.CreatedAt)
.FirstOrDefaultAsync(ct);
if (existing is not null)
return existing;
var channel = new ChatChannel
{
Name = $"game:{matchKey}",
ChannelType = ChatChannelType.Match,
MatchId = matchKey,
CreatedAt = now,
};
chatDb.Channels.Add(channel);
await chatDb.SaveChangesAsync(ct);
return channel;
}
private static async Task EnsureMemberAsync(
ChatDbContext chatDb,
Guid channelId,
Guid accountId,
DateTimeOffset now,
CancellationToken ct)
{
var member = await chatDb.Members
.FirstOrDefaultAsync(m => m.ChannelId == channelId && m.AccountId == accountId, ct);
if (member is null)
{
chatDb.Members.Add(new ChatMember
{
ChannelId = channelId,
AccountId = accountId,
JoinedAt = now,
LeftAt = null,
});
await chatDb.SaveChangesAsync(ct);
return;
}
if (member.LeftAt is null)
return;
member.LeftAt = null;
member.JoinedAt = now;
await chatDb.SaveChangesAsync(ct);
}
}