mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 10:20:16 +01:00
Implemented player login
This commit is contained in:
36
traque-front/context/teamContext.jsx
Normal file
36
traque-front/context/teamContext.jsx
Normal file
@@ -0,0 +1,36 @@
|
||||
"use client";
|
||||
import { useLocation } from "@/hook/useLocation";
|
||||
import { useSocketListener } from "@/hook/useSocketListener";
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
import { useSocket } from "./socketContext";
|
||||
import { useTeamConnexion } from "./teamConnexionContext";
|
||||
|
||||
const teamContext = createContext()
|
||||
function TeamProvider({children}) {
|
||||
const [enemyPosition, setEnemyPosition] = useState();
|
||||
const currentPosition = useLocation(10000);
|
||||
const {teamSocket} = useSocket();
|
||||
const {loggedIn} = useTeamConnexion();
|
||||
|
||||
useSocketListener(teamSocket, "enemy_position", setEnemyPosition);
|
||||
|
||||
//Send the current position to the server when the user is logged in
|
||||
useEffect(() => {
|
||||
console.log("sending position", currentPosition);
|
||||
if(loggedIn) {
|
||||
teamSocket.emit("update_position", currentPosition);
|
||||
}
|
||||
}, [loggedIn, currentPosition]);
|
||||
|
||||
return (
|
||||
<teamContext.Provider value={{enemyPosition, currentPosition}}>
|
||||
{children}
|
||||
</teamContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function useTeamContext() {
|
||||
return useContext(teamContext);
|
||||
}
|
||||
|
||||
export { TeamProvider, useTeamContext };
|
||||
Reference in New Issue
Block a user