From bae684d83a5dcbdec729692e64b6a8b9dbdf8937 Mon Sep 17 00:00:00 2001 From: Victor Kuo <42633156+vck3000@users.noreply.github.com> Date: Sun, 11 Feb 2024 13:45:40 -0500 Subject: [PATCH] Game timer changePhase (#565) * changePhase * Fix run issues --- src/gameplay/avalon/cards/lady of the lake.js | 2 +- src/gameplay/avalon/cards/ref of the rain.js | 2 +- src/gameplay/avalon/cards/sire of the sea.js | 2 +- src/gameplay/avalon/phases/lady.js | 2 +- src/gameplay/avalon/phases/ref.js | 2 +- src/gameplay/avalon/phases/sire.js | 2 +- src/gameplay/avalon/roles/assassin.ts | 2 +- src/gameplay/commonPhases/pickingTeam.js | 9 +++++---- src/gameplay/commonPhases/votingMission.js | 2 +- src/gameplay/commonPhases/votingTeam.js | 4 ++-- src/gameplay/game.ts | 16 ++++++++++------ src/gameplay/room.ts | 3 ++- src/sockets/sockets.ts | 4 ++++ 13 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/gameplay/avalon/cards/lady of the lake.js b/src/gameplay/avalon/cards/lady of the lake.js index be1fb6d3f..1da5c2fa4 100644 --- a/src/gameplay/avalon/cards/lady of the lake.js +++ b/src/gameplay/avalon/cards/lady of the lake.js @@ -57,7 +57,7 @@ class LadyOfTheLake { numSuccess < 3 && numFail < 3 ) { - this.thisRoom.phase = Phase.lady; + this.thisRoom.changePhase(Phase.lady); this.lastMissionUsed = this.thisRoom.missionNum; return true; diff --git a/src/gameplay/avalon/cards/ref of the rain.js b/src/gameplay/avalon/cards/ref of the rain.js index 47bab6a52..e565a8ec7 100644 --- a/src/gameplay/avalon/cards/ref of the rain.js +++ b/src/gameplay/avalon/cards/ref of the rain.js @@ -66,7 +66,7 @@ class RefOfTheLake { numSuccess < 3 && numFail < 3 ) { - this.thisRoom.phase = Phase.ref; + this.thisRoom.changePhase(Phase.ref); this.lastMissionUsed = this.thisRoom.missionNum; return true; diff --git a/src/gameplay/avalon/cards/sire of the sea.js b/src/gameplay/avalon/cards/sire of the sea.js index 91c40f0a7..c26a3e2a5 100644 --- a/src/gameplay/avalon/cards/sire of the sea.js +++ b/src/gameplay/avalon/cards/sire of the sea.js @@ -63,7 +63,7 @@ class SireOfTheSea { numSuccess < 3 && numFail < 3 ) { - this.thisRoom.phase = Phase.sire; + this.thisRoom.changePhase(Phase.sire); this.lastMissionUsed = this.thisRoom.missionNum; return true; diff --git a/src/gameplay/avalon/phases/lady.js b/src/gameplay/avalon/phases/lady.js index bd2808340..9cb535b0c 100644 --- a/src/gameplay/avalon/phases/lady.js +++ b/src/gameplay/avalon/phases/lady.js @@ -115,7 +115,7 @@ class Lady { ); // update phase - this.thisRoom.phase = Phase.pickingTeam; + this.thisRoom.changePhase(Phase.pickingTeam); } // The requester is not the lady holder. Ignore the request. else { diff --git a/src/gameplay/avalon/phases/ref.js b/src/gameplay/avalon/phases/ref.js index 041090c60..0bb8dc946 100644 --- a/src/gameplay/avalon/phases/ref.js +++ b/src/gameplay/avalon/phases/ref.js @@ -115,7 +115,7 @@ class Ref { ); // update phase - this.thisRoom.phase = Phase.pickingTeam; + this.thisRoom.changePhase(Phase.pickingTeam); } // The requester is not the ref holder. Ignore the request. else { diff --git a/src/gameplay/avalon/phases/sire.js b/src/gameplay/avalon/phases/sire.js index 17e896024..52c03821b 100644 --- a/src/gameplay/avalon/phases/sire.js +++ b/src/gameplay/avalon/phases/sire.js @@ -142,7 +142,7 @@ class Sire { ); // update phase - this.thisRoom.phase = Phase.pickingTeam; + this.thisRoom.changePhase(Phase.pickingTeam); } // The requester is not the sire holder. Ignore the request. else { diff --git a/src/gameplay/avalon/roles/assassin.ts b/src/gameplay/avalon/roles/assassin.ts index 86666b7e5..d0d1852b7 100644 --- a/src/gameplay/avalon/roles/assassin.ts +++ b/src/gameplay/avalon/roles/assassin.ts @@ -85,7 +85,7 @@ class Assassin implements Role { ) { // Set the assassination phase this.room.startAssassinationTime = new Date(); - this.room.phase = this.specialPhase; + this.room.changePhase(this.specialPhase); return true; } } diff --git a/src/gameplay/commonPhases/pickingTeam.js b/src/gameplay/commonPhases/pickingTeam.js index 1869ae130..57c6e12b6 100644 --- a/src/gameplay/commonPhases/pickingTeam.js +++ b/src/gameplay/commonPhases/pickingTeam.js @@ -1,5 +1,6 @@ import usernamesIndexes from '../../myFunctions/usernamesIndexes'; import Phase from '../avalon/phases/phases'; +import { MIN_PLAYERS } from '../game'; function PickingTeam(thisRoom_) { this.thisRoom = thisRoom_; @@ -39,7 +40,7 @@ PickingTeam.prototype.gameMove = function ( let num = this.thisRoom.numPlayersOnMission[ - this.thisRoom.playersInGame.length - this.thisRoom.minPlayers + this.thisRoom.playersInGame.length - MIN_PLAYERS ][this.thisRoom.missionNum - 1]; // console.log("Num player for this.thisRoom mission : " + num); @@ -89,7 +90,7 @@ PickingTeam.prototype.gameMove = function ( this.thisRoom.VHUpdateTeamPick(); - this.thisRoom.phase = Phase.votingTeam; + this.thisRoom.changePhase(Phase.votingTeam); } else { console.log( `User ${socket.request.user.username} is not the team leader. Cannot pick.`, @@ -135,7 +136,7 @@ PickingTeam.prototype.buttonSettings = function (indexOfPlayer) { PickingTeam.prototype.numOfTargets = function (indexOfPlayer) { let num = this.thisRoom.numPlayersOnMission[ - this.thisRoom.playersInGame.length - this.thisRoom.minPlayers + this.thisRoom.playersInGame.length - MIN_PLAYERS ][this.thisRoom.missionNum - 1]; // console.log("Num player for this.thisRoom mission : " + num); @@ -161,7 +162,7 @@ PickingTeam.prototype.getStatusMessage = function (indexOfPlayer) { ) { const num = this.thisRoom.numPlayersOnMission[ - this.thisRoom.playersInGame.length - this.thisRoom.minPlayers + this.thisRoom.playersInGame.length - MIN_PLAYERS ][this.thisRoom.missionNum - 1]; return `Your turn to pick a team. Pick ${num} players.`; diff --git a/src/gameplay/commonPhases/votingMission.js b/src/gameplay/commonPhases/votingMission.js index 5db24a81b..e319903f9 100644 --- a/src/gameplay/commonPhases/votingMission.js +++ b/src/gameplay/commonPhases/votingMission.js @@ -147,7 +147,7 @@ VotingMission.prototype.gameMove = function ( 1 + this.thisRoom.playersInGame.length) % this.thisRoom.playersInGame.length; - this.thisRoom.phase = Phase.pickingTeam; + this.thisRoom.changePhase(Phase.pickingTeam); } this.thisRoom.requireSave = true; } diff --git a/src/gameplay/commonPhases/votingTeam.js b/src/gameplay/commonPhases/votingTeam.js index 05154da9f..8dd614d57 100644 --- a/src/gameplay/commonPhases/votingTeam.js +++ b/src/gameplay/commonPhases/votingTeam.js @@ -56,7 +56,7 @@ VotingTeam.prototype.gameMove = function ( const outcome = calcVotes(this.thisRoom.votes); if (outcome === 'yes') { - this.thisRoom.phase = Phase.votingMission; + this.thisRoom.changePhase(Phase.votingMission); this.thisRoom.playersYetToVote = this.thisRoom.proposedTeam.slice(); var str = `Mission ${this.thisRoom.missionNum}.${ @@ -84,7 +84,7 @@ VotingTeam.prototype.gameMove = function ( this.thisRoom.finishGame('Spy'); } else if (outcome === 'no') { this.thisRoom.proposedTeam = []; - this.thisRoom.phase = Phase.pickingTeam; + this.thisRoom.changePhase(Phase.pickingTeam); var str = `Mission ${this.thisRoom.missionNum}.${ this.thisRoom.pickNum diff --git a/src/gameplay/game.ts b/src/gameplay/game.ts index 7ad4ae5d0..9c58a8765 100644 --- a/src/gameplay/game.ts +++ b/src/gameplay/game.ts @@ -17,7 +17,7 @@ import { gameModeObj } from './gameModes'; import Phase from './avalon/phases/phases'; export const WAITING = 'Waiting'; -const MIN_PLAYERS = 5; +export const MIN_PLAYERS = 5; class Game extends Room { gameStarted = false; @@ -161,7 +161,7 @@ class Game extends Room { _.merge(this.specialCards, storedData.specialCards); this.phaseBeforeFrozen = this.phase; - this.phase = Phase.frozen; + this.changePhase(Phase.frozen); } playerJoinRoom(socket, inputPassword) { @@ -184,7 +184,7 @@ class Game extends Room { this.phase === 'frozen' && this.socketsOfPlayers.length >= this.playersInGame.length ) { - this.phase = this.phaseBeforeFrozen; + this.changePhase(this.phaseBeforeFrozen); } const resultOfRoomJoin = Room.prototype.playerJoinRoom.call( @@ -714,6 +714,10 @@ class Game extends Room { this.distributeGameData(); } + changePhase(phase: Phase) { + this.phase = phase; + } + toShowGuns() { // Common phases if ( @@ -1122,7 +1126,7 @@ class Game extends Room { throw new Error('Winner var is not Resistance or Spy'); const thisGame = this; - this.phase = Phase.finished; + this.changePhase(Phase.finished); if (this.checkRoleCardSpecialMoves() === true) { return; @@ -1809,7 +1813,7 @@ class Game extends Room { `${rolePrefix} ${modUsername} has unpaused the game.`, 'server-text', ); - this.phase = this.phaseBeforePause; + this.changePhase(this.phaseBeforePause); this.distributeGameData(); } // if unpaused, we pause @@ -1821,7 +1825,7 @@ class Game extends Room { ); // store the current phase, change to paused and update. this.phaseBeforePause = this.phase; - this.phase = Phase.paused; + this.changePhase(Phase.paused); this.distributeGameData(); } } diff --git a/src/gameplay/room.ts b/src/gameplay/room.ts index 512b9d1b2..25f2ac9ca 100644 --- a/src/gameplay/room.ts +++ b/src/gameplay/room.ts @@ -3,6 +3,7 @@ import { AVALON, AVALON_BOT, GAME_MODE_NAMES, gameModeObj } from './gameModes'; import commonPhasesIndex from './indexCommonPhases'; import usernamesIndexes from '../myFunctions/usernamesIndexes'; import { SocketUser } from '../sockets/types'; +import { MIN_PLAYERS } from './game'; class Room { host: string; @@ -656,7 +657,7 @@ class Room { if (this.canJoin === true) { // check before starting - if (this.socketsOfPlayers.length < this.minPlayers) { + if (this.socketsOfPlayers.length < MIN_PLAYERS) { // NEED AT LEAST FIVE PLAYERS, SHOW ERROR MESSAGE BACK // console.log("Not enough players."); this.socketsOfPlayers[0].emit( diff --git a/src/sockets/sockets.ts b/src/sockets/sockets.ts index bef1b1e76..a95a365f1 100644 --- a/src/sockets/sockets.ts +++ b/src/sockets/sockets.ts @@ -143,6 +143,10 @@ setTimeout(async () => { .skip(i) .limit(1) .exec((err, foundSaveGameArr) => { + if (!foundSaveGameArr || foundSaveGameArr.length === 0) { + run = false; + return; + } const foundSaveGame = foundSaveGameArr[0]; if (foundSaveGame && foundSaveGame.room) {