Skip to main content

GameState

The Game State provides a snapshot of the current state of the game. It is sent to clients when the game starts, and is updated whenever the game state changes. Clients can use this data to render the game state to the user.

Importantly, the Game State that is emitted to clients is filtered to only include information that the client is allowed to know. For example, the role field of a player is only included if the player's role is known to the client. Additionally, the messages field does not include private messages that the client is not allowed to see. This is done to prevent cheating.

Data Type

The data type for GameState is defined here, and is reproduced below for convenience:

export type GameState = {
/** the id of the game */
id: string
/** the current day number of the game */
dayNumber: number
/** the current phase of the game */
phase: Phase
/** whether there is a full moon tonight */
fullMoon: boolean
/** message history */
messages: GameMessage[]
/** the players in the game */
players: {
[playerNumber: string]: {
/** the player's number */
num: number
/** the player's alias */
alias: string
/** whether the player is alive */
isAlive: boolean
/** the role of the player. undefined if the player's role is not known to the client */
role?: Role
}
}
/** the player number of the player who is currently on trial */
accusedPlayer?: number
/** the player number of the player receiving this game state (i.e. the client) */
selfPlayer: number
/** the unix epoch timestamp of this game state */
timestamp: number
}

Sample Data

{
"id": "4RZB",
"phase": "night",
"fullMoon": false,
"dayNumber": 1,
"messages": [
{
"type": "event",
"author": {
"playerNum": 0,
"alias": "Game"
},
"message": "The game has begun!",
"visibility": "all"
},
{
"type": "chat",
"author": {
"playerNum": 1,
"alias": "Devora"
},
"message": "what up",
"visibility": "all"
},
{
"type": "chat",
"author": {
"playerNum": 2,
"alias": "Betta"
},
"message": "heyo",
"visibility": "all"
},
{
"type": "event",
"author": {
"playerNum": 0,
"alias": "Game"
},
"message": "The sun sets...",
"visibility": "all"
}
],
"players": {
"1": {
"num": 1,
"alias": "Devora",
"isAlive": true,
"role": "assassin"
},
"2": {
"num": 2,
"alias": "Betta",
"isAlive": true
},
"3": {
"num": 3,
"alias": "Issy",
"isAlive": true,
"role": "framer"
},
"4": {
"num": 4,
"alias": "Loella",
"isAlive": true
},
"5": {
"num": 5,
"alias": "Dorrie",
"isAlive": true
},
"6": {
"num": 6,
"alias": "Sasha",
"isAlive": true
}
},
"selfPlayer": 1
}