add bots to a game

This commit is contained in:
AlecM33
2023-01-30 12:40:28 -05:00
parent e362f6bdb2
commit 79309f5062
16 changed files with 106 additions and 43 deletions

View File

@@ -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',

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;