@colyseus/schema 3.0.31 → 3.0.33

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.
package/src/debug.ts ADDED
@@ -0,0 +1,55 @@
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());
@@ -241,9 +241,14 @@ export class ChangeTree<T extends Ref=any> {
241
241
  operation(op: OPERATION) {
242
242
  // operations without index use negative values to represent them
243
243
  // this is checked during .encode() time.
244
- this.changes.operations.push(-op);
244
+ if (this.filteredChanges !== undefined) {
245
+ this.filteredChanges.operations.push(-op);
246
+ enqueueChangeTree(this.root, this, 'filteredChanges');
245
247
 
246
- enqueueChangeTree(this.root, this, 'changes');
248
+ } else {
249
+ this.changes.operations.push(-op);
250
+ enqueueChangeTree(this.root, this, 'changes');
251
+ }
247
252
  }
248
253
 
249
254
  change(index: number, operation: OPERATION = OPERATION.ADD) {
package/src/utils.ts CHANGED
@@ -29,6 +29,9 @@ export function dumpChanges(schema: Schema) {
29
29
 
30
30
  // for (const refId in $root.changes) {
31
31
  $root.changes.forEach(changeTree => {
32
+ // skip if ChangeTree is undefined
33
+ if (changeTree === undefined) { return; }
34
+
32
35
  const changes = changeTree.indexedOperations;
33
36
 
34
37
  dump.refs.push(`refId#${changeTree.refId}`);