mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
swapping moderator
This commit is contained in:
@@ -10,7 +10,8 @@ const globals = {
|
||||
RESUME_TIMER: 'resumeTimer',
|
||||
GET_TIME_REMAINING: 'getTimeRemaining',
|
||||
KILL_PLAYER: 'killPlayer',
|
||||
REVEAL_PLAYER: 'revealPlayer'
|
||||
REVEAL_PLAYER: 'revealPlayer',
|
||||
TRANSFER_MODERATOR: 'transferModerator'
|
||||
},
|
||||
STATUS: {
|
||||
LOBBY: "lobby",
|
||||
|
||||
@@ -9,6 +9,7 @@ class Game {
|
||||
this.timerParams = timerParams;
|
||||
this.isFull = false;
|
||||
this.timeRemaining = null;
|
||||
this.spectators = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,33 @@ class GameManager {
|
||||
})
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
socket.on(globals.CLIENT_COMMANDS.TRANSFER_MODERATOR, (accessCode, personId) => {
|
||||
let game = this.activeGameRunner.activeGames[accessCode];
|
||||
if (game) {
|
||||
let person = game.people.find((person) => person.id === personId)
|
||||
if (!person) {
|
||||
person = game.spectators.find((spectator) => spectator.id === personId)
|
||||
}
|
||||
if (person && (person.out || person.userType === globals.USER_TYPES.SPECTATOR)) {
|
||||
this.logger.debug('game ' + accessCode + ': transferring mod powers to ' + person.name);
|
||||
if (game.people.includes(game.moderator)) { // the current moderator was at one point a dealt-in player.
|
||||
game.moderator.userType = globals.USER_TYPES.KILLED_PLAYER; // restore their state from before being made mod.
|
||||
} else {
|
||||
game.moderator.userType = globals.USER_TYPES.SPECTATOR;
|
||||
if (!game.spectators.includes(game.moderator)) {
|
||||
game.spectators.push(game.moderator);
|
||||
}
|
||||
if (game.spectators.includes(person)) {
|
||||
game.spectators.splice(game.spectators.indexOf(person), 1);
|
||||
}
|
||||
}
|
||||
person.userType = globals.USER_TYPES.MODERATOR;
|
||||
game.moderator = person;
|
||||
namespace.in(accessCode).emit(globals.EVENTS.SYNC_GAME_STATE);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -265,7 +292,10 @@ function handleRequestForGameState(namespace, logger, gameRunner, accessCode, pe
|
||||
const game = gameRunner.activeGames[accessCode];
|
||||
if (game) {
|
||||
let matchingPerson = game.people.find((person) => person.cookie === personCookie);
|
||||
if (!matchingPerson && game.moderator.cookie === personCookie) {
|
||||
if (!matchingPerson) {
|
||||
matchingPerson = game.spectators.find((spectator) => spectator.cookie = personCookie);
|
||||
}
|
||||
if (game.moderator.cookie === personCookie) {
|
||||
matchingPerson = game.moderator;
|
||||
}
|
||||
if (matchingPerson) {
|
||||
|
||||
@@ -34,7 +34,6 @@ function getGameStateBasedOnPermissions(game, person, gameRunner) {
|
||||
people: game.people
|
||||
.filter((person) => {
|
||||
return person.assigned === true
|
||||
&& (person.userType !== globals.USER_TYPES.MODERATOR && person.userType !== globals.USER_TYPES.TEMPORARY_MODERATOR)
|
||||
})
|
||||
.map((filteredPerson) => mapPerson(filteredPerson)),
|
||||
timerParams: game.timerParams,
|
||||
@@ -49,7 +48,8 @@ function getGameStateBasedOnPermissions(game, person, gameRunner) {
|
||||
deck: game.deck,
|
||||
people: mapPeopleForModerator(game.people, client),
|
||||
timerParams: game.timerParams,
|
||||
isFull: game.isFull
|
||||
isFull: game.isFull,
|
||||
spectators: game.spectators
|
||||
}
|
||||
case globals.USER_TYPES.TEMPORARY_MODERATOR:
|
||||
return {
|
||||
@@ -58,19 +58,38 @@ function getGameStateBasedOnPermissions(game, person, gameRunner) {
|
||||
moderator: mapPerson(game.moderator),
|
||||
client: client,
|
||||
deck: game.deck,
|
||||
people: mapPeopleForTempModerator(game.people, client),
|
||||
people: game.people
|
||||
.filter((person) => {
|
||||
return person.assigned === true
|
||||
})
|
||||
.map((filteredPerson) => mapPerson(filteredPerson)),
|
||||
timerParams: game.timerParams,
|
||||
isFull: game.isFull
|
||||
}
|
||||
case globals.USER_TYPES.SPECTATOR:
|
||||
return {
|
||||
accessCode: game.accessCode,
|
||||
status: game.status,
|
||||
moderator: mapPerson(game.moderator),
|
||||
client: client,
|
||||
deck: game.deck,
|
||||
people: game.people
|
||||
.filter((person) => {
|
||||
return person.assigned === true
|
||||
})
|
||||
.map((filteredPerson) => mapPerson(filteredPerson)),
|
||||
timerParams: game.timerParams,
|
||||
isFull: game.isFull,
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function mapPeopleForModerator(people, client) {
|
||||
function mapPeopleForModerator(people) {
|
||||
return people
|
||||
.filter((person) => {
|
||||
return person.assigned === true && person.cookie !== client.cookie
|
||||
return person.assigned === true
|
||||
})
|
||||
.map((person) => ({
|
||||
name: person.name,
|
||||
@@ -84,20 +103,6 @@ function mapPeopleForModerator(people, client) {
|
||||
}));
|
||||
}
|
||||
|
||||
function mapPeopleForTempModerator(people, client) {
|
||||
return people
|
||||
.filter((person) => {
|
||||
return person.assigned === true && person.cookie !== client.cookie
|
||||
})
|
||||
.map((person) => ({
|
||||
name: person.name,
|
||||
id: person.id,
|
||||
userType: person.userType,
|
||||
out: person.out,
|
||||
revealed: person.revealed
|
||||
}));
|
||||
}
|
||||
|
||||
function mapPerson(person) {
|
||||
if (person.revealed) {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user