Skip to content

Commit

Permalink
Game timer changePhase (#565)
Browse files Browse the repository at this point in the history
* changePhase

* Fix run issues
  • Loading branch information
vck3000 authored Feb 11, 2024
1 parent da643b4 commit bae684d
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/gameplay/avalon/cards/lady of the lake.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/gameplay/avalon/cards/ref of the rain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/gameplay/avalon/cards/sire of the sea.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/gameplay/avalon/phases/lady.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/gameplay/avalon/phases/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/gameplay/avalon/phases/sire.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/gameplay/avalon/roles/assassin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/gameplay/commonPhases/pickingTeam.js
Original file line number Diff line number Diff line change
@@ -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_;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.`,
Expand Down Expand Up @@ -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);

Expand All @@ -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.`;
Expand Down
2 changes: 1 addition & 1 deletion src/gameplay/commonPhases/votingMission.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/gameplay/commonPhases/votingTeam.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.${
Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/gameplay/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand Down Expand Up @@ -714,6 +714,10 @@ class Game extends Room {
this.distributeGameData();
}

changePhase(phase: Phase) {
this.phase = phase;
}

toShowGuns() {
// Common phases
if (
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/gameplay/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions src/sockets/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bae684d

Please sign in to comment.