2024-04-13 19:50:37 +02:00
|
|
|
import {Guid} from './Guid.ts';
|
|
|
|
|
2024-04-13 17:56:33 +02:00
|
|
|
export type PlayerResponse = {
|
|
|
|
readonly name: string;
|
2024-04-13 23:07:08 +02:00
|
|
|
readonly id: Guid;
|
2024-04-13 19:50:37 +02:00
|
|
|
readonly scores: {
|
|
|
|
readonly kills: number;
|
|
|
|
readonly deaths: number;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export type NameId = {
|
|
|
|
name: string,
|
|
|
|
id: Guid
|
2024-04-13 17:56:33 +02:00
|
|
|
};
|
|
|
|
|
2024-04-13 19:50:37 +02:00
|
|
|
export async function fetchTyped<T>({url, method}: { url: URL; method: string; }) {
|
|
|
|
const response = await fetch(url, {method});
|
2024-04-13 17:56:33 +02:00
|
|
|
if (!response.ok)
|
|
|
|
return null;
|
|
|
|
return await response.json() as T;
|
|
|
|
}
|
|
|
|
|
2024-04-13 19:50:37 +02:00
|
|
|
export function postPlayer({name, id}: NameId) {
|
2024-04-13 17:56:33 +02:00
|
|
|
const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL);
|
|
|
|
url.searchParams.set('name', name);
|
2024-04-13 19:50:37 +02:00
|
|
|
url.searchParams.set('id', id);
|
|
|
|
|
|
|
|
return fetchTyped<NameId>({url, method: 'POST'});
|
2024-04-13 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 23:07:08 +02:00
|
|
|
export function getPlayer(id: Guid) {
|
2024-04-13 17:56:33 +02:00
|
|
|
const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL);
|
|
|
|
url.searchParams.set('id', id);
|
2024-04-13 19:50:37 +02:00
|
|
|
|
|
|
|
return fetchTyped<PlayerResponse>({url, method: 'GET'});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getScores() {
|
|
|
|
const url = new URL('/scores', import.meta.env.VITE_TANK_API);
|
|
|
|
return fetchTyped<PlayerResponse[]>({url, method: 'GET'});
|
2024-04-13 17:56:33 +02:00
|
|
|
}
|