Files
electric-cherry/src/routes/leaderboard/players/[guid]/+page.server.ts
T

47 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { bankProfile } from '$lib/server/fleet/bank.js';
import { error } from '@sveltejs/kit';
import { playerReport } from '$lib/server/reports/index.js';
import { isLeaderboardMetric } from '$lib/server/fleet/leaderboard.js';
import { leaderboardReturnHref } from '$lib/leaderboard-navigation.js';
import type { LeaderboardPlayerCount } from '$lib/leaderboard.js';
export async function load({ params, url }: { params: { guid: string }; url: URL }) {
const game = url.searchParams.get('game');
const map = url.searchParams.get('map');
const requestedMetric = url.searchParams.get('metric');
const players = url.searchParams.get('players');
if (
(game !== null && game !== 't5' && game !== 't6') ||
(map !== null && map.length > 1024) ||
(requestedMetric !== null && !isLeaderboardMetric(requestedMetric)) ||
(players !== null && !/^[1-4]$/.test(players))
) {
error(
400,
'Use game t5 or t6, a supported metric, players 1–4, and a map key up to 1024 characters.'
);
}
const metric =
requestedMetric !== null && isLeaderboardMetric(requestedMetric)
? requestedMetric
: 'highestRound';
const options = {
game: game || undefined,
map: map || undefined,
players: players === null ? undefined : (Number(players) as LeaderboardPlayerCount),
metric
};
const { profile, entry } = await playerReport(
params.guid,
Number(url.searchParams.get('offset') ?? 0),
options
);
if (!profile) error(404, 'Player not found');
return {
profile,
bank: bankProfile(params.guid),
returnTo: leaderboardReturnHref(url.searchParams.get('returnTo')),
scope: { game: game ?? '', map: map ?? '', players: players ?? '', metric, entry }
};
}