From 4ea6f529dd68b01788673ce0cdee26342f22779d Mon Sep 17 00:00:00 2001 From: h0vscat Date: Thu, 18 Jun 2020 10:12:13 -0400 Subject: [PATCH 1/9] add env --- server/socket_io/GameIO.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/server/socket_io/GameIO.js b/server/socket_io/GameIO.js index 9aefd45..71cb0d8 100644 --- a/server/socket_io/GameIO.js +++ b/server/socket_io/GameIO.js @@ -44,10 +44,11 @@ class GameIO { } }); - // setInterval(() => { - // countdown--; - // this.gameIO.to(matchId).emit("timer", { countdown: countdown }); - // }, 1000); + setInterval(() => { + console.log("Timer started"); + // countdown--; + // this.gameIO.to(matchId).emit("timer", { countdown: countdown }); + }, 1000); socket.on("end turn", (matchId) => { const match = MatchManager.getMatch(matchId); From 1fa57f15106f231a578ea4ba3ee3009213fa981b Mon Sep 17 00:00:00 2001 From: h0vscat Date: Thu, 18 Jun 2020 21:22:11 -0400 Subject: [PATCH 2/9] add timer --- client/src/pages/game/GamePrompt.js | 16 +++++++++++++--- server/engine/Game.js | 8 ++++---- server/socket_io/GameIO.js | 29 +++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/client/src/pages/game/GamePrompt.js b/client/src/pages/game/GamePrompt.js index c283967..7470c2f 100644 --- a/client/src/pages/game/GamePrompt.js +++ b/client/src/pages/game/GamePrompt.js @@ -1,10 +1,20 @@ -import React from "react"; +import React, { useState, useEffect, useContext } from "react"; import { Box, Container, Grid, Typography } from "@material-ui/core"; import TimerIcon from "@material-ui/icons/Timer"; - +import { AppContext } from "../../App"; import { TEAM_ROLE } from "../game_lobby/team_select/TeamPresets"; function GamePrompt({ gameState, player }) { + const { gameIO } = useContext(AppContext); + + const [timer, setTimer] = useState(45); + + useEffect(() => { + timer > 0 && setTimeout(() => setTimer(timer - 1), 1000); + gameIO.state.io.on("start timer", () => setTimer(45)); + }); + + return ( @@ -14,7 +24,7 @@ function GamePrompt({ gameState, player }) { - 45s left + {timer}s left diff --git a/server/engine/Game.js b/server/engine/Game.js index 7139975..8488f04 100644 --- a/server/engine/Game.js +++ b/server/engine/Game.js @@ -13,14 +13,14 @@ const TEAM_ROLE = { }; const DEFAULT_RED_TEAM_STATE = [ - { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: null }, - { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, + { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: {id:"id2", name:"player2" }}, + { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: {id:"id1", name:"player1" }}, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, ]; const DEFAULT_BLUE_TEAM_STATE = [ - { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: null }, + { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: null}, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, @@ -41,7 +41,7 @@ class Game { this.redPoints = 0; this.bluePoints = 0; - this.numGuessLeft = 0; + this.numGuessLeft = MAX_NUM_OF_GUESSES; this.maxNumOfGuess = MAX_NUM_OF_GUESSES; this.winner = null; diff --git a/server/socket_io/GameIO.js b/server/socket_io/GameIO.js index 71cb0d8..c785402 100644 --- a/server/socket_io/GameIO.js +++ b/server/socket_io/GameIO.js @@ -8,6 +8,8 @@ class GameIO { constructor() { this.gameIO = null; this.chatIO = null; + + this.timer = null; } connect(io) { @@ -34,6 +36,8 @@ class GameIO { socket.on("game start", (matchId) => { this.gameIO.to(matchId).emit("resolve start game"); + this.gameIO.to(matchId).emit("start timer"); + this.timer = this.startTimerInterval(matchId); }); socket.on("game state onload", (matchId) => { @@ -44,16 +48,13 @@ class GameIO { } }); - setInterval(() => { - console.log("Timer started"); - // countdown--; - // this.gameIO.to(matchId).emit("timer", { countdown: countdown }); - }, 1000); - socket.on("end turn", (matchId) => { + clearInterval(this.timer); const match = MatchManager.getMatch(matchId); match.nextGameTurn(); this.gameIO.to(matchId).emit("game state change", match.getGameState()); + this.gameIO.to(matchId).emit("start timer"); + this.timer = this.startTimerInterval(matchId); }); socket.on("card select", (matchId, data) => { @@ -121,10 +122,26 @@ class GameIO { .to(matchId) .emit("game state change", match.getGameState()); } + this.gameIO.to(matchId).emit("start timer"); }); }); } + startTimerInterval(matchId) { + setInterval(() => { + console.log("timer started"); + const match = MatchManager.getMatch(matchId); + console.log(match.getGameState()); + if (match) { + match.nextGameTurn(); + this.gameIO + .to(matchId) + .emit("game state change", match.getGameState()); + } + this.gameIO.to(matchId).emit("start timer"); + }, 46000); + } + setChatIO(chatIO) { this.chatIO = chatIO; } From fe03e4142db27005568a33367716458681c0b8c6 Mon Sep 17 00:00:00 2001 From: h0vscat Date: Thu, 18 Jun 2020 22:18:45 -0400 Subject: [PATCH 3/9] fix --- client/src/pages/game/GamePrompt.js | 12 ++++++++++-- server/engine/Game.js | 2 +- server/socket_io/GameIO.js | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/client/src/pages/game/GamePrompt.js b/client/src/pages/game/GamePrompt.js index 7470c2f..2f8aee6 100644 --- a/client/src/pages/game/GamePrompt.js +++ b/client/src/pages/game/GamePrompt.js @@ -9,9 +9,17 @@ function GamePrompt({ gameState, player }) { const [timer, setTimer] = useState(45); + let countDown = null; + + gameIO.state.io.on("start timer", () => { + clearTimeout(countDown); + setTimer(45) + }); + useEffect(() => { - timer > 0 && setTimeout(() => setTimer(timer - 1), 1000); - gameIO.state.io.on("start timer", () => setTimer(45)); + if (timer > 0) { + countDown = setTimeout(() => setTimer(timer - 1), 1000); + } }); diff --git a/server/engine/Game.js b/server/engine/Game.js index 8488f04..6456676 100644 --- a/server/engine/Game.js +++ b/server/engine/Game.js @@ -270,7 +270,7 @@ class Game { resetGame() { this.bluePoints = 0; this.redPoints = 0; - this.numGuessLeft = 0; + this.numGuessLeft = MAX_NUM_OF_GUESSES; this.gameBoard.generateNewRound(); this.gameTurn = [GameTurns.BLUE_SPY_TURN, GameTurns.RED_SPY_TURN][ Math.round(Math.random()) diff --git a/server/socket_io/GameIO.js b/server/socket_io/GameIO.js index c785402..f8ccd14 100644 --- a/server/socket_io/GameIO.js +++ b/server/socket_io/GameIO.js @@ -114,6 +114,7 @@ class GameIO { }); socket.on("start new game", (matchId) => { + clearInterval(this.timer); const match = MatchManager.getMatch(matchId); if (match) { @@ -123,6 +124,7 @@ class GameIO { .emit("game state change", match.getGameState()); } this.gameIO.to(matchId).emit("start timer"); + this.timer = this.startTimerInterval(matchId); }); }); } From 7d116eb5507f5090defa761c03e9db94a126b394 Mon Sep 17 00:00:00 2001 From: h0vscat Date: Thu, 18 Jun 2020 22:43:37 -0400 Subject: [PATCH 4/9] fix failed to clear timer --- server/engine/Game.js | 6 +++--- server/socket_io/GameIO.js | 16 ++++++---------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/server/engine/Game.js b/server/engine/Game.js index 6456676..a613eb9 100644 --- a/server/engine/Game.js +++ b/server/engine/Game.js @@ -13,14 +13,14 @@ const TEAM_ROLE = { }; const DEFAULT_RED_TEAM_STATE = [ - { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: {id:"id2", name:"player2" }}, - { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: {id:"id1", name:"player1" }}, + { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: null }, + { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, ]; const DEFAULT_BLUE_TEAM_STATE = [ - { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: null}, + { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, diff --git a/server/socket_io/GameIO.js b/server/socket_io/GameIO.js index f8ccd14..d1d1637 100644 --- a/server/socket_io/GameIO.js +++ b/server/socket_io/GameIO.js @@ -36,7 +36,6 @@ class GameIO { socket.on("game start", (matchId) => { this.gameIO.to(matchId).emit("resolve start game"); - this.gameIO.to(matchId).emit("start timer"); this.timer = this.startTimerInterval(matchId); }); @@ -49,11 +48,10 @@ class GameIO { }); socket.on("end turn", (matchId) => { - clearInterval(this.timer); const match = MatchManager.getMatch(matchId); match.nextGameTurn(); this.gameIO.to(matchId).emit("game state change", match.getGameState()); - this.gameIO.to(matchId).emit("start timer"); + this.timer = this.startTimerInterval(matchId); }); @@ -114,7 +112,6 @@ class GameIO { }); socket.on("start new game", (matchId) => { - clearInterval(this.timer); const match = MatchManager.getMatch(matchId); if (match) { @@ -123,24 +120,23 @@ class GameIO { .to(matchId) .emit("game state change", match.getGameState()); } - this.gameIO.to(matchId).emit("start timer"); this.timer = this.startTimerInterval(matchId); }); }); } startTimerInterval(matchId) { - setInterval(() => { + clearInterval(this.timer); + this.gameIO.to(matchId).emit("start timer"); + return setInterval(() => { console.log("timer started"); const match = MatchManager.getMatch(matchId); console.log(match.getGameState()); if (match) { match.nextGameTurn(); - this.gameIO - .to(matchId) - .emit("game state change", match.getGameState()); + this.gameIO.to(matchId).emit("game state change", match.getGameState()); + this.gameIO.to(matchId).emit("start timer"); } - this.gameIO.to(matchId).emit("start timer"); }, 46000); } From 4fd55f0f34fe53e22008d079c7f6125d96e83b20 Mon Sep 17 00:00:00 2001 From: h0vscat Date: Sat, 20 Jun 2020 21:54:32 -0400 Subject: [PATCH 5/9] del testing code --- client/src/pages/game/GamePrompt.js | 17 +++++++++-------- server/engine/Game.js | 19 ------------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/client/src/pages/game/GamePrompt.js b/client/src/pages/game/GamePrompt.js index 2f8aee6..8c5517a 100644 --- a/client/src/pages/game/GamePrompt.js +++ b/client/src/pages/game/GamePrompt.js @@ -8,17 +8,18 @@ function GamePrompt({ gameState, player }) { const { gameIO } = useContext(AppContext); const [timer, setTimer] = useState(45); - - let countDown = null; - - gameIO.state.io.on("start timer", () => { - clearTimeout(countDown); - setTimer(45) - }); + const [countDown, setCountDown] = useState(null); + + useEffect(() => { + gameIO.state.io.on("start timer", () => { + clearTimeout(countDown); + setTimer(45) + }); + }, []); useEffect(() => { if (timer > 0) { - countDown = setTimeout(() => setTimer(timer - 1), 1000); + setCountDown(setTimeout(() => setTimer(timer - 1), 1000)); } }); diff --git a/server/engine/Game.js b/server/engine/Game.js index a613eb9..216690f 100644 --- a/server/engine/Game.js +++ b/server/engine/Game.js @@ -281,22 +281,3 @@ class Game { module.exports = Game; -// const newGame = new Game("user1"); -// newGame.giveHint(1); -// newGame.nextGameTurn(); -// console.log("============game Turn============\n", newGame.getGameState().gameTurn); -// newGame.vote({ index: 0, player: { name: "user1", team: "Red", id: "id_1" } }); -// newGame.vote({ index: 1, player: { name: "user2", team: "Red", id: "id_2" } }); -// newGame.vote({ index: 1, player: { name: "user1", team: "Red", id: "id_1" } }); -// newGame.vote({ index: 2, player: { name: "user2", team: "Red", id: "id_2" } }); -// newGame.nextGameTurn(); -// console.log( -// "=============cards===========\n", -// newGame.getGameState().gameBoard.getCards()[0], "index 0\n", -// newGame.getGameState().gameBoard.getCards()[1], "index 1\n", -// newGame.getGameState().gameBoard.getCards()[2], "index 2\n" -// ); -// console.log("============points============\n", "red points", newGame.getRedPoints(), "blue points", newGame.getBluePoints()); -// console.log("=============game Turn=========\n", newGame.getGameTurn()); -// newGame.giveHint(3); -// newGame.nextGameTurn(); From a179a982f80023c8aa27e8bfad9ac53ca8678b47 Mon Sep 17 00:00:00 2001 From: h0vscat Date: Sun, 28 Jun 2020 23:13:42 -0400 Subject: [PATCH 6/9] timer to each running instance --- client/src/pages/game/GamePrompt.js | 8 ++--- server/engine/Game.js | 46 +++++++++++++++++++---------- server/socket_io/GameIO.js | 28 ++++-------------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/client/src/pages/game/GamePrompt.js b/client/src/pages/game/GamePrompt.js index 8c5517a..3c257ee 100644 --- a/client/src/pages/game/GamePrompt.js +++ b/client/src/pages/game/GamePrompt.js @@ -6,22 +6,22 @@ import { TEAM_ROLE } from "../game_lobby/team_select/TeamPresets"; function GamePrompt({ gameState, player }) { const { gameIO } = useContext(AppContext); - - const [timer, setTimer] = useState(45); + const [timer, setTimer] = useState(gameState.timeLeft); const [countDown, setCountDown] = useState(null); useEffect(() => { gameIO.state.io.on("start timer", () => { clearTimeout(countDown); - setTimer(45) + setTimer(gameState.timeLeft); }); }, []); useEffect(() => { + if (countDown) clearTimeout(countDown); if (timer > 0) { setCountDown(setTimeout(() => setTimer(timer - 1), 1000)); } - }); + },[timer]); return ( diff --git a/server/engine/Game.js b/server/engine/Game.js index 216690f..e2137a7 100644 --- a/server/engine/Game.js +++ b/server/engine/Game.js @@ -27,6 +27,7 @@ const DEFAULT_BLUE_TEAM_STATE = [ ]; const MAX_NUM_OF_GUESSES = 25; +const TIME_INTERVAL = 45; class Game { constructor(hostId) { @@ -44,7 +45,8 @@ class Game { this.numGuessLeft = MAX_NUM_OF_GUESSES; this.maxNumOfGuess = MAX_NUM_OF_GUESSES; this.winner = null; - + this.timer = null; + this.timeLeft = TIME_INTERVAL; this.gameBoard = new Board(); } @@ -60,6 +62,9 @@ class Game { reduceNumGuessLeft() { this.numGuessLeft -= 1; } + reduceTimeLeft() { + this.timeLeft -= 1; + } //getters getHostId() { @@ -92,6 +97,9 @@ class Game { getVotedCards() { return this.votedCards; } + getTimeLeft() { + return this.timeLeft; + } getBoard() { return this.gameBoard; } @@ -104,6 +112,7 @@ class Game { gameBoard: { blueAgentNum, cards, redAgentNum }, numGuessLeft: this.numGuessLeft, winner: this.winner, + timeLeft: this.timeLeft }; } @@ -120,14 +129,19 @@ class Game { setWinner(team) { this.winner = team; } - setRedTeam(team) { this.redTeam = team; } - setBlueTeam(team) { this.blueTeam = team; } + setTimer(timer) { + this.timer = timer; + } + setTimeLeft() { + this.timeLeft = TIME_INTERVAL; + } + vote(data) { switch (data.player.team) { @@ -255,18 +269,6 @@ class Game { return false; } - stopGuess() { - switch (this.gameTurn) { - case GameTurns.BLUE_AGENT_TURN: - this.setNumGuessLeft = 0; - this.setGameTurn(GameTurns.RED_SPY_TURN); - break; - case GameTurns.RED_AGENT_TURN: - this.setNumGuessLeft = 0; - this.setGameTurn(GameTurns.BLUE_SPY_TURN); - break; - } - } resetGame() { this.bluePoints = 0; this.redPoints = 0; @@ -277,6 +279,20 @@ class Game { ]; this.winner = null; } + + startTimerInterval(gameIO, matchId) { + clearInterval(this.timer); + this.setTimeLeft(); + this.setTimer(setInterval(() => { + if (this.getTimeLeft() === -1) { + this.nextGameTurn(); + gameIO.to(matchId).emit("game state change", this.getGameState()); + gameIO.to(matchId).emit("start timer"); + this.startTimerInterval(gameIO, matchId); + } + this.reduceTimeLeft(); + }, 1000)); + } } module.exports = Game; diff --git a/server/socket_io/GameIO.js b/server/socket_io/GameIO.js index d1d1637..8788b9b 100644 --- a/server/socket_io/GameIO.js +++ b/server/socket_io/GameIO.js @@ -2,14 +2,11 @@ const NAMESPACE = "/game"; const MatchManager = require("../manager/MatchManager"); let connection = null; -let countdown = 30; class GameIO { constructor() { this.gameIO = null; this.chatIO = null; - - this.timer = null; } connect(io) { @@ -36,7 +33,8 @@ class GameIO { socket.on("game start", (matchId) => { this.gameIO.to(matchId).emit("resolve start game"); - this.timer = this.startTimerInterval(matchId); + const match = MatchManager.getMatch(matchId); + match.startTimerInterval(this.gameIO, matchId); }); socket.on("game state onload", (matchId) => { @@ -50,9 +48,9 @@ class GameIO { socket.on("end turn", (matchId) => { const match = MatchManager.getMatch(matchId); match.nextGameTurn(); + match.startTimerInterval(this.gameIO, matchId); this.gameIO.to(matchId).emit("game state change", match.getGameState()); - - this.timer = this.startTimerInterval(matchId); + this.gameIO.to(matchId).emit("start timer"); }); socket.on("card select", (matchId, data) => { @@ -116,30 +114,16 @@ class GameIO { if (match) { match.resetGame(); + match.startTimerInterval(this.gameIO, matchId); this.gameIO .to(matchId) .emit("game state change", match.getGameState()); + this.gameIO.to(matchId).emit("start timer"); } - this.timer = this.startTimerInterval(matchId); }); }); } - startTimerInterval(matchId) { - clearInterval(this.timer); - this.gameIO.to(matchId).emit("start timer"); - return setInterval(() => { - console.log("timer started"); - const match = MatchManager.getMatch(matchId); - console.log(match.getGameState()); - if (match) { - match.nextGameTurn(); - this.gameIO.to(matchId).emit("game state change", match.getGameState()); - this.gameIO.to(matchId).emit("start timer"); - } - }, 46000); - } - setChatIO(chatIO) { this.chatIO = chatIO; } From e17c26dad7aa1c7df7dbb3161fd065e9fab45e21 Mon Sep 17 00:00:00 2001 From: h0vscat Date: Tue, 30 Jun 2020 23:18:09 -0400 Subject: [PATCH 7/9] fix the sync issue --- client/src/pages/game/GamePrompt.js | 20 +++++++---------- server/engine/Game.js | 34 +++++++++++------------------ server/socket_io/GameIO.js | 4 ++-- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/client/src/pages/game/GamePrompt.js b/client/src/pages/game/GamePrompt.js index 3c257ee..26a230f 100644 --- a/client/src/pages/game/GamePrompt.js +++ b/client/src/pages/game/GamePrompt.js @@ -5,23 +5,19 @@ import { AppContext } from "../../App"; import { TEAM_ROLE } from "../game_lobby/team_select/TeamPresets"; function GamePrompt({ gameState, player }) { - const { gameIO } = useContext(AppContext); - const [timer, setTimer] = useState(gameState.timeLeft); + const [timer, setTimer] = useState(null); const [countDown, setCountDown] = useState(null); useEffect(() => { - gameIO.state.io.on("start timer", () => { - clearTimeout(countDown); - setTimer(gameState.timeLeft); - }); - }, []); + setCountDown(Math.floor((gameState.timeEnd - Date.now()) / 1000)); + }, [gameState.timeEnd]); useEffect(() => { - if (countDown) clearTimeout(countDown); - if (timer > 0) { - setCountDown(setTimeout(() => setTimer(timer - 1), 1000)); + clearTimeout(timer); + if (countDown > 0) { + setTimer(setTimeout(() => setCountDown(countDown - 1), 1000)); } - },[timer]); + },[countDown]); return ( @@ -33,7 +29,7 @@ function GamePrompt({ gameState, player }) { - {timer}s left + {gameState.winner ? 0 : countDown}s left diff --git a/server/engine/Game.js b/server/engine/Game.js index e2137a7..c8594d0 100644 --- a/server/engine/Game.js +++ b/server/engine/Game.js @@ -13,21 +13,21 @@ const TEAM_ROLE = { }; const DEFAULT_RED_TEAM_STATE = [ - { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: null }, + { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: {id: "id1", name: "name1"} }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, ]; const DEFAULT_BLUE_TEAM_STATE = [ - { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: null }, + { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: {id: "id3", name: "name3"} }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, ]; const MAX_NUM_OF_GUESSES = 25; -const TIME_INTERVAL = 45; +const TIME_INTERVAL = 46; class Game { constructor(hostId) { @@ -46,7 +46,7 @@ class Game { this.maxNumOfGuess = MAX_NUM_OF_GUESSES; this.winner = null; this.timer = null; - this.timeLeft = TIME_INTERVAL; + this.timeEnd = null; this.gameBoard = new Board(); } @@ -62,9 +62,6 @@ class Game { reduceNumGuessLeft() { this.numGuessLeft -= 1; } - reduceTimeLeft() { - this.timeLeft -= 1; - } //getters getHostId() { @@ -97,9 +94,6 @@ class Game { getVotedCards() { return this.votedCards; } - getTimeLeft() { - return this.timeLeft; - } getBoard() { return this.gameBoard; } @@ -112,7 +106,7 @@ class Game { gameBoard: { blueAgentNum, cards, redAgentNum }, numGuessLeft: this.numGuessLeft, winner: this.winner, - timeLeft: this.timeLeft + timeEnd: this.timeEnd }; } @@ -138,8 +132,8 @@ class Game { setTimer(timer) { this.timer = timer; } - setTimeLeft() { - this.timeLeft = TIME_INTERVAL; + setTimeEnd() { + this.timeEnd = Date.now() + TIME_INTERVAL * 1000; } @@ -278,20 +272,18 @@ class Game { Math.round(Math.random()) ]; this.winner = null; + this.timeEnd = null; + clearInterval(this.timer); } startTimerInterval(gameIO, matchId) { clearInterval(this.timer); - this.setTimeLeft(); - this.setTimer(setInterval(() => { - if (this.getTimeLeft() === -1) { + this.setTimeEnd(); + this.setTimer(setInterval(() => { + this.setTimeEnd(); this.nextGameTurn(); gameIO.to(matchId).emit("game state change", this.getGameState()); - gameIO.to(matchId).emit("start timer"); - this.startTimerInterval(gameIO, matchId); - } - this.reduceTimeLeft(); - }, 1000)); + }, TIME_INTERVAL * 1000)); } } diff --git a/server/socket_io/GameIO.js b/server/socket_io/GameIO.js index 8788b9b..9cc487e 100644 --- a/server/socket_io/GameIO.js +++ b/server/socket_io/GameIO.js @@ -32,9 +32,10 @@ class GameIO { }); socket.on("game start", (matchId) => { - this.gameIO.to(matchId).emit("resolve start game"); const match = MatchManager.getMatch(matchId); match.startTimerInterval(this.gameIO, matchId); + this.gameIO.to(matchId).emit("resolve start game"); + socket.emit("game state change", match.getGameState()); }); socket.on("game state onload", (matchId) => { @@ -50,7 +51,6 @@ class GameIO { match.nextGameTurn(); match.startTimerInterval(this.gameIO, matchId); this.gameIO.to(matchId).emit("game state change", match.getGameState()); - this.gameIO.to(matchId).emit("start timer"); }); socket.on("card select", (matchId, data) => { From 6951de6c6e42282e9fa4e41fba2ddfc056246022 Mon Sep 17 00:00:00 2001 From: h0vscat Date: Tue, 30 Jun 2020 23:20:17 -0400 Subject: [PATCH 8/9] remove test code --- server/engine/Game.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/engine/Game.js b/server/engine/Game.js index c8594d0..8a3589f 100644 --- a/server/engine/Game.js +++ b/server/engine/Game.js @@ -13,14 +13,14 @@ const TEAM_ROLE = { }; const DEFAULT_RED_TEAM_STATE = [ - { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: {id: "id1", name: "name1"} }, + { team: TeamColor.RED, role: TEAM_ROLE.SPYMASTER, user: null}, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.RED, role: TEAM_ROLE.FIELD_AGENT, user: null }, ]; const DEFAULT_BLUE_TEAM_STATE = [ - { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: {id: "id3", name: "name3"} }, + { team: TeamColor.BLUE, role: TEAM_ROLE.SPYMASTER, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, { team: TeamColor.BLUE, role: TEAM_ROLE.FIELD_AGENT, user: null }, From b08b66951ba8fc837f0b69e664ac6e6b861b3522 Mon Sep 17 00:00:00 2001 From: h0vscat Date: Wed, 1 Jul 2020 14:31:21 -0400 Subject: [PATCH 9/9] delete unnecessary code --- server/socket_io/GameIO.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server/socket_io/GameIO.js b/server/socket_io/GameIO.js index 9cc487e..1f6e85f 100644 --- a/server/socket_io/GameIO.js +++ b/server/socket_io/GameIO.js @@ -118,7 +118,6 @@ class GameIO { this.gameIO .to(matchId) .emit("game state change", match.getGameState()); - this.gameIO.to(matchId).emit("start timer"); } }); });