From 554c905e90794a5a11b9b6463a6f69be3ab520f3 Mon Sep 17 00:00:00 2001 From: Henry <31442053+HenrySpartGlobal@users.noreply.github.com> Date: Sun, 28 Jul 2024 19:02:53 +0100 Subject: [PATCH] feat: ensure players only appear once (#110) * feat: ensure players only appear once * feat: remove !!! --- src/db/model.d.ts | 10 ++++++++++ src/db/queries/leaderboard.ts | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/db/model.d.ts b/src/db/model.d.ts index b31d86a..a525f53 100644 --- a/src/db/model.d.ts +++ b/src/db/model.d.ts @@ -25,6 +25,8 @@ export interface MmrRating { sigma: string | null; counter: number | null; game: string | null; + queue_channel_id: number | null; + time: number | null; } export interface Points { @@ -33,6 +35,13 @@ export interface Points { wins: number | null; losses: number | null; game: string | null; + queue_channel_id: number | null; +} + +export interface QueueChannels { + guild_id: number | null; + channel_id: number | null; + unique_leaderboard: boolean | null; } export interface DB { @@ -40,4 +49,5 @@ export interface DB { igns: Igns; mmr_rating: MmrRating; points: Points; + queuechannels: QueueChannels; } \ No newline at end of file diff --git a/src/db/queries/leaderboard.ts b/src/db/queries/leaderboard.ts index e350a7f..d5f4c9f 100644 --- a/src/db/queries/leaderboard.ts +++ b/src/db/queries/leaderboard.ts @@ -46,7 +46,13 @@ export async function fetchLeaderboardRaw( let builder = db .selectFrom("mmr_rating") .where("mmr_rating.guild_id", "=", guildId) - // join with mmr_rating, same user_id and guild_id + .where("mmr_rating.time", "=", + (qb) => qb.selectFrom("mmr_rating as latest_mmr") + .select(sql`MAX(${sql.ref('latest_mmr.time')})`.as("max_time")) + .whereRef("latest_mmr.user_id", "=", "mmr_rating.user_id") + .where("latest_mmr.guild_id", "=", guildId) + ) + // join with points, same user_id and guild_id .leftJoin("points", (eb) => eb.on(b => b.and([ b(b.ref("points.user_id"), "=", b.ref("mmr_rating.user_id")), b(b.ref("points.guild_id"), "=", b.ref("mmr_rating.guild_id"))