automatic rejoin on reload or server restart

This commit is contained in:
Vinzenz Schroeter 2024-04-13 19:50:37 +02:00
commit 64a61ef2b3

View file

@ -1,25 +1,42 @@
import {Guid} from './Guid.ts';
export type PlayerResponse = {
readonly name: string;
readonly id: string;
readonly kills: number;
readonly deaths: number;
readonly scores: {
readonly kills: number;
readonly deaths: number;
};
};
export async function fetchTyped<T>({ url, method }: { url: URL; method: string; }) {
const response = await fetch(url, { method });
export type NameId = {
name: string,
id: Guid
};
export async function fetchTyped<T>({url, method}: { url: URL; method: string; }) {
const response = await fetch(url, {method});
if (!response.ok)
return null;
return await response.json() as T;
}
export function postPlayer(name: string) {
export function postPlayer({name, id}: NameId) {
const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL);
url.searchParams.set('name', name);
return fetchTyped<PlayerResponse>({ url, method: 'POST' });
url.searchParams.set('id', id);
return fetchTyped<NameId>({url, method: 'POST'});
}
export function getPlayer(id: string) {
const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL);
url.searchParams.set('id', id);
return fetchTyped<PlayerResponse>({ url, method: 'GET' });
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'});
}