Skip to content

Commit

Permalink
Split game top on 3 and 15 runes
Browse files Browse the repository at this point in the history
  • Loading branch information
O4epegb committed Dec 1, 2024
1 parent 0d423fc commit 80e00ca
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
32 changes: 30 additions & 2 deletions apps/api/src/app/getters/getTopStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import { findGamesIncludeServer } from './findGamesIncludeServer'
const LIMIT = 10

export const getTopStats = async () => {
const [titles, winrates, winners, gamesByTC, gamesByDuration, gamesByScore] = await Promise.all([
const [
titles,
winrates,
winners,
gamesByTC,
gamesByDuration,
gamesByScore,
gamesByTC15Runes,
gamesByDuration15Runes,
gamesByScore3Runes,
] = await Promise.all([
prisma.$queryRaw<Array<{ playerId: string; titles: number }>>`
SELECT "playerId", CAST(COUNT(DISTINCT "title") AS INTEGER) AS titles
FROM "Game"
Expand All @@ -22,7 +32,7 @@ export const getTopStats = async () => {
HAVING COUNT("playerId") >= 75
ORDER BY winrate DESC
LIMIT 10
`,
`,
prisma.game.groupBy({
by: ['playerId'],
where: { isWin: true },
Expand All @@ -45,6 +55,21 @@ export const getTopStats = async () => {
orderBy: { score: 'desc' },
take: LIMIT,
}),
findGamesIncludeServer({
where: { isWin: true, runes: 15, player: { isBot: false } },
orderBy: { turns: 'asc' },
take: LIMIT,
}),
findGamesIncludeServer({
where: { isWin: true, runes: 15, player: { isBot: false } },
orderBy: { duration: 'asc' },
take: LIMIT,
}),
findGamesIncludeServer({
where: { isWin: true, runes: 3, player: { isBot: false } },
orderBy: { score: 'desc' },
take: LIMIT,
}),
])

const players = await prisma.player.findMany({
Expand All @@ -71,5 +96,8 @@ export const getTopStats = async () => {
gamesByTC,
gamesByDuration,
gamesByScore,
gamesByTC15Runes,
gamesByDuration15Runes,
gamesByScore3Runes,
}
}
18 changes: 18 additions & 0 deletions apps/web/src/screens/main/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ export const Stats = memo(
highlight="Duration"
onLinkClick={onLinkClick}
/>
<Table
games={top.gamesByScore3Runes}
title="Top highscores (3 runes only)"
highlight="Score"
onLinkClick={onLinkClick}
/>
<Table
games={top.gamesByTC15Runes}
title="Fastest wins by turn count (15 runes only)"
highlight="Turns"
onLinkClick={onLinkClick}
/>
<Table
games={top.gamesByDuration15Runes}
title="Fastest wins by realtime (15 runes only)"
highlight="Duration"
onLinkClick={onLinkClick}
/>
<Table
games={top.gamesByScore}
title="Top highscores"
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/screens/main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export type Response = {
gamesByTC: Array<Game>
gamesByDuration: Array<Game>
gamesByScore: Array<Game>
gamesByTC15Runes: Array<Game>
gamesByDuration15Runes: Array<Game>
gamesByScore3Runes: Array<Game>
}
}

Expand Down
22 changes: 22 additions & 0 deletions apps/web/src/utils/RaceConditionGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export class RaceConditionGuard {
private lastPromise: PromiseLike<unknown> | null = null

getGuardedPromise<T>(promise: PromiseLike<T>) {
this.lastPromise = promise
return this.lastPromise.then(this.preventRaceCondition()) as Promise<T>
}

preventRaceCondition() {
const currentPromise = this.lastPromise
return (response: unknown) => {
if (this.lastPromise !== currentPromise) {
return new Promise(() => null)
}
return response
}
}

cancel = () => {
this.lastPromise = null
}
}
23 changes: 0 additions & 23 deletions apps/web/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,6 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

export class RaceConditionGuard {
private lastPromise: PromiseLike<unknown> | null = null

getGuardedPromise<T>(promise: PromiseLike<T>) {
this.lastPromise = promise
return this.lastPromise.then(this.preventRaceCondition()) as Promise<T>
}

preventRaceCondition() {
const currentPromise = this.lastPromise
return (response: unknown) => {
if (this.lastPromise !== currentPromise) {
return new Promise(() => null)
}
return response
}
}

cancel = () => {
this.lastPromise = null
}
}

export const pluralize = (string: string, count: number) => string + (count === 1 ? '' : 's')

export const formatDuration = (seconds: number) => {
Expand Down

0 comments on commit 80e00ca

Please sign in to comment.