2024-04-13 19:50:37 +02:00
|
|
|
import {Guid} from './Guid.ts';
|
|
|
|
|
2024-04-14 18:26:13 +02:00
|
|
|
export type ServerResponse<T> = {
|
|
|
|
ok: boolean;
|
|
|
|
statusCode: number;
|
|
|
|
statusText: string;
|
|
|
|
additionalErrorText?: string;
|
|
|
|
successResult?: T;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Player = {
|
2024-04-13 17:56:33 +02:00
|
|
|
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;
|
2024-04-19 13:34:56 +02:00
|
|
|
readonly wallsDestroyed: number;
|
2024-04-13 19:50:37 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export type NameId = {
|
|
|
|
name: string,
|
|
|
|
id: Guid
|
2024-04-13 17:56:33 +02:00
|
|
|
};
|
|
|
|
|
2024-04-14 18:26:13 +02:00
|
|
|
export async function fetchTyped<T>({url, method}: { url: URL; method: string; }): Promise<ServerResponse<T>> {
|
2024-04-13 19:50:37 +02:00
|
|
|
const response = await fetch(url, {method});
|
2024-04-14 18:26:13 +02:00
|
|
|
const result: ServerResponse<T> = {
|
|
|
|
ok: response.ok,
|
|
|
|
statusCode: response.status,
|
|
|
|
statusText: response.statusText
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.ok)
|
|
|
|
result.successResult = await response.json();
|
|
|
|
else
|
|
|
|
result.additionalErrorText = await response.text();
|
|
|
|
return result;
|
2024-04-13 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 19:50:37 +02:00
|
|
|
export function postPlayer({name, id}: NameId) {
|
2024-04-16 00:07:44 +02:00
|
|
|
const url = new URL('/player', import.meta.env.VITE_TANK_API);
|
2024-04-13 17:56:33 +02:00
|
|
|
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
|
|
|
}
|