2024-04-30 10:52:29 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-04-14 18:26:13 +02:00
|
|
|
export type Player = {
|
2024-04-13 17:56:33 +02:00
|
|
|
readonly name: string;
|
2024-04-22 19:03:07 +02:00
|
|
|
readonly scores: Scores;
|
2024-04-13 19:50:37 +02:00
|
|
|
};
|
2024-04-30 10:52:29 +02:00
|
|
|
|
2024-05-05 13:51:59 +02:00
|
|
|
type TankInfo = {
|
|
|
|
readonly magazine: string;
|
|
|
|
readonly position: { x: number; y: number };
|
|
|
|
readonly orientation: number;
|
|
|
|
readonly moving: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PlayerInfoMessage = {
|
|
|
|
readonly name: string;
|
|
|
|
readonly scores: Scores;
|
|
|
|
readonly controls: string;
|
|
|
|
readonly tank?: TankInfo;
|
|
|
|
readonly openConnections: number;
|
|
|
|
}
|
|
|
|
|
2024-05-05 23:33:08 +02:00
|
|
|
export type MapInfo = {
|
|
|
|
readonly name: string;
|
|
|
|
readonly typeName: string;
|
|
|
|
readonly preview: string;
|
|
|
|
}
|
|
|
|
|
2024-05-04 14:25:37 +02:00
|
|
|
export function useMyWebSocket<T = unknown>(url: string, options: Options = {}) {
|
2024-04-30 10:52:29 +02:00
|
|
|
return useWebSocket<T>(url, {
|
|
|
|
shouldReconnect: () => true,
|
2024-05-04 14:25:37 +02:00
|
|
|
reconnectAttempts: 2,
|
2024-04-30 10:52:29 +02:00
|
|
|
onReconnectStop: () => alert('server connection failed. please reload.'),
|
|
|
|
...options
|
|
|
|
});
|
|
|
|
}
|