mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 10:20:16 +01:00
added zone settings in admin ui
This commit is contained in:
BIN
2024-04-08-Note-11-05.xopp
Normal file
BIN
2024-04-08-Note-11-05.xopp
Normal file
Binary file not shown.
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { useLocation } from "@/hook/useLocation";
|
import { useLocation } from "@/hook/useLocation";
|
||||||
import { use, useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import "leaflet/dist/leaflet.css";
|
import "leaflet/dist/leaflet.css";
|
||||||
import { Circle, MapContainer, TileLayer, useMap } from "react-leaflet";
|
import { Circle, MapContainer, TileLayer, useMap } from "react-leaflet";
|
||||||
import { useMapCircleDraw } from "@/hook/mapDrawing";
|
import { useMapCircleDraw } from "@/hook/mapDrawing";
|
||||||
@@ -18,7 +18,7 @@ function MapPan(props) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function MapEventListener({onClick, onMouseMove}) {
|
function MapEventListener({ onClick, onMouseMove }) {
|
||||||
const map = useMap();
|
const map = useMap();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
map.on('click', onClick);
|
map.on('click', onClick);
|
||||||
@@ -36,16 +36,16 @@ function MapEventListener({onClick, onMouseMove}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_ZOOM = 17;
|
const DEFAULT_ZOOM = 17;
|
||||||
export function CircularAreaPicker({area, setArea, ...props}) {
|
export function CircularAreaPicker({ area, setArea, ...props }) {
|
||||||
const location = useLocation(Infinity);
|
const location = useLocation(Infinity);
|
||||||
const {handleClick, handleMouseMove, center, radius} = useMapCircleDraw(area, setArea);
|
const { handleClick, handleMouseMove, center, radius } = useMapCircleDraw(area, setArea);
|
||||||
return (
|
return (
|
||||||
<MapContainer {...props} className='min-h-full w-full ' center={[0, 0]} zoom={0} scrollWheelZoom={true}>
|
<MapContainer {...props} className='min-h-full w-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"
|
||||||
/>
|
/>
|
||||||
{center && radius && <Circle center={center} radius={radius} fillColor="blue"/>}
|
{center && radius && <Circle center={center} radius={radius} fillColor="blue" />}
|
||||||
<MapPan center={location} zoom={DEFAULT_ZOOM} />
|
<MapPan center={location} zoom={DEFAULT_ZOOM} />
|
||||||
<MapEventListener onClick={handleClick} onMouseMove={handleMouseMove} />
|
<MapEventListener onClick={handleClick} onMouseMove={handleMouseMove} />
|
||||||
</MapContainer>)
|
</MapContainer>)
|
||||||
@@ -54,10 +54,10 @@ export const EditMode = {
|
|||||||
MIN: 0,
|
MIN: 0,
|
||||||
MAX: 1
|
MAX: 1
|
||||||
}
|
}
|
||||||
export function ZonePicker({minArea, setMinArea, maxArea, setMaxArea,editMode, ...props}) {
|
export function ZonePicker({ minZone, setMinZone, maxZone, setMaxZone, editMode, ...props }) {
|
||||||
const location = useLocation(Infinity);
|
const location = useLocation(Infinity);
|
||||||
const {handleClick: maxClick, handleMouseMove: maxHover, center: maxCenter, radius: maxRadius} = useMapCircleDraw(minArea, setMinArea);
|
const { handleClick: maxClick, handleMouseMove: maxHover, center: maxCenter, radius: maxRadius } = useMapCircleDraw(minZone, setMinZone);
|
||||||
const {handleClick: minClick, handleMouseMove: minHover, center: minCenter, radius: minRadius} = useMapCircleDraw(maxArea, setMaxArea);
|
const { handleClick: minClick, handleMouseMove: minHover, center: minCenter, radius: minRadius } = useMapCircleDraw(maxZone, setMaxZone);
|
||||||
function handleClick(e) {
|
function handleClick(e) {
|
||||||
if (editMode == EditMode.MAX) {
|
if (editMode == EditMode.MAX) {
|
||||||
maxClick(e);
|
maxClick(e);
|
||||||
@@ -78,9 +78,10 @@ export function ZonePicker({minArea, setMinArea, maxArea, setMaxArea,editMode, .
|
|||||||
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"
|
||||||
/>
|
/>
|
||||||
{minCenter && minRadius && <Circle center={minCenter} radius={minRadius} color="blue" fillColor="blue"/>}
|
{minCenter && minRadius && <Circle center={minCenter} radius={minRadius} color="blue" fillColor="blue" />}
|
||||||
{maxCenter && maxRadius && <Circle center={maxCenter} radius={maxRadius} color="red" fillColor="red"/>}
|
{maxCenter && maxRadius && <Circle center={maxCenter} radius={maxRadius} color="red" fillColor="red" />}
|
||||||
<MapPan center={location} zoom={DEFAULT_ZOOM} />
|
<MapPan center={location} zoom={DEFAULT_ZOOM} />
|
||||||
<MapEventListener onClick={handleClick} onMouseMove={handleMouseMove} />
|
<MapEventListener onClick={handleClick} onMouseMove={handleMouseMove} />
|
||||||
</MapContainer>)
|
</MapContainer>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,51 @@
|
|||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import BlueButton, { RedButton } from "../util/button";
|
import BlueButton, { GreenButton, RedButton } from "../util/button";
|
||||||
import { EditMode, ZonePicker } from "./mapPicker";
|
import { EditMode, ZonePicker } from "./mapPicker";
|
||||||
|
import TextInput from "../util/textInput";
|
||||||
|
import useAdmin from "@/hook/useAdmin";
|
||||||
|
|
||||||
export function ZoneSelector() {
|
export function ZoneSelector() {
|
||||||
const [editMode, setEditMode] = useState(EditMode.MIN);
|
const [editMode, setEditMode] = useState(EditMode.MIN);
|
||||||
const [minZone, setMinZone] = useState(null);
|
const [minZone, setMinZone] = useState(null);
|
||||||
const [maxZone, setMaxZone] = useState(null);
|
const [maxZone, setMaxZone] = useState(null);
|
||||||
|
const [reductionCount, setReductionCount] = useState("");
|
||||||
|
const [reductionDuration, setReductionDuration] = useState("");
|
||||||
|
const [reductionInterval, setReductionInterval] = useState("");
|
||||||
|
const {zoneSettings, changeZoneSettings} = useAdmin();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (zoneSettings) {
|
||||||
|
setMinZone(zoneSettings.min);
|
||||||
|
setMaxZone(zoneSettings.max);
|
||||||
|
setReductionCount(zoneSettings.reductionCount.toString());
|
||||||
|
setReductionDuration(zoneSettings.reductionDuration.toString());
|
||||||
|
setReductionInterval(zoneSettings.reductionInterval.toString());
|
||||||
|
}
|
||||||
|
}, [zoneSettings]);
|
||||||
|
|
||||||
|
function handleSettingsSubmit() {
|
||||||
|
changeZoneSettings({min:minZone, max:maxZone, reductionCount: Number(reductionCount), reductionDuration: Number(reductionDuration), reductionInterval: Number(reductionInterval)});
|
||||||
|
}
|
||||||
|
|
||||||
return <div className='w-2/5 h-full gap-1 bg-gray-200 p-10 flex flex-col text-center shadow-2xl overflow-y-scroll'>
|
return <div className='w-2/5 h-full gap-1 bg-gray-200 p-10 flex flex-col text-center shadow-2xl overflow-y-scroll'>
|
||||||
<h2 className="text-2xl">Teams ready status</h2>
|
<h2 className="text-2xl">Edit zones</h2>
|
||||||
{editMode == EditMode.MIN && <RedButton onClick={() => setEditMode(EditMode.MAX)}>Edit end zone</RedButton>}
|
{editMode == EditMode.MIN && <RedButton onClick={() => setEditMode(EditMode.MAX)}>Edit end zone</RedButton>}
|
||||||
{editMode == EditMode.MAX && <BlueButton onClick={() => setEditMode(EditMode.MIN)}>Edit start zone</BlueButton>}
|
{editMode == EditMode.MAX && <BlueButton onClick={() => setEditMode(EditMode.MIN)}>Edit start zone</BlueButton>}
|
||||||
<div className='h-96'>
|
<div className='h-96'>
|
||||||
<ZonePicker minZone={minZone} maxZone={maxZone} editMode={editMode} setMinZone={setMinZone} setMaxZone={setMaxZone} />
|
<ZonePicker minZone={minZone} maxZone={maxZone} editMode={editMode} setMinZone={setMinZone} setMaxZone={setMaxZone} />
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<p>Number of reductions</p>
|
||||||
|
<TextInput value={reductionCount} onChange={(e) => setReductionCount(e.target.value)}></TextInput>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p>Duration of each reduction</p>
|
||||||
|
<TextInput value={reductionDuration} onChange={(e) => setReductionDuration(e.target.value)}></TextInput>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p>Interval between reductions</p>
|
||||||
|
<TextInput value={reductionInterval} onChange={(e) => setReductionInterval(e.target.value)}></TextInput>
|
||||||
|
</div>
|
||||||
|
<GreenButton onClick={handleSettingsSubmit}>Save</GreenButton>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
@@ -7,11 +7,11 @@ import { GameState } from "@/util/gameState";
|
|||||||
|
|
||||||
const adminContext = createContext();
|
const adminContext = createContext();
|
||||||
|
|
||||||
function AdminProvider({children}) {
|
function AdminProvider({ children }) {
|
||||||
const [teams, setTeams] = useState([]);
|
const [teams, setTeams] = useState([]);
|
||||||
const [zone, setZone] = useState(null)
|
const [zoneSettings, setZoneSettings] = useState(null)
|
||||||
const { adminSocket } = useSocket();
|
const { adminSocket } = useSocket();
|
||||||
const {loggedIn} = useAdminConnexion();
|
const { loggedIn } = useAdminConnexion();
|
||||||
const [gameState, setGameState] = useState(GameState.SETUP);
|
const [gameState, setGameState] = useState(GameState.SETUP);
|
||||||
|
|
||||||
useSocketListener(adminSocket, "game_state", setGameState);
|
useSocketListener(adminSocket, "game_state", setGameState);
|
||||||
@@ -22,9 +22,9 @@ function AdminProvider({children}) {
|
|||||||
|
|
||||||
//Bind listeners to update the team list and the game status on socket message
|
//Bind listeners to update the team list and the game status on socket message
|
||||||
useSocketListener(adminSocket, "teams", setTeams);
|
useSocketListener(adminSocket, "teams", setTeams);
|
||||||
useSocketListener(adminSocket, "zone", setZone);
|
useSocketListener(adminSocket, "zone_settings", setZoneSettings);
|
||||||
|
|
||||||
const value = useMemo(() => ({teams, zone, setZone, setTeams, gameState}), [zone,teams, gameState]);
|
const value = useMemo(() => ({ teams, zoneSettings, setZoneSettings, setTeams, gameState }), [zoneSettings, teams, gameState]);
|
||||||
return (
|
return (
|
||||||
<adminContext.Provider value={value}>
|
<adminContext.Provider value={value}>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useLocation } from "./useLocation";
|
|
||||||
|
|
||||||
export function useMapCircleDraw(area, setArea) {
|
export function useMapCircleDraw(area, setArea) {
|
||||||
const [drawing, setDrawing] = useState(false);
|
const [drawing, setDrawing] = useState(false);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useAdminContext } from "@/context/adminContext";
|
|||||||
import { useSocket } from "@/context/socketContext";
|
import { useSocket } from "@/context/socketContext";
|
||||||
|
|
||||||
export default function useAdmin(){
|
export default function useAdmin(){
|
||||||
const {teams, gameState, zone } = useAdminContext();
|
const {teams, gameState, zoneSettings } = useAdminContext();
|
||||||
const {adminSocket} = useSocket();
|
const {adminSocket} = useSocket();
|
||||||
|
|
||||||
function pollTeams() {
|
function pollTeams() {
|
||||||
@@ -38,10 +38,10 @@ export default function useAdmin(){
|
|||||||
adminSocket.emit("change_state", state);
|
adminSocket.emit("change_state", state);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setZone(zone) {
|
function changeZoneSettings(zone) {
|
||||||
adminSocket.emit("set_zone", zone);
|
adminSocket.emit("set_zone_settings", zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {teams, zone, gameState,setZone, pollTeams, getTeam, getTeamName, reorderTeams, addTeam, removeTeam, changeState, updateTeam };
|
return {teams, zoneSettings, gameState,changeZoneSettings, pollTeams, getTeam, getTeamName, reorderTeams, addTeam, removeTeam, changeState, updateTeam };
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user