@colyseus/schema 3.0.47 → 3.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,130 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- const nanoid_1 = require("nanoid");
10
- const _1 = require(".");
11
- class Attribute extends _1.Schema {
12
- }
13
- __decorate([
14
- (0, _1.type)("string")
15
- ], Attribute.prototype, "name", void 0);
16
- __decorate([
17
- (0, _1.type)("number")
18
- ], Attribute.prototype, "value", void 0);
19
- class Item extends _1.Schema {
20
- constructor() {
21
- super(...arguments);
22
- this.attributes = new _1.ArraySchema();
23
- }
24
- }
25
- __decorate([
26
- (0, _1.type)("number")
27
- ], Item.prototype, "price", void 0);
28
- __decorate([
29
- (0, _1.type)([Attribute])
30
- ], Item.prototype, "attributes", void 0);
31
- class Position extends _1.Schema {
32
- }
33
- __decorate([
34
- (0, _1.type)("number")
35
- ], Position.prototype, "x", void 0);
36
- __decorate([
37
- (0, _1.type)("number")
38
- ], Position.prototype, "y", void 0);
39
- class Player extends _1.Schema {
40
- constructor() {
41
- super(...arguments);
42
- this.position = new Position();
43
- this.items = new _1.MapSchema();
44
- }
45
- }
46
- __decorate([
47
- (0, _1.type)(Position)
48
- ], Player.prototype, "position", void 0);
49
- __decorate([
50
- (0, _1.type)({ map: Item })
51
- ], Player.prototype, "items", void 0);
52
- class State extends _1.Schema {
53
- constructor() {
54
- super(...arguments);
55
- this.players = new _1.MapSchema();
56
- }
57
- }
58
- __decorate([
59
- (0, _1.type)({ map: Player })
60
- ], State.prototype, "players", void 0);
61
- __decorate([
62
- (0, _1.type)("string")
63
- ], State.prototype, "currentTurn", void 0);
64
- const state = new State();
65
- _1.Encoder.BUFFER_SIZE = 4096 * 4096;
66
- const encoder = new _1.Encoder(state);
67
- let now = Date.now();
68
- // for (let i = 0; i < 10000; i++) {
69
- // const player = new Player();
70
- // state.players.set(`p-${nanoid()}`, player);
71
- //
72
- // player.position.x = (i + 1) * 100;
73
- // player.position.y = (i + 1) * 100;
74
- // for (let j = 0; j < 10; j++) {
75
- // const item = new Item();
76
- // player.items.set(`item-${j}`, item);
77
- // item.price = (i + 1) * 50;
78
- // for (let k = 0; k < 5; k++) {
79
- // const attr = new Attribute();
80
- // attr.name = `Attribute ${k}`;
81
- // attr.value = k;
82
- // item.attributes.push(attr);
83
- // }
84
- // }
85
- // }
86
- // console.log("time to make changes:", Date.now() - now);
87
- // measure time to .encodeAll()
88
- now = Date.now();
89
- // for (let i = 0; i < 1000; i++) {
90
- // encoder.encodeAll();
91
- // }
92
- // console.log(Date.now() - now);
93
- const total = 100;
94
- const allEncodes = Date.now();
95
- let avgTimeToEncode = 0;
96
- let avgTimeToMakeChanges = 0;
97
- for (let i = 0; i < total; i++) {
98
- now = Date.now();
99
- for (let j = 0; j < 50; j++) {
100
- const player = new Player();
101
- state.players.set(`p-${(0, nanoid_1.nanoid)()}`, player);
102
- player.position.x = (j + 1) * 100;
103
- player.position.y = (j + 1) * 100;
104
- for (let k = 0; k < 10; k++) {
105
- const item = new Item();
106
- item.price = (j + 1) * 50;
107
- for (let l = 0; l < 5; l++) {
108
- const attr = new Attribute();
109
- attr.name = `Attribute ${l}`;
110
- attr.value = l;
111
- item.attributes.push(attr);
112
- }
113
- player.items.set(`item-${k}`, item);
114
- }
115
- }
116
- const timeToMakeChanges = Date.now() - now;
117
- console.log("time to make changes:", timeToMakeChanges);
118
- avgTimeToMakeChanges += timeToMakeChanges;
119
- now = Date.now();
120
- encoder.encode();
121
- encoder.discardChanges();
122
- const timeToEncode = Date.now() - now;
123
- console.log("time to encode:", timeToEncode);
124
- avgTimeToEncode += timeToEncode;
125
- }
126
- console.log("avg time to encode:", (avgTimeToEncode) / total);
127
- console.log("avg time to make changes:", (avgTimeToMakeChanges) / total);
128
- console.log("time for all encodes:", Date.now() - allEncodes);
129
- console.log(Array.from(encoder.encodeAll()).length, "bytes");
130
- //# sourceMappingURL=bench_encode.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bench_encode.js","sourceRoot":"","sources":["../src/bench_encode.ts"],"names":[],"mappings":";;;;;;;;AAAA,mCAAgC;AAChC,wBAAkE;AAElE,MAAM,SAAU,SAAQ,SAAM;CAG7B;AAFmB;IAAf,IAAA,OAAI,EAAC,QAAQ,CAAC;uCAAc;AACb;IAAf,IAAA,OAAI,EAAC,QAAQ,CAAC;wCAAe;AAGlC,MAAM,IAAK,SAAQ,SAAM;IAAzB;;QAEyB,eAAU,GAAG,IAAI,cAAW,EAAa,CAAC;IACnE,CAAC;CAAA;AAFmB;IAAf,IAAA,OAAI,EAAC,QAAQ,CAAC;mCAAe;AACT;IAApB,IAAA,OAAI,EAAC,CAAE,SAAS,CAAE,CAAC;wCAA2C;AAGnE,MAAM,QAAS,SAAQ,SAAM;CAG5B;AAFmB;IAAf,IAAA,OAAI,EAAC,QAAQ,CAAC;mCAAW;AACV;IAAf,IAAA,OAAI,EAAC,QAAQ,CAAC;mCAAW;AAG9B,MAAM,MAAO,SAAQ,SAAM;IAA3B;;QACoB,aAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC;QACrB,UAAK,GAAG,IAAI,YAAS,EAAQ,CAAC;IACvD,CAAC;CAAA;AAFmB;IAAf,IAAA,OAAI,EAAC,QAAQ,CAAC;wCAA2B;AACrB;IAApB,IAAA,OAAI,EAAC,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;qCAA+B;AAGvD,MAAM,K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AAC,cAAc,EAAE,CAAC;IAEzB,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC;IACtC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,YAAY,CAAC,CAAC;IAC7C,eAAe,IAAI,YAAY,CAAC;AACpC,CAAC;AACD,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,CAAC,eAAe,CAAC,GAAG,KAAK,CAAC,CAAC;AAC9D,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE,CAAC,oBAAoB,CAAC,GAAG,KAAK,CAAC,CAAC;AACzE,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC,CAAC;AAE9D,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC","sourcesContent":["import { nanoid } from \"nanoid\";\nimport { Schema, type, MapSchema, ArraySchema, Encoder } from \".\";\n\nclass Attribute extends Schema {\n @type(\"string\") name: string;\n @type(\"number\") value: number;\n}\n\nclass Item extends Schema {\n @type(\"number\") price: number;\n @type([ Attribute ]) attributes = new ArraySchema<Attribute>();\n}\n\nclass Position extends Schema {\n @type(\"number\") x: number;\n @type(\"number\") y: number;\n}\n\nclass Player extends Schema {\n @type(Position) position = new Position();\n @type({ map: Item }) items = new MapSchema<Item>();\n}\n\nclass State extends Schema {\n @type({ map: Player }) players = new MapSchema<Player>();\n @type(\"string\") currentTurn;\n}\n\nconst state = new State();\n\nEncoder.BUFFER_SIZE = 4096 * 4096;\nconst encoder = new Encoder(state);\n\n\nlet now = Date.now();\n\n// for (let i = 0; i < 10000; i++) {\n// const player = new Player();\n// state.players.set(`p-${nanoid()}`, player);\n//\n// player.position.x = (i + 1) * 100;\n// player.position.y = (i + 1) * 100;\n// for (let j = 0; j < 10; j++) {\n// const item = new Item();\n// player.items.set(`item-${j}`, item);\n// item.price = (i + 1) * 50;\n// for (let k = 0; k < 5; k++) {\n// const attr = new Attribute();\n// attr.name = `Attribute ${k}`;\n// attr.value = k;\n// item.attributes.push(attr);\n// }\n// }\n// }\n// console.log(\"time to make changes:\", Date.now() - now);\n\n\n// measure time to .encodeAll()\n\nnow = Date.now();\n// for (let i = 0; i < 1000; i++) {\n// encoder.encodeAll();\n// }\n// console.log(Date.now() - now);\n\nconst total = 100;\nconst allEncodes = Date.now();\n\nlet avgTimeToEncode = 0;\nlet avgTimeToMakeChanges = 0;\n\nfor (let i = 0; i < total; i++) {\n now = Date.now();\n for (let j = 0; j < 50; j++) {\n const player = new Player();\n state.players.set(`p-${nanoid()}`, player);\n\n player.position.x = (j + 1) * 100;\n player.position.y = (j + 1) * 100;\n for (let k = 0; k < 10; k++) {\n const item = new Item();\n item.price = (j + 1) * 50;\n for (let l = 0; l < 5; l++) {\n const attr = new Attribute();\n attr.name = `Attribute ${l}`;\n attr.value = l;\n item.attributes.push(attr);\n }\n player.items.set(`item-${k}`, item);\n }\n }\n const timeToMakeChanges = Date.now() - now;\n console.log(\"time to make changes:\", timeToMakeChanges);\n avgTimeToMakeChanges += timeToMakeChanges;\n\n now = Date.now();\n encoder.encode();\n encoder.discardChanges();\n\n const timeToEncode = Date.now() - now;\n console.log(\"time to encode:\", timeToEncode);\n avgTimeToEncode += timeToEncode;\n}\nconsole.log(\"avg time to encode:\", (avgTimeToEncode) / total);\nconsole.log(\"avg time to make changes:\", (avgTimeToMakeChanges) / total);\nconsole.log(\"time for all encodes:\", Date.now() - allEncodes);\n\nconsole.log(Array.from(encoder.encodeAll()).length, \"bytes\");\n"]}
package/lib/debug.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/lib/debug.js DELETED
@@ -1,51 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const fs = require("fs");
4
- const index_1 = require("./index");
5
- const contents = fs.readFileSync("/Users/endel/Projects/colyseus/clients/bubbits/project/@bubbits/backend/schema-debug.txt", { encoding: "utf8" }).toString();
6
- let isCommentBlock = false;
7
- let lastComment = "";
8
- let decoder;
9
- function getBuffer(line) {
10
- const start = line.lastIndexOf(":");
11
- const buffer = Buffer.from(new Uint8Array(line.substring(start + 1).split(",").map(n => Number(n))));
12
- console.log(`(${buffer.byteLength}) ${Array.from(buffer).join(",")}`);
13
- // console.log("");
14
- // console.log("");
15
- // console.log("> ", line);
16
- // console.log("> substring:", line.substring(start + 1))
17
- return buffer;
18
- }
19
- function decode(buffer) {
20
- try {
21
- decoder.decode(buffer);
22
- }
23
- catch (e) {
24
- console.error(e);
25
- console.log("Last log:\n\n");
26
- console.log(lastComment);
27
- }
28
- }
29
- contents.split("\n").forEach((line) => {
30
- if (line.startsWith("#")) {
31
- // reset last comment.
32
- if (isCommentBlock === false) {
33
- lastComment = "";
34
- }
35
- isCommentBlock = true;
36
- lastComment += line.substring(line.indexOf(":") + 1) + "\n";
37
- return;
38
- }
39
- isCommentBlock = false;
40
- if (line.startsWith("handshake:") && !decoder) {
41
- decoder = index_1.Reflection.decode(getBuffer(line));
42
- }
43
- else if (line.startsWith("state:")) {
44
- decode(getBuffer(line));
45
- }
46
- else if (line.startsWith("patch:")) {
47
- decode(getBuffer(line));
48
- }
49
- });
50
- console.log(decoder.state.toJSON());
51
- //# sourceMappingURL=debug.js.map
package/lib/debug.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"debug.js","sourceRoot":"","sources":["../src/debug.ts"],"names":[],"mappings":";;AAAA,yBAAyB;AACzB,mCAA8C;AAE9C,MAAM,QAAQ,GAAG,EAAE,CAAC,YAAY,CAAC,0FAA0F,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;AAE9J,IAAI,cAAc,GAAG,KAAK,CAAC;AAC3B,IAAI,WAAW,GAAG,EAAE,CAAC;AAErB,IAAI,OAAgB,CAAC;AAErB,SAAS,SAAS,CAAC,IAAY;IAC3B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AC5D,OAAO;IACX,CAAC;IAED,cAAc,GAAG,KAAK,CAAC;IAEvB,IAAI,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5C,OAAO,GAAG,kBAAU,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;IAEjD,CAAC;SAAM,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;IAE5B,CAAC;SAAM,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;IAC5B,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC","sourcesContent":["import * as fs from \"fs\";\nimport { Reflection, Decoder } from \"./index\";\n\nconst contents = fs.readFileSync(\"/Users/endel/Projects/colyseus/clients/bubbits/project/@bubbits/backend/schema-debug.txt\", { encoding: \"utf8\" }).toString();\n\nlet isCommentBlock = false;\nlet lastComment = \"\";\n\nlet decoder: Decoder;\n\nfunction getBuffer(line: string) {\n const start = line.lastIndexOf(\":\");\n const buffer = Buffer.from(new Uint8Array(line.substring(start + 1).split(\",\").map(n => Number(n))));\n console.log(`(${buffer.byteLength}) ${Array.from(buffer).join(\",\")}`)\n // console.log(\"\");\n // console.log(\"\");\n // console.log(\"> \", line);\n // console.log(\"> substring:\", line.substring(start + 1))\n return buffer;\n}\n\nfunction decode(buffer: Buffer) {\n try {\n decoder.decode(buffer);\n } catch (e) {\n console.error(e);\n console.log(\"Last log:\\n\\n\")\n console.log(lastComment);\n }\n}\n\ncontents.split(\"\\n\").forEach((line) => {\n if (line.startsWith(\"#\")) {\n // reset last comment.\n if (isCommentBlock === false) { lastComment = \"\"; }\n\n isCommentBlock = true;\n lastComment += line.substring(line.indexOf(\":\") + 1) + \"\\n\";\n return;\n }\n\n isCommentBlock = false;\n\n if (line.startsWith(\"handshake:\") && !decoder) {\n decoder = Reflection.decode(getBuffer(line));\n\n } else if (line.startsWith(\"state:\")) {\n decode(getBuffer(line));\n\n } else if (line.startsWith(\"patch:\")) {\n decode(getBuffer(line));\n }\n});\n\nconsole.log(decoder.state.toJSON());"]}
@@ -1,108 +0,0 @@
1
- import { nanoid } from "nanoid";
2
- import { Schema, type, MapSchema, ArraySchema, Encoder } from ".";
3
-
4
- class Attribute extends Schema {
5
- @type("string") name: string;
6
- @type("number") value: number;
7
- }
8
-
9
- class Item extends Schema {
10
- @type("number") price: number;
11
- @type([ Attribute ]) attributes = new ArraySchema<Attribute>();
12
- }
13
-
14
- class Position extends Schema {
15
- @type("number") x: number;
16
- @type("number") y: number;
17
- }
18
-
19
- class Player extends Schema {
20
- @type(Position) position = new Position();
21
- @type({ map: Item }) items = new MapSchema<Item>();
22
- }
23
-
24
- class State extends Schema {
25
- @type({ map: Player }) players = new MapSchema<Player>();
26
- @type("string") currentTurn;
27
- }
28
-
29
- const state = new State();
30
-
31
- Encoder.BUFFER_SIZE = 4096 * 4096;
32
- const encoder = new Encoder(state);
33
-
34
-
35
- let now = Date.now();
36
-
37
- // for (let i = 0; i < 10000; i++) {
38
- // const player = new Player();
39
- // state.players.set(`p-${nanoid()}`, player);
40
- //
41
- // player.position.x = (i + 1) * 100;
42
- // player.position.y = (i + 1) * 100;
43
- // for (let j = 0; j < 10; j++) {
44
- // const item = new Item();
45
- // player.items.set(`item-${j}`, item);
46
- // item.price = (i + 1) * 50;
47
- // for (let k = 0; k < 5; k++) {
48
- // const attr = new Attribute();
49
- // attr.name = `Attribute ${k}`;
50
- // attr.value = k;
51
- // item.attributes.push(attr);
52
- // }
53
- // }
54
- // }
55
- // console.log("time to make changes:", Date.now() - now);
56
-
57
-
58
- // measure time to .encodeAll()
59
-
60
- now = Date.now();
61
- // for (let i = 0; i < 1000; i++) {
62
- // encoder.encodeAll();
63
- // }
64
- // console.log(Date.now() - now);
65
-
66
- const total = 100;
67
- const allEncodes = Date.now();
68
-
69
- let avgTimeToEncode = 0;
70
- let avgTimeToMakeChanges = 0;
71
-
72
- for (let i = 0; i < total; i++) {
73
- now = Date.now();
74
- for (let j = 0; j < 50; j++) {
75
- const player = new Player();
76
- state.players.set(`p-${nanoid()}`, player);
77
-
78
- player.position.x = (j + 1) * 100;
79
- player.position.y = (j + 1) * 100;
80
- for (let k = 0; k < 10; k++) {
81
- const item = new Item();
82
- item.price = (j + 1) * 50;
83
- for (let l = 0; l < 5; l++) {
84
- const attr = new Attribute();
85
- attr.name = `Attribute ${l}`;
86
- attr.value = l;
87
- item.attributes.push(attr);
88
- }
89
- player.items.set(`item-${k}`, item);
90
- }
91
- }
92
- const timeToMakeChanges = Date.now() - now;
93
- console.log("time to make changes:", timeToMakeChanges);
94
- avgTimeToMakeChanges += timeToMakeChanges;
95
-
96
- now = Date.now();
97
- encoder.encode();
98
- encoder.discardChanges();
99
-
100
- const timeToEncode = Date.now() - now;
101
- console.log("time to encode:", timeToEncode);
102
- avgTimeToEncode += timeToEncode;
103
- }
104
- console.log("avg time to encode:", (avgTimeToEncode) / total);
105
- console.log("avg time to make changes:", (avgTimeToMakeChanges) / total);
106
- console.log("time for all encodes:", Date.now() - allEncodes);
107
-
108
- console.log(Array.from(encoder.encodeAll()).length, "bytes");
package/src/debug.ts DELETED
@@ -1,55 +0,0 @@
1
- import * as fs from "fs";
2
- import { Reflection, Decoder } from "./index";
3
-
4
- const contents = fs.readFileSync("/Users/endel/Projects/colyseus/clients/bubbits/project/@bubbits/backend/schema-debug.txt", { encoding: "utf8" }).toString();
5
-
6
- let isCommentBlock = false;
7
- let lastComment = "";
8
-
9
- let decoder: Decoder;
10
-
11
- function getBuffer(line: string) {
12
- const start = line.lastIndexOf(":");
13
- const buffer = Buffer.from(new Uint8Array(line.substring(start + 1).split(",").map(n => Number(n))));
14
- console.log(`(${buffer.byteLength}) ${Array.from(buffer).join(",")}`)
15
- // console.log("");
16
- // console.log("");
17
- // console.log("> ", line);
18
- // console.log("> substring:", line.substring(start + 1))
19
- return buffer;
20
- }
21
-
22
- function decode(buffer: Buffer) {
23
- try {
24
- decoder.decode(buffer);
25
- } catch (e) {
26
- console.error(e);
27
- console.log("Last log:\n\n")
28
- console.log(lastComment);
29
- }
30
- }
31
-
32
- contents.split("\n").forEach((line) => {
33
- if (line.startsWith("#")) {
34
- // reset last comment.
35
- if (isCommentBlock === false) { lastComment = ""; }
36
-
37
- isCommentBlock = true;
38
- lastComment += line.substring(line.indexOf(":") + 1) + "\n";
39
- return;
40
- }
41
-
42
- isCommentBlock = false;
43
-
44
- if (line.startsWith("handshake:") && !decoder) {
45
- decoder = Reflection.decode(getBuffer(line));
46
-
47
- } else if (line.startsWith("state:")) {
48
- decode(getBuffer(line));
49
-
50
- } else if (line.startsWith("patch:")) {
51
- decode(getBuffer(line));
52
- }
53
- });
54
-
55
- console.log(decoder.state.toJSON());