mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 10:20:16 +01:00
fixed udpate position functionnality
This commit is contained in:
@@ -69,9 +69,6 @@ export default class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateLocation(teamId, location) {
|
updateLocation(teamId, location) {
|
||||||
if(!this.started) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
let team = this.getTeam(teamId);
|
let team = this.getTeam(teamId);
|
||||||
if(team == undefined) {
|
if(team == undefined) {
|
||||||
return false;
|
return false;
|
||||||
@@ -81,9 +78,6 @@ export default class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sendLocation(teamId) {
|
sendLocation(teamId) {
|
||||||
if(!this.started) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
let team = this.getTeam(teamId);
|
let team = this.getTeam(teamId);
|
||||||
if(team == undefined) {
|
if(team == undefined) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -163,12 +163,13 @@ io.of("player").on("connection", (socket) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("login", (teamId) => {
|
socket.on("login", (loginTeamId) => {
|
||||||
if(game.getTeam(teamId) === undefined) {
|
if(game.getTeam(loginTeamId) === undefined) {
|
||||||
socket.emit("login_response", false);
|
socket.emit("login_response", false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
game.getTeam(teamId).sockets.push(socket.id);
|
teamId = loginTeamId;
|
||||||
|
game.getTeam(loginTeamId).sockets.push(socket.id);
|
||||||
socket.emit("login_response", true);
|
socket.emit("login_response", true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -178,8 +179,14 @@ io.of("player").on("connection", (socket) => {
|
|||||||
|
|
||||||
socket.on("send_position", () => {
|
socket.on("send_position", () => {
|
||||||
game.sendLocation(teamId);
|
game.sendLocation(teamId);
|
||||||
game.getTeam(teamId).sockets.forEach(s => {
|
let team = game.getTeam(teamId);
|
||||||
io.of("player").to(s).emit("enemy_position", game.getTeam(teamId).enemyLocation);
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,9 +20,13 @@ export default function Track() {
|
|||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='h-full flex flex-col justify-between'>
|
<div className='h-full flex flex-col justify-between justify-items-stretch'>
|
||||||
<LiveMap currentPosition={currentPosition} enemyPosition={enemyPosition} className="h-4/5" />
|
<div className='h-5/6'>
|
||||||
<Button onClick={sendCurrentPosition}>Update position</Button>
|
<LiveMap currentPosition={currentPosition} enemyPosition={enemyPosition}/>
|
||||||
|
</div>
|
||||||
|
<div className="h-1/6">
|
||||||
|
<Button onClick={sendCurrentPosition}>Update position</Button>
|
||||||
|
</div>
|
||||||
<div className='shadow-lg m-5 p-2 flex flex-col text-center mx-auto w-4/5 rounded'>
|
<div className='shadow-lg m-5 p-2 flex flex-col text-center mx-auto w-4/5 rounded'>
|
||||||
<p className='text-xl text-black'>30min</p>
|
<p className='text-xl text-black'>30min</p>
|
||||||
<p className='text-gray-700'> before penalty</p>
|
<p className='text-gray-700'> before penalty</p>
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ function MapPan(props) {
|
|||||||
export default function LiveMap({enemyPosition, currentPosition, ...props}) {
|
export default function LiveMap({enemyPosition, currentPosition, ...props}) {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MapContainer {...props} center={[0,0]} zoom={0} scrollWheelZoom={true}>
|
<MapContainer {...props} className='min-h-full' center={[0,0]} zoom={0} scrollWheelZoom={true}>
|
||||||
<TileLayer
|
<TileLayer
|
||||||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||||||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
export default function Button({ children, ...props }) {
|
export default function Button({ children, ...props }) {
|
||||||
return (<button {...props} className="bg-blue-600 hover:bg-blue-500 ease-out duration-200 text-white w-full p-4 shadow-sm rounded">
|
return (<button {...props} className="bg-blue-600 hover:bg-blue-500 ease-out duration-200 text-white w-full h-full p-4 shadow-sm rounded">
|
||||||
{children}
|
{children}
|
||||||
</button>)
|
</button>)
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { createContext, useContext, useMemo, useState } from "react";
|
import { createContext, useContext, useEffect, useMemo, useState } from "react";
|
||||||
import { useSocket } from "./socketContext";
|
import { useSocket } from "./socketContext";
|
||||||
import { useSocketListener } from "@/hook/useSocketListener";
|
import { useSocketListener } from "@/hook/useSocketListener";
|
||||||
|
import { useLocalStorage } from "@/hook/useLocalStorage";
|
||||||
|
|
||||||
const adminContext = createContext();
|
const adminContext = createContext();
|
||||||
const AdminConnexionProvider = ({ children }) => {
|
const AdminConnexionProvider = ({ children }) => {
|
||||||
const [loggedIn, setLoggedIn] = useState(false);
|
const [loggedIn, setLoggedIn] = useState(false);
|
||||||
|
const [savedPassword, setSavedPassword] = useLocalStorage("admin_password", null);
|
||||||
const { adminSocket } = useSocket();
|
const { adminSocket } = useSocket();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (savedPassword && !loggedIn) {
|
||||||
|
adminSocket.emit("login", savedPassword);
|
||||||
|
}
|
||||||
|
}, [savedPassword]);
|
||||||
|
|
||||||
function login(password) {
|
function login(password) {
|
||||||
adminSocket.emit("login", password);
|
setSavedPassword(password)
|
||||||
}
|
}
|
||||||
|
|
||||||
useSocketListener(adminSocket, "login_response", setLoggedIn);
|
useSocketListener(adminSocket, "login_response", setLoggedIn);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export default function useGame() {
|
|||||||
const {currentPosition, enemyPosition} = useTeamContext();
|
const {currentPosition, enemyPosition} = useTeamContext();
|
||||||
|
|
||||||
function sendCurrentPosition() {
|
function sendCurrentPosition() {
|
||||||
teamSocket.emit("send_position", currentPosition);
|
teamSocket.emit("send_position");
|
||||||
}
|
}
|
||||||
|
|
||||||
return { sendCurrentPosition, login, enemyPosition, currentPosition, loggedIn, teamId };
|
return { sendCurrentPosition, login, enemyPosition, currentPosition, loggedIn, teamId };
|
||||||
|
|||||||
27
traque-front/hook/useLocalStorage.jsx
Normal file
27
traque-front/hook/useLocalStorage.jsx
Normal file
@@ -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];
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user