Kopie van https://gitlab.com/studieverenigingvia/ict/centurion met een paar aanpassingen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.6 KiB

4 years ago
import React, {useEffect, useRef, useState} from 'react';
4 years ago
import {Card, Col, InputNumber, Row} from "antd"
4 years ago
import {emit} from "../util/socket";
4 years ago
import "../css/lobby.sass";
4 years ago
import beer from "../img/beer.png"
import {NumberParam, useQueryParam} from 'use-query-params';
4 years ago
import {useSocket} from "use-socketio/lib";
4 years ago
4 years ago
const Lobby = (props: any) => {
const [lobbyId, setLobbyId] = useQueryParam('lobby', NumberParam);
4 years ago
const [isLeader, setIsLeader] = useState(false);
4 years ago
const [isRunning, setIsRunning] = useState(false);
const [seekTime, setSeekTime] = useState(0);
4 years ago
const [userCount, setUserCount] = useState(0);
4 years ago
4 years ago
const {socket} = useSocket("welcome", async (obj: any) => {
if (lobbyId) {
// lobbyId is already defined, this means we have a queryparam set.
await onChangeLobbyInput(lobbyId);
return;
}
console.log("Got welcome", lobbyId);
setLobbyId(obj.lobby.name);
setIsLeader(socket.id === obj.lobby.leaderId);
setUserCount(obj.lobby.users?.length || 0);
});
const socketRef = useRef<SocketIOClient.Socket>(socket);
4 years ago
async function onChangeLobbyInput(i: any) {
setLobbyId(i);
4 years ago
4 years ago
const result: any = await emit(socket,'join_lobby', i);
4 years ago
setIsLeader(socket.id === result.lobby.leaderId);
setUserCount(result.lobby.users?.length || 0);
4 years ago
setIsRunning(result.lobby.running);
4 years ago
}
useEffect(() => {
async function wrapper() {
4 years ago
const result: any = await emit(socketRef.current,'lobby_info');
setIsLeader(socketRef.current.id === result.lobby.leaderId);
4 years ago
setUserCount(result.lobby.users?.length || 0);
4 years ago
setIsRunning(result.lobby.running);
console.log("Got lobby_info");
4 years ago
}
wrapper();
}, []);
const seekItem = (
<span>
Start bij: <InputNumber size="small" min={0} max={60000} value={seekTime}
onChange={time => setSeekTime(time || 0)}/>
sec.
</span>
);
4 years ago
return (
4 years ago
<div className="lobby">
<Row>
<Col span={24}>
4 years ago
<h1 id="centurion-title">
<img src={beer} className="beer" alt="beer"/>Centurion!
<img src={beer} className="beer" alt="beer"/></h1>
4 years ago
</Col>
</Row>
<Row>
<Col className="hints" span={24}>
<div>Honderd minuten...</div>
<div>Honderd shots...</div>
<div>Kun jij het aan?</div>
</Col>
</Row>
<Row>
<Col className="control" span={8} offset={8}>
<Card title={`Lobby setup (${lobbyId}):`} actions={isLeader ? [
<span onClick={async () => {
4 years ago
await emit(socket,'request_start', seekTime)
}}>Start</span>,
4 years ago
] : [<span onClick={props.start}>Join</span>]}>
4 years ago
<span>
Huidige lobby <InputNumber size="small" min={100} max={100000} value={lobbyId}
onChange={onChangeLobbyInput}/>
4 years ago
</span>
<p>Er zijn {userCount} gebruiker(s) aanwezig.</p>
<p> {isLeader ? 'Jij bent de baas.' : 'Wachten op de baas...'} </p>
{isLeader ? seekItem : ''}
4 years ago
</Card>
4 years ago
4 years ago
</Col>
</Row>
</div>
4 years ago
);
};
export default Lobby;