-
Notifications
You must be signed in to change notification settings - Fork 4
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() {}
}
Called when a room is created and the first client is joined
public onCreate() {
console.log("We're up & running!")
}
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.
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 })
}
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 })
}
}
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
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")
}
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 }
}
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() })
Send all clients in the room a message
Example
public onLeave(client: Client) {
this.broadcast("CHAT", { message: `${client.id} left!` })
}
Async. Forces all clients to be removed from the room and disposes the room
Example
await this.dispose()
Current state in the room.
The rooms unique ID generated when created
List of clients connected and in the room. Read more on the client object here.
Custom options registered in server.register()
Options provided by the creator of the room
Custom game values entered in the Blueboat Admin Panel
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)