From e6563f2da56ea154cfc9d39003468a74c9d11c62 Mon Sep 17 00:00:00 2001 From: Quentin Roussel Date: Tue, 26 Mar 2024 14:35:10 +0100 Subject: [PATCH] fixed udpate position functionnality --- traque-back/game.js | 6 ----- traque-back/index.js | 17 ++++++++---- traque-front/app/team/track/page.js | 10 ++++--- traque-front/components/team/map.jsx | 2 +- traque-front/components/util/button.jsx | 2 +- .../context/adminConnexionContext.jsx | 12 +++++++-- traque-front/hook/useGame.jsx | 2 +- traque-front/hook/useLocalStorage.jsx | 27 +++++++++++++++++++ 8 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 traque-front/hook/useLocalStorage.jsx diff --git a/traque-back/game.js b/traque-back/game.js index 2011d97..ff7c370 100644 --- a/traque-back/game.js +++ b/traque-back/game.js @@ -69,9 +69,6 @@ export default class Game { } updateLocation(teamId, location) { - if(!this.started) { - return false; - } let team = this.getTeam(teamId); if(team == undefined) { return false; @@ -81,9 +78,6 @@ export default class Game { } sendLocation(teamId) { - if(!this.started) { - return false; - } let team = this.getTeam(teamId); if(team == undefined) { return false; diff --git a/traque-back/index.js b/traque-back/index.js index af0ae89..57622be 100644 --- a/traque-back/index.js +++ b/traque-back/index.js @@ -163,12 +163,13 @@ io.of("player").on("connection", (socket) => { } }); - socket.on("login", (teamId) => { - if(game.getTeam(teamId) === undefined) { + socket.on("login", (loginTeamId) => { + if(game.getTeam(loginTeamId) === undefined) { socket.emit("login_response", false); return; } - game.getTeam(teamId).sockets.push(socket.id); + teamId = loginTeamId; + game.getTeam(loginTeamId).sockets.push(socket.id); socket.emit("login_response", true); }); @@ -178,8 +179,14 @@ io.of("player").on("connection", (socket) => { socket.on("send_position", () => { game.sendLocation(teamId); - game.getTeam(teamId).sockets.forEach(s => { - io.of("player").to(s).emit("enemy_position", game.getTeam(teamId).enemyLocation); + let team = game.getTeam(teamId); + if(team === undefined) { + socket.emit("error", "Team not found"); + return; + } + game.updateTeamChasing(); + team.sockets.forEach(s => { + io.of("player").to(s).emit("enemy_position", team.enemyLocation); }); }); }); diff --git a/traque-front/app/team/track/page.js b/traque-front/app/team/track/page.js index 8f6bf3b..df90b3a 100644 --- a/traque-front/app/team/track/page.js +++ b/traque-front/app/team/track/page.js @@ -20,9 +20,13 @@ export default function Track() { return ( -
- - +
+
+ +
+
+ +

30min

before penalty

diff --git a/traque-front/components/team/map.jsx b/traque-front/components/team/map.jsx index 085167d..9b98f75 100644 --- a/traque-front/components/team/map.jsx +++ b/traque-front/components/team/map.jsx @@ -26,7 +26,7 @@ function MapPan(props) { export default function LiveMap({enemyPosition, currentPosition, ...props}) { return ( - + + return () } \ No newline at end of file diff --git a/traque-front/context/adminConnexionContext.jsx b/traque-front/context/adminConnexionContext.jsx index 506c049..d2949ad 100644 --- a/traque-front/context/adminConnexionContext.jsx +++ b/traque-front/context/adminConnexionContext.jsx @@ -1,15 +1,23 @@ "use client"; -import { createContext, useContext, useMemo, useState } from "react"; +import { createContext, useContext, useEffect, useMemo, useState } from "react"; import { useSocket } from "./socketContext"; import { useSocketListener } from "@/hook/useSocketListener"; +import { useLocalStorage } from "@/hook/useLocalStorage"; const adminContext = createContext(); const AdminConnexionProvider = ({ children }) => { const [loggedIn, setLoggedIn] = useState(false); + const [savedPassword, setSavedPassword] = useLocalStorage("admin_password", null); const { adminSocket } = useSocket(); + useEffect(() => { + if (savedPassword && !loggedIn) { + adminSocket.emit("login", savedPassword); + } + }, [savedPassword]); + function login(password) { - adminSocket.emit("login", password); + setSavedPassword(password) } useSocketListener(adminSocket, "login_response", setLoggedIn); diff --git a/traque-front/hook/useGame.jsx b/traque-front/hook/useGame.jsx index 67fe9ef..b9e200b 100644 --- a/traque-front/hook/useGame.jsx +++ b/traque-front/hook/useGame.jsx @@ -10,7 +10,7 @@ export default function useGame() { const {currentPosition, enemyPosition} = useTeamContext(); function sendCurrentPosition() { - teamSocket.emit("send_position", currentPosition); + teamSocket.emit("send_position"); } return { sendCurrentPosition, login, enemyPosition, currentPosition, loggedIn, teamId }; diff --git a/traque-front/hook/useLocalStorage.jsx b/traque-front/hook/useLocalStorage.jsx new file mode 100644 index 0000000..103da22 --- /dev/null +++ b/traque-front/hook/useLocalStorage.jsx @@ -0,0 +1,27 @@ +"use client"; +import { useEffect, useState } from "react"; + +export function useLocalStorage(key, initialValue) { + const [storedValue, setStoredValue] = useState(initialValue); + + useEffect(() => { + try { + const item = window.localStorage.getItem(key); + setStoredValue(item ? JSON.parse(item) : initialValue); + } catch (error) { + console.log(error); + } + }, []); + + const setValue = value => { + try { + const valueToStore = value instanceof Function ? value(storedValue) : value; + setStoredValue(valueToStore); + window.localStorage.setItem(key, JSON.stringify(valueToStore)); + } catch (error) { + console.log(error); + } + } + + return [storedValue, setValue]; +} \ No newline at end of file