diff --git a/app/features/mmr/queries/seasonAllMMRByUserId.server.ts b/app/features/mmr/queries/seasonAllMMRByUserId.server.ts index 03c5b29e8..884716851 100644 --- a/app/features/mmr/queries/seasonAllMMRByUserId.server.ts +++ b/app/features/mmr/queries/seasonAllMMRByUserId.server.ts @@ -1,7 +1,7 @@ import { sql } from "~/db/sql"; import { MATCHES_COUNT_NEEDED_FOR_LEADERBOARD } from "~/features/leaderboards/leaderboards-constants"; -const stm = sql.prepare(/* sql */ ` +const groupedSkillsStm = sql.prepare(/* sql */ ` select max("Skill"."ordinal") as "ordinal", date( @@ -23,6 +23,22 @@ const stm = sql.prepare(/* sql */ ` order by "date" asc `); +const mostRecentStm = sql.prepare(/* sql */ ` + select + "Skill"."ordinal" + from + "Skill" + where + "Skill"."id" = ( + select max("id") + from "Skill" + where "userId" = @userId + and "season" = @season + and "matchesCount" >= ${MATCHES_COUNT_NEEDED_FOR_LEADERBOARD} + group by "userId" + ) +`); + export function seasonAllMMRByUserId({ userId, season, @@ -30,9 +46,22 @@ export function seasonAllMMRByUserId({ userId: number; season: number; }) { - return stm.all({ userId, season }) as Array<{ + return groupedSkillsStm.all({ userId, season }) as Array<{ ordinal: number; date: string; - isMostRecent: number; }>; } + +export function currentMMRByUserId({ + userId, + season, +}: { + userId: number; + season: number; +}) { + return ( + mostRecentStm.get({ userId, season }) as { + ordinal: number; + } | null + )?.ordinal; +} diff --git a/app/routes/u.$identifier/seasons.tsx b/app/routes/u.$identifier/seasons.tsx index 96488540d..a6acdb268 100644 --- a/app/routes/u.$identifier/seasons.tsx +++ b/app/routes/u.$identifier/seasons.tsx @@ -14,7 +14,10 @@ import { } from "~/components/Image"; import { db } from "~/db"; import { ordinalToSp } from "~/features/mmr"; -import { seasonAllMMRByUserId } from "~/features/mmr/queries/seasonAllMMRByUserId.server"; +import { + currentMMRByUserId, + seasonAllMMRByUserId, +} from "~/features/mmr/queries/seasonAllMMRByUserId.server"; import { currentSeason, seasonObject } from "~/features/mmr/season"; import { userSkills } from "~/features/mmr/tiered.server"; import { useIsMounted } from "~/hooks/useIsMounted"; @@ -68,6 +71,7 @@ export const loader = async ({ params, request }: LoaderArgs) => { }; return { + currentOrdinal: currentMMRByUserId({ season: 0, userId: user.id }), skills: seasonAllMMRByUserId({ season: 0, userId: user.id }), tier, matches: { @@ -114,9 +118,9 @@ export default function UserSeasonsPage() { return (