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.
centurion/frontend/src/components/Centurion.tsx

82 lines
2.0 KiB

5 years ago
import React, {useEffect, useRef, useState} from "react";
import {Row} from "antd";
import {useSocket} from "use-socketio";
import {NumberParam, useQueryParam} from "use-query-params";
import {Tick} from "../types/types";
import NextShot from "./NextShot";
import Feed from "./Feed";
import ShotsTaken from "./ShotsTaken";
import Lobby from "./Lobby";
import logo from "../img/via-logo.svg";
const Centurion = () => {
5 years ago
const [started, setStarted] = useState(false);
const [wantsToStart, setWantsToStart] = useState(false);
const player = useRef(new Audio("centurion.m4a"));
const [noSound] = useQueryParam('noSound', NumberParam);
function goStart() {
console.log("Starting from wrapper..");
setWantsToStart(true);
}
useSocket("started", async (obj: any) => {
5 years ago
console.log("got started");
setStarted(true);
if (typeof noSound === 'undefined') {
await player.current.play();
}
});
useEffect(() => {
if (noSound === 1) {
// Won't play sound so we don't get DOM Interaction errors.
setWantsToStart(true);
}
}, [noSound]);
5 years ago
useSocket("tick_event", async (tick: Tick) => {
if (!started && wantsToStart) {
if (player.current.paused) {
if (typeof noSound === 'undefined') {
await player.current.play();
}
}
setStarted(true);
player.current.currentTime = tick.current;
}
});
const feedContent = (
<Row>
<NextShot/>
<Feed/>
<ShotsTaken/>
</Row>
);
const lobbyContent = (
<Lobby start={goStart}/>
);
const content = started ? feedContent : lobbyContent;
return (
<>
<section className="content">
{content}
</section>
<footer>
<img src={logo} className="via-logo" alt="logo"/>
</footer>
</>
);
};
export default Centurion;