diff --git a/components/buzzer.js b/components/buzzer.js index f831462..5877528 100644 --- a/components/buzzer.js +++ b/components/buzzer.js @@ -37,6 +37,7 @@ export default function Buzzer(props) { }; useEffect(() => { + cookieCutter.set("session", `${props.room}:${props.id}:0`); setInterval(() => { if (ws.current.readyState !== 1) { setError( @@ -52,10 +53,6 @@ export default function Buzzer(props) { } }, 1000); - if (props.id !== null && props.team !== null) { - setBuzzerReg(props.id); - } - ws.current.addEventListener("message", (evt) => { let received_msg = evt.data; let json = JSON.parse(received_msg); @@ -75,6 +72,7 @@ export default function Buzzer(props) { } else if (json.action === "quit") { props.setGame(null); props.setTeam(null); + location.reload(); } else if (json.action === "set_timer") { setTimer(json.data); } else if (json.action === "stop_timer") { @@ -251,7 +249,6 @@ export default function Buzzer(props) { +
+ + + +
{error != null && error !== "" ?

👾 {error}

: null} )} diff --git a/pages/api/ws.js b/pages/api/ws.js index 99aa752..c47630b 100644 --- a/pages/api/ws.js +++ b/pages/api/ws.js @@ -408,9 +408,24 @@ const ioHandler = (req, res) => { JSON.stringify({ action: "data", data: game }), ); } catch (e) { - console.error("Problem in register ", e); + console.error("Problem in buzzer register ", e); } pingInterval(rooms[message.room], id, ws); + } else if (message.action === "registerspectator") { + let id = message.id; + let game = rooms[message.room].game; + try { + console.debug("spectator ready: ", id); + // get inital latency, client pongs on registered + ws.send(JSON.stringify({ action: "ping", id: id })); + ws.send(JSON.stringify({ action: "registered", id: id })); + wss.broadcast( + message.room, + JSON.stringify({ action: "data", data: game }), + ); + } catch (e) { + console.error("Problem in spectator register ", e); + } } else if (message.action === "pong") { let game = rooms[message.room].game; let player = game.registeredPlayers[message.id]; diff --git a/pages/game.js b/pages/game.js index 5ec0333..700f358 100644 --- a/pages/game.js +++ b/pages/game.js @@ -16,6 +16,7 @@ export default function Game(props) { const [timer, setTimer] = useState(0); const [error, setErrorVal] = useState(""); const [showMistake, setShowMistake] = useState(false); + const [isHost, setIsHost] = useState(false); const ws = useRef(null); let refreshCounter = 0; @@ -67,6 +68,11 @@ export default function Game(props) { })}`; } setGame(json.data); + let session = cookieCutter.get("session"); + let [_, id] = session.split(":"); + if (json.data?.registeredPlayers[id] == "host") { + setIsHost(true); + } } else if ( json.action === "mistake" || json.action === "show_mistake" @@ -75,7 +81,7 @@ export default function Game(props) { audio.play(); setShowMistake(true); setTimeout(() => { - setShowMistake(false) + setShowMistake(false); }, 2000); } else if (json.action === "quit") { setGame({}); @@ -144,14 +150,14 @@ export default function Game(props) { } else if (game.is_final_round) { gameSession = (
-
+
); } else { gameSession = ( -
+
@@ -167,8 +173,24 @@ export default function Game(props) { } return ( <> + {!isHost ? ( +
+ +
+ ) : null}
- +