Refactor leaderboard logic: incorporate room context into leaderboard stats and channel metadata handling

This commit is contained in:
Alexa
2026-06-05 23:55:26 +03:00
parent 97e0fa5716
commit fe1c2c53e3
10 changed files with 257 additions and 106 deletions
+8 -9
View File
@@ -497,11 +497,12 @@ public class DorkNetDbContext(DbContextOptions<DorkNetDbContext> options) : DbCo
modelBuilder.Entity<LeaderboardStatEntity>(e =>
{
e.HasKey(s => s.Id);
// One row per (player, channel) — enforced unique so
// One row per (room, player, channel) — enforced unique so
// SetStats can upsert without race conditions.
e.HasIndex(s => new { s.PlayerId, s.StatChannel }).IsUnique();
// Rank computation: ORDER BY Value DESC over a channel.
e.HasIndex(s => new { s.StatChannel, s.Value });
e.HasIndex(s => new { s.RoomId, s.PlayerId, s.StatChannel }).IsUnique();
// Rank computation: ORDER BY Value over a room/channel
// (direction is chosen by leaderboard metadata).
e.HasIndex(s => new { s.RoomId, s.StatChannel, s.Value });
});
modelBuilder.Entity<PlayerEloEntity>(e =>
@@ -672,11 +673,9 @@ public class DorkNetDbContext(DbContextOptions<DorkNetDbContext> options) : DbCo
modelBuilder.Entity<LeaderboardChannelMetaEntity>(e =>
{
// Channel int IS the primary key — there's only one
// metadata row per stat-channel id, and the channel id
// is what the watch reports on POST /SetStats.
e.HasKey(c => c.Channel);
e.Property(c => c.Channel).ValueGeneratedNever();
// Channel ids are reused between rooms; a leaderboard is
// identified by (RoomId, Channel), not the channel alone.
e.HasKey(c => new { c.RoomId, c.Channel });
e.HasIndex(c => c.RoomId);
});
}
@@ -1,7 +1,8 @@
namespace DorkNet.Server.Data.Entities;
/// <summary>
/// Admin-curated metadata for a leaderboard <c>StatChannel</c> id. The
/// Admin-curated metadata for a room-scoped leaderboard
/// <c>StatChannel</c> id. The
/// 2020 watch reports raw integer channel ids via
/// <c>POST api/Leaderboard/v1/SetStats</c>; this table lets admins map
/// each channel to a specific room (so the room detail page can list
@@ -9,22 +10,19 @@ namespace DorkNet.Server.Data.Entities;
/// here override the hardcoded <c>AdminController.KnownStatChannels</c>
/// fallback. Channels with no row stay listed as "Channel N".
///
/// One channel id, one row. Repeated <c>SetStats</c> calls for the same
/// channel from any room write to that channel's value rows; the meta
/// row only changes when an admin renames or re-maps it.
/// One row per (room, channel). Repeated <c>SetStats</c> calls for the
/// same channel in different rooms do not share metadata or values.
/// </summary>
public class LeaderboardChannelMetaEntity
{
/// <summary>Same int the watch reports for this channel. Primary
/// key — there's only ever one meta row per channel.</summary>
public int Channel { get; set; }
/// <summary>Room this channel "belongs to" for the per-room detail
/// view. 0 = global / unscoped (e.g. cross-room player progression
/// counters). The same room can own many channels (e.g. Stunt
/// Runner has one channel per course).</summary>
/// view. 0 = global / unscoped legacy bucket.</summary>
public long RoomId { get; set; }
/// <summary>Same int the watch reports for this channel. Only unique
/// together with <see cref="RoomId"/>.</summary>
public int Channel { get; set; }
public string Name { get; set; } = string.Empty;
/// <summary>When true, a lower stored value is a better score (race
@@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations;
namespace DorkNet.Server.Data.Entities;
/// <summary>
/// One leaderboard stat row per (player, statChannel). Backs
/// One leaderboard stat row per (room, player, statChannel). Backs
/// <c>api/Leaderboard/v1/SetStats</c> + <c>api/Leaderboard/v2/getPlayerRank</c>.
///
/// The 2020 watch's <c>RecNet.Leaderboards</c> uses an int channel
@@ -17,6 +17,11 @@ public class LeaderboardStatEntity
public long Id { get; set; }
public long PlayerId { get; set; }
/// <summary>Room the score was reported from. The same stat channel
/// id is reused by different rooms, so room scope is part of the
/// leaderboard identity. 0 means legacy/global/unscoped.</summary>
public long RoomId { get; set; }
/// <summary>RecNet stat channel id (small int per game mode +
/// stat type — e.g. paintball-CTF-wins, dodgeball-hits).</summary>
public int StatChannel { get; set; }