-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontend] Add stats to user page (#166)
- Loading branch information
Showing
4 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default async function ProfileItem({ | ||
title, | ||
children, | ||
}: { | ||
title: string; | ||
children: React.ReactNode; | ||
}) { | ||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<div className="text-xl font-bold">{title}</div> | ||
{children} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { getMatchHistory } from "@/app/lib/actions"; | ||
import ProfileItem from "./profile-item"; | ||
|
||
async function getStats(userId: number) { | ||
const history = await getMatchHistory(userId); | ||
const wins = history.filter( | ||
(m) => | ||
m.result == "COMPLETE" && | ||
m.players.some((p) => p.user.id === userId && p.winLose === "WIN"), | ||
).length; | ||
const losses = history.filter( | ||
(m) => | ||
m.result == "COMPLETE" && | ||
m.players.some((p) => p.user.id === userId && p.winLose === "LOSE"), | ||
).length; | ||
const winRate = wins / (wins + losses); | ||
const winRatePercent = Math.round(winRate * 100); | ||
const loseRatePercent = 100 - winRatePercent; | ||
const incompletes = history.filter((m) => m.result == "INCOMPLETE").length; | ||
const totalPointsWon = history.reduce((sum, m) => { | ||
return sum + (m.players.find((p) => p.user.id === userId)?.score ?? 0); | ||
}, 0); | ||
const totalPointsLost = history.reduce((sum, m) => { | ||
return sum + (m.players.find((p) => p.user.id !== userId)?.score ?? 0); | ||
}, 0); | ||
return { | ||
wins, | ||
losses, | ||
winRatePercent, | ||
loseRatePercent, | ||
incompletes, | ||
totalPointsWon, | ||
totalPointsLost, | ||
}; | ||
} | ||
|
||
export default async function Stats({ userId }: { userId: number }) { | ||
const { | ||
wins, | ||
losses, | ||
winRatePercent, | ||
loseRatePercent, | ||
incompletes, | ||
totalPointsWon, | ||
totalPointsLost, | ||
} = await getStats(userId); | ||
return ( | ||
<ProfileItem title="Stats"> | ||
<div className="flex flex-col gap-1"> | ||
<div> | ||
Win: {wins} ({winRatePercent}%) | ||
</div> | ||
<div> | ||
Lose: {losses} ({loseRatePercent}%) | ||
</div> | ||
<div>Incomplete Match: {incompletes}</div> | ||
<div>Total Points Won: {totalPointsWon}</div> | ||
<div>Total Points Lost: {totalPointsLost}</div> | ||
</div> | ||
</ProfileItem> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters