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

@@ -1,8 +1,7 @@
"use client";
import { createContext, useContext, useEffect, useMemo, } from "react";
import { createContext, useContext, useMemo, } from "react";
import { useSocket } from "./socketContext";
import { useSocketAuth } from "@/hook/useSocketAuth";
import { redirect, usePathname } from "next/navigation";
import { usePasswordProtect } from "@/hook/usePasswordProtect";
const adminConnexionContext = createContext();

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}

View File

@@ -5,14 +5,17 @@ import { createContext, useContext, useEffect, useMemo, useState } from "react";
import { useSocket } from "./socketContext";
import { useTeamConnexion } from "./teamConnexionContext";
const teamContext = createContext()
function TeamProvider({children}) {
const [enemyPosition, setEnemyPosition] = useState();
const [currentPosition, setCurrentPosition] = useState();
const [gameState, setGameState] = useState(GameState.SETUP);
const measuredLocation = useLocation(10000);
const {teamSocket} = useSocket();
const {loggedIn} = useTeamConnexion();
useSocketListener(teamSocket, "game_state", setGameState);
useSocketListener(teamSocket, "enemy_position", setEnemyPosition);
useSocketListener(teamSocket, "live_location", setCurrentPosition);
@@ -24,7 +27,7 @@ function TeamProvider({children}) {
}
}, [loggedIn, measuredLocation]);
const value = useMemo(() => ({enemyPosition, currentPosition}), [enemyPosition, currentPosition]);
const value = useMemo(() => ({enemyPosition, currentPosition, gameState}), [enemyPosition, currentPosition, gameState]);
return (
<teamContext.Provider value={value}>
{children}