Skip to content

Commit

Permalink
Prevent starting the on going race #220
Browse files Browse the repository at this point in the history
  • Loading branch information
neu5 committed Sep 14, 2023
1 parent b4b8c96 commit 1c4930c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 33 deletions.
12 changes: 6 additions & 6 deletions packages/client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 1 addition & 12 deletions packages/client/src/sockets/sockets.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down
26 changes: 19 additions & 7 deletions packages/client/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ const debounce = (func: Function, timeFrame: number = 500) => {
let timeoutId: ReturnType<typeof setTimeout>;

return (...args: Array<any>) => {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
func.apply(null, args);
}, timeFrame);
if (timeoutId) {
clearTimeout(timeoutId);
}

timeoutId = setTimeout(() => {
func.apply(null, args);
}, timeFrame);
};
};

Expand All @@ -227,6 +227,17 @@ const log = throttle((...args: Array<any>) => {
console.log(...args);
}, 1000);

const toggleStartRaceBtns = (
startRaceBtn: HTMLElement,
canStartTheRace: boolean
) => {
if (canStartTheRace) {
startRaceBtn.removeAttribute("disabled");
} else {
startRaceBtn.setAttribute("disabled", "disabled");
}
};

export {
addBox,
addColors,
Expand All @@ -235,5 +246,6 @@ export {
addRigidVehicle,
debounce,
log,
toggleStartRaceBtns,
TOAST_COLORS,
};
6 changes: 6 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const io = new Server<ServerToClientEvents>(httpServer);
export type Game = {
config: GameConfig;
objects: GameObject[];
race: {
isStarted: boolean
}
};

let game: Game = {
Expand All @@ -35,6 +38,9 @@ let game: Game = {
depth: 0,
},
objects: [],
race: {
isStarted: false
}
};

io.use((socket: Socket, next) => {
Expand Down
16 changes: 8 additions & 8 deletions packages/server/src/sockets/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1c4930c

Please sign in to comment.