implemented game state in front end

This commit is contained in:
Quentin Roussel
2024-03-29 00:33:40 +01:00
parent ba2a4a0ea1
commit ae2587bc3a
14 changed files with 51 additions and 35 deletions

View File

@@ -3,15 +3,17 @@ import { createContext, useContext, useEffect, useMemo, useState } from "react";
import { useSocket } from "./socketContext";
import { useSocketListener } from "@/hook/useSocketListener";
import { useAdminConnexion } from "./adminConnexionContext";
import { GameState } from "@/util/gameState";
const adminContext = createContext();
function AdminProvider({children}) {
const [teams, setTeams] = useState([]);
const [started, setStarted] = useState(false);
const { adminSocket } = useSocket();
const {loggedIn} = useAdminConnexion();
const [gameState, setGameState] = useState(GameState.SETUP);
useSocketListener(adminSocket, "game_state", setGameState);
//Send a request to get the teams when the user logs in
useEffect(() => {
adminSocket.emit("get_teams");
@@ -19,9 +21,8 @@ function AdminProvider({children}) {
//Bind listeners to update the team list and the game status on socket message
useSocketListener(adminSocket, "teams", setTeams);
useSocketListener(adminSocket, "game_started", setStarted);
const value = useMemo(() => ({teams, setTeams, started, setStarted}), [teams, started]);
const value = useMemo(() => ({teams, setTeams, gameState}), [teams, gameState]);
return (
<adminContext.Provider value={value}>
{children}