servicepoint-tanks/tank-frontend/src/serverCalls.tsx

62 lines
1.6 KiB
TypeScript
Raw Normal View History

import useWebSocket, {Options} from 'react-use-websocket';
2024-04-21 14:34:45 +02:00
export function makeApiUrl(path: string, protocol: 'http' | 'ws' = 'http') {
2024-04-22 21:26:46 +02:00
return new URL(`${protocol}://${window.location.hostname}${path}`);
2024-04-21 14:34:45 +02:00
}
2024-04-22 19:03:07 +02:00
export type Scores = {
readonly kills: number;
readonly deaths: number;
readonly wallsDestroyed: number;
2024-04-22 21:26:46 +02:00
readonly shotsFired: number;
readonly overallScore: number;
2024-04-29 13:54:29 +02:00
readonly powerUpsCollected: number;
2024-04-29 14:13:04 +02:00
readonly pixelsMoved: number;
2024-04-22 19:03:07 +02:00
};
type Tank = {
2024-05-08 01:01:11 +02:00
readonly pixelPosition: { x: number; y: number };
2024-05-05 13:51:59 +02:00
readonly orientation: number;
readonly moving: boolean;
readonly bulletStats: BulletStats;
readonly reloadingUntil: string;
readonly nextShotAfter: string;
2024-05-08 01:01:11 +02:00
readonly usedBullets: number;
readonly maxBullets: number;
2024-05-05 13:51:59 +02:00
}
export type Player = {
2024-05-05 13:51:59 +02:00
readonly name: string;
readonly scores: Scores;
readonly openConnections: number;
readonly lastInput: string;
}
export type PlayerInfoMessage = {
readonly player: Player;
readonly controls: string;
readonly tank?: Tank;
2024-05-05 13:51:59 +02:00
}
2024-05-05 23:33:08 +02:00
export type MapInfo = {
readonly name: string;
readonly typeName: string;
readonly preview: string;
}
export type BulletStats = {
speed: number;
acceleration: number,
explosive: boolean,
smart: boolean
};
2024-05-04 14:25:37 +02:00
export function useMyWebSocket<T = unknown>(url: string, options: Options = {}) {
return useWebSocket<T>(url, {
shouldReconnect: () => true,
2024-05-04 14:25:37 +02:00
reconnectAttempts: 2,
onReconnectStop: () => alert('server connection failed. please reload.'),
...options
});
}