feat: chat v lobby

This commit is contained in:
Vít Kilián authored and batmanisko committed 2026-10-22 10:29:20 +02:00
1 parent 9a3987a2fa
commit 6beb15afe3
10 files changed
+130 -2

No files matched your search

+17 -1
View File
@@ -1,4 +1,4 @@
import { Socket } from "socket.io";
import { Server, Socket } from "socket.io";
import { getLogin } from "./auth";
/** Sdílené pomůcky pro websocketové minihry (reakční hra, had). */
@@ -60,3 +60,19 @@ export function gameHandler(socket: Socket, errorEvent: string, action: (login:
/** Minimální počet hráčů — lokálně lze hrát i sám. */
export const minPlayersForEnv = (prod: number) => process.env.NODE_ENV === 'development' ? 1 : prod;
/**
* Jednoduchý chat v lobby miniher — zprávy se nikde neukládají, server je jen
* přepošle všem (klient si je filtruje podle hry).
*/
export function registerLobbyChat(io: Server) {
io.on('connection', socket => {
socket.on('lobbychat:send', gameHandler(socket, 'lobbychat:error', (login, payload) => {
const { game, text } = (payload ?? {}) as { game?: unknown, text?: unknown };
if (typeof game !== 'string' || typeof text !== 'string') return;
const trimmed = text.trim().slice(0, 200);
if (!trimmed) return;
io.emit('lobbychat:message', { game: game.slice(0, 20), login, text: trimmed });
}));
});
}