successful editing of role list

This commit is contained in:
AlecM33
2023-08-03 16:52:03 -04:00
parent 0d82227824
commit 24ae53209f
19 changed files with 260 additions and 103 deletions

View File

@@ -56,7 +56,8 @@ const globals = {
UPDATE_SOCKET: 'updateSocket',
ASSIGN_DEDICATED_MOD: 'assignDedicatedMod',
TIMER_EVENT: 'timerEvent',
KICK_PERSON: 'kickPerson'
KICK_PERSON: 'kickPerson',
UPDATE_GAME_ROLES: 'updateGameRoles'
},
SYNCABLE_EVENTS: function () {
return [
@@ -76,7 +77,8 @@ const globals = {
this.EVENT_IDS.RESUME_TIMER,
this.EVENT_IDS.PAUSE_TIMER,
this.EVENT_IDS.END_TIMER,
this.EVENT_IDS.KICK_PERSON
this.EVENT_IDS.KICK_PERSON,
this.EVENT_IDS.UPDATE_GAME_ROLES
];
},
TIMER_EVENTS: function () {

View File

@@ -29,6 +29,33 @@ class GameCreationRequest {
return Promise.resolve();
}
};
static deckIsValid = (deck) => {
if (Array.isArray(deck) && deck.length > 0) {
for (const entry of deck) {
if (entry !== null
&& typeof entry === 'object'
&& typeof entry.role === 'string'
&& entry.role.length > 0
&& entry.role.length <= globals.MAX_CUSTOM_ROLE_NAME_LENGTH
&& typeof entry.team === 'string'
&& (entry.team === globals.ALIGNMENT.GOOD || entry.team === globals.ALIGNMENT.EVIL)
&& typeof entry.description === 'string'
&& entry.description.length > 0
&& entry.description.length <= globals.MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH
&& (!entry.custom || typeof entry.custom === 'boolean')
&& typeof entry.quantity === 'number'
&& entry.quantity >= 1
&& entry.quantity <= 50
) {
continue;
}
return false;
}
return true;
}
return false;
}
}
function valid (gameParams) {
@@ -39,7 +66,7 @@ function valid (gameParams) {
&& gameParams.moderatorName.length > 0
&& gameParams.moderatorName.length <= 30
&& timerParamsAreValid(gameParams.hasTimer, gameParams.timerParams)
&& deckIsValid(gameParams.deck);
&& GameCreationRequest.deckIsValid(gameParams.deck);
}
function timerParamsAreValid (hasTimer, timerParams) {
@@ -58,31 +85,4 @@ function timerParamsAreValid (hasTimer, timerParams) {
}
}
function deckIsValid (deck) {
if (Array.isArray(deck) && deck.length > 0) {
for (const entry of deck) {
if (entry !== null
&& typeof entry === 'object'
&& typeof entry.role === 'string'
&& entry.role.length > 0
&& entry.role.length <= globals.MAX_CUSTOM_ROLE_NAME_LENGTH
&& typeof entry.team === 'string'
&& (entry.team === globals.ALIGNMENT.GOOD || entry.team === globals.ALIGNMENT.EVIL)
&& typeof entry.description === 'string'
&& entry.description.length > 0
&& entry.description.length <= globals.MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH
&& (!entry.custom || typeof entry.custom === 'boolean')
&& typeof entry.quantity === 'number'
&& entry.quantity >= 1
&& entry.quantity <= 50
) {
continue;
}
return false;
}
return true;
}
return false;
}
module.exports = GameCreationRequest;

View File

@@ -1,6 +1,7 @@
const globals = require('../config/globals');
const GameStateCurator = require('./GameStateCurator');
const UsernameGenerator = require('./UsernameGenerator');
const GameCreationRequest = require('../model/GameCreationRequest');
const EVENT_IDS = globals.EVENT_IDS;
const Events = [
@@ -57,6 +58,28 @@ const Events = [
);
}
},
{
id: EVENT_IDS.UPDATE_GAME_ROLES,
stateChange: async (game, socketArgs, vars) => {
if (GameCreationRequest.deckIsValid(socketArgs.deck)) {
game.deck = socketArgs.deck;
game.gameSize = socketArgs.deck.reduce(
(accumulator, currentValue) => accumulator + currentValue.quantity,
0
);
}
},
communicate: async (game, socketArgs, vars) => {
if (vars.ackFn) {
vars.ackFn();
}
vars.gameManager.namespace.in(game.accessCode).emit(
EVENT_IDS.UPDATE_GAME_ROLES,
game.deck,
game.gameSize
);
}
},
{
id: EVENT_IDS.ADD_SPECTATOR,
stateChange: async (game, socketArgs, vars) => {

View File

@@ -302,6 +302,10 @@ class GameManager {
return array;
};
deal = () => {
}
isGameFull = (game) => {
return !game.people.find((person) => person.userType === globals.USER_TYPES.PLAYER && person.assigned === false);
}