@colyseus/schema 3.0.46 → 3.0.48

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,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());