diff --git a/Server/src/SOR.Admin/Components/Pages/Accounts/AccountDetail.razor b/Server/src/SOR.Admin/Components/Pages/Accounts/AccountDetail.razor index de08b06..83a251d 100644 --- a/Server/src/SOR.Admin/Components/Pages/Accounts/AccountDetail.razor +++ b/Server/src/SOR.Admin/Components/Pages/Accounts/AccountDetail.razor @@ -51,6 +51,10 @@ else
@(_account.EmailConfirmed ? "Yes" : "No")
2FA
@(_account.TwoFactorEnabled ? "Enabled" : "Disabled")
+
Rank Score
+
@_account.RankScore
+
Origin Points
+
@_account.OriginPoints
@@ -268,6 +272,29 @@ else + +
+
+
Award Origin Points
+
+
+
+ + +
+ +
+
+ Immediately credits the specified OriginPoints to this account's balance. +
+
+
+
+
@@ -486,6 +513,9 @@ else private bool _grantFoil; private bool _granting; + // Award OP + private int _awardOpAmount = 100; + // Account properties private List _props = new(); private Guid _editingPropId = Guid.Empty; @@ -674,6 +704,27 @@ else } } + // ── Award OP ────────────────────────────────────────────────────────────── + + private async Task AwardOpAsync() + { + if (_account == null || _awardOpAmount <= 0) return; + + _account.OriginPoints += _awardOpAmount; + var result = await UserManager.UpdateAsync(_account); + if (result.Succeeded) + { + _message = $"Awarded {_awardOpAmount} OP. New balance: {_account.OriginPoints}."; + _isError = false; + _awardOpAmount = 100; + } + else + { + _message = string.Join(" ", result.Errors.Select(e => e.Description)); + _isError = true; + } + } + // ── Grant Cards ─────────────────────────────────────────────────────────── private async Task ResetStarterDeckSelectionAsync() diff --git a/Server/src/SOR.Admin/Components/Pages/Game/GameSessionsPage.razor b/Server/src/SOR.Admin/Components/Pages/Game/GameSessionsPage.razor index c246720..18c847f 100644 --- a/Server/src/SOR.Admin/Components/Pages/Game/GameSessionsPage.razor +++ b/Server/src/SOR.Admin/Components/Pages/Game/GameSessionsPage.razor @@ -4,6 +4,7 @@ @using SOR.Database.Contexts @using SOR.Database.Enums @inject GameDbContext Db +@inject ChatDbContext ChatDb @inject IJSRuntime JS Game Sessions – SOR Admin @@ -249,8 +250,12 @@ else private async Task PurgeAllAsync() { - if (!await JS.InvokeAsync("confirm", "Permanently delete ALL sessions and action logs?")) + if (!await JS.InvokeAsync("confirm", "Permanently delete ALL sessions, action logs, and match chat channels?")) return; + // Delete match chat channels (cascade removes members + messages). + await ChatDb.Channels + .Where(c => c.ChannelType == ChatChannelType.Match) + .ExecuteDeleteAsync(); // Cascade delete on action_logs means deleting sessions removes logs too. await Db.GameSessions.ExecuteDeleteAsync(); _page = 1; diff --git a/Server/src/SOR.Admin/Components/Pages/Game/RankedRulesPage.razor b/Server/src/SOR.Admin/Components/Pages/Game/RankedRulesPage.razor index 8d864e6..c8a5d37 100644 --- a/Server/src/SOR.Admin/Components/Pages/Game/RankedRulesPage.razor +++ b/Server/src/SOR.Admin/Components/Pages/Game/RankedRulesPage.razor @@ -123,6 +123,36 @@ else
+ +
+
+
Origin Points Rewards
+
+
+ + +
Base OP awarded to the winner after a ranked game.
+
+
+ + +
Consolation OP awarded to the loser.
+
+
+ + +
+ Per-rank-point scaling in thousandths. 0 = flat rewards. + Positive = upset wins pay more. Formula: + adjustment = (loserRank − winnerRank) × multiplier ÷ 1000. +
+
+
+
+
} diff --git a/Server/src/SOR.Chat/ChatChannelMembership.cs b/Server/src/SOR.Chat/ChatChannelMembership.cs index e650583..72e4f99 100644 --- a/Server/src/SOR.Chat/ChatChannelMembership.cs +++ b/Server/src/SOR.Chat/ChatChannelMembership.cs @@ -62,6 +62,8 @@ internal static class ChatChannelMembership foreach (var member in members) member.LeftAt = now; + channel.ArchivedAt = now; + await chatDb.SaveChangesAsync(ct); } diff --git a/Server/src/SOR.Database/Entities/Chat/ChatChannel.cs b/Server/src/SOR.Database/Entities/Chat/ChatChannel.cs index 2ea35ac..ad97bda 100644 --- a/Server/src/SOR.Database/Entities/Chat/ChatChannel.cs +++ b/Server/src/SOR.Database/Entities/Chat/ChatChannel.cs @@ -13,6 +13,9 @@ public sealed class ChatChannel public DateTimeOffset CreatedAt { get; set; } + /// Set when the match ends and the channel is archived (members marked Left). + public DateTimeOffset? ArchivedAt { get; set; } + public ICollection Members { get; set; } = new List(); public ICollection Messages { get; set; } = new List(); } diff --git a/Server/src/SOR.Database/Entities/Game/GameRuleSet.cs b/Server/src/SOR.Database/Entities/Game/GameRuleSet.cs index 995ceab..cfa63ce 100644 --- a/Server/src/SOR.Database/Entities/Game/GameRuleSet.cs +++ b/Server/src/SOR.Database/Entities/Game/GameRuleSet.cs @@ -49,6 +49,21 @@ public sealed class GameRuleSet /// Battlefield height (rows). Default: 8. public int BoardHeight { get; set; } = 8; + // ── Origin Points Rewards ───────────────────────────────────────────────── + + /// OP awarded to the winner of a ranked game. + public int WinnerOpReward { get; set; } = 50; + + /// OP awarded to the loser of a ranked game (consolation). + public int LoserOpReward { get; set; } = 10; + + /// + /// Per-rank-point scaling for the OP award (in thousandths, i.e. 0 = flat). + /// adjustment = (loserRank − winnerRank) × RankDiffOpMultiplierPct / 1000. + /// Positive diff = winner was underdog → pays more. Negative = expected win → pays less. + /// + public int RankDiffOpMultiplierPct { get; set; } = 0; + // ── Timestamps ─────────────────────────────────────────────────────────── public DateTimeOffset CreatedAt { get; set; } diff --git a/Server/src/SOR.Database/Migrations/Chat/20260309200000_AddArchivedAtToChatChannel.Designer.cs b/Server/src/SOR.Database/Migrations/Chat/20260309200000_AddArchivedAtToChatChannel.Designer.cs new file mode 100644 index 0000000..ca32020 --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Chat/20260309200000_AddArchivedAtToChatChannel.Designer.cs @@ -0,0 +1,275 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using SOR.Database.Contexts; + +#nullable disable + +namespace SOR.Database.Migrations.Chat +{ + [DbContext(typeof(ChatDbContext))] + [Migration("20260309200000_AddArchivedAtToChatChannel")] + partial class AddArchivedAtToChatChannel + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("chat") + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("SOR.Database.Entities.Chat.AccountPresence", b => + { + b.Property("AccountId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CurrentMatchId") + .HasColumnType("uuid"); + + b.Property("LastSeenAt") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone"); + + b.HasKey("AccountId"); + + b.ToTable("account_presence", "chat"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.BlockedUser", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("BlockedAccountId") + .HasColumnType("uuid"); + + b.Property("BlockedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("BlockerAccountId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("BlockerAccountId"); + + b.HasIndex("BlockerAccountId", "BlockedAccountId") + .IsUnique(); + + b.ToTable("blocked_users", "chat"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.ChatChannel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ArchivedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("ChannelType") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("MatchId") + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("ChannelType"); + + b.HasIndex("MatchId"); + + b.ToTable("channels", "chat"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.ChatMember", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccountId") + .HasColumnType("uuid"); + + b.Property("ChannelId") + .HasColumnType("uuid"); + + b.Property("JoinedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("LeftAt") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("AccountId"); + + b.HasIndex("ChannelId", "AccountId") + .IsUnique(); + + b.ToTable("members", "chat"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.ChatMessage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ChannelId") + .HasColumnType("uuid"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text"); + + b.Property("EditedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("IsDeleted") + .HasColumnType("boolean"); + + b.Property("SenderAccountId") + .HasColumnType("uuid"); + + b.Property("SentAt") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("SenderAccountId"); + + b.HasIndex("ChannelId", "SentAt"); + + b.ToTable("messages", "chat"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.FriendRequest", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("RecipientAccountId") + .HasColumnType("uuid"); + + b.Property("RequesterAccountId") + .HasColumnType("uuid"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("RecipientAccountId"); + + b.HasIndex("Status"); + + b.HasIndex("RequesterAccountId", "RecipientAccountId") + .IsUnique(); + + b.ToTable("friend_requests", "chat"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.Notification", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Body") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("NotificationType") + .IsRequired() + .HasColumnType("text"); + + b.Property("PayloadJson") + .HasColumnType("jsonb") + .HasColumnName("payload"); + + b.Property("ReadAt") + .HasColumnType("timestamp with time zone"); + + b.Property("RecipientAccountId") + .HasColumnType("uuid"); + + b.Property("Title") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("RecipientAccountId", "ReadAt", "CreatedAt"); + + b.ToTable("notifications", "chat"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.ChatMember", b => + { + b.HasOne("SOR.Database.Entities.Chat.ChatChannel", "Channel") + .WithMany("Members") + .HasForeignKey("ChannelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Channel"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.ChatMessage", b => + { + b.HasOne("SOR.Database.Entities.Chat.ChatChannel", "Channel") + .WithMany("Messages") + .HasForeignKey("ChannelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Channel"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Chat.ChatChannel", b => + { + b.Navigation("Members"); + + b.Navigation("Messages"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Server/src/SOR.Database/Migrations/Chat/20260309200000_AddArchivedAtToChatChannel.cs b/Server/src/SOR.Database/Migrations/Chat/20260309200000_AddArchivedAtToChatChannel.cs new file mode 100644 index 0000000..8b3f184 --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Chat/20260309200000_AddArchivedAtToChatChannel.cs @@ -0,0 +1,31 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SOR.Database.Migrations.Chat +{ + /// + public partial class AddArchivedAtToChatChannel : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ArchivedAt", + schema: "chat", + table: "channels", + type: "timestamp with time zone", + nullable: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "ArchivedAt", + schema: "chat", + table: "channels"); + } + } +} diff --git a/Server/src/SOR.Database/Migrations/Chat/ChatDbContextModelSnapshot.cs b/Server/src/SOR.Database/Migrations/Chat/ChatDbContextModelSnapshot.cs index 05abe26..84712e8 100644 --- a/Server/src/SOR.Database/Migrations/Chat/ChatDbContextModelSnapshot.cs +++ b/Server/src/SOR.Database/Migrations/Chat/ChatDbContextModelSnapshot.cs @@ -78,6 +78,9 @@ namespace SOR.Database.Migrations.Chat .ValueGeneratedOnAdd() .HasColumnType("uuid"); + b.Property("ArchivedAt") + .HasColumnType("timestamp with time zone"); + b.Property("ChannelType") .IsRequired() .HasColumnType("text"); diff --git a/Server/src/SOR.Database/Migrations/Game/20260309210000_AddOpRewardFields.Designer.cs b/Server/src/SOR.Database/Migrations/Game/20260309210000_AddOpRewardFields.Designer.cs new file mode 100644 index 0000000..354fba5 --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Game/20260309210000_AddOpRewardFields.Designer.cs @@ -0,0 +1,309 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using SOR.Database.Contexts; + +#nullable disable + +namespace SOR.Database.Migrations.Game +{ + [DbContext(typeof(GameDbContext))] + [Migration("20260309210000_AddOpRewardFields")] + partial class AddOpRewardFields + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("game") + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("SOR.Database.Entities.Game.CustomLobby", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("DrawPerTurnOverride") + .HasColumnType("integer"); + + b.Property("FirstPlayerSkipsDrawOverride") + .HasColumnType("boolean"); + + b.Property("GameSessionId") + .HasColumnType("uuid"); + + b.Property("GuestAccountId") + .HasColumnType("uuid"); + + b.Property("GuestDeckId") + .HasColumnType("uuid"); + + b.Property("HostAccountId") + .HasColumnType("uuid"); + + b.Property("HostDeckId") + .HasColumnType("uuid"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OpeningHandSizeOverride") + .HasColumnType("integer"); + + b.Property("SpAccumulationPerTurnOverride") + .HasColumnType("integer"); + + b.Property("SpCapOverride") + .HasColumnType("integer"); + + b.Property("SpHasCapOverride") + .HasColumnType("boolean"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("HostAccountId"); + + b.HasIndex("Status", "CreatedAt"); + + b.ToTable("custom_lobbies", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameActionLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ActionType") + .IsRequired() + .HasColumnType("text"); + + b.Property("ActorAccountId") + .HasColumnType("uuid"); + + b.Property("GameSessionId") + .HasColumnType("uuid"); + + b.Property("OccurredAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PayloadJson") + .HasColumnType("jsonb") + .HasColumnName("payload"); + + b.Property("ResultJson") + .HasColumnType("jsonb") + .HasColumnName("result"); + + b.Property("SequenceNumber") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ActorAccountId"); + + b.HasIndex("GameSessionId", "SequenceNumber") + .IsUnique(); + + b.ToTable("action_logs", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameRuleSet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("BoardHeight") + .HasColumnType("integer"); + + b.Property("BoardWidth") + .HasColumnType("integer"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("DrawPerTurn") + .HasColumnType("integer"); + + b.Property("FirstPlayerSkipsDraw") + .HasColumnType("boolean"); + + b.Property("IsCustomDefault") + .HasColumnType("boolean"); + + b.Property("IsRankedDefault") + .HasColumnType("boolean"); + + b.Property("LoserOpReward") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OpeningHandSize") + .HasColumnType("integer"); + + b.Property("RankDiffOpMultiplierPct") + .HasColumnType("integer"); + + b.Property("SpAccumulationPerTurn") + .HasColumnType("integer"); + + b.Property("SpCap") + .HasColumnType("integer"); + + b.Property("SpHasCap") + .HasColumnType("boolean"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("WinnerOpReward") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("IsCustomDefault"); + + b.HasIndex("IsRankedDefault"); + + b.ToTable("game_rule_sets", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameSession", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("EndedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PlayerOneAccountId") + .HasColumnType("uuid"); + + b.Property("PlayerOneDeckId") + .HasColumnType("uuid"); + + b.Property("PlayerOneReady") + .HasColumnType("boolean"); + + b.Property("PlayerTwoAccountId") + .HasColumnType("uuid"); + + b.Property("PlayerTwoDeckId") + .HasColumnType("uuid"); + + b.Property("PlayerTwoReady") + .HasColumnType("boolean"); + + b.Property("RuleSnapshotJson") + .HasColumnType("jsonb") + .HasColumnName("rule_snapshot"); + + b.Property("StartedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("StateJson") + .HasColumnType("jsonb") + .HasColumnName("state"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.Property("WinCondition") + .HasColumnType("text"); + + b.Property("WinnerAccountId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("PlayerOneAccountId"); + + b.HasIndex("PlayerTwoAccountId"); + + b.HasIndex("Status", "CreatedAt"); + + b.ToTable("game_sessions", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.MatchmakingEntry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccountId") + .HasColumnType("uuid"); + + b.Property("DeckId") + .HasColumnType("uuid"); + + b.Property("JoinedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("MatchedGameSessionId") + .HasColumnType("uuid"); + + b.Property("RankScore") + .HasColumnType("integer"); + + b.Property("ResolvedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("AccountId", "Status"); + + b.HasIndex("Status", "RankScore"); + + b.ToTable("matchmaking_entries", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameActionLog", b => + { + b.HasOne("SOR.Database.Entities.Game.GameSession", "Session") + .WithMany("ActionLog") + .HasForeignKey("GameSessionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Session"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameSession", b => + { + b.Navigation("ActionLog"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Server/src/SOR.Database/Migrations/Game/20260309210000_AddOpRewardFields.cs b/Server/src/SOR.Database/Migrations/Game/20260309210000_AddOpRewardFields.cs new file mode 100644 index 0000000..b6936dd --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Game/20260309210000_AddOpRewardFields.cs @@ -0,0 +1,57 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SOR.Database.Migrations.Game +{ + /// + public partial class AddOpRewardFields : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "WinnerOpReward", + schema: "game", + table: "game_rule_sets", + type: "integer", + nullable: false, + defaultValue: 50); + + migrationBuilder.AddColumn( + name: "LoserOpReward", + schema: "game", + table: "game_rule_sets", + type: "integer", + nullable: false, + defaultValue: 10); + + migrationBuilder.AddColumn( + name: "RankDiffOpMultiplierPct", + schema: "game", + table: "game_rule_sets", + type: "integer", + nullable: false, + defaultValue: 0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "WinnerOpReward", + schema: "game", + table: "game_rule_sets"); + + migrationBuilder.DropColumn( + name: "LoserOpReward", + schema: "game", + table: "game_rule_sets"); + + migrationBuilder.DropColumn( + name: "RankDiffOpMultiplierPct", + schema: "game", + table: "game_rule_sets"); + } + } +} diff --git a/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs b/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs index a123756..25abad5 100644 --- a/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs +++ b/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs @@ -149,6 +149,9 @@ namespace SOR.Database.Migrations.Game b.Property("IsRankedDefault") .HasColumnType("boolean"); + b.Property("LoserOpReward") + .HasColumnType("integer"); + b.Property("Name") .IsRequired() .HasColumnType("text"); @@ -156,6 +159,9 @@ namespace SOR.Database.Migrations.Game b.Property("OpeningHandSize") .HasColumnType("integer"); + b.Property("RankDiffOpMultiplierPct") + .HasColumnType("integer"); + b.Property("SpAccumulationPerTurn") .HasColumnType("integer"); @@ -168,6 +174,9 @@ namespace SOR.Database.Migrations.Game b.Property("UpdatedAt") .HasColumnType("timestamp with time zone"); + b.Property("WinnerOpReward") + .HasColumnType("integer"); + b.HasKey("Id"); b.HasIndex("IsCustomDefault"); diff --git a/Server/src/SOR.Game/GameMessages.cs b/Server/src/SOR.Game/GameMessages.cs index 62d15f6..cb294ed 100644 --- a/Server/src/SOR.Game/GameMessages.cs +++ b/Server/src/SOR.Game/GameMessages.cs @@ -176,7 +176,9 @@ public sealed record GameOverPush( Guid LoserAccountId, string WinCondition, // "health" | "concede" | "disconnect" int WinnerRankDelta, - int LoserRankDelta); + int LoserRankDelta, + int WinnerOpDelta, + int LoserOpDelta); /// /// Pushed to both players when a custom lobby is full and a GameSession has been created. diff --git a/Server/src/SOR.Game/Handlers/GameAttackHandler.cs b/Server/src/SOR.Game/Handlers/GameAttackHandler.cs index b965375..af07b29 100644 --- a/Server/src/SOR.Game/Handlers/GameAttackHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameAttackHandler.cs @@ -2,6 +2,7 @@ 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; @@ -16,17 +17,20 @@ internal sealed class GameAttackHandler : GameHandlerBase, IMessageHandler 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) + IClientPush clientPush, + IEventBus eventBus) { _serializer = serializer; _gameDb = gameDb; _identityDb = identityDb; _clientPush = clientPush; + _eventBus = eventBus; } public string MessageType => "game.attack"; @@ -57,7 +61,7 @@ internal sealed class GameAttackHandler : GameHandlerBase, IMessageHandler : session.PlayerOneAccountId; await FinalizeGameAsync( - _gameDb, _identityDb, _clientPush, + _gameDb, _identityDb, _clientPush, _eventBus, session, winnerAccountId: Guid.Parse(actorId), loserAccountId: loserId, diff --git a/Server/src/SOR.Game/Handlers/GameConcedeHandler.cs b/Server/src/SOR.Game/Handlers/GameConcedeHandler.cs index 0911bde..0f8594c 100644 --- a/Server/src/SOR.Game/Handlers/GameConcedeHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameConcedeHandler.cs @@ -2,6 +2,7 @@ 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; @@ -16,17 +17,20 @@ internal sealed class GameConcedeHandler : GameHandlerBase, IMessageHandler private readonly GameDbContext _gameDb; private readonly SorIdentityDbContext _identityDb; private readonly IClientPush _clientPush; + private readonly IEventBus _eventBus; public GameConcedeHandler( IMessageSerializer serializer, GameDbContext gameDb, SorIdentityDbContext identityDb, - IClientPush clientPush) + IClientPush clientPush, + IEventBus eventBus) { _serializer = serializer; _gameDb = gameDb; _identityDb = identityDb; _clientPush = clientPush; + _eventBus = eventBus; } public string MessageType => "game.concede"; @@ -52,7 +56,7 @@ internal sealed class GameConcedeHandler : GameHandlerBase, IMessageHandler : session.PlayerOneAccountId; await FinalizeGameAsync( - _gameDb, _identityDb, _clientPush, + _gameDb, _identityDb, _clientPush, _eventBus, session, winnerAccountId: winnerId, loserAccountId: Guid.Parse(actorId), diff --git a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs index 7a23f7a..6377cf5 100644 --- a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs +++ b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs @@ -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; /// @@ -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); + /// /// 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. /// 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( + 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); } }