mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
add bots to a game
This commit is contained in:
@@ -100,7 +100,9 @@ const globals = {
|
||||
PLAYER: 'player',
|
||||
TEMPORARY_MODERATOR: 'temp mod',
|
||||
KILLED_PLAYER: 'killed',
|
||||
SPECTATOR: 'spectator'
|
||||
KILLED_BOT: 'killed bot',
|
||||
SPECTATOR: 'spectator',
|
||||
BOT: 'bot'
|
||||
},
|
||||
ERROR_MESSAGE: {
|
||||
GAME_IS_FULL: 'This game is full',
|
||||
|
||||
@@ -9,7 +9,8 @@ class Game {
|
||||
hasDedicatedModerator,
|
||||
originalModeratorId,
|
||||
createTime,
|
||||
timerParams = null
|
||||
timerParams = null,
|
||||
isTestGame = false
|
||||
) {
|
||||
this.accessCode = accessCode;
|
||||
this.status = status;
|
||||
@@ -26,7 +27,7 @@ class Game {
|
||||
this.previousModeratorId = null;
|
||||
this.createTime = createTime;
|
||||
this.timerParams = timerParams;
|
||||
this.isFull = this.gameSize === 1 && !this.hasDedicatedModerator;
|
||||
this.isFull = (this.gameSize === 1 && !this.hasDedicatedModerator) || isTestGame;
|
||||
this.timeRemaining = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,19 @@ class GameCreationRequest {
|
||||
hasTimer,
|
||||
timerParams,
|
||||
moderatorName,
|
||||
hasDedicatedModerator
|
||||
hasDedicatedModerator,
|
||||
isTestGame
|
||||
) {
|
||||
this.deck = deck;
|
||||
this.hasTimer = hasTimer;
|
||||
this.timerParams = timerParams;
|
||||
this.moderatorName = moderatorName;
|
||||
this.hasDedicatedModerator = hasDedicatedModerator;
|
||||
this.isTestGame = isTestGame;
|
||||
}
|
||||
|
||||
static validate = (gameParams) => {
|
||||
const expectedKeys = ['deck', 'hasTimer', 'timerParams', 'moderatorName', 'hasDedicatedModerator'];
|
||||
const expectedKeys = ['deck', 'hasTimer', 'timerParams', 'moderatorName', 'hasDedicatedModerator', 'isTestGame'];
|
||||
if (gameParams === null
|
||||
|| typeof gameParams !== 'object'
|
||||
|| expectedKeys.some((key) => !Object.keys(gameParams).includes(key))
|
||||
@@ -31,6 +33,7 @@ class GameCreationRequest {
|
||||
|
||||
function valid (gameParams) {
|
||||
return typeof gameParams.hasTimer === 'boolean'
|
||||
&& typeof gameParams.isTestGame === 'boolean'
|
||||
&& typeof gameParams.hasDedicatedModerator === 'boolean'
|
||||
&& typeof gameParams.moderatorName === 'string'
|
||||
&& gameParams.moderatorName.length > 0
|
||||
|
||||
@@ -86,7 +86,9 @@ const Events = [
|
||||
stateChange: async (game, socketArgs, vars) => {
|
||||
const person = game.people.find((person) => person.id === socketArgs.personId);
|
||||
if (person && !person.out) {
|
||||
person.userType = globals.USER_TYPES.KILLED_PLAYER;
|
||||
person.userType = person.userType === globals.USER_TYPES.BOT
|
||||
? globals.USER_TYPES.KILLED_BOT
|
||||
: globals.USER_TYPES.KILLED_PLAYER;
|
||||
person.out = true;
|
||||
person.killed = true;
|
||||
}
|
||||
|
||||
@@ -59,8 +59,10 @@ class GameManager {
|
||||
gameParams.hasTimer,
|
||||
gameParams.timerParams,
|
||||
gameParams.moderatorName,
|
||||
gameParams.hasDedicatedModerator
|
||||
gameParams.hasDedicatedModerator,
|
||||
gameParams.isTestGame
|
||||
);
|
||||
console.log(req.isTestGame);
|
||||
const newAccessCode = await this.generateAccessCode(globals.ACCESS_CODE_CHAR_POOL);
|
||||
if (newAccessCode === null) {
|
||||
return Promise.reject(globals.ERROR_MESSAGE.NO_UNIQUE_ACCESS_CODE);
|
||||
@@ -76,14 +78,15 @@ class GameManager {
|
||||
const newGame = new Game(
|
||||
newAccessCode,
|
||||
globals.STATUS.LOBBY,
|
||||
initializePeopleForGame(req.deck, moderator, this.shuffle),
|
||||
initializePeopleForGame(req.deck, moderator, this.shuffle, req.isTestGame),
|
||||
req.deck,
|
||||
req.hasTimer,
|
||||
moderator.id,
|
||||
req.hasDedicatedModerator,
|
||||
moderator.id,
|
||||
new Date().toJSON(),
|
||||
req.timerParams
|
||||
req.timerParams,
|
||||
req.isTestGame
|
||||
);
|
||||
await this.eventManager.publisher.set(newAccessCode, JSON.stringify(newGame), {
|
||||
EX: globals.STALE_GAME_SECONDS
|
||||
@@ -242,6 +245,10 @@ class GameManager {
|
||||
game.people[i].userType = globals.USER_TYPES.PLAYER;
|
||||
game.people[i].out = false;
|
||||
}
|
||||
if (game.people[i].userType === globals.USER_TYPES.KILLED_BOT) {
|
||||
game.people[i].userType = globals.USER_TYPES.BOT;
|
||||
game.people[i].out = false;
|
||||
}
|
||||
game.people[i].revealed = false;
|
||||
game.people[i].killed = false;
|
||||
if (game.people[i].gameRole) {
|
||||
@@ -314,7 +321,7 @@ function initializeModerator (name, hasDedicatedModerator) {
|
||||
return new Person(createRandomId(), createRandomId(), name, userType);
|
||||
}
|
||||
|
||||
function initializePeopleForGame (uniqueRoles, moderator, shuffle) {
|
||||
function initializePeopleForGame (uniqueRoles, moderator, shuffle, isTestGame) {
|
||||
const people = [];
|
||||
|
||||
const cards = [];
|
||||
@@ -335,10 +342,11 @@ function initializePeopleForGame (uniqueRoles, moderator, shuffle) {
|
||||
createRandomId(),
|
||||
createRandomId(),
|
||||
UsernameGenerator.generate(),
|
||||
globals.USER_TYPES.PLAYER,
|
||||
isTestGame ? globals.USER_TYPES.BOT : globals.USER_TYPES.PLAYER,
|
||||
cards[j].role,
|
||||
cards[j].description,
|
||||
cards[j].team
|
||||
cards[j].team,
|
||||
isTestGame
|
||||
);
|
||||
person.customRole = cards[j].custom;
|
||||
person.hasEnteredName = false;
|
||||
|
||||
Reference in New Issue
Block a user