mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 02:10:18 +01:00
Ajout traque-app
This commit is contained in:
24
traque-app/context/socketContext.jsx
Normal file
24
traque-app/context/socketContext.jsx
Normal file
@@ -0,0 +1,24 @@
|
||||
import { createContext, useContext, useMemo } from "react";
|
||||
import { io } from "socket.io-client";
|
||||
|
||||
const HOST = '192.168.15.129'; // IP of the machine hosting the server
|
||||
|
||||
const SOCKET_URL = (HOST == "traque.rezel.net" ? "wss://" : "ws://") + HOST + "/player";
|
||||
const SERVER_URL = (HOST == "traque.rezel.net" ? "https://" : "http://") + HOST + "/back";
|
||||
|
||||
export const teamSocket = io(SOCKET_URL, {
|
||||
path: "/back/socket.io",
|
||||
});
|
||||
|
||||
export const SocketContext = createContext();
|
||||
|
||||
export default function SocketProvider({ children }) {
|
||||
const value = useMemo(() => ({ teamSocket, SERVER_URL }), [teamSocket]);
|
||||
return (
|
||||
<SocketContext.Provider value={value}>{children}</SocketContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useSocket() {
|
||||
return useContext(SocketContext);
|
||||
}
|
||||
24
traque-app/context/teamConnexionContext.jsx
Normal file
24
traque-app/context/teamConnexionContext.jsx
Normal file
@@ -0,0 +1,24 @@
|
||||
import { createContext, useContext, useMemo } from "react";
|
||||
import { useSocket } from "./socketContext";
|
||||
import { useSocketAuth } from "../hook/useSocketAuth";
|
||||
|
||||
const teamConnexionContext = createContext();
|
||||
const TeamConnexionProvider = ({ children }) => {
|
||||
const { teamSocket } = useSocket();
|
||||
const { login, password: teamId, loggedIn, loading, logout } = useSocketAuth(teamSocket, "team_password");
|
||||
|
||||
const value = useMemo(() => ({ teamId, login, logout, loggedIn, loading}), [teamId, login, loggedIn, loading]);
|
||||
|
||||
return (
|
||||
<teamConnexionContext.Provider value={value}>
|
||||
{children}
|
||||
</teamConnexionContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function useTeamConnexion() {
|
||||
return useContext(teamConnexionContext);
|
||||
}
|
||||
|
||||
export { TeamConnexionProvider, useTeamConnexion };
|
||||
|
||||
50
traque-app/context/teamContext.jsx
Normal file
50
traque-app/context/teamContext.jsx
Normal file
@@ -0,0 +1,50 @@
|
||||
import { useLocation } from "../hook/useLocation";
|
||||
import { useSocketListener } from "../hook/useSocketListener";
|
||||
import { createContext, useContext, useMemo, useRef, useState } from "react";
|
||||
import { useSocket } from "./socketContext";
|
||||
import { GameState } from "../util/gameState";
|
||||
import useSendDeviceInfo from "../hook/useSendDeviceInfo";
|
||||
|
||||
|
||||
const teamContext = createContext()
|
||||
function TeamProvider({children}) {
|
||||
const [teamInfos, setTeamInfos] = useState({});
|
||||
const [gameState, setGameState] = useState(GameState.SETUP);
|
||||
const [gameSettings, setGameSettings] = useState(null);
|
||||
const [zoneExtremities, setZoneExtremities] = useState(null);
|
||||
const [nextZoneDate, setNextZoneDate] = useState(null);
|
||||
const [location, getLocationAuthorization, startLocationTracking, stopLocationTracking] = useLocation(5000, 10);
|
||||
const {teamSocket} = useSocket();
|
||||
const teamInfosRef = useRef();
|
||||
|
||||
useSendDeviceInfo();
|
||||
|
||||
teamInfosRef.current = teamInfos;
|
||||
|
||||
function setCurrentZone(data) {
|
||||
const newBegin = {points : data.begin.points.map( p => ({latitude: p.lat,longitude: p.lng}) ), duration: data.begin.duration};
|
||||
const newEnd = {points : data.end.points.map( p => ({latitude: p.lat,longitude: p.lng}) ), duration: data.end.duration};
|
||||
setZoneExtremities({begin: newBegin, end: newEnd});
|
||||
setNextZoneDate(data.endDate);
|
||||
}
|
||||
|
||||
useSocketListener(teamSocket, "update_team", (newTeamInfos) => {setTeamInfos({...teamInfosRef.current, ...newTeamInfos})});
|
||||
useSocketListener(teamSocket, "game_state", setGameState);
|
||||
useSocketListener(teamSocket, "current_zone", setCurrentZone);
|
||||
useSocketListener(teamSocket, "game_settings", setGameSettings);
|
||||
|
||||
const value = useMemo(() => (
|
||||
{teamInfos, gameState, zoneExtremities, nextZoneDate, gameSettings, location, getLocationAuthorization, startLocationTracking, stopLocationTracking}
|
||||
), [teamInfos, gameState, zoneExtremities, nextZoneDate, gameSettings, location]);
|
||||
return (
|
||||
<teamContext.Provider value={value}>
|
||||
{children}
|
||||
</teamContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function useTeamContext() {
|
||||
return useContext(teamContext);
|
||||
}
|
||||
|
||||
export { TeamProvider, useTeamContext };
|
||||
Reference in New Issue
Block a user