Skip to content

Commit 3cbf9c7

Browse files
committed
Use global counts from teerank in the frontend
1 parent d410527 commit 3cbf9c7

File tree

7 files changed

+49
-83
lines changed

7 files changed

+49
-83
lines changed

apps/frontend/app/all/clans/page.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ClanList } from '../../../components/ClanList';
2-
import { getGlobalCounts } from '../../../utils/globalCounts';
2+
import { getGlobalCounts } from '@teerank/teerank';
33
import prisma from '../../../utils/prisma';
4+
import redis from '../../../utils/redis';
45
import { searchParamSchema } from '../schema';
56

67
export const metadata = {
@@ -15,7 +16,7 @@ export default async function Index({
1516
}) {
1617
const { page } = searchParamSchema.parse(searchParams);
1718

18-
const [clans, { clanCount }] = await Promise.all([
19+
const [clans, globalCounts] = await Promise.all([
1920
prisma.clan.findMany({
2021
select: {
2122
name: true,
@@ -31,12 +32,12 @@ export default async function Index({
3132
skip: (page - 1) * 100,
3233
}),
3334

34-
getGlobalCounts(),
35+
getGlobalCounts(redis),
3536
]);
3637

3738
return (
3839
<ClanList
39-
clanCount={clanCount}
40+
clanCount={globalCounts.clans}
4041
clans={clans.map((clan, index) => ({
4142
rank: (page - 1) * 100 + index + 1,
4243
name: clan.name,

apps/frontend/app/all/layout.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
import { getGlobalCounts } from '../../utils/globalCounts';
1+
import { getGlobalCounts } from '@teerank/teerank';
2+
import redis from '../../utils/redis';
23
import { LayoutTabs } from './LayoutTabs';
34

45
export default async function Index({
56
children,
67
}: {
78
children: React.ReactNode;
89
}) {
9-
const {
10-
playerCount,
11-
clanCount,
12-
gameServerCount,
13-
} = await getGlobalCounts();
10+
const globalCounts = await getGlobalCounts(redis);
1411

1512
return (
1613
<div className="flex flex-col gap-4 py-8">
1714
<LayoutTabs
18-
playerCount={playerCount}
19-
clanCount={clanCount}
20-
serverCount={gameServerCount}
15+
playerCount={globalCounts.players}
16+
clanCount={globalCounts.clans}
17+
serverCount={globalCounts.gameServers}
2118
/>
2219

2320
{children}

apps/frontend/app/all/page.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { PlayerList } from '../../components/PlayerList';
2-
import { getGlobalCounts } from '../../utils/globalCounts';
2+
import { getGlobalCounts } from '@teerank/teerank';
33
import prisma from '../../utils/prisma';
44
import { searchParamSchema } from './schema';
5+
import redis from '../../utils/redis';
56

67
export const metadata = {
78
title: 'All Players - Teerank',
@@ -44,15 +45,15 @@ export default async function Index({
4445
skip: (page - 1) * 100,
4546
});
4647

47-
const { playerCount } = await getGlobalCounts();
48+
const globalCounts = await getGlobalCounts(redis);
4849

4950
return (
5051
<>
5152
<p className="hidden">
5253
{`Teerank is a simple and fast ranking system for Teeworlds.`}
5354
</p>
5455
<PlayerList
55-
playerCount={playerCount}
56+
playerCount={globalCounts.players}
5657
rankMethod={null}
5758
showLastSeen={true}
5859
players={players.map((player, index) => ({

apps/frontend/app/all/servers/page.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ServerList } from '../../../components/ServerList';
2-
import { getGlobalCounts } from '../../../utils/globalCounts';
2+
import { getGlobalCounts } from '@teerank/teerank';
33
import prisma from '../../../utils/prisma';
44
import { searchParamSchema } from '../schema';
55
import { ComponentProps } from 'react';
6+
import redis from '../../../utils/redis';
67

78
export const metadata = {
89
title: 'All Servers - Teerank',
@@ -28,7 +29,7 @@ export default async function Index({
2829
skip: (page - 1) * 100,
2930
});
3031

31-
const { gameServerCount } = await getGlobalCounts();
32+
const globalCounts = await getGlobalCounts(redis);
3233

3334
const servers = gameServerStates.map((gameServerState, index) => ({
3435
rank: (page - 1) * 100 + index + 1,
@@ -41,5 +42,10 @@ export default async function Index({
4142
port: gameServerState.gameServer.port,
4243
}));
4344

44-
return <ServerList serverCount={gameServerCount} servers={servers} />;
45+
return (
46+
<ServerList
47+
serverCount={globalCounts.gameServers}
48+
servers={servers}
49+
/>
50+
);
4551
}

apps/frontend/app/page.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Link from 'next/link';
22
import { twMerge } from 'tailwind-merge';
33
import prisma from '../utils/prisma';
4-
import { getGlobalCounts } from '../utils/globalCounts';
4+
import { getGlobalCounts } from '@teerank/teerank';
55
import { formatInteger } from '../utils/format';
66
import { encodeString } from '../utils/encoding';
7+
import redis from '../utils/redis';
78

89
export const metadata = {
910
title: 'Teerank',
@@ -88,8 +89,7 @@ export default async function Index() {
8889
players,
8990
clans,
9091
gameTypes,
91-
gameTypesCount,
92-
{ playerCount, clanCount },
92+
globalCounts,
9393
] = await Promise.all([
9494
prisma.player.findMany({
9595
select: {
@@ -128,8 +128,7 @@ export default async function Index() {
128128
take: 3,
129129
}),
130130

131-
prisma.gameType.count(),
132-
getGlobalCounts(),
131+
getGlobalCounts(redis),
133132
]);
134133

135134
return (
@@ -141,7 +140,7 @@ export default async function Index() {
141140
<RankCard
142141
title="Players"
143142
titleHref="/all"
144-
count={playerCount}
143+
count={globalCounts.players}
145144
rankings={players.map((player) => player.name)}
146145
formatRankingHref={(playerName) =>
147146
`/player/${encodeString(playerName)}`
@@ -150,15 +149,15 @@ export default async function Index() {
150149
<RankCard
151150
title="Clans"
152151
titleHref="/all/clans"
153-
count={clanCount}
152+
count={globalCounts.clans}
154153
rankings={clans.map((clan) => clan.name)}
155154
formatRankingHref={(clanName) => `/clan/${encodeString(clanName)}`}
156155
/>
157156
<RankCard
158157
title="Gametypes"
159158
titleHref="/gametypes"
160-
count={gameTypesCount}
161-
rankings={gameTypes.map((gameTypes) => gameTypes.name)}
159+
count={globalCounts.gameTypes}
160+
rankings={gameTypes.map((gameType) => gameType.name)}
162161
formatRankingHref={(gameType) =>
163162
`/gametype/${encodeString(gameType)}`
164163
}

apps/frontend/utils/globalCounts.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

apps/frontend/utils/redis.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Redis } from 'ioredis'
2+
3+
const redisClientSingleton = () => {
4+
return new Redis()
5+
}
6+
7+
type RedisClientSingleton = ReturnType<typeof redisClientSingleton>
8+
9+
const globalForRedis = globalThis as unknown as {
10+
redis: RedisClientSingleton | undefined
11+
}
12+
13+
const redis = globalForRedis.redis ?? redisClientSingleton()
14+
15+
export default redis
16+
17+
if (process.env.NODE_ENV !== 'production') globalForRedis.redis = redis

0 commit comments

Comments
 (0)