import React, { useEffect, useRef, useState } from 'react' import TextInput from '../util/textInput' import BlueButton, { RedButton } from '../util/button'; import useAdmin from '@/hook/useAdmin'; import dynamic from 'next/dynamic'; import { env } from 'next-runtime-env'; import { GameState } from '@/util/gameState'; const CircularAreaPicker = dynamic(() => import('./mapPicker').then((mod) => mod.CircularAreaPicker), { ssr: false }); export default function TeamEdit({ selectedTeamId, setSelectedTeamId }) { const teamImage = useRef(null); const [avgSpeed, setAvgSpeed] = useState(0); // Speed in m/s const [newTeamName, setNewTeamName] = React.useState(''); const { updateTeam, getTeamName, removeTeam, getTeam, teams, gameState, startDate } = useAdmin(); const [team, setTeam] = useState({}); const NEXT_PUBLIC_SOCKET_HOST = env("NEXT_PUBLIC_SOCKET_HOST"); var protocol = "https://"; if (NEXT_PUBLIC_SOCKET_HOST == "localhost") { protocol = "http://"; } const SERVER_URL = protocol + NEXT_PUBLIC_SOCKET_HOST + "/back"; console.log(SERVER_URL); useEffect(() => { let team = getTeam(selectedTeamId); if (team != undefined) { setNewTeamName(team.name); setTeam(team); } teamImage.current.src = SERVER_URL + "/photo/my?team=" + selectedTeamId + "&t=" + new Date().getTime(); }, [selectedTeamId, teams]) // Update the average speed useEffect(() => { const time = Math.floor((team.finishDate ? team.finishDate - startDate : Date.now() - startDate) / 1000); setAvgSpeed(team.distance/time); }, [team.distance, team.finishDate]); function handleRename(e) { e.preventDefault(); updateTeam(team.id, { name: newTeamName }); } function handleRemove() { removeTeam(team.id); setSelectedTeamId(null); } function handleAddPenalty(increment) { let newPenalties = team.penalties + increment; newPenalties = Math.max(0, newPenalties); newPenalties = Math.min(3, newPenalties); updateTeam(team.id, { penalties: newPenalties }); } function formatTimeHours(time) { // time is in seconds if (!Number.isInteger(time)) return "Inconnue"; if (time < 0) time = 0; const hours = Math.floor(time / 3600); const minutes = Math.floor(time / 60); const seconds = Math.floor(time % 60); return String(hours).padStart(2,"0") + ":" + String(minutes).padStart(2,"0") + ":" + String(seconds).padStart(2,"0"); } return (team &&
Starting zone
Secret : {String(team.id).padStart(6, '0').replace(/(\d{3})(\d{3})/, '$1 $2')}
Capture code : {String(team.captureCode).padStart(4, '0')}
Chasing : {getTeamName(team.chasing)}
Chased by : {getTeamName(team.chased)}
Distance: { (team.distance / 1000).toFixed(1) }km
Time : {formatTimeHours(Math.floor((team.finishDate ? team.finishDate - startDate : Date.now() - startDate) / 1000))}
Average speed : {(avgSpeed*3.6).toFixed(1)}km/h
Captures : {team.nCaptures}
Sent location : {team.nSentLocation}
Oberved : {team.nObserved}
Phone model : {team.phoneModel ?? "Unknown"}
Phone name : {team.phoneName ?? "Unknown"}
Battery: {team.battery ? team.battery + "%" : "Unknown"}
Penalties :
{team.penalties}