MafiaGame

sockMafia Game class

Author: Accalia
License: MIT

MafiaGame~MafiaGame

MafiaGame class

Kind: inner class of MafiaGame

new MafiaGame(data, dao)

Mafia Game constructor. Creates a new MafiaGame instance

Param Type Description
data object Persisted game data
data.name string Name of the game
data.topicId number Id of the topic the game is played in
data.day number Current day of the game
data.phases Array.<string> Configured day phases
data.phase string Current phase of day
data.isActive boolean Is Game Active?
data.livePlayers object.<string, object> Map of live players by userslug
data.deadPlayers object.<string, object> Map of dead players by userslug
data.moderators object.<string, object> Map of moderators by userslug
data.actions Array.<object> List of actions performed in this game
data.values object Key value map of custom values stored as part of the game
dao MafiaDao MafiaDao this game is a part of

mafiaGame.topicId ⇒ number

Get the topic id this game is playing in

Kind: instance property of MafiaGame
Returns: number - the topicId the game is playing in

mafiaGame.name ⇒ string

Get the game name

Kind: instance property of MafiaGame
Returns: string - The name of the game

mafiaGame.day ⇒ number

Get the day the game is in

Kind: instance property of MafiaGame
Returns: number - The current day

mafiaGame.phase ⇒ string

Gets the current phase of day the game is in

Kind: instance property of MafiaGame
Returns: string - The current game phase

mafiaGame.isDay ⇒ boolean

Gets wether the game is in a day phase.

This is simply asking "does the current phase contain day?"

Kind: instance property of MafiaGame
Returns: boolean - true if the current phase contains day, false otherwise

mafiaGame.isNight ⇒ boolean

Gets wether the game is in a night phase.

This is simply asking "does the current phase contain night?"

Kind: instance property of MafiaGame
Returns: boolean - true if the current phase contains night, false otherwise

mafiaGame.isActive ⇒ boolean

gets wether the game is active

Kind: instance property of MafiaGame
Returns: boolean - true if the game is active, false otherwise.

mafiaGame.livePlayers ⇒ Array.<MafiaUser>

Get a randomly ordered list of living players

Kind: instance property of MafiaGame
Returns: Array.<MafiaUser> - A randomly ordered list of living players

mafiaGame.deadPlayers ⇒ Array.<MafiaUser>

Get a randomly ordered list of dead players

Kind: instance property of MafiaGame
Returns: Array.<MafiaUser> - A randomly ordered list of dead players

mafiaGame.allPlayers ⇒ Array.<MafiaUser>

Get a randomly ordered list of all players

Kind: instance property of MafiaGame
Returns: Array.<MafiaUser> - A randomly ordered list of all players

mafiaGame.moderators ⇒ Array.<MafiaUser>

Get an ordered list of game moderators

Kind: instance property of MafiaGame
Returns: Array.<MafiaUser> - An ordered list of game moderators

mafiaGame.aliases ⇒ Array.<string>

Get a list of the aliases for this game

Kind: instance property of MafiaGame
Returns: Array.<string> - A list of aliases for this game.

mafiaGame.setActive() ⇒ Promise.<Game>

sets the game to active state

Kind: instance method of MafiaGame
Returns: Promise.<Game> - resloves to self on completion

mafiaGame.setInactive() ⇒ Promise.<Game>

sets the game to active state

Kind: instance method of MafiaGame
Returns: Promise.<Game> - resloves to self on completion

mafiaGame.save() ⇒ Promise.<MafiaGame>

Save game data to disk

Kind: instance method of MafiaGame
Returns: Promise.<MafiaGame> - Resolves to self on completion.

mafiaGame.addPlayer(username) ⇒ Promise.<MafiaUser>

Add a new player to the list of living players

Kind: instance method of MafiaGame
Returns: Promise.<MafiaUser> - Resolves to added user, Rejects if user already exists in game

Param Type Description
username string Username of the new player

mafiaGame.addModerator(username) ⇒ Promise.<MafiaUser>

Add a moderator to the game

