Skip to content

Instantly share code, notes, and snippets.

@doorbash
Created July 17, 2019 15:03
Show Gist options
  • Save doorbash/c07960944e3628a130375604dff132f4 to your computer and use it in GitHub Desktop.
Save doorbash/c07960944e3628a130375604dff132f4 to your computer and use it in GitHub Desktop.
import { Schema, type, ArraySchema, MapSchema } from "@colyseus/schema"
import { Room, Client } from "colyseus"
class PrimitivesTest extends Schema {
@type("uint8")
_uint8: number = 255;
@type("uint16")
_uint16: number = 65535;
@type("uint32")
_uint32: number = 4294967295;
@type("uint64")
_uint64: number = 18446744073709551615;
@type("int8")
_int8: number = -128;
@type("int16")
_int16: number = -32768;
@type("int32")
_int32: number = -2147483648;
@type("int64")
_int64: number = -9223372036854775808;
@type("float32")
_float32_n: number = -3.4e+38;
@type("float32")
_float32_p: number = +3.4E+38;
@type("float64")
_float_64_n:number = -1.7E+308;
@type("float64")
_float_64_p:number = +1.7E+308;
@type("boolean")
_boolean:boolean = true;
@type("string")
_string:string = "hello world!";
}
class Player extends Schema {
@type("int32")
x:number = 10;
}
class Cell extends Schema {
@type("float32")
x:number;
@type("float32")
y:number;
}
class MyState extends Schema {
@type(PrimitivesTest)
primitives:PrimitivesTest;
@type([Player])
players = new ArraySchema<Player>();
@type({map: Cell})
cells = new MapSchema<Cell>();
@type({map : "int32"})
map_1 = new MapSchema();
@type(["int32"])
array_1 = new ArraySchema();
}
export class ChatRoom extends Room {
maxClients = 4;
autoDispose = true;
onInit(options) {
console.log("Room created!", options);
this.setState(new MyState());
var player:Player = new Player();
player.x = 1
this.state.players.push(player);
player = new Player();
player.x = 2
this.state.players.push(player);
player = new Player();
player.x = 3
this.state.players.push(player);
player = new Player();
player.x = 4
this.state.players.push(player);
player = new Player();
player.x = 5
this.state.players.push(player);
player = new Player();
player.x = 6
this.state.players.push(player);
var cell:Cell = new Cell();
cell.x = 1;
cell.y = 1;
this.state.cells["xxx"] = cell;
cell = new Cell();
cell.x = 2;
cell.y = 2;
this.state.cells["yyy"] = cell;
cell = new Cell();
cell.x = 3;
cell.y = 3;
this.state.cells["zzz"] = cell;
// this.clock.setInterval(() => {
// if(this.state.cells["yyy"] !== undefined){
// delete this.state.cells["yyy"]
// } else {
// var cell:Cell = new Cell();
// cell.x = 100;
// cell.y = 100;
// this.state.cells["yyy"] = cell;
// }
// }, 5000);
this.clock.setTimeout(() => {
this.state.map_1["cccccccc"] = 1;
}, 10000);
this.clock.setTimeout(() => {
this.state.map_1["cccccccc"] = 10;
}, 15000);
// this.state.map_1["qqq"] = 1;
// this.state.map_1["www"] = 2;
// this.state.map_1["eee"] = 3;
// this.state.map_1["eee"] = 4;
// this.state.map_1["rrr"] = 5;
// this.state.array_1.push(10);
// this.state.array_1.push(11);
// this.state.array_1.push(12);
// this.state.array_1.push(13);
// this.state.array_1.push(14);
// this.state.array_1.push(15);
// this.state.array_1.push(16);
// this.state.array_1.push(17);
// this.state.array_1.push(18);
// this.state.array_1.push(19);
// this.state.array_1.push(20);
}
onJoin?(client: Client, options?: any, auth?: any): void | Promise<any> {
console.log('onJoin(', client.id, ')', options);
}
onLeave(client) {
console.log("onLeave(" + client.sessionId + ")");
}
onMessage(client, data) {
console.log("Room received message from", client.id, ":", data);
}
onDispose() {
console.log("Dispose Room");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment