Skip to content

Commit 353e732

Browse files
tknkaaCopilot
andcommitted
Adjust rank label
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f285802 commit 353e732

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

app/routes/gameover.tsx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { desc, eq } from "drizzle-orm";
1+
import { desc, eq, sql } from "drizzle-orm";
22
import { useNavigate } from "react-router";
33
import { getAuth } from "~/lib/auth";
44
import { authClient } from "~/lib/auth-client";
@@ -8,6 +8,9 @@ import type { Route } from "./+types/gameover";
88

99
export interface GameOverData {
1010
finalScore: number;
11+
rank: number;
12+
totalPlayers: number;
13+
gapToTop: number;
1114
totalKyoku: number;
1215
agariCount: number;
1316
ryukyokuCount: number;
@@ -49,9 +52,25 @@ export async function loader({
4952
0,
5053
);
5154
const finalScore = 25000 + totalScoreDelta;
55+
const totals = await db
56+
.select({
57+
userId: kyoku.userId,
58+
totalScore: sql<number>`25000 + coalesce(sum(${kyoku.scoreDelta}), 0)`,
59+
})
60+
.from(kyoku)
61+
.groupBy(kyoku.userId)
62+
.orderBy(desc(sql<number>`25000 + coalesce(sum(${kyoku.scoreDelta}), 0)`));
63+
const topScore = totals[0]?.totalScore ?? finalScore;
64+
const userIndex = totals.findIndex((total) => total.userId === userId);
65+
const userTotalScore = totals[userIndex]?.totalScore ?? finalScore;
66+
const rank = userIndex === -1 ? totals.length + 1 : userIndex + 1;
67+
const gapToTop = Math.max(0, topScore - userTotalScore);
5268

5369
return {
5470
finalScore,
71+
rank,
72+
totalPlayers: totals.length,
73+
gapToTop,
5574
totalKyoku,
5675
agariCount,
5776
ryukyokuCount,
@@ -60,17 +79,18 @@ export async function loader({
6079
}
6180

6281
export default function Page({ loaderData }: Route.ComponentProps) {
63-
const { finalScore, totalKyoku, agariCount, ryukyokuCount, tenpaiCount } =
64-
loaderData;
82+
const {
83+
finalScore,
84+
totalKyoku,
85+
agariCount,
86+
ryukyokuCount,
87+
tenpaiCount,
88+
rank,
89+
totalPlayers,
90+
gapToTop,
91+
} = loaderData;
6592
const navigate = useNavigate();
66-
const scoreComment =
67-
finalScore === 57000
68-
? "もうあなたが麻雀です"
69-
: finalScore >= 40000
70-
? "素晴らしい"
71-
: finalScore >= 35000
72-
? "まずまず"
73-
: "まずは基本から";
93+
const rankMessage = `${totalPlayers}人中${rank}位: 1位まで${gapToTop}点差`;
7494

7595
const anonymousLoginAndStart = async () => {
7696
const user = await authClient.getSession();
@@ -93,7 +113,7 @@ export default function Page({ loaderData }: Route.ComponentProps) {
93113
<p className="text-4xl font-bold text-white mb-2">
94114
最終スコア: {finalScore}
95115
</p>
96-
<p className="text-sm text-yellow-300">{scoreComment}</p>
116+
<p className="text-sm text-yellow-300">{rankMessage}</p>
97117
</div>
98118

99119
<div className="grid grid-cols-2 gap-4 text-white">

0 commit comments

Comments
 (0)