Kind: instance method of MafiaGame
Returns: Promise.<MafiaUser> - Resolves to added moderator, Rejects if moderator already exists in game

Param Type Description
username string Username of the game moderator

mafiaGame.getModerator(mod) ⇒ MafiaUser

Get a game moderator

Kind: instance method of MafiaGame
Returns: MafiaUser - Requested MafiaUser, null if no matching user found

Param Type Description
mod string | MafiaUser Moderator to fetch

mafiaGame.getPlayer(user) ⇒ MafiaUser

Get a player

Kind: instance method of MafiaGame
Returns: MafiaUser - Requested User
Throws:

  • Error.<E_USER_NOT_EXIST> Throes error if requested user is not part of the game
Param Type Description
user string | MafiaUser User to fetch

mafiaGame.killPlayer(user) ⇒ Promise.<MafiaUser>

Kill a player in the game

Kind: instance method of MafiaGame
Returns: Promise.<MafiaUser> - Resolves to terminated user, Rejects if target user was not alive to be killed.

Param Type Description
user string | MafiaUser User to kill

mafiaGame.resurectPlayer(user) ⇒ Promise.<MafiaUser>

Return a player to the laand of the living

Kind: instance method of MafiaGame
Returns: Promise.<MafiaUser> - resolves to the resurected user, rejects if user was not dead to resurect.

Param Type Description
user string | MafiaUser Player to Lazarus

mafiaGame.nextPhase() ⇒ Promise.<MafiaGame>

Advance to the next phase of day

Advances automatically to the next day if in the last phase of the day

Kind: instance method of MafiaGame
Returns: Promise.<MafiaGame> - Resolves to self after advancing phase.

mafiaGame.newDay() ⇒ Promise.<MafiaGame>

Advance to the first phase of the next day

This will skip any unplayed phases in the current day.

Kind: instance method of MafiaGame
Returns: Promise.<MafiaGame> - Resolves to self after advancing day

mafiaGame.getAction(actor, [target], [type], [actionToken], [day], [includeRevokedActions]) ⇒ MafiaAction

Get a specific game action

Kind: instance method of MafiaGame
Returns: MafiaAction - Action matching the provided query, null if no action matched query

Param Type Default Description
actor string | MafiaUser Actor for the requested action
[target] string | MafiaUser Target for the requested action
[type] string "'vote'" Action type of the requested action
[actionToken] string ActionToken of the requested action
[day] number this.day Day of the game for the requested action
[includeRevokedActions] boolean true If true include actions that have been revoked

mafiaGame.getActionOfType([type], [target], [actionToken], [day], [includeRevokedActions]) ⇒ MafiaAction

Get latest game action of the given type

Kind: instance method of MafiaGame
Returns: MafiaAction - Action matching the provided query, null if no action matched query

Param Type Default Description
[type] string "'vote'" Action type of the requested action
[target] string | MafiaUser Target for the requested action
[actionToken] string ActionToken of the requested action
[day] number this.day Day of the game for the requested action
[includeRevokedActions] boolean false If true include actions that have been revoked

mafiaGame.getActions([type], [day], [includeDeadPlayers]) ⇒ Array.<MafiaAction>

Get actions for a particular day of a particular type

Kind: instance method of MafiaGame
Returns: Array.<MafiaAction> - List of actions that match the provided query

Param Type Default Description
[type] string "'vote'" Action type to retrieve
[day] number this.day Day to retrieve actions for
[includeDeadPlayers] boolean false Include actions cast by players who are now dead?

mafiaGame.registerAction(postId, actor, [target], [type], [actionToken]) ⇒ Promise.<MafiaAction>

Register a new action

Automatically revokes matching prior action by actor

Kind: instance method of MafiaGame
Returns: Promise.<MafiaAction> - Resolves to created action

Param Type Default Description
postId number Id of the post the ation was cast in
actor string | MafiaUser Actor for the Action
[target] string | MafiaAction null Target for the Action
[type] string "'vote'" Type of action to register
[actionToken] string "'vote'" Token of the action to register

