From f6884d9b9631928d19b041d2f33e460e7c0d5b6b Mon Sep 17 00:00:00 2001 From: Joshua Cold Date: Sun, 13 Jun 2021 11:32:23 -0600 Subject: [PATCH] team viewer/kicker component - can quit player from admin - don't clear room code on main page --- components/Admin/players.js | 67 +++++++++++++++++++++++++++++++++ components/admin.js | 75 +++++++++++++++++++++++-------------- pages/index.js | 1 - server.js | 8 ++++ 4 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 components/Admin/players.js diff --git a/components/Admin/players.js b/components/Admin/players.js new file mode 100644 index 00000000..7d9d95bd --- /dev/null +++ b/components/Admin/players.js @@ -0,0 +1,67 @@ +import { useTranslation } from "react-i18next"; +import '../../i18n/i18n' +import "tailwindcss/tailwind.css"; +import { useState, useEffect, useRef } from 'react'; + +export default function Players(props){ + let game = props.game + const { i18n, t } = useTranslation(); + let team1 = [] + let team2 = [] + + if(game.registeredPlayers){ + for (const [id, player] of Object.entries(game.registeredPlayers)) { + if(player.hasOwnProperty("team")){ + let content = {id: id, name: player.name} + player.team === 0? team1.push(content) : team2.push(content) + } + } + } + + function teamSection(team){ + return( +
+ {team.map((x,i) => +
+ {/* information about player */} +
+

{x.name}

+
+ +
+ )} + +
+ ) + } + + return( +
+
+ {/* team 1 section */} +
+

{ game.teams[0].name }

+
+ {teamSection(team1)} +
+ {/* seperator */} +
+ {/* team 2 section */} +
+

{ game.teams[1].name }

+
+ {teamSection(team2)} +
+
+
+ ) +} diff --git a/components/admin.js b/components/admin.js index 6b0297c7..7c0effe7 100644 --- a/components/admin.js +++ b/components/admin.js @@ -2,6 +2,7 @@ import { useState, useEffect, useRef } from 'react'; import "tailwindcss/tailwind.css"; import { useTranslation } from "react-i18next"; import '../i18n/i18n' +import Players from './Admin/players' import LanguageSwitcher from "./language" export default function Admin(props){ @@ -99,13 +100,13 @@ export default function Admin(props){
-
- - - -
+
+ + + +
+

{t("buzzerHelpText")}

+ + : +
+ {/* disabled clear buzzers button */} + +

{t("buzzerHelpText")}

+
+ } + +
+
+ {game.buzzed.map((x,i) => +
+

{t("number", {count: i+1})}. {game.registeredPlayers[x.id].name}

+

{t("team")}: {game.teams[game.registeredPlayers[x.id].team].name}

+

{t("time")}: {(((x.time - game.tick)/1000) % 60).toFixed(2)} {t("seconds")}

+
+ )} -
+
+ - {game.buzzed.length > 0? -
- -

{t("buzzerHelpText")}

-
:null - } +

{t("gameBoard")}

diff --git a/pages/index.js b/pages/index.js index 990b48dd..573a345e 100644 --- a/pages/index.js +++ b/pages/index.js @@ -61,7 +61,6 @@ export default function Home(){ else if (json.action === "quit"){ console.debug("player quit") setPlayerID(null) - setRoomCode("") setRegisteredRoomCode(null) setGame({}) setHost(false) diff --git a/server.js b/server.js index 52500b2b..0258806d 100644 --- a/server.js +++ b/server.js @@ -223,6 +223,12 @@ wss.on('connection', function connection(ws, req) { clearInterval(entry) } } + // clear ws from server + if(rooms[message.room].connections){ + for (const [id, ws_entry] of Object.entries(rooms[message.room].connections)) { + ws_entry.close() + } + } delete rooms[message.room] }else{ let interval = rooms[message.room].intervals[message.id] @@ -230,12 +236,14 @@ wss.on('connection', function connection(ws, req) { if(interval){ clearInterval(interval) } + let ws = rooms[message.room].connections[message.id] ws.send(JSON.stringify({ action: "quit" })) rooms[message.room].game.buzzed.forEach((b, index) => { if(b.id === message.id){ rooms[message.room].game.buzzed.splice(index, 1) } }) + ws.close() delete rooms[message.room].game.registeredPlayers[message.id] delete rooms[message.room].connections[message.id] wss.broadcast(message.room,JSON.stringify(