From a15fadf4f9b82f4f88a82d2e0eca12bdbabfd61c Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Mon, 14 Mar 2022 10:52:39 -0700 Subject: [PATCH] Ice server info Signed-off-by: James Ketrenos --- client/src/App.css | 1 + client/src/App.js | 6 ++++-- client/src/Board.js | 7 +++++-- client/src/Placard.js | 5 +++++ server/routes/games.js | 40 +++++++++++++++++++++++++++++++++++----- 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/client/src/App.css b/client/src/App.css index 6b09670..06bc91e 100755 --- a/client/src/App.css +++ b/client/src/App.css @@ -1,5 +1,6 @@ body { font-family: 'Droid Sans', 'Arial Narrow', Arial, sans-serif; + overflow: hidden; } #root { diff --git a/client/src/App.js b/client/src/App.js index 42886f3..7c18a05 100755 --- a/client/src/App.js +++ b/client/src/App.js @@ -220,15 +220,17 @@ const Table = () => { `Try refreshing your browser in a few seconds.`; console.error(error); setError(error); - throw error; + throw new Error(error); } return res.json(); }).then((update) => { if (update.id !== gameId) { - console.log(`New game started: ${update.id}`); + console.log(`Game available: ${update.id}`); history.push(`${gamesPath}/${update.id}`); setGameId(update.id); } + }).catch((error) => { + console.error(error); }); }, [ gameId, setGameId ]); diff --git a/client/src/Board.js b/client/src/Board.js index 2e766a9..7497646 100644 --- a/client/src/Board.js +++ b/client/src/Board.js @@ -213,11 +213,14 @@ const Board = () => { >
; }; - const sendPlacement = useCallback((type, index) => { + const staticSendCallback = (type, index) => { ws.send(JSON.stringify({ type, index })); - }, [ws]); + }; + const refStaticSendCallback = useRef(staticSendCallback); + useEffect(() => { refStaticSendCallback.current = staticSendCallback; }); + const sendPlacement = refStaticSendCallback.current; const onRoadClicked = useCallback((road) => { console.log(`Road clicked: ${road.index}`); diff --git a/client/src/Placard.js b/client/src/Placard.js index 34930be..1c53c89 100644 --- a/client/src/Placard.js +++ b/client/src/Placard.js @@ -43,6 +43,11 @@ const Placard = ({type, disabled, count, buildActive, setBuildActive}) => { setBuildActive(false); }; + + if (!type) { + return <>; + } + if (type === 'B') { type = 'blue'; } else if (type === 'O') { type = 'orange'; } else if (type === 'R') { type = 'red'; } diff --git a/server/routes/games.js b/server/routes/games.js index eae46dc..3af2215 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -891,7 +891,7 @@ const colorToWord = (color) => { const getActiveCount = (game) => { let active = 0; for (let color in game.players) { - if (game.players[color].name) { + if (!game.players[color].name) { continue; } active++; @@ -1000,7 +1000,7 @@ const setPlayerColor = (game, session, color) => { name: session.name, color: session.color, live: session.live, - private: session.player + private: session.player, }); sendUpdateToPlayers(game, update); }; @@ -2858,6 +2858,10 @@ const setGameState = (game, session, state) => { if (game.state !== 'lobby') { return `You can only start the game from the lobby.`; } + const active = getActiveCount(game); + if (active < 2) { + return `You need at least two players to start the game.`; + } addChatMessage(game, null, `${session.name} requested to start the game.`); game.state = state; @@ -3271,7 +3275,7 @@ const gotoLobby = (game, session) => { router.ws("/ws/:id", async (ws, req) => { if (!req.cookies || !req.cookies.player) { - ws.send({ type: 'error', error: `Unable to set session cookie` }); + ws.send({ type: 'error', error: `Unable to find session cookie` }); return; } @@ -3832,6 +3836,7 @@ const resetGame = (game) => { largestArmy: '', largestArmySize: 0, winner: undefined, + active: 0 }); /* Populate the game corner and road placement data as cleared */ @@ -3888,6 +3893,7 @@ const resetGame = (game) => { for (let key in game.sessions) { const session = game.sessions[key]; if (session.color) { + game.active++; session.player = game.players[session.color]; session.player.status = 'Active'; session.player.lastActive = Date.now(); @@ -3910,7 +3916,7 @@ const createGame = (id) => { break; } } - console.log(`${info} creating ${id}`); + console.log(`${info}: creating ${id}`); const game = { id: id, @@ -3922,7 +3928,8 @@ const createGame = (id) => { W: newPlayer('W') }, sessions: {}, - unselected: [] + unselected: [], + active: 0 }; [ "pips", "borders", "tiles" ].forEach((field) => { @@ -4036,4 +4043,27 @@ router.get("/", (req, res/*, next*/) => { return res.status(200).send({ player: playerId }); }); +router.post("/:id?", async (req, res/*, next*/) => { + const { id } = req.params; + + let playerId; + if (!req.cookies.player) { + playerId = crypto.randomBytes(16).toString('hex'); + res.cookie('player', playerId); + } else { + playerId = req.cookies.player; + } + + if (id) { + console.log(`[${playerId.substring(0,8)}]: Attempting load of ${id}`); + } else { + console.log(`[${playerId.substring(0,8)}]: Creating new game.`); + } + const game = await loadGame(id); /* will create game if it doesn't exist */ + console.log(`[${playerId.substring(0,8)}]: ${game.id} loaded.`); + + return res.status(200).send({ id: game.id }); +}); + + module.exports = router;