mafiaGame.revokeAction(postId, actor, [target], [type], [actionToken]) ⇒ Promise.<MafiaAction>

Revoke a registered action

Kind: instance method of MafiaGame
Returns: Promise.<MafiaAction> - Resolves to revoked action

Param Type Default Description
postId number Id of the post the action was revokec in
actor string | MafiaUser Actor for the Action
[target] string | MafiaAction null Target for the Action
[type] string "'vote'" Type of action to revoke
[actionToken] string "'vote'" Token of the action to revoke

mafiaGame.getValue(key) ⇒ *

Get a custom value attached to the game

Kind: instance method of MafiaGame
Returns: * - Stored value for key

Param Type Description
key string Value storage key

mafiaGame.setValue(key, data) ⇒ Promise.<*>

Store a custom value attached to the game

Kind: instance method of MafiaGame
Returns: Promise.<*> - Resolves to prior stored value

Param Type Description
key string Value storage key
data * Value to store

mafiaGame.addAlias(alias) ⇒ Promise

Add an alias to this game

Kind: instance method of MafiaGame
Returns: Promise - Resolves when alias has been added, Rejects if alias would conflict.

Param Type Description
alias string Alias to add to the game

mafiaGame.removeAlias(alias) ⇒ Promise.<boolean>

Remove an alias from this game

Kind: instance method of MafiaGame
Returns: Promise.<boolean> - Resolves true if alias existed, false otherwise

Param Type Description
alias string Alias to remove from the game

mafiaGame.addTopic(topicId) ⇒ Promise

Add a topic to this game

Kind: instance method of MafiaGame
Returns: Promise - Resolves when topic has been added, Rejects if topic would conflict.

Param Type Description
topicId number ID of topic to add to the game

mafiaGame.removeTopic(topicId) ⇒ Promise.<boolean>

Remove a topic from this game

Kind: instance method of MafiaGame
Returns: Promise.<boolean> - Resolves true if topic was member of game, false otherwise.

Param Type Description
topicId number ID of topic to remove from the game

mafiaGame.addChat(chatId) ⇒ Promise

Add a chat thread to this game

Kind: instance method of MafiaGame
Returns: Promise - Resolves when chat has been added, Rejects if chat would conflict.

Param Type Description
chatId number ID of chat to add to the game

mafiaGame.removeChat(chatId) ⇒ Promise.<boolean>

Remove a chat from this game

Kind: instance method of MafiaGame
Returns: Promise.<boolean> - Resolves true if chat was member of game, false otherwise.

Param Type Description
chatId number ID of chat to remove from the game

mafiaGame.toJSON() ⇒ object

Create a serializeable representation of the DAO object.

Kind: instance method of MafiaGame
Returns: object - A serializeable clone of this action's internal data store.

MafiaGame~getUserSlug(user) ⇒ string

Turn a string or a MafiaUser into a userslug.

used for indexes into objects and for matching users.

Kind: inner method of MafiaGame
Returns: string - Userslug for the input

Param Type Description
user string | MafiaUser Username of user or MafiaUser object

MafiaGame~getUser(game, source, user) ⇒ MafiaUser

Get a MafiaUser from a data dictionary

Kind: inner method of MafiaGame
Returns: MafiaUser - Mathing MafiaUser or null if no user matched

Param Type Description
game MafiaGame The MafiaGame the user belongs to.
source object Serialized User Mapping to retrieve MafiaUser from
user string | MafiaUser User to retrieve from source

MafiaGame~shuffle(arr) ⇒ Array.<*>

Perform an in place Fischer Yates shuffle on an array

Kind: inner method of MafiaGame
Returns: Array.<*> - The shuffled input array, because why not?

Param Type Description
arr Array.<*> Array to shuffle. Will be modified in place

MafiaGame~setDefault(value, fallback) ⇒ *

Return the existing value, unless falsy then return fallback value

Kind: inner method of MafiaGame
Returns: * - value if value is truthy, else fallback

Param Type Description
value * Existing value
fallback * Fallback value