mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
change spectator option to checkbox, update tutorial images
This commit is contained in:
@@ -4,7 +4,7 @@ const globals = {
|
||||
ACCESS_CODE_GENERATION_ATTEMPTS: 50,
|
||||
CLOCK_TICK_INTERVAL_MILLIS: 100,
|
||||
MAX_CUSTOM_ROLE_NAME_LENGTH: 50,
|
||||
MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH: 500,
|
||||
MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH: 1000,
|
||||
ALIGNMENT: {
|
||||
GOOD: 'good',
|
||||
EVIL: 'evil'
|
||||
|
||||
85
server/model/GameCreationRequest.js
Normal file
85
server/model/GameCreationRequest.js
Normal file
@@ -0,0 +1,85 @@
|
||||
const globals = require("../config/globals");
|
||||
|
||||
class GameCreationRequest {
|
||||
constructor (
|
||||
deck,
|
||||
hasTimer,
|
||||
timerParams,
|
||||
moderatorName,
|
||||
hasDedicatedModerator
|
||||
) {
|
||||
this.deck = deck;
|
||||
this.hasTimer = hasTimer;
|
||||
this.timerParams = timerParams;
|
||||
this.moderatorName = moderatorName;
|
||||
this.hasDedicatedModerator = hasDedicatedModerator;
|
||||
}
|
||||
|
||||
static validate = (gameParams) => {
|
||||
const expectedKeys = ['deck', 'hasTimer', 'timerParams', 'moderatorName', 'hasDedicatedModerator'];
|
||||
if (gameParams === null
|
||||
|| typeof gameParams !== 'object'
|
||||
|| expectedKeys.some((key) => !Object.keys(gameParams).includes(key))
|
||||
|| !valid(gameParams)
|
||||
) {
|
||||
return Promise.reject(globals.ERROR_MESSAGE.BAD_CREATE_REQUEST);
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function valid (gameParams) {
|
||||
return typeof gameParams.hasTimer === 'boolean'
|
||||
&& typeof gameParams.hasDedicatedModerator === 'boolean'
|
||||
&& typeof gameParams.moderatorName === 'string'
|
||||
&& gameParams.moderatorName.length > 0
|
||||
&& gameParams.moderatorName.length <= 30
|
||||
&& timerParamsAreValid(gameParams.hasTimer, gameParams.timerParams)
|
||||
&& deckIsValid(gameParams.deck);
|
||||
}
|
||||
|
||||
function timerParamsAreValid (hasTimer, timerParams) {
|
||||
if (hasTimer === false) {
|
||||
return timerParams === null;
|
||||
} else {
|
||||
if (timerParams === null || typeof timerParams !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (timerParams.hours === null && timerParams.minutes > 0 && timerParams.minutes < 60)
|
||||
|| (timerParams.minutes === null && timerParams.hours > 0 && timerParams.hours < 6)
|
||||
|| (timerParams.hours === 0 && timerParams.minutes > 0 && timerParams.minutes < 60)
|
||||
|| (timerParams.minutes === 0 && timerParams.hours > 0 && timerParams.hours < 6)
|
||||
|| (timerParams.hours > 0 && timerParams.hours < 6 && timerParams.minutes >= 0 && timerParams.minutes < 60);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -3,6 +3,7 @@ const Game = require('../model/Game');
|
||||
const Person = require('../model/Person');
|
||||
const GameStateCurator = require('./GameStateCurator');
|
||||
const UsernameGenerator = require('./UsernameGenerator');
|
||||
const GameCreationRequest = require("../model/GameCreationRequest");
|
||||
|
||||
class GameManager {
|
||||
constructor (logger, environment, activeGameRunner) {
|
||||
@@ -22,41 +23,43 @@ class GameManager {
|
||||
};
|
||||
|
||||
createGame = (gameParams) => {
|
||||
const expectedKeys = ['deck', 'hasTimer', 'timerParams', 'moderatorName', 'hasDedicatedModerator'];
|
||||
if (typeof gameParams !== 'object'
|
||||
|| expectedKeys.some((key) => !Object.keys(gameParams).includes(key))
|
||||
|| !valid(gameParams)
|
||||
) {
|
||||
this.logger.error('Tried to create game with invalid options.');
|
||||
return Promise.reject(globals.ERROR_MESSAGE.BAD_CREATE_REQUEST);
|
||||
} else {
|
||||
return GameCreationRequest.validate(gameParams).then(() => {
|
||||
let req = new GameCreationRequest(
|
||||
gameParams.deck,
|
||||
gameParams.hasTimer,
|
||||
gameParams.timerParams,
|
||||
gameParams.moderatorName,
|
||||
gameParams.hasDedicatedModerator
|
||||
);
|
||||
this.pruneStaleGames();
|
||||
const newAccessCode = this.generateAccessCode(globals.ACCESS_CODE_CHAR_POOL);
|
||||
if (newAccessCode === null) {
|
||||
return Promise.reject(globals.ERROR_MESSAGE.NO_UNIQUE_ACCESS_CODE);
|
||||
}
|
||||
const moderator = initializeModerator(gameParams.moderatorName, gameParams.hasDedicatedModerator);
|
||||
const moderator = initializeModerator(req.moderatorName, req.hasDedicatedModerator);
|
||||
moderator.assigned = true;
|
||||
if (gameParams.timerParams !== null) {
|
||||
gameParams.timerParams.paused = false;
|
||||
if (req.timerParams !== null) {
|
||||
req.timerParams.paused = false;
|
||||
}
|
||||
const newGame = new Game(
|
||||
newAccessCode,
|
||||
globals.STATUS.LOBBY,
|
||||
initializePeopleForGame(gameParams.deck, moderator, this.shuffle),
|
||||
gameParams.deck,
|
||||
gameParams.hasTimer,
|
||||
initializePeopleForGame(req.deck, moderator, this.shuffle),
|
||||
req.deck,
|
||||
req.hasTimer,
|
||||
moderator,
|
||||
gameParams.hasDedicatedModerator,
|
||||
req.hasDedicatedModerator,
|
||||
moderator.id,
|
||||
new Date().toJSON(),
|
||||
gameParams.timerParams
|
||||
req.timerParams
|
||||
);
|
||||
|
||||
this.activeGameRunner.activeGames.set(newAccessCode, newGame);
|
||||
|
||||
return Promise.resolve({ accessCode: newAccessCode, cookie: moderator.cookie, environment: this.environment });
|
||||
}
|
||||
}).catch((message) => {
|
||||
return Promise.reject(message);
|
||||
});
|
||||
};
|
||||
|
||||
startGame = (game, namespace) => {
|
||||
@@ -494,54 +497,6 @@ function getGameSize (cards) {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
function valid (gameParams) {
|
||||
return typeof gameParams.hasTimer === 'boolean'
|
||||
&& typeof gameParams.hasDedicatedModerator === 'boolean'
|
||||
&& typeof gameParams.moderatorName === 'string'
|
||||
&& gameParams.moderatorName.length > 0
|
||||
&& gameParams.moderatorName.length <= 30
|
||||
&& timerParamsAreValid(gameParams.hasTimer, gameParams.timerParams)
|
||||
&& deckIsValid(gameParams.deck);
|
||||
}
|
||||
|
||||
function timerParamsAreValid (hasTimer, timerParams) {
|
||||
if (hasTimer === false) {
|
||||
return timerParams === null;
|
||||
} else {
|
||||
if (timerParams === null || typeof timerParams !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (timerParams.hours === null && timerParams.minutes > 0 && timerParams.minutes < 60)
|
||||
|| (timerParams.minutes === null && timerParams.hours > 0 && timerParams.hours < 6)
|
||||
|| (timerParams.hours === 0 && timerParams.minutes > 0 && timerParams.minutes < 60)
|
||||
|| (timerParams.minutes === 0 && timerParams.hours > 0 && timerParams.hours < 6)
|
||||
|| (timerParams.hours > 0 && timerParams.hours < 6 && timerParams.minutes >= 0 && timerParams.minutes < 60);
|
||||
}
|
||||
}
|
||||
|
||||
function deckIsValid (deck) {
|
||||
if (Array.isArray(deck) && deck.length > 0) {
|
||||
for (const entry of deck) {
|
||||
if (entry !== null && typeof entry === 'object') {
|
||||
if (typeof entry.role !== 'string' || 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 > globals.MAX_CUSTOM_ROLE_DESCRIPTION_LENGTH
|
||||
|| (entry.custom && typeof entry.custom !== 'boolean')
|
||||
|| typeof entry.quantity !== 'number' || entry.quantity < 1 || entry.quantity > 50
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function addSpectator (game, name, logger, namespace) {
|
||||
const spectator = new Person(
|
||||
createRandomId(),
|
||||
|
||||
Reference in New Issue
Block a user