How to use @colyseus/schema - 10 common examples

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 halftheopposite / tosios / packages / server / src / entities / Prop.ts View on Github external
import { type } from '@colyseus/schema';
import { Types } from '@tosios/common';
import { Rectangle } from './Rectangle';

export class Prop extends Rectangle {

  @type('string')
  public type: Types.PropType;

  @type('boolean')
  public active: boolean;

  // Init
  constructor(type: Types.PropType, x: number, y: number, width: number, height: number) {
    super(x, y, width, height);

    this.type = type;
    this.active = true;
  }
}
github halftheopposite / tosios / packages / server / src / entities / Bullet.ts View on Github external
import { type } from '@colyseus/schema';
import { Circle } from './Circle';

export class Bullet extends Circle {

  @type('string')
  public playerId: string;

  @type('number')
  public rotation: number;

  @type('number')
  public fromX: number;

  @type('number')
  public fromY: number;

  @type('boolean')
  public active: boolean;

  @type('string')
  public color: string;

  @type('number')
  public shotAt: number;

  // Init
  constructor(
github halftheopposite / tosios / packages / server / src / entities / Game.ts View on Github external
import { Schema, type } from '@colyseus/schema';
import { Types } from '@tosios/common';

export class Game extends Schema {

  @type('string')
  public mapName: string;

  @type('string')
  public state: Types.GameState;

  @type('number')
  public lobbyEndsAt: number;

  @type('number')
  public gameEndsAt: number;

  @type('number')
  public maxPlayers: number;

  private lobbyDuration: number;
  private gameDuration: number;

  // Init
  constructor(
github colyseus / colyseus / src / rooms / LobbyRoom.ts View on Github external
import { Schema, type } from '@colyseus/schema';
import { Room } from '../Room';

class RoomData extends Schema { // tslint:disable-line
  @type('string') public id: string;
  @type('string') public name: string;
  @type('number') public clients: number;
  @type('number') public maxClients: number;
  @type('string') public metadata: string;
}

class LobbyState extends Schema { // tslint:disable-line
  @type([RoomData]) public rooms: RoomData[];
}

export class LobbyRoom extends Room { // tslint:disable-line

  public onCreate(options: any) {
    this.setState(new LobbyState());
    this.clock.setInterval(() => this.fetch(), Math.max(1, options.updateInterval || 5000) * 1000);
  }

  public fetch() {
github halftheopposite / tosios / packages / server / src / entities / Prop.ts View on Github external
import { type } from '@colyseus/schema';
import { Types } from '@tosios/common';
import { Rectangle } from './Rectangle';

export class Prop extends Rectangle {

  @type('string')
  public type: Types.PropType;

  @type('boolean')
  public active: boolean;

  // Init
  constructor(type: Types.PropType, x: number, y: number, width: number, height: number) {
    super(x, y, width, height);

    this.type = type;
    this.active = true;
  }
}
github damian-pastorini / reldens / packages / users / server / player.js View on Github external
this.username = data.username;
            this.p2body = false;
            // set scene and position:
            this.state = new PlayerState(player.state);
            this.canAttack = true;
        } catch (err) {
            ErrorManager.error(['Missing user data.', err]);
        }
    }

}

type('string')(Player.prototype, 'sessionId');
type('string')(Player.prototype, 'username');
type('number')(Player.prototype, 'status');
schema.defineTypes(Player, {state: PlayerState});

module.exports.Player = Player;
github colyseus / colyseus / src / rooms / RelayRoom.ts View on Github external
import { Context, defineTypes, MapSchema, Schema, type } from '@colyseus/schema';

import { Client } from '..';
import { Room } from '../Room';

/**
 * Create another context to avoid these types from being in the user's global `Context`
 */
const context = new Context();

class Player extends Schema { // tslint:disable-line
  public connected: boolean;
  public sessionId: string;
}
defineTypes(Player, {
  connected: 'boolean',
  sessionId: 'string',
}, context);

class State extends Schema { // tslint:disable-line
  public players = new MapSchema();
}
defineTypes(State, {
  players: { map: Player },
}, context);

/**
 * client.joinOrCreate("relayroom", {
 *   maxClients: 10,
 *   allowReconnectionTime: 20
 * });
github colyseus / colyseus / src / rooms / RelayRoom.ts View on Github external
*/
const context = new Context();

class Player extends Schema { // tslint:disable-line
  public connected: boolean;
  public sessionId: string;
}
defineTypes(Player, {
  connected: 'boolean',
  sessionId: 'string',
}, context);

class State extends Schema { // tslint:disable-line
  public players = new MapSchema();
}
defineTypes(State, {
  players: { map: Player },
}, context);

/**
 * client.joinOrCreate("relayroom", {
 *   maxClients: 10,
 *   allowReconnectionTime: 20
 * });
 */

export class RelayRoom extends Room { // tslint:disable-line
  public allowReconnectionTime: number = 0;

  public onCreate(options) {
    this.setState(new State());
github colyseus / colyseus / src / rooms / RelayRoom.ts View on Github external
import { Context, defineTypes, MapSchema, Schema, type } from '@colyseus/schema';

import { Client } from '..';
import { Room } from '../Room';

/**
 * Create another context to avoid these types from being in the user's global `Context`
 */
const context = new Context();

class Player extends Schema { // tslint:disable-line
  public connected: boolean;
  public sessionId: string;
}
defineTypes(Player, {
  connected: 'boolean',
  sessionId: 'string',
}, context);

class State extends Schema { // tslint:disable-line
  public players = new MapSchema();
}
defineTypes(State, {
  players: { map: Player },
}, context);
github damian-pastorini / reldens / packages / rooms / server / state.js View on Github external
constructor(roomData)
    {
        super();
        // @NOTE: this JSON is to send the scene data to the client, here we could remove data we don't want to send.
        this.sceneData = JSON.stringify(roomData);
        this.players = new MapSchema();
    }