Skip to content
Josh Feinsilber edited this page Jul 12, 2019 · 2 revisions

Defining New Room

To define a new Room, import Room from Blueboat and extend it

Example

import {Room} from 'blueboat'

class GameRoom extends Room {
  public onCreate() {}
  public onJoin() {}
}

Abstract Methods

onCreate

Called when a room is created and the first client is joined

public onCreate() {
  console.log("We're up & running!")
}

canClientJoin

Called when a client attempts to join a room. If returned true, the client will be allowed to join. If false, the client will not be allowed to join. Options is the object provided by the client trying to join.

Example (only allow 15 total players)

public canClientJoin(client: SimpleClient, options?: any) {
  return this.clients.length <= 15
}

By default, all clients that request to join will be allowed.


onJoin

Called when a client joins a room. Useful for adding a new player entity to the state, or broadcasting a message to alert others that someone has joined.

Example

public onJoin(client: Client, options?: any) {
  this.state.players[client.id] = new Player({ name: options.name })
}

onMessage

Called when a client sends a message to the server. Used to match client actions with updated state

Example

public onMessage(client: Client, action: string, data?: any) {
  if (action === "ADD_MESSAGE") {
    this.state.messages.push({ text: data, sender: client.id })
  }
}

onLeave

Called when a client leaves the room, either from disconnecting or forcefully being removed

Example

public onLeave(client: Client, intentional: boolean) {
  delete this.state.players[client.id]
}

intentional is true if the client was removed via the client.removeFromRoom() method


beforeDispose

Called before the game server closes, or the room is disposed of. Can return a promise to run async operations.

Example

public onDispose() {
  this.broadcast("GAME_DISPOSING")
}

onDispose

Called after the game server closes, or the room is disposed of. Can return a promise to run async operations.

Example

public async onDispose() {
 try {
   await db.completedGames.add({ id: this.roomId })
 } catch (e) { return }
}

Public methods

setState

Used to set the initial state. Future state updates should be made by mutating the this.state object directly.

Example

this.setState({players: {}, gameStartTime: Date.now() })

broadcast

Send all clients in the room a message

Example

public onLeave(client: Client) {
  this.broadcast("CHAT", { message: `${client.id} left!` })
}

dispose

Async. Forces all clients to be removed from the room and disposes the room

Example

await this.dispose()

Room Values

state

Current state in the room.


roomId

The rooms unique ID generated when created


clients

List of clients connected and in the room. Read more on the client object here.


options

Custom options registered in server.register()


creatorOptions

Options provided by the creator of the room


initialGameValues

Custom game values entered in the Blueboat Admin Panel


clock

Used for timing events since Node's setInterval and setTimeout are not accurate

Example

this.clock.setInterval(() => console.log('Hi every 5 seconds!'), 5000)
console.log('Hi! Wait 2 more seconds')
this.clock.setTimeout(() => console.log('Good job! Those 2 seconds are over!'), 2000)