mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
restart functionality
This commit is contained in:
@@ -183,11 +183,12 @@ class GameManager {
|
||||
this.activeGameRunner.activeGames[newAccessCode] = new Game(
|
||||
newAccessCode,
|
||||
globals.STATUS.LOBBY,
|
||||
initializePeopleForGame(gameParams.deck, moderator),
|
||||
initializePeopleForGame(gameParams.deck, moderator, this.shuffle),
|
||||
gameParams.deck,
|
||||
gameParams.hasTimer,
|
||||
moderator,
|
||||
gameParams.hasDedicatedModerator,
|
||||
moderator.id,
|
||||
gameParams.timerParams
|
||||
);
|
||||
this.activeGameRunner.activeGames[newAccessCode].createTime = new Date().toJSON();
|
||||
@@ -310,39 +311,65 @@ class GameManager {
|
||||
};
|
||||
|
||||
restartGame = async (game, namespace) => {
|
||||
// kill any outstanding timer threads
|
||||
if (this.activeGameRunner.timerThreads[game.accessCode]) {
|
||||
this.logger.info('KILLING STALE TIMER PROCESS FOR ' + accessCode);
|
||||
this.logger.info('KILLING STALE TIMER PROCESS FOR ' + game.accessCode);
|
||||
this.activeGameRunner.timerThreads[game.accessCode].kill();
|
||||
delete this.activeGameRunner.timerThreads[game.accessCode];
|
||||
}
|
||||
game.status = globals.STATUS.IN_PROGRESS;
|
||||
let cards = [];
|
||||
|
||||
// re-shuffle the deck
|
||||
const cards = [];
|
||||
for (const card of game.deck) {
|
||||
for (let i = 0; i < card.quantity; i ++) {
|
||||
cards.push(card);
|
||||
}
|
||||
}
|
||||
shuffle(cards);
|
||||
|
||||
this.shuffle(cards);
|
||||
|
||||
// make sure no players are marked as out or revealed, and give them new cards.
|
||||
for (let i = 0; i < game.people.length; i ++) {
|
||||
if (game.people[i].out) {
|
||||
game.people[i].out = false;
|
||||
}
|
||||
if (game.people[i].userType === globals.USER_TYPES.KILLED_PLAYER) {
|
||||
game.people[i].userType = globals.USER_TYPES.PLAYER;
|
||||
}
|
||||
game.people[i].revealed = false;
|
||||
game.people[i].gameRole = cards[i].role;
|
||||
game.people[i].gameRoleDescription = cards[i].description;
|
||||
game.people[i].alignment = cards[i].team;
|
||||
}
|
||||
|
||||
/* If the game was originally set up with a TEMP mod and the game has gone far enough to establish
|
||||
a DEDICATED mod, make the current mod a TEMP mod for the restart. */
|
||||
if (!game.hasDedicatedModerator && game.moderator.userType === globals.USER_TYPES.MODERATOR) {
|
||||
game.moderator.userType = globals.USER_TYPES.TEMPORARY_MODERATOR;
|
||||
}
|
||||
|
||||
/* If the game was originally set up with a DEDICATED moderator and the current mod is DIFFERENT from that mod
|
||||
(i.e. they transferred their powers at some point), check if the current mod was once a player (i.e. they have
|
||||
a game role). If they were once a player, make them a temp mod for the restart. Otherwise, they were a
|
||||
spectator, and we want to leave them as a dedicated moderator.
|
||||
*/
|
||||
if (game.hasDedicatedModerator && game.moderator.id !== game.originalModeratorId) {
|
||||
if (game.moderator.gameRole) {
|
||||
game.moderator.userType = globals.USER_TYPES.TEMPORARY_MODERATOR;
|
||||
} else {
|
||||
game.moderator.userType = globals.USER_TYPES.MODERATOR;
|
||||
}
|
||||
}
|
||||
|
||||
// start the new game
|
||||
game.status = globals.STATUS.IN_PROGRESS;
|
||||
if (game.hasTimer) {
|
||||
game.timerParams.paused = true;
|
||||
this.activeGameRunner.runGame(game, namespace);
|
||||
}
|
||||
/* If the game was originally set up with a temporary moderator and the game has gone far enough to establish
|
||||
a dedicated moderator, make the current moderator a temp mod for the restarting of the same game. */
|
||||
if (!game.hasDedicatedModerator && game.moderator.userType !== globals.USER_TYPES.TEMPORARY_MODERATOR) {
|
||||
game.moderator.userType = globals.USER_TYPES.TEMPORARY_MODERATOR;
|
||||
}
|
||||
|
||||
namespace.in(game.accessCode).emit(globals.CLIENT_COMMANDS.START_GAME);
|
||||
}
|
||||
};
|
||||
|
||||
handleRequestForGameState = async (namespace, logger, gameRunner, accessCode, personCookie, ackFn, clientSocket) => {
|
||||
const game = gameRunner.activeGames[accessCode];
|
||||
@@ -391,6 +418,23 @@ class GameManager {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
-- To shuffle an array a of n elements (indices 0..n-1):
|
||||
for i from n−1 downto 1 do
|
||||
j ← random integer such that 0 ≤ j ≤ i
|
||||
exchange a[j] and a[i]
|
||||
*/
|
||||
shuffle = (array) => {
|
||||
for (let i = array.length - 1; i > 0; i --) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
const temp = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = temp;
|
||||
}
|
||||
|
||||
return array;
|
||||
};
|
||||
}
|
||||
|
||||
function getRandomInt (max) {
|
||||
@@ -404,9 +448,9 @@ function initializeModerator (name, hasDedicatedModerator) {
|
||||
return new Person(createRandomId(), createRandomId(), name, userType);
|
||||
}
|
||||
|
||||
function initializePeopleForGame (uniqueCards, moderator) {
|
||||
function initializePeopleForGame (uniqueCards, moderator, shuffle) {
|
||||
const people = [];
|
||||
let cards = [];
|
||||
const cards = [];
|
||||
let numberOfRoles = 0;
|
||||
for (const card of uniqueCards) {
|
||||
for (let i = 0; i < card.quantity; i ++) {
|
||||
@@ -446,23 +490,6 @@ function initializePeopleForGame (uniqueCards, moderator) {
|
||||
return people;
|
||||
}
|
||||
|
||||
/*
|
||||
-- To shuffle an array a of n elements (indices 0..n-1):
|
||||
for i from n−1 downto 1 do
|
||||
j ← random integer such that 0 ≤ j ≤ i
|
||||
exchange a[j] and a[i]
|
||||
*/
|
||||
function shuffle(array) {
|
||||
for (let i = array.length - 1; i > 0; i --) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
const temp = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = temp;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
function createRandomId () {
|
||||
let id = '';
|
||||
for (let i = 0; i < globals.USER_SIGNATURE_LENGTH; i ++) {
|
||||
|
||||
Reference in New Issue
Block a user