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-05-08 00:29:33 +02:00
|
|
|
type Tank = {
|
2024-05-05 13:51:59 +02:00
|
|
|
readonly position: { x: number; y: number };
|
|
|
|
readonly orientation: number;
|
|
|
|
readonly moving: boolean;
|
2024-05-08 00:29:33 +02:00
|
|
|
readonly bulletStats: BulletStats;
|
|
|
|
readonly reloadingUntil: string;
|
|
|
|
readonly nextShotAfter: string;
|
|
|
|
readonly magazine: {
|
|
|
|
readonly empty: boolean;
|
|
|
|
readonly usedBullets: number;
|
|
|
|
readonly maxBullets: number;
|
|
|
|
readonly displayString: string;
|
|
|
|
};
|
2024-05-05 13:51:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
export type Player = {
|
2024-05-05 13:51:59 +02:00
|
|
|
readonly name: string;
|
|
|
|
readonly scores: Scores;
|
|
|
|
readonly openConnections: number;
|
2024-05-08 00:29:33 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
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 = {}) {
|
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
|
|
|
|
});
|
|
|
|
}
|