servicepoint-tanks/tank-frontend/src/PlayerInfo.tsx

45 lines
1.2 KiB
TypeScript
Raw Normal View History

import {useEffect, useState} from 'react';
2024-04-13 17:56:33 +02:00
import './PlayerInfo.css'
import {PlayerResponse, getPlayer} from './serverCalls';
2024-04-13 23:07:08 +02:00
import {Guid} from "./Guid.ts";
import Column from "./components/Column.tsx";
2024-04-13 17:56:33 +02:00
2024-04-13 23:53:09 +02:00
export default function PlayerInfo({playerId, logout}: {
2024-04-13 23:07:08 +02:00
playerId: Guid,
2024-04-13 23:53:09 +02:00
logout: () => void
2024-04-13 23:07:08 +02:00
}) {
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'>
2024-04-13 23:53:09 +02:00
<h3>
2024-04-14 00:49:39 +02:00
{player ? `Playing as ${player?.name}` : 'loading...'}
2024-04-13 23:53:09 +02:00
</h3>
2024-04-14 00:49:39 +02:00
<table>
<tbody>
<tr>
<td>kills:</td>
<td>{player?.scores.kills}</td>
</tr>
<tr>
<td>deaths:</td>
<td>{player?.scores.deaths}</td>
</tr>
</tbody>
</table>
2024-04-13 23:07:08 +02:00
</Column>;
2024-04-13 17:56:33 +02:00
}