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:
@@ -1,28 +1,14 @@
|
||||
import { useAdminContext } from "@/context/adminContext";
|
||||
import { useSocket } from "@/context/socketContext";
|
||||
import { Underdog } from "next/font/google";
|
||||
|
||||
const { useEffect, useState } = require("react");
|
||||
|
||||
export default function useAdmin(){
|
||||
const {teams, setTeams, started, setStarted} = useAdminContext();
|
||||
const {teams, started} = useAdminContext();
|
||||
const {adminSocket} = useSocket();
|
||||
|
||||
function pollTeams() {
|
||||
adminSocket.emit("get_teams");
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
pollTeams();
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
adminSocket.emit("get_teams");
|
||||
adminSocket.on("teams", setTeams);
|
||||
return () => {
|
||||
adminSocket.off("teams", setTeams);
|
||||
}
|
||||
}, []);
|
||||
|
||||
function getTeam(teamId) {
|
||||
return teams.find(team => team.id === teamId);
|
||||
}
|
||||
@@ -56,13 +42,6 @@ export default function useAdmin(){
|
||||
adminSocket.emit("stop_game");
|
||||
}
|
||||
|
||||
useState(() => {
|
||||
adminSocket.on("game_started", setStarted);
|
||||
return () => {
|
||||
adminSocket.off("game_started", setStarted);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { teams, started, pollTeams, getTeam, getTeamName, reorderTeams, addTeam, removeTeam, startGame, stopGame, setTeamName };
|
||||
|
||||
}
|
||||
@@ -1,61 +1,17 @@
|
||||
"use client";
|
||||
|
||||
import { useSocket } from "@/context/socketContext";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTeamConnexion } from "@/context/teamConnexionContext";
|
||||
import { useTeamContext } from "@/context/teamContext";
|
||||
|
||||
export default function useGame() {
|
||||
const {userSocket} = useSocket();
|
||||
const [loggedIn, setLoggedIn] = useState(false);
|
||||
const [teamId, setTeamId] = useState(null);
|
||||
const [enemyPosition, setEnemyPosition] = useState([0, 0]);
|
||||
const [currentPosition, setCurrentPosition] = useState([0, 0]);
|
||||
|
||||
function updateCurrentPosition(position) {
|
||||
setCurrentPosition(position);
|
||||
userSocket.emit("update_position", position);
|
||||
}
|
||||
const {teamSocket} = useSocket();
|
||||
const {loggedIn, login, teamId} = useTeamConnexion();
|
||||
const {currentPosition, enemyPosition} = useTeamContext();
|
||||
|
||||
function sendCurrentPosition() {
|
||||
userSocket.emit("send_position", currentPosition);
|
||||
teamSocket.emit("send_position", currentPosition);
|
||||
}
|
||||
useEffect(() => {
|
||||
function updateEnemyPosition(position) {
|
||||
setEnemyPosition(position);
|
||||
}
|
||||
userSocket.on("enemy_position", updateEnemyPosition);
|
||||
|
||||
return () => {
|
||||
userSocket.off("enemy_position", updateEnemyPosition);
|
||||
}
|
||||
}, []);
|
||||
|
||||
function login(teamId) {
|
||||
setTeamId(teamId);
|
||||
userSocket.emit("login", teamId);
|
||||
}
|
||||
useEffect(() => {
|
||||
function updateLoginStatus(status) {
|
||||
setLoggedIn(status);
|
||||
}
|
||||
userSocket.on("login_reponse", updateLoginStatus);
|
||||
|
||||
return () => {
|
||||
userSocket.off("login_response", updateLoginStatus);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
function udpate() {
|
||||
console.log("update")
|
||||
const position = navigator.geolocation.getCurrentPosition((position) => {
|
||||
updateCurrentPosition([position.coords.latitude, position.coords.longitude]);
|
||||
}, () => { }, { enableHighAccuracy: true, timeout: Infinity, maximumAge: 0 });
|
||||
}
|
||||
setInterval(udpate, 1000);
|
||||
return () => {
|
||||
clearInterval(udpate);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { updateCurrentPosition, sendCurrentPosition, login, enemyPosition, currentPosition, loggedIn, teamId };
|
||||
return { sendCurrentPosition, login, enemyPosition, currentPosition, loggedIn, teamId };
|
||||
}
|
||||
21
traque-front/hook/useLocation.jsx
Normal file
21
traque-front/hook/useLocation.jsx
Normal file
@@ -0,0 +1,21 @@
|
||||
"use client";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
/**
|
||||
* A hook that returns the location of the user and updates it periodically
|
||||
* @returns {Object} The location of the user
|
||||
*/
|
||||
export function useLocation(interval) {
|
||||
const [location, setLocation] = useState();
|
||||
useEffect(() => {
|
||||
function update() {
|
||||
navigator.geolocation.getCurrentPosition((position) => {
|
||||
setLocation([position.coords.latitude, position.coords.longitude]);
|
||||
setTimeout(update, interval);
|
||||
}, () => { }, { enableHighAccuracy: true, timeout: Infinity, maximumAge: 0 });
|
||||
}
|
||||
update();
|
||||
}, []);
|
||||
|
||||
return location;
|
||||
}
|
||||
10
traque-front/hook/useSocketListener.jsx
Normal file
10
traque-front/hook/useSocketListener.jsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import { useState } from "react";
|
||||
|
||||
export function useSocketListener(socket, event, callback) {
|
||||
return useState(() => {
|
||||
socket.on(event, callback);
|
||||
return () => {
|
||||
socket.off(event, callback);
|
||||
}
|
||||
}, []);
|
||||
}
|
||||
Reference in New Issue
Block a user