play and pause button, more improved lobby view

This commit is contained in:
Alec
2021-12-01 15:07:24 -05:00
parent 7f86aa22aa
commit 27dfd55cda
14 changed files with 222 additions and 130 deletions

View File

@@ -7,7 +7,8 @@ const globals = {
GET_ENVIRONMENT: 'getEnvironment',
START_GAME: 'startGame',
PAUSE_TIMER: 'pauseTimer',
RESUME_TIMER: 'resumeTimer'
RESUME_TIMER: 'resumeTimer',
GET_TIME_REMAINING: 'getTimeRemaining'
},
STATUS: {
LOBBY: "lobby",

View File

@@ -60,7 +60,7 @@ class ActiveGameRunner {
minutes: game.timerParams.minutes
});
game.startTime = new Date().toJSON();
namespace.in(game.accessCode).emit(globals.GAME_PROCESS_COMMANDS.START_TIMER);
//namespace.in(game.accessCode).emit(globals.GAME_PROCESS_COMMANDS.START_TIMER);
}
}

View File

@@ -39,6 +39,7 @@ class GameManager {
game.status = globals.STATUS.IN_PROGRESS;
namespace.in(accessCode).emit(globals.EVENTS.SYNC_GAME_STATE);
if (game.hasTimer) {
game.timerParams.paused = true;
this.activeGameRunner.runGame(game, namespace);
}
}
@@ -58,7 +59,7 @@ class GameManager {
});
}
}
})
});
socket.on(globals.CLIENT_COMMANDS.RESUME_TIMER, (accessCode) => {
this.logger.trace(accessCode);
@@ -74,7 +75,22 @@ class GameManager {
});
}
}
})
});
socket.on(globals.CLIENT_COMMANDS.GET_TIME_REMAINING, (accessCode) => {
let game = this.activeGameRunner.activeGames[accessCode];
if (game) {
let thread = this.activeGameRunner.timerThreads[accessCode];
if (thread) {
thread.send({
command: globals.GAME_PROCESS_COMMANDS.GET_TIME_REMAINING,
accessCode: accessCode,
socketId: socket.id,
logLevel: this.logger.logLevel
});
}
}
});
}
@@ -250,7 +266,7 @@ function handleRequestForGameState(namespace, logger, gameRunner, accessCode, pe
game.isFull = isFull;
socket.to(accessCode).emit(
globals.EVENTS.PLAYER_JOINED,
{name: unassignedPerson.name},
{name: unassignedPerson.name, userType: unassignedPerson.userType},
isFull
);
} else {

View File

@@ -2,19 +2,17 @@ const globals = require("../config/globals")
const GameStateCurator = {
getGameStateFromPerspectiveOfPerson: (game, person, gameRunner, socket, logger) => {
if (game.timerParams && game.status === globals.STATUS.IN_PROGRESS) {
getTimeRemaining(game.accessCode, gameRunner, socket, logger)
}
return getGameStateBasedOnPermissions(game, person, gameRunner);
}
}
function getGameStateBasedOnPermissions(game, person, gameRunner) {
let client = game.status === globals.STATUS.LOBBY // people won't be able to know their role until past the lobby stage.
? { name: person.name, id: person.id }
? { name: person.name, id: person.id, userType: person.userType }
: {
name: person.name,
id: person.id,
userType: person.userType,
gameRole: person.gameRole,
gameRoleDescription: person.gameRoleDescription,
alignment: person.alignment
@@ -25,12 +23,12 @@ function getGameStateBasedOnPermissions(game, person, gameRunner) {
accessCode: game.accessCode,
status: game.status,
moderator: mapPerson(game.moderator),
userType: globals.USER_TYPES.PLAYER,
client: client,
deck: game.deck,
people: game.people
.filter((person) => {
return person.assigned === true && person.id !== client.id && person.userType !== globals.USER_TYPES.MODERATOR
return person.assigned === true && person.id !== client.id
&& (person.userType !== globals.USER_TYPES.MODERATOR && person.userType !== globals.USER_TYPES.TEMPORARY_MODERATOR)
})
.map((filteredPerson) => ({ name: filteredPerson.name, userType: filteredPerson.userType })),
timerParams: game.timerParams,
@@ -41,7 +39,6 @@ function getGameStateBasedOnPermissions(game, person, gameRunner) {
accessCode: game.accessCode,
status: game.status,
moderator: mapPerson(game.moderator),
userType: globals.USER_TYPES.MODERATOR,
client: client,
deck: game.deck,
people: mapPeopleForModerator(game.people, client),
@@ -53,7 +50,6 @@ function getGameStateBasedOnPermissions(game, person, gameRunner) {
accessCode: game.accessCode,
status: game.status,
moderator: mapPerson(game.moderator),
userType: globals.USER_TYPES.TEMPORARY_MODERATOR,
client: client,
deck: game.deck,
people: mapPeopleForTempModerator(game.people, client),
@@ -72,6 +68,7 @@ function mapPeopleForModerator(people, client) {
})
.map((person) => ({
name: person.name,
userType: person.userType,
gameRole: person.gameRole,
gameRoleDescription: person.gameRoleDescription,
alignment: person.alignment
@@ -85,23 +82,12 @@ function mapPeopleForTempModerator(people, client) {
})
.map((person) => ({
name: person.name,
userType: person.userType
}));
}
function mapPerson(person) {
return { name: person.name };
}
function getTimeRemaining(accessCode, gameRunner, socket, logger) {
let thread = gameRunner.timerThreads[accessCode];
if (thread) {
thread.send({
command: globals.GAME_PROCESS_COMMANDS.GET_TIME_REMAINING,
accessCode: accessCode,
socketId: socket.id,
logLevel: logger.logLevel
});
}
return { name: person.name, userType: person.userType };
}
module.exports = GameStateCurator;

View File

@@ -36,7 +36,7 @@ class ServerTimer {
this.totalTime = null;
}
runTimer () {
runTimer (pausedInitially=true) {
let total = convertFromHoursToMilliseconds(this.hours) + convertFromMinutesToMilliseconds(this.minutes);
this.totalTime = total;
this.currentTimeInMillis = total;
@@ -47,18 +47,22 @@ class ServerTimer {
this.timesUpResolver = resolve;
});
const instance = this;
this.ticking = setTimeout(function () {
stepFn(
instance,
expected
);
}, this.tickInterval);
if (!pausedInitially) {
this.ticking = setTimeout(function () {
stepFn(
instance,
expected
);
}, this.tickInterval);
}
return this.timesUpPromise;
}
stopTimer() {
clearTimeout(this.ticking);
if (this.ticking) {
clearTimeout(this.ticking);
}
let now = Date.now();
this.logger.debug(
'ELAPSED (PAUSE): ' + (now - this.start) + 'ms (~'