Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer renyi 48 #71

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions client/src/pages/game/GamePrompt.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
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 [timer, setTimer] = useState(null);
const [countDown, setCountDown] = useState(null);

useEffect(() => {
setCountDown(Math.floor((gameState.timeEnd - Date.now()) / 1000));
}, [gameState.timeEnd]);

useEffect(() => {
clearTimeout(timer);
if (countDown > 0) {
setTimer(setTimeout(() => setCountDown(countDown - 1), 1000));
}
},[countDown]);


return (
<Container>
<Grid container justify="space-between" alignItems="center">
Expand All @@ -14,7 +29,7 @@ function GamePrompt({ gameState, player }) {
<TimerIcon fontSize="large" />
</Grid>
<Grid item>
<Typography variant="h6">45s left</Typography>
<Typography variant="h6">{gameState.winner ? 0 : countDown}s left</Typography>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - why not just have it set as just countdown? I don't see the need to just show a 0 on the timer when the game is over

Copy link
Collaborator Author

@reny1cao reny1cao Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's because It feels odd to me that the timer is running after the game ended

</Grid>
</Grid>
</Grid>
Expand Down
63 changes: 26 additions & 37 deletions server/engine/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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: 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 },
Expand All @@ -27,6 +27,7 @@ const DEFAULT_BLUE_TEAM_STATE = [
];

const MAX_NUM_OF_GUESSES = 25;
const TIME_INTERVAL = 46;

class Game {
constructor(hostId) {
Expand All @@ -41,10 +42,11 @@ 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;

this.timer = null;
this.timeEnd = null;
this.gameBoard = new Board();
}

Expand Down Expand Up @@ -104,6 +106,7 @@ class Game {
gameBoard: { blueAgentNum, cards, redAgentNum },
numGuessLeft: this.numGuessLeft,
winner: this.winner,
timeEnd: this.timeEnd
};
}

Expand All @@ -120,14 +123,19 @@ class Game {
setWinner(team) {
this.winner = team;
}

setRedTeam(team) {
this.redTeam = team;
}

setBlueTeam(team) {
this.blueTeam = team;
}
setTimer(timer) {
this.timer = timer;
}
setTimeEnd() {
this.timeEnd = Date.now() + TIME_INTERVAL * 1000;
}


vote(data) {
switch (data.player.team) {
Expand Down Expand Up @@ -255,48 +263,29 @@ 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;
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())
];
this.winner = null;
this.timeEnd = null;
clearInterval(this.timer);
}

startTimerInterval(gameIO, matchId) {
clearInterval(this.timer);
this.setTimeEnd();
this.setTimer(setInterval(() => {
this.setTimeEnd();
this.nextGameTurn();
gameIO.to(matchId).emit("game state change", this.getGameState());
}, TIME_INTERVAL * 1000));
}
}

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();
11 changes: 5 additions & 6 deletions server/socket_io/GameIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const NAMESPACE = "/game";
const MatchManager = require("../manager/MatchManager");

let connection = null;
let countdown = 30;

class GameIO {
constructor() {
Expand Down Expand Up @@ -33,7 +32,10 @@ class GameIO {
});

socket.on("game start", (matchId) => {
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) => {
Expand All @@ -44,14 +46,10 @@ class GameIO {
}
});

// setInterval(() => {
// countdown--;
// this.gameIO.to(matchId).emit("timer", { countdown: countdown });
// }, 1000);

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());
});

Expand Down Expand Up @@ -116,6 +114,7 @@ class GameIO {

if (match) {
match.resetGame();
match.startTimerInterval(this.gameIO, matchId);
this.gameIO
.to(matchId)
.emit("game state change", match.getGameState());
Expand Down