mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
edit names
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const GameStateCurator = require('./GameStateCurator');
|
||||
const GameCreationRequest = require('../model/GameCreationRequest');
|
||||
const { EVENT_IDS, STATUS, USER_TYPES, GAME_PROCESS_COMMANDS, REDIS_CHANNELS } = require('../config/globals');
|
||||
const { EVENT_IDS, STATUS, USER_TYPES, GAME_PROCESS_COMMANDS, REDIS_CHANNELS, PRIMITIVES } = require('../config/globals');
|
||||
|
||||
const Events = [
|
||||
{
|
||||
@@ -55,6 +55,42 @@ const Events = [
|
||||
);
|
||||
}
|
||||
},
|
||||
{
|
||||
id: EVENT_IDS.CHANGE_NAME,
|
||||
stateChange: async (game, socketArgs, vars) => {
|
||||
const toChangeIndex = game.people.findIndex(
|
||||
(person) => person.id === socketArgs.personId
|
||||
);
|
||||
if (toChangeIndex >= 0) {
|
||||
if (vars.gameManager.isNameTaken(game, socketArgs.newName)) {
|
||||
vars.hasNameChanged = false;
|
||||
if (game.people[toChangeIndex].name.toLowerCase().trim() === socketArgs.newName.toLowerCase().trim()) {
|
||||
return;
|
||||
}
|
||||
vars.ackFn({ errorFlag: 1, message: 'This name is taken.' });
|
||||
} else if (socketArgs.newName.length > PRIMITIVES.MAX_PERSON_NAME_LENGTH) {
|
||||
vars.ackFn({ errorFlag: 1, message: 'Your new name is too long - the max is 30 characters.' });
|
||||
vars.hasNameChanged = false;
|
||||
} else if (socketArgs.newName.length === 0) {
|
||||
vars.ackFn({ errorFlag: 1, message: 'Your new name cannot be empty.' });
|
||||
vars.hasNameChanged = false;
|
||||
} else {
|
||||
game.people[toChangeIndex].name = socketArgs.newName;
|
||||
vars.ackFn({ errorFlag: 0, message: 'Name updated!' });
|
||||
vars.hasNameChanged = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
communicate: async (game, socketArgs, vars) => {
|
||||
if (vars.hasNameChanged) {
|
||||
vars.gameManager.namespace.in(game.accessCode).emit(
|
||||
EVENT_IDS.CHANGE_NAME,
|
||||
socketArgs.personId,
|
||||
socketArgs.newName
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
id: EVENT_IDS.UPDATE_GAME_ROLES,
|
||||
stateChange: async (game, socketArgs, vars) => {
|
||||
|
||||
@@ -182,7 +182,7 @@ class GameManager {
|
||||
if (matchingPerson) {
|
||||
return Promise.resolve(matchingPerson.cookie);
|
||||
}
|
||||
if (isNameTaken(game, name)) {
|
||||
if (this.isNameTaken(game, name)) {
|
||||
return Promise.reject({ status: 400, reason: 'This name is taken.' });
|
||||
}
|
||||
if (joinAsSpectator
|
||||
@@ -334,6 +334,11 @@ class GameManager {
|
||||
findPersonByField = (game, fieldName, value) => {
|
||||
return game.people.find(person => person[fieldName] === value);
|
||||
}
|
||||
|
||||
isNameTaken (game, name) {
|
||||
const processedName = name.toLowerCase().trim();
|
||||
return game.people.find((person) => person.name.toLowerCase().trim() === processedName);
|
||||
}
|
||||
}
|
||||
|
||||
function getRandomInt (max) {
|
||||
@@ -383,11 +388,6 @@ function createRandomId () {
|
||||
return id;
|
||||
}
|
||||
|
||||
function isNameTaken (game, name) {
|
||||
const processedName = name.toLowerCase().trim();
|
||||
return game.people.find((person) => person.name.toLowerCase().trim() === processedName);
|
||||
}
|
||||
|
||||
async function addSpectator (game, name, logger, namespace, eventManager, instanceId, refreshGame) {
|
||||
const spectator = new Person(
|
||||
createRandomId(),
|
||||
|
||||
Reference in New Issue
Block a user