diff --git a/benchmark/FossilDeltaComparison.ts b/benchmark/FossilDeltaComparison.ts index 2067c15d..69375b4a 100644 --- a/benchmark/FossilDeltaComparison.ts +++ b/benchmark/FossilDeltaComparison.ts @@ -54,15 +54,15 @@ console.log("(@colyseus/state) INITIAL STATE SIZE:", encoded.length); console.log("(msgpack) INITIAL STATE SIZE:", firstStateEncoded.length); console.log(""); -{ - // CHANGE X/Y OF A SINGLE ENTITY - const x = Math.floor(Math.random() * 200); - const y = Math.floor(Math.random() * 200); - state.players[fixedUnitId].x = x - state.players[fixedUnitId].y = y - msgpackState.players[fixedUnitId].x = x - msgpackState.players[fixedUnitId].y = y -} +// { +// // CHANGE X/Y OF A SINGLE ENTITY +// const x = Math.floor(Math.random() * 200); +// const y = Math.floor(Math.random() * 200); +// state.players[fixedUnitId].x = x +// state.players[fixedUnitId].y = y +// msgpackState.players[fixedUnitId].x = x +// msgpackState.players[fixedUnitId].y = y +// } // { // // CHANGE X/Y OF 50 ENTITIES @@ -79,19 +79,19 @@ console.log(""); // } // } -// { -// // CHANGE ALL X/Y VALUES -// let i = 0; -// for (let id in msgpackState.players) { -// const x = Math.floor(Math.random() * 200); -// const y = Math.floor(Math.random() * 200); -// state.players[id].x = x -// state.players[id].y = y -// msgpackState.players[id].x = x -// msgpackState.players[id].y = y -// i++; -// } -// } +{ + // CHANGE ALL X/Y VALUES + let i = 0; + for (let id in msgpackState.players) { + const x = Math.floor(Math.random() * 200); + const y = Math.floor(Math.random() * 200); + state.players[id].x = x + state.players[id].y = y + msgpackState.players[id].x = x + msgpackState.players[id].y = y + i++; + } +} encoded = state.encode(); decodedState.decode(encoded); diff --git a/package.json b/package.json index 6264d703..1d5e9c1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@colyseus/schema", - "version": "1.0.0-alpha.9", + "version": "1.0.0-alpha.10", "description": "Schema-based binary serializer / de-serializer.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/test/EdgeCasesTest.ts b/test/EdgeCasesTest.ts index d7b9c8ea..d66a0871 100644 --- a/test/EdgeCasesTest.ts +++ b/test/EdgeCasesTest.ts @@ -1,10 +1,58 @@ import * as assert from "assert"; import * as nanoid from "nanoid"; -import { MapSchema, Schema, type, ArraySchema, dumpChanges } from "../src"; +import { MapSchema, Schema, type, ArraySchema, dumpChanges, defineTypes, Reflection, Context } from "../src"; import { State, Player } from "./Schema"; describe("Edge cases", () => { + it("Schema should support more than 256 fields", () => { + const maxFields = 1000; + class State extends Schema {}; + + const schema = {}; + for (let i = 0; i < maxFields; i++) { schema[`field_${i}`] = "string"; } + defineTypes(State, schema); + + const state = new State(); + for (let i = 0; i < maxFields; i++) { state[`field_${i}`] = "value " + i; } + + const decodedState = Reflection.decode(Reflection.encode(state)); + decodedState.decode(state.encode()); + + for (let i = 0; i < maxFields; i++) { + assert.equal("value " + i, decodedState[`field_${i}`]); + } + }); + + it("Should support up to 255 schema types", () => { + const maxSchemaTypes = 255; + const context = new Context(); + const type = Context.create(context); + + class State extends Schema {} + + const schemas: any = {}; + + for (let i = 0; i < maxSchemaTypes; i++) { + class Child extends Schema { @type("string") str: string; }; + schemas[i] = Child; + defineTypes(State, { [`field_${i}`]: Child, }, context); + } + + const state = new State(); + for (let i = 0; i < maxSchemaTypes; i++) { + const child = new schemas[i](); + child.str = "value " + i; + state[`field_${i}`] = child; + } + + const decodedState = Reflection.decode(Reflection.encode(state)); + decodedState.decode(state.encode()); + + for (let i = 0; i < maxSchemaTypes; i++) { + assert.equal("value " + i, decodedState[`field_${i}`].str); + } + }); it("NIL check should not collide", () => { class State extends Schema { diff --git a/test/NextIterationTest.ts b/test/NextIterationTest.ts index 8a9ba460..3db2250a 100644 --- a/test/NextIterationTest.ts +++ b/test/NextIterationTest.ts @@ -670,7 +670,7 @@ describe("Next Iteration", () => { player1.name = "This field should not be encoded!"; const encoded = state.encode(); - console.log("LAST ENCODE (THIS SHOULD BE EMPTY) =>", encoded.length, encoded); + assert.equal(0, encoded.length); }); });