Skip to content

Simulations and Socket.IO

Morris Raycroft edited this page Dec 16, 2021 · 10 revisions

When a facilitator enters the administration panel or a player joins the game page for a simulation, they will initially be connected to the server's Socket.IO instance. Socket.IO is a library that is used to handle real-time bi-directional communication, which we use to provide the interactive experience for players and administrators.

Room status format

In order to persist information inbetween Socket.IO packets, data is stored in memory with JavaScript objects (accessed by using the current room ID as a key.) See server/events/utils.js for more info.

{
  // roomStatus is for storing general values that are commonly used by the app
  roomStatus {
    running      // boolean value; when false, game is paused
    level        // integer that represents which page simulation is currently on
                 // only used when
    startTime    // unix timestamp, represents when game has started/last unpaused
    timeElapsed  // represents how much time has passed since last paused
    settings {
      advanceMode  // "student", "teacher", or an integer value (timed mode in minutes)
                   // determines who has the privilege to advance the simulation
                   // student by default
      roleMode     // "student", "teacher", "random", or "randomByLevel"
                   // determines how roles are assigned in the simulation
                   // student by default
    }
    variables    // object that stores game variables (key is variable name)
                 // which can be manipulated by gamepieces
    gamepieces   // object that stores status of gamepieces (key is gamepiece id)
                 // values are also objects and vary depending on the gamepiece
  }

  // messages is for storing the messages that get sent from the sidebar menu
  messages [{
    sender {
      name       // string values; name and role player entered when joining game
      role
      id         // player's current socket.io connection id, subject to change
      dbid       // unique id stored in player's localstorage which stays consistent
                 // used to determine who they are if regular id changes
                 // if the player was registered before joining, this is their
                 // primary key from their database record
      invited    // boolean value; whether or not player was registered before joining
    }
    room         // room code id (similar to the one in the game room url)
    message      // message content string value
    timeSent     // unix timestamp
  }]

  // interactions is similar to the "gamepieces"/"variables" object in roomStatus
  // however, this is a list of *every* action performed ingame
  // as opposed to roomStatus which gets rewritten upon every interaction
  // mainly for logging purposes
  interactions [{
    timestamp    // unix timestamp
    player       // identical to the "sender" object in messages
    
    // the following values are present if a GAMEPIECE has been updated
    gamepieceId  // id of the gamepiece
    parameters   // object; values are changes within the gamepiece's status
                 // varies depending on the gamepiece type
    changedState // boolean value; if true, this has updated the roomStatus
                 // if false, this does not change state/is only for logging
                 // this will pretty much always be true

    // the following values are present if a VARIABLE has been updated
    variable     // variable name and value
    value
  }]
}

You can view a real example of a game status object by downloading the JSON of a previous run:

Clone this wiki locally