diff --git a/packages/client/src/main.ts b/packages/client/src/main.ts index deec9f3..8cc834f 100644 --- a/packages/client/src/main.ts +++ b/packages/client/src/main.ts @@ -7,7 +7,7 @@ import { ui } from "./ui"; import { loginDialog } from "./ui/dialog-login"; import { startRace } from "./scene/scene"; // import { UIDialogWrapper, UIcreatePlayersList, UIsetCurrentPlayer } from "./ui"; -import { debounce } from "./utils"; +import { debounce, toggleStartRaceBtns } from "./utils"; import type { Game, GameConfig, GameObject, Position } from "@neu5/types/src"; import type { Quaternion } from "@babylonjs/core"; @@ -216,20 +216,20 @@ const startEngineLoop = ({ engine, playersMap, scene }: { engine: Engine, player socket.on( "server:start-race", async ({ - playersList, config, + isRaceStarted, objects, - // race, + playersList }: { - playersList: PlayersMap; config: GameConfig; + isRaceStarted: boolean; objects: GameObject[]; - // race: Race; + playersList: PlayersMap; }) => { scene.dispose(); engine.stopRenderLoop(); - // toggleRaceBtns(race.isStarted); + toggleStartRaceBtns(game.elements.startRaceBtn, !isRaceStarted); const newScene = await startRace({ engine, diff --git a/packages/client/src/sockets/sockets.ts b/packages/client/src/sockets/sockets.ts index afa55ef..669b167 100644 --- a/packages/client/src/sockets/sockets.ts +++ b/packages/client/src/sockets/sockets.ts @@ -1,6 +1,6 @@ import { io } from "socket.io-client"; import Toastify from "toastify-js"; -import { TOAST_COLORS, log } from "../utils"; +import { TOAST_COLORS, log, toggleStartRaceBtns } from "../utils"; import type { Socket } from "socket.io-client"; import type { Game, RoomList, UsersList } from "@neu5/types/src"; @@ -10,17 +10,6 @@ type ExtendedSocket = Socket & { userID?: string; }; -const toggleStartRaceBtns = ( - startRaceBtn: HTMLElement, - canStartTheRace: boolean -) => { - if (canStartTheRace) { - startRaceBtn.removeAttribute("disabled"); - } else { - startRaceBtn.setAttribute("disabled", "disabled"); - } -}; - const createSocketHandler = ({ dialog, game, diff --git a/packages/client/src/utils/index.ts b/packages/client/src/utils/index.ts index df15a26..2c748cd 100644 --- a/packages/client/src/utils/index.ts +++ b/packages/client/src/utils/index.ts @@ -200,13 +200,13 @@ const debounce = (func: Function, timeFrame: number = 500) => { let timeoutId: ReturnType; return (...args: Array) => { - if (timeoutId) { - clearTimeout(timeoutId); - } - - timeoutId = setTimeout(() => { - func.apply(null, args); - }, timeFrame); + if (timeoutId) { + clearTimeout(timeoutId); + } + + timeoutId = setTimeout(() => { + func.apply(null, args); + }, timeFrame); }; }; @@ -227,6 +227,17 @@ const log = throttle((...args: Array) => { console.log(...args); }, 1000); +const toggleStartRaceBtns = ( + startRaceBtn: HTMLElement, + canStartTheRace: boolean +) => { + if (canStartTheRace) { + startRaceBtn.removeAttribute("disabled"); + } else { + startRaceBtn.setAttribute("disabled", "disabled"); + } +}; + export { addBox, addColors, @@ -235,5 +246,6 @@ export { addRigidVehicle, debounce, log, + toggleStartRaceBtns, TOAST_COLORS, }; diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 0239845..654a568 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -26,6 +26,9 @@ const io = new Server(httpServer); export type Game = { config: GameConfig; objects: GameObject[]; + race: { + isStarted: boolean + } }; let game: Game = { @@ -35,6 +38,9 @@ let game: Game = { depth: 0, }, objects: [], + race: { + isStarted: false + } }; io.use((socket: Socket, next) => { diff --git a/packages/server/src/sockets/sockets.ts b/packages/server/src/sockets/sockets.ts index 173fd95..5c8b29e 100644 --- a/packages/server/src/sockets/sockets.ts +++ b/packages/server/src/sockets/sockets.ts @@ -166,9 +166,9 @@ const createSocketHandlers = ({ io.emit("server:send users", sessionStore.getAuthorizedUsers()); emitRoomInfo({ io, room: roomRace, sessionStore }); - // if (!game.isRaceStarted) { - socket.emit("server:user can join the room"); - // } + if (!game.race.isStarted) { + socket.emit("server:user can join the room"); + } socket.emit("server:close dialog"); }); @@ -224,20 +224,20 @@ const createSocketHandlers = ({ }); socket.on("client:start the race", async () => { - // race.isStarted = true; + game.race.isStarted = true; game.config = { width: 100, height: 100, depth: 0.1, }; - const a = await startRace({ game, room: roomRace, sessionStore }); - raceLoop = a.loop; - playersMap = a.playersMap; + const race = await startRace({ game, room: roomRace, sessionStore }); + raceLoop = race.loop; + playersMap = race.playersMap; io.emit("server:start-race", { playersList: playersMapToArray(playersMap), - // race, + isRaceStarted: game.race.isStarted, config: game.config, objects: game.objects.map(({ isWall, name, position, quaternion }) => ({ name,