2024-04-13 19:50:37 +02:00
|
|
|
import {useEffect, useState} from 'react';
|
2024-04-13 17:56:33 +02:00
|
|
|
import './PlayerInfo.css'
|
2024-04-13 19:50:37 +02:00
|
|
|
import {PlayerResponse, getPlayer} from './serverCalls';
|
2024-04-13 23:07:08 +02:00
|
|
|
import {Guid} from "./Guid.ts";
|
|
|
|
import Column from "./components/Column.tsx";
|
|
|
|
import Row from "./components/Row.tsx";
|
|
|
|
import Button from "./components/Button.tsx";
|
2024-04-13 17:56:33 +02:00
|
|
|
|
2024-04-13 23:07:08 +02:00
|
|
|
export default function PlayerInfo({playerId, logout, reset}: {
|
|
|
|
playerId: Guid,
|
|
|
|
logout: () => void,
|
|
|
|
reset: () => void
|
|
|
|
}) {
|
2024-04-13 17:56:33 +02:00
|
|
|
const [player, setPlayer] = useState<PlayerResponse | null>();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-04-13 23:07:08 +02:00
|
|
|
const refresh = () => {
|
|
|
|
getPlayer(playerId).then(value => {
|
|
|
|
if (value)
|
|
|
|
setPlayer(value);
|
|
|
|
else
|
|
|
|
logout();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-04-13 17:56:33 +02:00
|
|
|
const timer = setInterval(refresh, 5000);
|
|
|
|
return () => clearInterval(timer);
|
2024-04-13 23:07:08 +02:00
|
|
|
}, [playerId]);
|
2024-04-13 17:56:33 +02:00
|
|
|
|
2024-04-13 23:07:08 +02:00
|
|
|
return <Column className='PlayerInfo'>
|
|
|
|
<Row>
|
|
|
|
<h3 className='grow'>
|
|
|
|
{player ? `Playing as "${player?.name}"` : 'loading...'}
|
|
|
|
</h3>
|
|
|
|
<Button className='PlayerInfo-Reset' onClick={() => reset()} text='x'/>
|
|
|
|
</Row>
|
|
|
|
<Row>
|
|
|
|
<p className='Elems'> kills: </p>
|
|
|
|
<p className='Elems'>{player?.scores.kills}</p>
|
|
|
|
</Row>
|
|
|
|
<Row>
|
|
|
|
<p className='Elems'>deaths: </p>
|
|
|
|
<p className='Elems'>{player?.scores.deaths}</p>
|
|
|
|
</Row>
|
|
|
|
</Column>;
|
2024-04-13 17:56:33 +02:00
|
|
|
}
|