Skip to content

Commit bae684d

Browse files
authored
Game timer changePhase (#565)
* changePhase * Fix run issues
1 parent da643b4 commit bae684d

File tree

13 files changed

+31
-21
lines changed

13 files changed

+31
-21
lines changed

src/gameplay/avalon/cards/lady of the lake.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class LadyOfTheLake {
5757
numSuccess < 3 &&
5858
numFail < 3
5959
) {
60-
this.thisRoom.phase = Phase.lady;
60+
this.thisRoom.changePhase(Phase.lady);
6161
this.lastMissionUsed = this.thisRoom.missionNum;
6262

6363
return true;

src/gameplay/avalon/cards/ref of the rain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class RefOfTheLake {
6666
numSuccess < 3 &&
6767
numFail < 3
6868
) {
69-
this.thisRoom.phase = Phase.ref;
69+
this.thisRoom.changePhase(Phase.ref);
7070
this.lastMissionUsed = this.thisRoom.missionNum;
7171

7272
return true;

src/gameplay/avalon/cards/sire of the sea.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class SireOfTheSea {
6363
numSuccess < 3 &&
6464
numFail < 3
6565
) {
66-
this.thisRoom.phase = Phase.sire;
66+
this.thisRoom.changePhase(Phase.sire);
6767
this.lastMissionUsed = this.thisRoom.missionNum;
6868

6969
return true;

src/gameplay/avalon/phases/lady.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Lady {
115115
);
116116

117117
// update phase
118-
this.thisRoom.phase = Phase.pickingTeam;
118+
this.thisRoom.changePhase(Phase.pickingTeam);
119119
}
120120
// The requester is not the lady holder. Ignore the request.
121121
else {

src/gameplay/avalon/phases/ref.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Ref {
115115
);
116116

117117
// update phase
118-
this.thisRoom.phase = Phase.pickingTeam;
118+
this.thisRoom.changePhase(Phase.pickingTeam);
119119
}
120120
// The requester is not the ref holder. Ignore the request.
121121
else {

src/gameplay/avalon/phases/sire.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Sire {
142142
);
143143

144144
// update phase
145-
this.thisRoom.phase = Phase.pickingTeam;
145+
this.thisRoom.changePhase(Phase.pickingTeam);
146146
}
147147
// The requester is not the sire holder. Ignore the request.
148148
else {

src/gameplay/avalon/roles/assassin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Assassin implements Role {
8585
) {
8686
// Set the assassination phase
8787
this.room.startAssassinationTime = new Date();
88-
this.room.phase = this.specialPhase;
88+
this.room.changePhase(this.specialPhase);
8989
return true;
9090
}
9191
}

src/gameplay/commonPhases/pickingTeam.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import usernamesIndexes from '../../myFunctions/usernamesIndexes';
22
import Phase from '../avalon/phases/phases';
3+
import { MIN_PLAYERS } from '../game';
34

45
function PickingTeam(thisRoom_) {
56
this.thisRoom = thisRoom_;
@@ -39,7 +40,7 @@ PickingTeam.prototype.gameMove = function (
3940

4041
let num =
4142
this.thisRoom.numPlayersOnMission[
42-
this.thisRoom.playersInGame.length - this.thisRoom.minPlayers
43+
this.thisRoom.playersInGame.length - MIN_PLAYERS
4344
][this.thisRoom.missionNum - 1];
4445
// console.log("Num player for this.thisRoom mission : " + num);
4546

@@ -89,7 +90,7 @@ PickingTeam.prototype.gameMove = function (
8990

9091
this.thisRoom.VHUpdateTeamPick();
9192

92-
this.thisRoom.phase = Phase.votingTeam;
93+
this.thisRoom.changePhase(Phase.votingTeam);
9394
} else {
9495
console.log(
9596
`User ${socket.request.user.username} is not the team leader. Cannot pick.`,
@@ -135,7 +136,7 @@ PickingTeam.prototype.buttonSettings = function (indexOfPlayer) {
135136
PickingTeam.prototype.numOfTargets = function (indexOfPlayer) {
136137
let num =
137138
this.thisRoom.numPlayersOnMission[
138-
this.thisRoom.playersInGame.length - this.thisRoom.minPlayers
139+
this.thisRoom.playersInGame.length - MIN_PLAYERS
139140
][this.thisRoom.missionNum - 1];
140141
// console.log("Num player for this.thisRoom mission : " + num);
141142

@@ -161,7 +162,7 @@ PickingTeam.prototype.getStatusMessage = function (indexOfPlayer) {
161162
) {
162163
const num =
163164
this.thisRoom.numPlayersOnMission[
164-
this.thisRoom.playersInGame.length - this.thisRoom.minPlayers
165+
this.thisRoom.playersInGame.length - MIN_PLAYERS
165166
][this.thisRoom.missionNum - 1];
166167

167168
return `Your turn to pick a team. Pick ${num} players.`;

src/gameplay/commonPhases/votingMission.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ VotingMission.prototype.gameMove = function (
147147
1 +
148148
this.thisRoom.playersInGame.length) %
149149
this.thisRoom.playersInGame.length;
150-
this.thisRoom.phase = Phase.pickingTeam;
150+
this.thisRoom.changePhase(Phase.pickingTeam);
151151
}
152152
this.thisRoom.requireSave = true;
153153
}

src/gameplay/commonPhases/votingTeam.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ VotingTeam.prototype.gameMove = function (
5656
const outcome = calcVotes(this.thisRoom.votes);
5757

5858
if (outcome === 'yes') {
59-
this.thisRoom.phase = Phase.votingMission;
59+
this.thisRoom.changePhase(Phase.votingMission);
6060
this.thisRoom.playersYetToVote = this.thisRoom.proposedTeam.slice();
6161

6262
var str = `Mission ${this.thisRoom.missionNum}.${
@@ -84,7 +84,7 @@ VotingTeam.prototype.gameMove = function (
8484
this.thisRoom.finishGame('Spy');
8585
} else if (outcome === 'no') {
8686
this.thisRoom.proposedTeam = [];
87-
this.thisRoom.phase = Phase.pickingTeam;
87+
this.thisRoom.changePhase(Phase.pickingTeam);
8888

8989
var str = `Mission ${this.thisRoom.missionNum}.${
9090
this.thisRoom.pickNum

src/gameplay/game.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { gameModeObj } from './gameModes';
1717
import Phase from './avalon/phases/phases';
1818

1919
export const WAITING = 'Waiting';
20-
const MIN_PLAYERS = 5;
20+
export const MIN_PLAYERS = 5;
2121

2222
class Game extends Room {
2323
gameStarted = false;
@@ -161,7 +161,7 @@ class Game extends Room {
161161
_.merge(this.specialCards, storedData.specialCards);
162162

163163
this.phaseBeforeFrozen = this.phase;
164-
this.phase = Phase.frozen;
164+
this.changePhase(Phase.frozen);
165165
}
166166

167167
playerJoinRoom(socket, inputPassword) {
@@ -184,7 +184,7 @@ class Game extends Room {
184184
this.phase === 'frozen' &&
185185
this.socketsOfPlayers.length >= this.playersInGame.length
186186
) {
187-
this.phase = this.phaseBeforeFrozen;
187+
this.changePhase(this.phaseBeforeFrozen);
188188
}
189189

190190
const resultOfRoomJoin = Room.prototype.playerJoinRoom.call(
@@ -714,6 +714,10 @@ class Game extends Room {
714714
this.distributeGameData();
715715
}
716716

717+
changePhase(phase: Phase) {
718+
this.phase = phase;
719+
}
720+
717721
toShowGuns() {
718722
// Common phases
719723
if (
@@ -1122,7 +1126,7 @@ class Game extends Room {
11221126
throw new Error('Winner var is not Resistance or Spy');
11231127

11241128
const thisGame = this;
1125-
this.phase = Phase.finished;
1129+
this.changePhase(Phase.finished);
11261130

11271131
if (this.checkRoleCardSpecialMoves() === true) {
11281132
return;
@@ -1809,7 +1813,7 @@ class Game extends Room {
18091813
`${rolePrefix} ${modUsername} has unpaused the game.`,
18101814
'server-text',
18111815
);
1812-
this.phase = this.phaseBeforePause;
1816+
this.changePhase(this.phaseBeforePause);
18131817
this.distributeGameData();
18141818
}
18151819
// if unpaused, we pause
@@ -1821,7 +1825,7 @@ class Game extends Room {
18211825
);
18221826
// store the current phase, change to paused and update.
18231827
this.phaseBeforePause = this.phase;
1824-
this.phase = Phase.paused;
1828+
this.changePhase(Phase.paused);
18251829
this.distributeGameData();
18261830
}
18271831
}

src/gameplay/room.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AVALON, AVALON_BOT, GAME_MODE_NAMES, gameModeObj } from './gameModes';
33
import commonPhasesIndex from './indexCommonPhases';
44
import usernamesIndexes from '../myFunctions/usernamesIndexes';
55
import { SocketUser } from '../sockets/types';
6+
import { MIN_PLAYERS } from './game';
67

78
class Room {
89
host: string;
@@ -656,7 +657,7 @@ class Room {
656657

657658
if (this.canJoin === true) {
658659
// check before starting
659-
if (this.socketsOfPlayers.length < this.minPlayers) {
660+
if (this.socketsOfPlayers.length < MIN_PLAYERS) {
660661
// NEED AT LEAST FIVE PLAYERS, SHOW ERROR MESSAGE BACK
661662
// console.log("Not enough players.");
662663
this.socketsOfPlayers[0].emit(

src/sockets/sockets.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ setTimeout(async () => {
143143
.skip(i)
144144
.limit(1)
145145
.exec((err, foundSaveGameArr) => {
146+
if (!foundSaveGameArr || foundSaveGameArr.length === 0) {
147+
run = false;
148+
return;
149+
}
146150
const foundSaveGame = foundSaveGameArr[0];
147151

148152
if (foundSaveGame && foundSaveGame.room) {

0 commit comments

Comments
 (0)