diff --git a/typescript/server/src/utils/user.test.ts b/typescript/server/src/utils/user.test.ts index 5005e863b..396f6cc3b 100644 --- a/typescript/server/src/utils/user.test.ts +++ b/typescript/server/src/utils/user.test.ts @@ -131,15 +131,17 @@ describe("GetUsersRankingAndOutOf", () => { expect(result).toEqual({ ranking: 1, outOf: 2 }); }); - it("returns ranking 1 when user rating is null (no one can be strictly greater than null)", async () => { + it("returns worst ranking when user rating is null", async () => { const user1 = await seedUser({ username: "null_rater" }); const user2 = await seedUser({ username: "other_player" }); + const user3 = await seedUser({ username: "other_better_player" }); await seedGameStats(user1.id, null); - await seedGameStats(user2.id, 10); + await seedGameStats(user2.id, 0); + await seedGameStats(user3.id, 10); const result = await GetUsersRankingAndOutOf(makeStats(user1.id, null)); - expect(result).toEqual({ ranking: 1, outOf: 2 }); + expect(result).toEqual({ ranking: 3, outOf: 3 }); }); it("does not include rows from different games in the count", async () => { diff --git a/typescript/server/src/utils/user.ts b/typescript/server/src/utils/user.ts index 581abb093..194c6425a 100644 --- a/typescript/server/src/utils/user.ts +++ b/typescript/server/src/utils/user.ts @@ -199,9 +199,13 @@ export async function GetUsersRankingAndOutOf( const result = await DB.selectFrom("game_profile") .select([ (eb) => eb.fn.countAll().as("out_of"), - sql`COUNT(*) FILTER (WHERE (ratings->>${ratingAlg})::numeric > ${userRating})`.as( - "ranking_count", - ), + sql`COUNT(*) FILTER ( + WHERE user_id != ${stats.userID} -- itll include self when null, doesnt affect normal ranking + AND ( + (ratings->>${ratingAlg})::numeric > ${userRating}::numeric + OR ${userRating}::numeric IS NULL + ) + )`.as("ranking_count"), ]) .where("game", "=", stats.game) .executeTakeFirstOrThrow();