fix problem with updating spectator count

This commit is contained in:
AlecM33
2022-12-30 14:26:14 -05:00
parent feee7146ce
commit 138fc08fde
7 changed files with 28 additions and 31 deletions

View File

@@ -50,7 +50,7 @@ export const globals = {
SYNC_GAME_STATE: 'syncGameState', SYNC_GAME_STATE: 'syncGameState',
START_TIMER: 'startTimer', START_TIMER: 'startTimer',
PLAYER_LEFT: 'playerLeft', PLAYER_LEFT: 'playerLeft',
NEW_SPECTATOR: 'newSpectator', UPDATE_SPECTATORS: 'newSpectator',
RESTART_GAME: 'restartGame' RESTART_GAME: 'restartGame'
}, },
USER_TYPES: { USER_TYPES: {

View File

@@ -203,12 +203,16 @@ export class InProgress {
} }
}); });
if (this.socket.hasListeners(globals.EVENT_IDS.NEW_SPECTATOR)) { if (this.socket.hasListeners(globals.EVENT_IDS.UPDATE_SPECTATORS)) {
this.socket.removeAllListeners(globals.EVENT_IDS.NEW_SPECTATOR); this.socket.removeAllListeners(globals.EVENT_IDS.UPDATE_SPECTATORS);
} }
this.socket.on(globals.EVENT_IDS.NEW_SPECTATOR, (spectator) => { this.socket.on(globals.EVENT_IDS.UPDATE_SPECTATORS, (updatedSpectatorList) => {
stateBucket.currentGameState.spectators.push(spectator); stateBucket.currentGameState.spectators = updatedSpectatorList;
SharedStateUtil.setNumberOfSpectators(
stateBucket.currentGameState.spectators.length,
document.getElementById('spectator-count')
);
if (this.stateBucket.currentGameState.client.userType === globals.USER_TYPES.MODERATOR if (this.stateBucket.currentGameState.client.userType === globals.USER_TYPES.MODERATOR
|| this.stateBucket.currentGameState.client.userType === globals.USER_TYPES.TEMPORARY_MODERATOR) { || this.stateBucket.currentGameState.client.userType === globals.USER_TYPES.TEMPORARY_MODERATOR) {
this.displayAvailableModerators(); this.displayAvailableModerators();

View File

@@ -99,8 +99,8 @@ export class Lobby {
} }
}); });
this.socket.on(globals.EVENT_IDS.NEW_SPECTATOR, (spectator) => { this.socket.on(globals.EVENT_IDS.UPDATE_SPECTATORS, (updatedSpectatorList) => {
this.stateBucket.currentGameState.spectators.push(spectator); this.stateBucket.currentGameState.spectators = updatedSpectatorList;
SharedStateUtil.setNumberOfSpectators( SharedStateUtil.setNumberOfSpectators(
this.stateBucket.currentGameState.spectators.length, this.stateBucket.currentGameState.spectators.length,
document.getElementById('spectator-count') document.getElementById('spectator-count')

View File

@@ -1,6 +1,5 @@
#join-game-modal { #join-game-modal {
border-left: 5px solid #b1afcd; border-left: 5px solid #b1afcd;
animation: entrance 0.5s forwards;
transform-origin: center; transform-origin: center;
display: block; display: block;
z-index: 1 !important; z-index: 1 !important;
@@ -46,15 +45,3 @@
#game-code { #game-code {
font-family: "Courier New", monospace; font-family: "Courier New", monospace;
} }
@keyframes entrance {
0% {
opacity: 0;
}
90% {
opacity: 1;
}
100% {
opacity: 1;
}
}

View File

@@ -68,7 +68,7 @@ const globals = {
PLAYER_JOINED: 'playerJoined', PLAYER_JOINED: 'playerJoined',
PLAYER_LEFT: 'playerLeft', PLAYER_LEFT: 'playerLeft',
SYNC_GAME_STATE: 'syncGameState', SYNC_GAME_STATE: 'syncGameState',
NEW_SPECTATOR: 'newSpectator', UPDATE_SPECTATORS: 'newSpectator',
BROADCAST: 'broadcast' BROADCAST: 'broadcast'
}, },
ENVIRONMENT: { ENVIRONMENT: {

View File

@@ -184,6 +184,11 @@ class GameManager {
transferModeratorPowers = (socket, game, person, namespace, logger) => { transferModeratorPowers = (socket, game, person, namespace, logger) => {
if (person && (person.out || person.userType === globals.USER_TYPES.SPECTATOR)) { if (person && (person.out || person.userType === globals.USER_TYPES.SPECTATOR)) {
let spectatorsUpdated = false;
if (game.spectators.includes(person)) {
game.spectators.splice(game.spectators.indexOf(person), 1);
spectatorsUpdated = true;
}
logger.debug('game ' + game.accessCode + ': transferring mod powers to ' + person.name); logger.debug('game ' + game.accessCode + ': transferring mod powers to ' + person.name);
if (game.moderator === person) { if (game.moderator === person) {
person.userType = globals.USER_TYPES.MODERATOR; person.userType = globals.USER_TYPES.MODERATOR;
@@ -197,16 +202,17 @@ class GameManager {
game.moderator.userType = globals.USER_TYPES.KILLED_PLAYER; // restore their state from before being made mod. game.moderator.userType = globals.USER_TYPES.KILLED_PLAYER; // restore their state from before being made mod.
} else if (game.moderator.userType === globals.USER_TYPES.MODERATOR) { } else if (game.moderator.userType === globals.USER_TYPES.MODERATOR) {
game.moderator.userType = globals.USER_TYPES.SPECTATOR; game.moderator.userType = globals.USER_TYPES.SPECTATOR;
if (!game.spectators.includes(game.moderator)) { game.spectators.push(game.moderator);
game.spectators.push(game.moderator); spectatorsUpdated = true;
}
if (game.spectators.includes(person)) {
game.spectators.splice(game.spectators.indexOf(person), 1);
}
namespace.in(game.accessCode).emit(globals.EVENTS.NEW_SPECTATOR);
} }
person.userType = globals.USER_TYPES.MODERATOR; person.userType = globals.USER_TYPES.MODERATOR;
game.moderator = person; game.moderator = person;
if (spectatorsUpdated === true) {
namespace.in(game.accessCode).emit(
globals.EVENTS.UPDATE_SPECTATORS,
game.spectators.map((spectator) => GameStateCurator.mapPerson(spectator))
);
}
this.namespace.to(person.socketId).emit(globals.EVENTS.SYNC_GAME_STATE); this.namespace.to(person.socketId).emit(globals.EVENTS.SYNC_GAME_STATE);
this.namespace.to(oldModerator.socketId).emit(globals.EVENTS.SYNC_GAME_STATE); this.namespace.to(oldModerator.socketId).emit(globals.EVENTS.SYNC_GAME_STATE);
} }
@@ -507,8 +513,8 @@ function addSpectator (game, name, logger, namespace) {
logger.trace('new spectator: ' + spectator.name); logger.trace('new spectator: ' + spectator.name);
game.spectators.push(spectator); game.spectators.push(spectator);
namespace.in(game.accessCode).emit( namespace.in(game.accessCode).emit(
globals.EVENTS.NEW_SPECTATOR, globals.EVENTS.UPDATE_SPECTATORS,
GameStateCurator.mapPerson(spectator) game.spectators.map((spectator) => { return GameStateCurator.mapPerson(spectator); })
); );
return Promise.resolve(spectator.cookie); return Promise.resolve(spectator.cookie);
} }

View File

@@ -292,7 +292,7 @@ describe('GameManager', () => {
expect(game.spectators.length).toEqual(1); expect(game.spectators.length).toEqual(1);
expect(game.spectators[0].name).toEqual('Jane'); expect(game.spectators[0].name).toEqual('Jane');
expect(game.spectators[0].userType).toEqual(USER_TYPES.SPECTATOR); expect(game.spectators[0].userType).toEqual(USER_TYPES.SPECTATOR);
expect(gameManager.namespace.in().emit).toHaveBeenCalledWith(globals.EVENTS.NEW_SPECTATOR, jasmine.anything()); expect(gameManager.namespace.in().emit).toHaveBeenCalledWith(globals.EVENTS.UPDATE_SPECTATORS, jasmine.anything());
}); });
}); });