mirror of
https://github.com/AlecM33/Werewolf.git
synced 2026-01-02 01:03:24 +01:00
redis effort part 5
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
export const globals = {
|
||||
CHAR_POOL: 'abcdefghijklmnopqrstuvwxyz0123456789',
|
||||
USER_SIGNATURE_LENGTH: 75,
|
||||
CLOCK_TICK_INTERVAL_MILLIS: 100,
|
||||
CLOCK_TICK_INTERVAL_MILLIS: 50,
|
||||
MAX_CUSTOM_ROLE_NAME_LENGTH: 50,
|
||||
MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH: 1000,
|
||||
TOAST_DURATION_DEFAULT: 6,
|
||||
@@ -54,7 +54,19 @@ export const globals = {
|
||||
UPDATE_SPECTATORS: 'updateSpectators',
|
||||
RESTART_GAME: 'restartGame',
|
||||
ASSIGN_DEDICATED_MOD: 'assignDedicatedMod'
|
||||
|
||||
},
|
||||
LOBBY_EVENTS: function () {
|
||||
return [
|
||||
this.EVENT_IDS.PLAYER_JOINED,
|
||||
this.EVENT_IDS.ADD_SPECTATOR
|
||||
];
|
||||
},
|
||||
IN_PROGRESS_EVENTS: function () {
|
||||
return [
|
||||
this.EVENT_IDS.KILL_PLAYER,
|
||||
this.EVENT_IDS.REVEAL_PLAYER,
|
||||
this.EVENT_IDS.ADD_SPECTATOR
|
||||
];
|
||||
},
|
||||
USER_TYPES: {
|
||||
MODERATOR: 'moderator',
|
||||
|
||||
@@ -4,7 +4,6 @@ import { HTMLFragments } from '../../front_end_components/HTMLFragments.js';
|
||||
import { Confirmation } from '../../front_end_components/Confirmation.js';
|
||||
import { ModalManager } from '../../front_end_components/ModalManager.js';
|
||||
import { GameTimerManager } from '../../timer/GameTimerManager.js';
|
||||
import { stateBucket } from '../StateBucket.js';
|
||||
import { SharedStateUtil } from './shared/SharedStateUtil.js';
|
||||
|
||||
export class InProgress {
|
||||
@@ -194,6 +193,7 @@ export class InProgress {
|
||||
});
|
||||
|
||||
this.socket.on(globals.EVENT_IDS.REVEAL_PLAYER, (revealData) => {
|
||||
console.log('here');
|
||||
const revealedPerson = this.stateBucket.currentGameState.people.find((person) => person.id === revealData.id);
|
||||
if (revealedPerson) {
|
||||
revealedPerson.revealed = true;
|
||||
@@ -217,27 +217,10 @@ export class InProgress {
|
||||
}
|
||||
});
|
||||
|
||||
if (this.socket.hasListeners(globals.EVENT_IDS.ADD_SPECTATOR)) {
|
||||
this.socket.removeAllListeners(globals.EVENT_IDS.ADD_SPECTATOR);
|
||||
}
|
||||
|
||||
this.socket.on(globals.EVENT_IDS.ADD_SPECTATOR, (spectator) => {
|
||||
stateBucket.currentGameState.people.push(spectator);
|
||||
this.stateBucket.currentGameState.people.push(spectator);
|
||||
SharedStateUtil.setNumberOfSpectators(
|
||||
stateBucket.currentGameState.people.filter(p => p.userType === globals.USER_TYPES.SPECTATOR).length,
|
||||
document.getElementById('spectator-count')
|
||||
);
|
||||
if (this.stateBucket.currentGameState.client.userType === globals.USER_TYPES.MODERATOR
|
||||
|| this.stateBucket.currentGameState.client.userType === globals.USER_TYPES.TEMPORARY_MODERATOR) {
|
||||
this.displayAvailableModerators();
|
||||
}
|
||||
});
|
||||
|
||||
this.socket.on(globals.EVENT_IDS.UPDATE_SPECTATORS, (spectators) => {
|
||||
stateBucket.currentGameState.people = stateBucket.currentGameState.people.filter(p => p.userType !== globals.USER_TYPES.SPECTATOR);
|
||||
stateBucket.currentGameState.people = stateBucket.currentGameState.people.concat(spectators);
|
||||
SharedStateUtil.setNumberOfSpectators(
|
||||
stateBucket.currentGameState.people.filter(p => p.userType === globals.USER_TYPES.SPECTATOR).length,
|
||||
this.stateBucket.currentGameState.people.filter(p => p.userType === globals.USER_TYPES.SPECTATOR).length,
|
||||
document.getElementById('spectator-count')
|
||||
);
|
||||
if (this.stateBucket.currentGameState.client.userType === globals.USER_TYPES.MODERATOR
|
||||
@@ -247,9 +230,11 @@ export class InProgress {
|
||||
});
|
||||
|
||||
if (this.stateBucket.currentGameState.timerParams) {
|
||||
const timerWorker = new Worker(new URL('../../timer/Timer.js', import.meta.url));
|
||||
const gameTimerManager = new GameTimerManager(stateBucket, this.socket);
|
||||
gameTimerManager.attachTimerSocketListeners(this.socket, timerWorker);
|
||||
if (!this.stateBucket.timerWorker) {
|
||||
this.stateBucket.timerWorker = new Worker(new URL('../../timer/Timer.js', import.meta.url));
|
||||
}
|
||||
const gameTimerManager = new GameTimerManager(this.stateBucket, this.socket);
|
||||
gameTimerManager.attachTimerSocketListeners(this.socket, this.stateBucket.timerWorker);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,8 +525,8 @@ function renderPotentialMods (gameState, group, transferModHandlers, socket) {
|
||||
socket.emit(
|
||||
globals.SOCKET_EVENTS.IN_GAME_MESSAGE,
|
||||
globals.EVENT_IDS.FETCH_GAME_STATE,
|
||||
stateBucket.currentGameState.accessCode,
|
||||
{ personId: stateBucket.currentGameState.client.cookie },
|
||||
gameState.accessCode,
|
||||
{ personId: gameState.client.cookie },
|
||||
(gameState) => {
|
||||
SharedStateUtil.gameStateAckFn(gameState, socket);
|
||||
}
|
||||
|
||||
@@ -118,21 +118,20 @@ export const SharedStateUtil = {
|
||||
const accessCode = splitUrl[1];
|
||||
if (/^[a-zA-Z0-9]+$/.test(accessCode) && accessCode.length === globals.ACCESS_CODE_LENGTH) {
|
||||
socket.emit(globals.SOCKET_EVENTS.IN_GAME_MESSAGE, globals.EVENT_IDS.FETCH_GAME_STATE, accessCode, { personId: cookie }, function (gameState) {
|
||||
// if (gameState === null) {
|
||||
// window.location = '/not-found?reason=' + encodeURIComponent('game-not-found');
|
||||
// } else {
|
||||
stateBucket.currentGameState = gameState;
|
||||
document.querySelector('.spinner-container')?.remove();
|
||||
document.querySelector('.spinner-background')?.remove();
|
||||
document.getElementById('game-content').innerHTML = HTMLFragments.INITIAL_GAME_DOM;
|
||||
toast('You are connected.', 'success', true, true, 'short');
|
||||
processGameState(stateBucket.currentGameState, cookie, socket, true, true);
|
||||
// }
|
||||
if (gameState === null) {
|
||||
window.location = '/not-found?reason=' + encodeURIComponent('game-not-found');
|
||||
} else {
|
||||
stateBucket.currentGameState = gameState;
|
||||
document.querySelector('.spinner-container')?.remove();
|
||||
document.querySelector('.spinner-background')?.remove();
|
||||
document.getElementById('game-content').innerHTML = HTMLFragments.INITIAL_GAME_DOM;
|
||||
toast('You are connected.', 'success', true, true, 'short');
|
||||
processGameState(stateBucket.currentGameState, cookie, socket, true, true);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
window.location = '/not-found?reason=' + encodeURIComponent('invalid-access-code');
|
||||
}
|
||||
// else {
|
||||
// window.location = '/not-found?reason=' + encodeURIComponent('invalid-access-code');
|
||||
// }
|
||||
},
|
||||
|
||||
buildSpectatorList (people) {
|
||||
@@ -198,6 +197,7 @@ function processGameState (
|
||||
}
|
||||
lobby.populateHeader();
|
||||
lobby.populatePlayers();
|
||||
globals.LOBBY_EVENTS().forEach(e => socket.removeAllListeners(e));
|
||||
lobby.setSocketHandlers();
|
||||
if ((
|
||||
currentGameState.client.userType === globals.USER_TYPES.MODERATOR
|
||||
@@ -213,6 +213,7 @@ function processGameState (
|
||||
document.querySelector('#game-control-prompt')?.remove();
|
||||
}
|
||||
const inProgressGame = new InProgress('game-state-container', stateBucket, socket);
|
||||
globals.IN_PROGRESS_EVENTS().forEach(e => socket.removeAllListeners(e));
|
||||
inProgressGame.setSocketHandlers();
|
||||
inProgressGame.setUserView(currentGameState.client.userType);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user