How to use the @colyseus/schema.ArraySchema function in @colyseus/schema

To help you get started, we’ve selected a few @colyseus/schema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github endel / colyseus-tic-tac-toe / server / rooms / tictactoe.ts View on Github external
import { Room, Delayed, Client } from 'colyseus';
import { type, Schema, MapSchema, ArraySchema } from '@colyseus/schema';

const TURN_TIMEOUT = 10
const BOARD_WIDTH = 3;

class State extends Schema {
  @type("string") currentTurn: string;
  @type({ map: "string" }) players = new MapSchema();
  @type(["number"]) board: number[] = new ArraySchema(0, 0, 0, 0, 0, 0, 0, 0, 0);
  @type("string") winner: string;
  @type("boolean") draw: boolean;
}

export class TicTacToe extends Room {
  maxClients = 2;
  randomMoveTimeout: Delayed;

  onCreate () {
    this.setState(new State())
  }

  onJoin (client: Client) {
    this.state.players[client.sessionId] = client.sessionId;

    if (Object.keys(this.state.players).length === 2) {