Set a default of 0 when a rating value is null when calculating rank (#1675)

* Set a default of 0 when a rating value is null when calculating rank

* make null values be last place

* adjust test for new null rating case
This commit is contained in:
Ben Hutchinson
2026-06-17 21:23:05 +01:00
committed by GitHub
parent 3df85b4cf7
commit eff9101f79
2 changed files with 12 additions and 6 deletions
+5 -3
View File
@@ -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 () => {
+7 -3
View File
@@ -199,9 +199,13 @@ export async function GetUsersRankingAndOutOf(
const result = await DB.selectFrom("game_profile")
.select([
(eb) => eb.fn.countAll().as("out_of"),
sql<number>`COUNT(*) FILTER (WHERE (ratings->>${ratingAlg})::numeric > ${userRating})`.as(
"ranking_count",
),
sql<number>`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();