Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { NextResponse } from 'next/server';
import { z } from 'zod';
import { decodeIp } from '../../../../../../../utils/encoding';
import prisma from '../../../../../../../utils/prisma';

const paramsSchema = z.object({
ip: z.string().transform(decodeIp),
port: z.coerce.number().int().positive().max(65535),
snapshotId: z.coerce.number().int().positive(),
});

export async function GET(
_request: Request,
{ params }: { params: { ip: string; port: string; snapshotId: string } }
) {
const parsedParams = paramsSchema.safeParse(params);

if (!parsedParams.success) {
return NextResponse.json({ error: 'Bad request' }, { status: 400 });
}

const { ip, port, snapshotId } = parsedParams.data;

const snapshot = await prisma.gameServerSnapshot.findFirst({
where: {
id: snapshotId,
gameServer: {
ip,
port,
},
},
select: {
id: true,
createdAt: true,
name: true,
numClients: true,
maxClients: true,
map: {
select: {
name: true,
gameTypeName: true,
},
},
clients: {
orderBy: {
score: 'desc',
},
select: {
playerName: true,
clanName: true,
score: true,
},
},
},
});

if (snapshot === null) {
return NextResponse.json({ error: 'Not found' }, { status: 404 });
}

return NextResponse.json(snapshot, {
headers: {
'Cache-Control': 'public, max-age=86400, immutable',
},
});
}
160 changes: 95 additions & 65 deletions apps/frontend/app/server/[ip]/[port]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Link from 'next/link';
import { List, ListCell } from '../../../../components/List';
import { searchParamPageSchema } from '../../../../utils/page';
import prisma from '../../../../utils/prisma';
import { encodeString } from '../../../../utils/encoding';
import { encodeIp, encodeString } from '../../../../utils/encoding';
import { SnapshotTimeline } from '../../../../components/SnapshotTimeline';
import { formatPlayTime } from '../../../../utils/format';
import { GameServer } from '@prisma/client';
import { formatDuration, intervalToDuration } from 'date-fns';
Expand Down Expand Up @@ -78,33 +79,51 @@ export default async function Index({

const { ip, port } = parsedParams.data;

const gameServer = await prisma.gameServer.findUnique({
where: {
ip_port: {
ip,
port,
const [gameServer, snapshots] = await Promise.all([
prisma.gameServer.findUnique({
where: {
ip_port: {
ip,
port,
},
},
},
include: {
gameServerState: {
include: {
clients: {
orderBy: {
score: 'desc',
include: {
gameServerState: {
include: {
clients: {
orderBy: {
score: 'desc',
},
skip: (page - 1) * 100,
take: 100,
},
skip: (page - 1) * 100,
take: 100,
},
_count: {
select: {
clients: true,
_count: {
select: {
clients: true,
},
},
map: true,
},
map: true,
},
},
},
});
}),
prisma.gameServerSnapshot.findMany({
where: {
gameServer: {
ip,
port,
},
},
orderBy: {
createdAt: 'asc',
},
select: {
id: true,
createdAt: true,
numClients: true,
},
}),
]);

if (gameServer === null) {
return notFound();
Expand Down Expand Up @@ -162,50 +181,61 @@ export default async function Index({
</section>
</header>

<List
pageCount={Math.ceil(gameServer.gameServerState._count.clients / 100)}
columns={[
{
title: '',
expand: false,
},
{
title: 'Name',
expand: true,
},
{
title: 'Clan',
expand: true,
},
{
title: 'Score',
expand: false,
},
]}
<SnapshotTimeline
snapshots={snapshots.map((snapshot) => ({
id: snapshot.id,
createdAt: snapshot.createdAt.toISOString(),
numClients: snapshot.numClients,
}))}
apiPath={`/api/server/${encodeIp(gameServer.ip)}/${
gameServer.port
}/snapshot`}
>
{gameServer.gameServerState.clients.map((client, index) => (
<>
<ListCell alignRight label={`${index + 1}`} />
<ListCell
label={client.playerName}
href={{
pathname: `/player/${encodeString(client.playerName)}`,
}}
/>
<ListCell
label={client.clanName ?? ''}
href={
client.clanName === null
? undefined
: {
pathname: `/clan/${encodeString(client.clanName)}`,
}
}
/>
<ListCell alignRight label={client.score.toString()} />
</>
))}
</List>
<List
pageCount={Math.ceil(gameServer.gameServerState._count.clients / 100)}
columns={[
{
title: '',
expand: false,
},
{
title: 'Name',
expand: true,
},
{
title: 'Clan',
expand: true,
},
{
title: 'Score',
expand: false,
},
]}
>
{gameServer.gameServerState.clients.map((client, index) => (
<>
<ListCell alignRight label={`${index + 1}`} />
<ListCell
label={client.playerName}
href={{
pathname: `/player/${encodeString(client.playerName)}`,
}}
/>
<ListCell
label={client.clanName ?? ''}
href={
client.clanName === null
? undefined
: {
pathname: `/clan/${encodeString(client.clanName)}`,
}
}
/>
<ListCell alignRight label={client.score.toString()} />
</>
))}
</List>
</SnapshotTimeline>
</main>
);
}
Loading
Loading