feat/FRO-31: Added ingame (#20)

Force push of Janis ingame changes

Co-authored-by: Janis <janis.e.20@gmx.de>
Reviewed-on: #20
This commit is contained in:
2025-12-11 00:15:50 +01:00
parent 97b7df2a75
commit 39898ed03b
11 changed files with 209 additions and 13 deletions

View File

@@ -1,3 +1,6 @@
import {useIngame} from "@/composables/useIngame.ts";
import type {GameInfo, LobbyInfo, TieInfo, TrumpInfo, WonInfo} from "@/types/GameTypes.ts";
const api = window.__RUNTIME_CONFIG__?.API_URL;
// ---------- Types ---------------------------------------------------------
@@ -6,6 +9,8 @@ export type ServerMessage<T = any> = {
id?: string;
event?: string;
status?: "success" | "error";
state?: "Lobby" | "InGame" | "SelectTrump" | "TieBreak" | "FinishedMatch";
stateData?: GameInfo | LobbyInfo | TieInfo | TrumpInfo | WonInfo;
data?: T;
error?: string;
};
@@ -28,9 +33,12 @@ let ws: WebSocket | null = null;
const pending = new Map<string, PendingEntry>();
const handlers = new Map<string, HandlerFn>();
const uState = useIngame();
let heartbeatTimer: ReturnType<typeof setInterval> | null = null;
let defaultHandler: HandlerFn | null = null;
function uuid(): string {
return crypto.randomUUID();
}
@@ -73,7 +81,7 @@ function setupSocketHandlers(socket: WebSocket) {
return;
}
const { id, event, status, data } = msg;
const { id, event, state, stateData, status, data } = msg;
// RPC response branch
if (id && status) {
@@ -91,6 +99,10 @@ function setupSocketHandlers(socket: WebSocket) {
return;
}
if (state && stateData) {
uState.setIngame(state, stateData);
}
// Server event → handler branch
if (id && event) {
const handler = handlers.get(event);
@@ -103,7 +115,17 @@ function setupSocketHandlers(socket: WebSocket) {
if (!handler) {
console.warn("[WS] No handler for event:", event);
reply("error", `No handler for '${event}'`);
if (defaultHandler) {
try {
await defaultHandler(data ?? {});
reply("success");
} catch (err) {
reply("error", (err as Error).message);
}
} else {
reply("error", `No handler for '${event}'`);
}
return;
}
@@ -232,5 +254,9 @@ export function onEvent(event: string, handler: HandlerFn) {
handlers.set(event, handler);
}
export function setDefaultHandler(handler: HandlerFn) {
defaultHandler = handler;
}
export const isWebSocketConnected = () =>
!!ws && ws.readyState === WebSocket.OPEN;