From 1638e8f3cb3c23f366eec4d2376f581e50ba1ef5 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sat, 19 Aug 2023 12:03:14 +0300 Subject: [PATCH] Seasons: Show current SP, not today's peak --- .../queries/seasonAllMMRByUserId.server.ts | 35 +++++++++++++++++-- app/routes/u.$identifier/seasons.tsx | 19 +++++----- 2 files changed, 42 insertions(+), 12 deletions(-) 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 (
- {data.skills.length > 0 ? ( + {data.currentOrdinal ? (
- + {data.skills.length >= 3 ? : null}
) : null} @@ -199,13 +203,12 @@ function SeasonHeader() { ); } -function Rank() { +function Rank({ currentOrdinal }: { currentOrdinal: number }) { const data = useLoaderData(); const maxOrdinal = Math.max(...data.skills.map((s) => s.ordinal)); - const peakAndCurrentSame = - data.skills[data.skills.length - 1].ordinal === maxOrdinal; + const peakAndCurrentSame = currentOrdinal === maxOrdinal; return (
@@ -215,9 +218,7 @@ function Rank() { {data.tier.name} {data.tier.isPlus ? "+" : ""}
-
- {ordinalToSp(data.skills[data.skills.length - 1].ordinal)}SP -
+
{ordinalToSp(currentOrdinal)}SP
{!peakAndCurrentSame ? (
Peak {ordinalToSp(maxOrdinal)}SP