@crowdedkingdoms/crowdyjs 18.30.0 → 18.31.1-dev.1
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/MIGRATION.md +39 -0
- package/README.md +21 -15
- package/dist/default-origin.d.ts +6 -6
- package/dist/default-origin.d.ts.map +1 -1
- package/dist/default-origin.js +6 -6
- package/dist/generated/graphql-schema.d.ts +2 -0
- package/dist/generated/graphql-schema.d.ts.map +1 -1
- package/dist/generated/graphql.d.ts +1 -0
- package/dist/generated/graphql.d.ts.map +1 -1
- package/dist/generated/graphql.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/stores/chunks.d.ts +101 -47
- package/dist/stores/chunks.d.ts.map +1 -1
- package/dist/stores/chunks.js +490 -200
- package/package.json +1 -1
package/dist/stores/chunks.js
CHANGED
|
@@ -42,6 +42,31 @@ const MAX_BOX_CHUNKS = 17 ** 3;
|
|
|
42
42
|
const MAX_BOXES = 8;
|
|
43
43
|
/** How far before the connection dropped a catch-up read starts, for the client's and server's clocks. */
|
|
44
44
|
const CATCH_UP_MARGIN_MS = 30000;
|
|
45
|
+
/**
|
|
46
|
+
* A server time more than this far past this machine's clock counts as this machine's clock plus
|
|
47
|
+
* this much, so that one wrong clock cannot hold a voxel against every later change.
|
|
48
|
+
*/
|
|
49
|
+
const MAX_FUTURE_SKEW_MS = 5 * 60000;
|
|
50
|
+
/** More boxes than this (after cutting each to 17³) is not a view: {@link ChunkStore.catchUp} refuses it. */
|
|
51
|
+
const MAX_CATCH_UP_BOXES = 64;
|
|
52
|
+
/** The newest optimistic edit of a voxel: what it shows. */
|
|
53
|
+
function shownEdit(cell) {
|
|
54
|
+
for (let i = cell.edits.length - 1; i >= 0; i -= 1) {
|
|
55
|
+
if (cell.edits[i].optimistic)
|
|
56
|
+
return cell.edits[i];
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
/** Whether two base64 voxel states carry the same bytes (empty and absent are the same). */
|
|
61
|
+
function sameEncodedState(a, b) {
|
|
62
|
+
if (!a || !b)
|
|
63
|
+
return !a && !b;
|
|
64
|
+
if (a === b)
|
|
65
|
+
return true;
|
|
66
|
+
const x = decodeBase64(a);
|
|
67
|
+
const y = decodeBase64(b);
|
|
68
|
+
return x.length === y.length && x.every((v, i) => v === y[i]);
|
|
69
|
+
}
|
|
45
70
|
function boxAround(coords) {
|
|
46
71
|
const lo = { ...coords[0] };
|
|
47
72
|
const hi = { ...coords[0] };
|
|
@@ -62,6 +87,49 @@ function storedAtMs(updatedAt) {
|
|
|
62
87
|
const ms = typeof updatedAt === 'number' ? updatedAt : Date.parse(String(updatedAt));
|
|
63
88
|
return Number.isFinite(ms) ? ms : null;
|
|
64
89
|
}
|
|
90
|
+
/** The widest box one read covers along an axis: ck-api reads at most 17³ chunks. */
|
|
91
|
+
const BOX_SIDE = 17;
|
|
92
|
+
/** `coords` in boxes of at most 17³: the bounds of the coords in each cell of a 17-chunk lattice. */
|
|
93
|
+
function tileBoxes(coords) {
|
|
94
|
+
const tiles = new Map();
|
|
95
|
+
for (const c of coords) {
|
|
96
|
+
const tile = `${Math.floor(c.x / BOX_SIDE)}:${Math.floor(c.y / BOX_SIDE)}:${Math.floor(c.z / BOX_SIDE)}`;
|
|
97
|
+
const box = tiles.get(tile);
|
|
98
|
+
if (!box) {
|
|
99
|
+
tiles.set(tile, { lo: { ...c }, hi: { ...c } });
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
box.lo = { x: Math.min(box.lo.x, c.x), y: Math.min(box.lo.y, c.y), z: Math.min(box.lo.z, c.z) };
|
|
103
|
+
box.hi = { x: Math.max(box.hi.x, c.x), y: Math.max(box.hi.y, c.y), z: Math.max(box.hi.z, c.z) };
|
|
104
|
+
}
|
|
105
|
+
return [...tiles.values()];
|
|
106
|
+
}
|
|
107
|
+
/** A box cut into boxes of at most 17³. */
|
|
108
|
+
function cutBox(low, high) {
|
|
109
|
+
const lo = { x: Math.min(low.x, high.x), y: Math.min(low.y, high.y), z: Math.min(low.z, high.z) };
|
|
110
|
+
const hi = { x: Math.max(low.x, high.x), y: Math.max(low.y, high.y), z: Math.max(low.z, high.z) };
|
|
111
|
+
const out = [];
|
|
112
|
+
for (let x = lo.x; x <= hi.x; x += BOX_SIDE) {
|
|
113
|
+
for (let y = lo.y; y <= hi.y; y += BOX_SIDE) {
|
|
114
|
+
for (let z = lo.z; z <= hi.z; z += BOX_SIDE) {
|
|
115
|
+
out.push({
|
|
116
|
+
lo: { x, y, z },
|
|
117
|
+
hi: {
|
|
118
|
+
x: Math.min(hi.x, x + BOX_SIDE - 1),
|
|
119
|
+
y: Math.min(hi.y, y + BOX_SIDE - 1),
|
|
120
|
+
z: Math.min(hi.z, z + BOX_SIDE - 1),
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
/** How many boxes of 17³ a box cuts into. */
|
|
129
|
+
function boxPieces(low, high) {
|
|
130
|
+
const side = (a, b) => Math.floor(Math.abs(b - a) / BOX_SIDE) + 1;
|
|
131
|
+
return side(low.x, high.x) * side(low.y, high.y) * side(low.z, high.z);
|
|
132
|
+
}
|
|
65
133
|
function boxVolume(box) {
|
|
66
134
|
return (box.hi.x - box.lo.x + 1) * (box.hi.y - box.lo.y + 1) * (box.hi.z - box.lo.z + 1);
|
|
67
135
|
}
|
|
@@ -185,17 +253,24 @@ function fitsDenseGrid(x, y, z, voxelType) {
|
|
|
185
253
|
* edit with a type outside 0-255 or a position outside 0-15 goes to the
|
|
186
254
|
* chunk's `overlay` instead). A `chunkChanged` (a hub's or mod's
|
|
187
255
|
* `world.set_chunk`) reads the chunk again.
|
|
256
|
+
* - Merging (18.31.0): each voxel holds the last value the server confirmed and
|
|
257
|
+
* the server's time for it (a stored row's write, an entry's `updatedAt`, a
|
|
258
|
+
* notification's `epochMillis`, a recorded edit's `createdAt`). A change older
|
|
259
|
+
* than the value held is dropped, whatever order changes arrive in, and a
|
|
260
|
+
* stored copy that arrives after a newer realtime change keeps that change;
|
|
261
|
+
* a change to a chunk still being read is kept the same way.
|
|
188
262
|
* - `setVoxel` applies locally (optimistic) and replicates via the UDP path,
|
|
189
263
|
* paced: at most 128 edits unanswered and 250 a second by default. The
|
|
190
|
-
* server delivers every accepted edit back to its sender: that echo
|
|
191
|
-
*
|
|
192
|
-
* local edit fires one change event
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
264
|
+
* server delivers every accepted edit back to its sender: that echo (sender
|
|
265
|
+
* uuid + sequence number + voxel + type + state) confirms the edit and is not
|
|
266
|
+
* applied again, so a local edit fires one change event. This client's
|
|
267
|
+
* unanswered edits show over the confirmed value; another client's edit that
|
|
268
|
+
* arrives meanwhile becomes the confirmed value. An edit the server refuses,
|
|
269
|
+
* or whose echo does not come back in time, is reported
|
|
270
|
+
* ({@link onEditRefused}) and the voxel shows the next unanswered edit, or
|
|
271
|
+
* the confirmed value.
|
|
197
272
|
* - After the realtime connection drops and comes back, it reads what changed
|
|
198
|
-
* in
|
|
273
|
+
* meanwhile in every chunk it tracks ({@link catchUp}).
|
|
199
274
|
* - `seed`/`flush` implement deterministic-worldgen write-back through
|
|
200
275
|
* `chunks.update`, one throttled chunk at a time. Only seeded chunks are
|
|
201
276
|
* written back: realtime edits are recorded by the server.
|
|
@@ -223,16 +298,20 @@ export class ChunkStore {
|
|
|
223
298
|
this.fallbackUuid = generateCrowdyUuid();
|
|
224
299
|
this.revisionValue = 0;
|
|
225
300
|
this.sequence = 0;
|
|
226
|
-
/**
|
|
227
|
-
this.
|
|
301
|
+
/** Voxels with this client's unanswered edits, per `chunkKey|voxelKey`. */
|
|
302
|
+
this.pendingCells = new Map();
|
|
303
|
+
/** Per chunk key: the server's time for each voxel whose value is newer than the stored row. */
|
|
304
|
+
this.cellTimes = new Map();
|
|
305
|
+
/** Per chunk key: the voxels merged since the stored copy. */
|
|
306
|
+
this.arrivals = new Map();
|
|
307
|
+
/** Confirmed values merged so far: without server times, what was merged after a read was asked for is newer. */
|
|
308
|
+
this.mergeSeq = 0;
|
|
228
309
|
/** Edits waiting to be sent, in order. */
|
|
229
310
|
this.sendQueue = [];
|
|
230
311
|
/** Sent edits not yet answered, by sequence number. */
|
|
231
312
|
this.inFlightEdits = new Map();
|
|
232
313
|
/** Chunks being read again; a key mapped to true is read once more when that read ends. */
|
|
233
314
|
this.reloading = new Map();
|
|
234
|
-
/** The area the last {@link ensureAround} covered, for a catch-up. */
|
|
235
|
-
this.lastArea = null;
|
|
236
315
|
/** When the realtime connection was last lost (while the store had loaded an area). */
|
|
237
316
|
this.disconnectedAt = null;
|
|
238
317
|
this.wasConnected = false;
|
|
@@ -258,18 +337,23 @@ export class ChunkStore {
|
|
|
258
337
|
y: Number(notification.chunkY),
|
|
259
338
|
z: Number(notification.chunkZ),
|
|
260
339
|
};
|
|
261
|
-
const
|
|
340
|
+
const key = chunkKey(coord);
|
|
341
|
+
let chunk = this.chunks.get(key);
|
|
342
|
+
// A chunk being read keeps what arrives meanwhile: its copy merges under it.
|
|
343
|
+
if (!chunk && (this.inFlight.has(key) || this.hydrating.has(key)))
|
|
344
|
+
chunk = this.ensureEntry(coord);
|
|
262
345
|
if (!chunk)
|
|
263
346
|
return; // only merge into chunks we track
|
|
264
|
-
|
|
347
|
+
const timeMs = Number(notification.epochMillis);
|
|
348
|
+
if (this.takeEcho(chunk, notification, timeMs))
|
|
265
349
|
return;
|
|
266
|
-
this.
|
|
350
|
+
this.confirm(chunk, notification.voxelX, notification.voxelY, notification.voxelZ, notification.voxelType, notification.voxelState || undefined, timeMs);
|
|
267
351
|
}));
|
|
268
352
|
// A refusal names the sequence number of the send it answers.
|
|
269
353
|
ctx.onDispose(ctx.on('genericError', (notification) => {
|
|
270
354
|
const entry = this.inFlightEdits.get(notification.sequenceNumber);
|
|
271
355
|
if (entry)
|
|
272
|
-
this.
|
|
356
|
+
this.finishEdit(entry.key, entry.edit, String(notification.errorCode));
|
|
273
357
|
}));
|
|
274
358
|
if (config.reloadReplacedChunks ?? true) {
|
|
275
359
|
ctx.onDispose(ctx.on('chunkChanged', (notification) => {
|
|
@@ -281,7 +365,7 @@ export class ChunkStore {
|
|
|
281
365
|
const chunk = this.chunks.get(chunkKey(coord));
|
|
282
366
|
if (!chunk)
|
|
283
367
|
return;
|
|
284
|
-
const updatedAt = Number(notification.updatedAtMs);
|
|
368
|
+
const updatedAt = this.clampTime(Number(notification.updatedAtMs));
|
|
285
369
|
if (chunk.storedAt !== null && updatedAt > 0 && chunk.storedAt >= updatedAt)
|
|
286
370
|
return;
|
|
287
371
|
// The server's copy is newer than a seed waiting to be written back over it.
|
|
@@ -363,6 +447,22 @@ export class ChunkStore {
|
|
|
363
447
|
return undefined;
|
|
364
448
|
return chunk.voxelStates.get(this.voxelIndex(x, y, z));
|
|
365
449
|
}
|
|
450
|
+
/**
|
|
451
|
+
* When the server confirmed the value the store holds for a voxel (epoch ms): the last change it
|
|
452
|
+
* merged there, or the stored row's write. Null when unknown. An unanswered local edit shows over
|
|
453
|
+
* that value and does not change it.
|
|
454
|
+
*/
|
|
455
|
+
confirmedAt(coord, x, y, z) {
|
|
456
|
+
const key = chunkKey(coord);
|
|
457
|
+
const cell = this.pendingCells.get(`${key}|${voxelKey(x, y, z)}`);
|
|
458
|
+
if (cell)
|
|
459
|
+
return cell.baseTimeMs > 0 ? cell.baseTimeMs : null;
|
|
460
|
+
const chunk = this.chunks.get(key);
|
|
461
|
+
if (!chunk)
|
|
462
|
+
return null;
|
|
463
|
+
const at = this.timeOf(chunk, voxelKey(x, y, z));
|
|
464
|
+
return at > 0 ? at : null;
|
|
465
|
+
}
|
|
366
466
|
/** Subscribe to per-chunk changes (loads, merges, edits). @returns off. */
|
|
367
467
|
onChunkChanged(listener) {
|
|
368
468
|
this.changeListeners.add(listener);
|
|
@@ -422,7 +522,6 @@ export class ChunkStore {
|
|
|
422
522
|
*/
|
|
423
523
|
async ensureAround(center, radius) {
|
|
424
524
|
const range = Math.max(0, Math.min(8, Math.floor(radius)));
|
|
425
|
-
this.lastArea = { center: { ...center }, radius: range };
|
|
426
525
|
const around = chunksAround(center, range);
|
|
427
526
|
const wanted = around.filter((coord) => {
|
|
428
527
|
const key = chunkKey(coord);
|
|
@@ -437,17 +536,19 @@ export class ChunkStore {
|
|
|
437
536
|
for (const key of wantedKeys)
|
|
438
537
|
this.inFlight.add(key);
|
|
439
538
|
const pending = new Set(wantedKeys);
|
|
539
|
+
const requestedSeq = this.mergeSeq;
|
|
440
540
|
try {
|
|
441
541
|
for (const box of coverWithBoxes(wanted)) {
|
|
442
542
|
await this.readBox(box, (coord, chunk) => {
|
|
443
543
|
const key = chunkKey(coord);
|
|
444
|
-
|
|
544
|
+
// Not asked for, or evicted while the read was out.
|
|
545
|
+
if (!wantedKeys.has(key) || !this.inFlight.has(key))
|
|
445
546
|
return;
|
|
446
|
-
// A chunk
|
|
547
|
+
// A chunk loaded meanwhile, or a seed, keeps what it has.
|
|
447
548
|
if (this.chunks.get(key)?.loadState === 'loaded' || this.chunks.get(key)?.dirty)
|
|
448
549
|
return;
|
|
449
550
|
returned.add(key);
|
|
450
|
-
this.applyServerChunk(coord, chunk.voxels ?? null, chunk.chunkState ?? null, chunk.updatedAt);
|
|
551
|
+
this.applyServerChunk(coord, chunk.voxels ?? null, chunk.chunkState ?? null, chunk.updatedAt, requestedSeq);
|
|
451
552
|
});
|
|
452
553
|
// Requested-but-absent chunks have never been stored server-side.
|
|
453
554
|
for (const coord of wanted) {
|
|
@@ -455,7 +556,7 @@ export class ChunkStore {
|
|
|
455
556
|
if (!pending.has(key) || !inBox(box, coord))
|
|
456
557
|
continue;
|
|
457
558
|
pending.delete(key);
|
|
458
|
-
if (!returned.has(key))
|
|
559
|
+
if (!returned.has(key) && this.inFlight.has(key))
|
|
459
560
|
this.markMissing(coord);
|
|
460
561
|
}
|
|
461
562
|
}
|
|
@@ -551,97 +652,83 @@ export class ChunkStore {
|
|
|
551
652
|
}
|
|
552
653
|
}
|
|
553
654
|
/**
|
|
554
|
-
* Hydrate one chunk's sparse voxel states (and chunk state) via a
|
|
555
|
-
*
|
|
556
|
-
*
|
|
655
|
+
* Hydrate one chunk's sparse voxel states (and chunk state) via a single-chunk fetch — bulk loads
|
|
656
|
+
* omit them — merged as a load is: a value merged since the read was asked for that is newer than
|
|
657
|
+
* the copy stays, and this client's unanswered edits show over it. Resolves once that read has
|
|
658
|
+
* ended: true when its copy was applied (or a newer one was already held), false when the chunk
|
|
659
|
+
* was never stored or was evicted while the read was out.
|
|
557
660
|
*/
|
|
558
661
|
async hydrate(coord) {
|
|
662
|
+
const key = chunkKey(coord);
|
|
663
|
+
const before = this.chunks.get(key);
|
|
664
|
+
const requestedSeq = this.mergeSeq;
|
|
559
665
|
const full = await this.ctx.client.chunks.get({
|
|
560
666
|
appId: this.ctx.appId,
|
|
561
667
|
coordinates: toChunkInput(coord),
|
|
562
668
|
});
|
|
669
|
+
if (before && this.chunks.get(key) !== before)
|
|
670
|
+
return false;
|
|
563
671
|
if (!full) {
|
|
564
672
|
this.markMissing(coord);
|
|
565
|
-
return;
|
|
673
|
+
return false;
|
|
566
674
|
}
|
|
567
675
|
const chunk = this.ensureEntry(coord);
|
|
568
|
-
|
|
569
|
-
|
|
676
|
+
const rowMs = storedAtMs(full.updatedAt);
|
|
677
|
+
const heldTo = rowMs === null ? null : this.clampTime(rowMs);
|
|
678
|
+
// A newer copy came first (another read's): the store already holds this one's news.
|
|
679
|
+
if (chunk.hydrated && heldTo !== null && chunk.storedAt !== null && chunk.storedAt > heldTo)
|
|
680
|
+
return true;
|
|
570
681
|
chunk.chunkState = this.decodeChunkState(full.chunkState ?? null);
|
|
571
682
|
chunk.voxelStatesTruncated = full.voxelStatesTruncated === true;
|
|
572
|
-
chunk.
|
|
573
|
-
// The stored copy replaces what the store held for the chunk (a `world.set_chunk` drops the
|
|
574
|
-
// chunk's older edits); a state blob this codec cannot read keeps the one cached before.
|
|
575
|
-
const cachedStates = new Map(chunk.voxelStates);
|
|
576
|
-
const cachedOverlay = new Map(chunk.overlay);
|
|
577
|
-
chunk.voxelStates.clear();
|
|
578
|
-
chunk.overlay.clear();
|
|
579
|
-
for (const entry of full.voxelStates ?? []) {
|
|
580
|
-
const { x, y, z } = entry.voxelCoord;
|
|
581
|
-
if (!fitsDenseGrid(x, y, z, entry.voxelType)) {
|
|
582
|
-
const key = voxelKey(x, y, z);
|
|
583
|
-
let state = entry.state ? cachedOverlay.get(key)?.state : undefined;
|
|
584
|
-
if (entry.state) {
|
|
585
|
-
try {
|
|
586
|
-
state = this.voxelStateCodec.decode(entry.state);
|
|
587
|
-
}
|
|
588
|
-
catch {
|
|
589
|
-
// As below: a foreign blob keeps the state already cached.
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
this.putOverlay(chunk, x, y, z, entry.voxelType, state);
|
|
593
|
-
continue;
|
|
594
|
-
}
|
|
595
|
-
const index = this.voxelIndex(x, y, z);
|
|
596
|
-
// A chunk stored with `voxels: null` still carries its recorded edits.
|
|
597
|
-
if (!chunk.voxels)
|
|
598
|
-
chunk.voxels = new Uint8Array(CHUNK_VOLUME);
|
|
599
|
-
chunk.voxels[index] = entry.voxelType;
|
|
600
|
-
if (entry.state) {
|
|
601
|
-
try {
|
|
602
|
-
chunk.voxelStates.set(index, this.voxelStateCodec.decode(entry.state));
|
|
603
|
-
}
|
|
604
|
-
catch {
|
|
605
|
-
// Foreign/legacy blobs keep the state cached before; the dense type still applied.
|
|
606
|
-
const cached = cachedStates.get(index);
|
|
607
|
-
if (cached !== undefined)
|
|
608
|
-
chunk.voxelStates.set(index, cached);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
this.reapplyPendingEdits(chunk);
|
|
683
|
+
this.adoptCopy(chunk, full.voxels ?? null, full.voxelStates ?? [], rowMs, requestedSeq);
|
|
613
684
|
chunk.loadState = 'loaded';
|
|
614
685
|
chunk.hydrated = true;
|
|
615
686
|
this.touch(chunk);
|
|
687
|
+
return true;
|
|
616
688
|
}
|
|
617
689
|
/**
|
|
618
|
-
* Read
|
|
619
|
-
*
|
|
620
|
-
*
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
*
|
|
690
|
+
* Read what changed since `since` in every chunk the store tracks (18.31.0; the last area
|
|
691
|
+
* {@link ensureAround} loaded before), or in `boxes`: the recorded edits
|
|
692
|
+
* (`voxels.listByDistance`, when the client has `voxels`) merge into the chunks the store tracks
|
|
693
|
+
* by their `createdAt`, the never-stored (`missing`) ones included, and a chunk whose stored row
|
|
694
|
+
* was written since then (`chunks.byDistance` with `since`: `updateChunk`, `world.set_chunk`) is
|
|
695
|
+
* read again. The area is read in boxes of at most 17³ chunks; a box larger than that is cut, and
|
|
696
|
+
* more than 64 such boxes is refused (resolves false). The store calls it when the realtime
|
|
697
|
+
* connection comes back ({@link ChunkStoreConfig.catchUpOnReconnect}), from 30 s before the
|
|
698
|
+
* connection dropped. Needs ck-api v2.67.0. Resolves whether anything was read.
|
|
624
699
|
*/
|
|
625
|
-
async catchUp(since) {
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
}, since);
|
|
700
|
+
async catchUp(since, boxes) {
|
|
701
|
+
let read;
|
|
702
|
+
if (boxes) {
|
|
703
|
+
let pieces = 0;
|
|
704
|
+
for (const box of boxes)
|
|
705
|
+
pieces += boxPieces(box.low, box.high);
|
|
706
|
+
if (pieces > MAX_CATCH_UP_BOXES)
|
|
707
|
+
return false;
|
|
708
|
+
read = boxes.flatMap((box) => cutBox(box.low, box.high));
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
read = tileBoxes([...this.chunks.values()].map((chunk) => chunk.coord));
|
|
712
|
+
}
|
|
713
|
+
if (read.length === 0)
|
|
714
|
+
return false;
|
|
715
|
+
const requestedSeq = this.mergeSeq;
|
|
716
|
+
const replaced = new Map();
|
|
643
717
|
const voxels = this.ctx.client.voxels;
|
|
644
|
-
|
|
718
|
+
for (const box of read) {
|
|
719
|
+
await this.readBox(box, (coord, stored) => {
|
|
720
|
+
const chunk = this.chunks.get(chunkKey(coord));
|
|
721
|
+
if (!chunk || chunk.dirty)
|
|
722
|
+
return;
|
|
723
|
+
const stamped = storedAtMs(stored.updatedAt);
|
|
724
|
+
const at = stamped === null ? null : this.clampTime(stamped);
|
|
725
|
+
if (chunk.loadState === 'loaded' && at !== null && chunk.storedAt !== null && chunk.storedAt >= at) {
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
replaced.set(chunk.key, coord);
|
|
729
|
+
}, since);
|
|
730
|
+
if (!voxels)
|
|
731
|
+
continue;
|
|
645
732
|
for (let skip = 0;;) {
|
|
646
733
|
const page = await whenNotBusy(() => voxels.listByDistance({
|
|
647
734
|
appId: this.ctx.appId,
|
|
@@ -653,14 +740,11 @@ export class ChunkStore {
|
|
|
653
740
|
}));
|
|
654
741
|
for (const group of page.chunks) {
|
|
655
742
|
const chunk = this.chunks.get(chunkKey(fromChunkInput(group.coordinates)));
|
|
656
|
-
if (!chunk
|
|
743
|
+
if (!chunk)
|
|
657
744
|
continue;
|
|
658
745
|
for (const edit of group.voxels) {
|
|
659
746
|
const { x, y, z } = edit.location;
|
|
660
|
-
|
|
661
|
-
if (this.livePendingEdits(`${chunk.key}|${voxelKey(x, y, z)}`).length > 0)
|
|
662
|
-
continue;
|
|
663
|
-
this.applyVoxel(chunk, x, y, z, edit.voxelType, edit.state ?? undefined);
|
|
747
|
+
this.confirm(chunk, x, y, z, edit.voxelType, edit.state ?? undefined, storedAtMs(edit.createdAt) ?? 0, requestedSeq);
|
|
664
748
|
}
|
|
665
749
|
}
|
|
666
750
|
const applied = page.limit ?? PAGE_SIZE;
|
|
@@ -669,7 +753,8 @@ export class ChunkStore {
|
|
|
669
753
|
skip = (page.skip ?? skip) + applied;
|
|
670
754
|
}
|
|
671
755
|
}
|
|
672
|
-
await Promise.all(replaced.map((coord) => this.reload(coord)));
|
|
756
|
+
await Promise.all([...replaced.values()].map((coord) => this.reload(coord)));
|
|
757
|
+
return true;
|
|
673
758
|
}
|
|
674
759
|
/**
|
|
675
760
|
* Edit one voxel: applies to the cache immediately (optimistic) and
|
|
@@ -688,14 +773,27 @@ export class ChunkStore {
|
|
|
688
773
|
return false;
|
|
689
774
|
const chunk = this.ensureEntry(input.chunk);
|
|
690
775
|
const optimistic = input.optimistic ?? true;
|
|
691
|
-
const
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
776
|
+
const key = `${chunk.key}|${voxelKey(voxel.x, voxel.y, voxel.z)}`;
|
|
777
|
+
this.expireEdits();
|
|
778
|
+
let cell = this.pendingCells.get(key);
|
|
779
|
+
if (!cell) {
|
|
780
|
+
// The value under the edits: what the store holds now, as the server confirmed it.
|
|
781
|
+
const vk = voxelKey(voxel.x, voxel.y, voxel.z);
|
|
782
|
+
const base = {
|
|
783
|
+
voxelType: this.voxelTypeAt(input.chunk, input.x, input.y, input.z),
|
|
784
|
+
};
|
|
785
|
+
const baseState = this.voxelStateAt(input.chunk, input.x, input.y, input.z);
|
|
786
|
+
if (baseState !== undefined)
|
|
787
|
+
base.state = baseState;
|
|
788
|
+
cell = {
|
|
789
|
+
coord: { ...input.chunk },
|
|
790
|
+
voxel,
|
|
791
|
+
edits: [],
|
|
792
|
+
base,
|
|
793
|
+
baseTimeMs: this.timeOf(chunk, vk),
|
|
794
|
+
baseArrival: this.arrivalOf(chunk, vk),
|
|
795
|
+
};
|
|
796
|
+
this.pendingCells.set(key, cell);
|
|
699
797
|
}
|
|
700
798
|
const edit = {
|
|
701
799
|
coord: { ...input.chunk },
|
|
@@ -705,11 +803,12 @@ export class ChunkStore {
|
|
|
705
803
|
voxelType: input.voxelType,
|
|
706
804
|
...(encodedState !== undefined ? { encodedState } : {}),
|
|
707
805
|
...(input.state !== undefined ? { state: input.state } : {}),
|
|
708
|
-
stale: !optimistic,
|
|
709
806
|
optimistic,
|
|
710
|
-
previous,
|
|
711
807
|
};
|
|
712
|
-
|
|
808
|
+
cell.edits.push(edit);
|
|
809
|
+
if (optimistic) {
|
|
810
|
+
this.applyVoxel(chunk, input.x, input.y, input.z, input.voxelType, undefined, input.state);
|
|
811
|
+
}
|
|
713
812
|
return new Promise((resolve, reject) => {
|
|
714
813
|
this.sendQueue.push({ key, edit, resolve, reject });
|
|
715
814
|
this.pump();
|
|
@@ -762,17 +861,35 @@ export class ChunkStore {
|
|
|
762
861
|
}
|
|
763
862
|
return failures;
|
|
764
863
|
}
|
|
765
|
-
/** Drop tracked chunks farther than `radius` from `center` (dirty ones kept). */
|
|
864
|
+
/** Drop tracked chunks farther than `radius` from `center` (dirty ones kept). Returns how many. */
|
|
766
865
|
pruneBeyond(center, radius) {
|
|
767
|
-
|
|
768
|
-
|
|
866
|
+
return this.pruneIf((chunk) => chunkDistance(chunk.coord, center) > radius);
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Drop one tracked chunk (18.31.0), as {@link pruneBeyond} does. Returns whether it was dropped:
|
|
870
|
+
* false when the store does not track it, or it is queued for write-back. A read of it that is
|
|
871
|
+
* out is not applied.
|
|
872
|
+
*/
|
|
873
|
+
evict(coord) {
|
|
874
|
+
const chunk = this.chunks.get(chunkKey(coord));
|
|
875
|
+
if (!chunk || chunk.dirty)
|
|
876
|
+
return false;
|
|
877
|
+
this.forget(chunk.key);
|
|
878
|
+
return true;
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Drop every tracked chunk `drop` returns true for (18.31.0), for a view that is not a cube (a
|
|
882
|
+
* sphere, a column, several players); chunks queued for write-back stay. Returns how many.
|
|
883
|
+
*/
|
|
884
|
+
pruneIf(drop) {
|
|
885
|
+
let dropped = 0;
|
|
886
|
+
for (const chunk of [...this.chunks.values()]) {
|
|
887
|
+
if (chunk.dirty || !drop(chunk))
|
|
769
888
|
continue;
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
this.hydrateFailures.delete(key);
|
|
773
|
-
this.revisionValue += 1;
|
|
774
|
-
}
|
|
889
|
+
this.forget(chunk.key);
|
|
890
|
+
dropped += 1;
|
|
775
891
|
}
|
|
892
|
+
return dropped;
|
|
776
893
|
}
|
|
777
894
|
// -- internals --------------------------------------------------------------
|
|
778
895
|
/**
|
|
@@ -879,40 +996,136 @@ export class ChunkStore {
|
|
|
879
996
|
this.reloading.delete(key);
|
|
880
997
|
}
|
|
881
998
|
}
|
|
882
|
-
applyServerChunk(coord, voxels, chunkState, updatedAt) {
|
|
999
|
+
applyServerChunk(coord, voxels, chunkState, updatedAt, requestedSeq) {
|
|
883
1000
|
const chunk = this.ensureEntry(coord);
|
|
884
|
-
if (voxels != null)
|
|
885
|
-
chunk.voxels = decodeBase64(voxels);
|
|
886
1001
|
chunk.chunkState = this.decodeChunkState(chunkState);
|
|
887
|
-
chunk
|
|
888
|
-
this.reapplyPendingEdits(chunk);
|
|
1002
|
+
this.adoptCopy(chunk, voxels, undefined, storedAtMs(updatedAt), requestedSeq);
|
|
889
1003
|
chunk.loadState = 'loaded';
|
|
890
1004
|
this.touch(chunk);
|
|
891
1005
|
}
|
|
892
1006
|
/**
|
|
893
|
-
*
|
|
894
|
-
*
|
|
1007
|
+
* Apply a stored copy (`entries` undefined: a bulk read, which has no `voxelStates`) under what
|
|
1008
|
+
* the store merged since the read was asked for: a value newer than the copy's for its voxel
|
|
1009
|
+
* stays (by the server's times; without them, a value merged after `requestedSeq`). This
|
|
1010
|
+
* client's unanswered edits show over the copy, which becomes their confirmed value unless the
|
|
1011
|
+
* one held is newer.
|
|
895
1012
|
*/
|
|
896
|
-
|
|
1013
|
+
adoptCopy(chunk, voxels, entries, rowMs, requestedSeq) {
|
|
1014
|
+
// Every server time is held to MAX_FUTURE_SKEW_MS past this machine's clock.
|
|
1015
|
+
const row = this.clampTime(rowMs ?? 0);
|
|
1016
|
+
const entryTimes = new Map();
|
|
1017
|
+
for (const entry of entries ?? []) {
|
|
1018
|
+
const vk = voxelKey(entry.voxelCoord.x, entry.voxelCoord.y, entry.voxelCoord.z);
|
|
1019
|
+
const entryMs = this.clampTime(storedAtMs(entry.updatedAt) ?? 0);
|
|
1020
|
+
// A voxel the copy lists twice counts at its latest time.
|
|
1021
|
+
entryTimes.set(vk, Math.max(entryTimes.get(vk) ?? 0, entryMs > 0 ? entryMs : row));
|
|
1022
|
+
}
|
|
1023
|
+
const copyTime = (vk) => entryTimes.get(vk) ?? row;
|
|
1024
|
+
const heldIsNewer = (vk, heldMs, heldArrival) => {
|
|
1025
|
+
const copyMs = copyTime(vk);
|
|
1026
|
+
return copyMs > 0 ? heldMs > copyMs : heldArrival > requestedSeq;
|
|
1027
|
+
};
|
|
897
1028
|
const prefix = `${chunk.key}|`;
|
|
898
|
-
|
|
899
|
-
|
|
1029
|
+
const kept = [];
|
|
1030
|
+
const replaced = [];
|
|
1031
|
+
for (const [vk, arrival] of this.arrivals.get(chunk.key) ?? []) {
|
|
1032
|
+
if (this.pendingCells.has(prefix + vk))
|
|
900
1033
|
continue;
|
|
901
|
-
const
|
|
902
|
-
if (
|
|
1034
|
+
const heldMs = this.timeOf(chunk, vk);
|
|
1035
|
+
if (!heldIsNewer(vk, heldMs, arrival.seq)) {
|
|
1036
|
+
replaced.push(arrival);
|
|
903
1037
|
continue;
|
|
904
|
-
|
|
905
|
-
const
|
|
1038
|
+
}
|
|
1039
|
+
const { x, y, z } = arrival;
|
|
1040
|
+
const value = {
|
|
1041
|
+
...arrival,
|
|
906
1042
|
voxelType: this.voxelTypeAt(chunk.coord, x, y, z),
|
|
1043
|
+
timeMs: heldMs,
|
|
907
1044
|
};
|
|
908
|
-
const
|
|
909
|
-
if (
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1045
|
+
const state = this.voxelStateAt(chunk.coord, x, y, z);
|
|
1046
|
+
if (state !== undefined)
|
|
1047
|
+
value.state = state;
|
|
1048
|
+
kept.push(value);
|
|
1049
|
+
}
|
|
1050
|
+
chunk.storedAt = row > 0 ? row : null;
|
|
1051
|
+
if (voxels != null)
|
|
1052
|
+
chunk.voxels = decodeBase64(voxels);
|
|
1053
|
+
this.cellTimes.delete(chunk.key);
|
|
1054
|
+
this.arrivals.delete(chunk.key);
|
|
1055
|
+
if (entries) {
|
|
1056
|
+
// The stored copy replaces what the store held for the chunk (a `world.set_chunk` drops the
|
|
1057
|
+
// chunk's older edits); a state blob this codec cannot read keeps the one cached before.
|
|
1058
|
+
const cachedStates = new Map(chunk.voxelStates);
|
|
1059
|
+
const cachedOverlay = new Map(chunk.overlay);
|
|
1060
|
+
chunk.voxelStates.clear();
|
|
1061
|
+
chunk.overlay.clear();
|
|
1062
|
+
for (const entry of entries) {
|
|
1063
|
+
const { x, y, z } = entry.voxelCoord;
|
|
1064
|
+
const vk = voxelKey(x, y, z);
|
|
1065
|
+
const entryMs = entryTimes.get(vk) ?? row;
|
|
1066
|
+
if (entryMs > row)
|
|
1067
|
+
this.mapOf(this.cellTimes, chunk.key).set(vk, entryMs);
|
|
1068
|
+
if (!fitsDenseGrid(x, y, z, entry.voxelType)) {
|
|
1069
|
+
let state = entry.state ? cachedOverlay.get(vk)?.state : undefined;
|
|
1070
|
+
if (entry.state) {
|
|
1071
|
+
try {
|
|
1072
|
+
state = this.voxelStateCodec.decode(entry.state);
|
|
1073
|
+
}
|
|
1074
|
+
catch {
|
|
1075
|
+
// As below: a foreign blob keeps the state already cached.
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
this.putOverlay(chunk, x, y, z, entry.voxelType, state);
|
|
1079
|
+
continue;
|
|
1080
|
+
}
|
|
1081
|
+
const index = this.voxelIndex(x, y, z);
|
|
1082
|
+
// A chunk stored with `voxels: null` still carries its recorded edits.
|
|
1083
|
+
if (!chunk.voxels)
|
|
1084
|
+
chunk.voxels = new Uint8Array(CHUNK_VOLUME);
|
|
1085
|
+
chunk.voxels[index] = entry.voxelType;
|
|
1086
|
+
if (entry.state) {
|
|
1087
|
+
try {
|
|
1088
|
+
chunk.voxelStates.set(index, this.voxelStateCodec.decode(entry.state));
|
|
1089
|
+
}
|
|
1090
|
+
catch {
|
|
1091
|
+
// Foreign/legacy blobs keep the state cached before; the dense type still applied.
|
|
1092
|
+
const cached = cachedStates.get(index);
|
|
1093
|
+
if (cached !== undefined)
|
|
1094
|
+
chunk.voxelStates.set(index, cached);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
else {
|
|
1100
|
+
// A bulk copy has no states: a merged voxel it supersedes keeps none of its own.
|
|
1101
|
+
for (const { x, y, z } of replaced) {
|
|
1102
|
+
chunk.overlay.delete(voxelKey(x, y, z));
|
|
1103
|
+
if (inDenseGrid(x, y, z))
|
|
1104
|
+
chunk.voxelStates.delete(this.voxelIndex(x, y, z));
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
for (const value of kept) {
|
|
1108
|
+
this.writeVoxel(chunk, value.x, value.y, value.z, value.voxelType, value.state);
|
|
1109
|
+
this.setTime(chunk, value.x, value.y, value.z, value.timeMs, value.seq);
|
|
1110
|
+
}
|
|
1111
|
+
for (const [key, cell] of this.pendingCells) {
|
|
1112
|
+
if (!key.startsWith(prefix))
|
|
914
1113
|
continue;
|
|
915
|
-
|
|
1114
|
+
const { x, y, z } = cell.voxel;
|
|
1115
|
+
const vk = voxelKey(x, y, z);
|
|
1116
|
+
if (!heldIsNewer(vk, cell.baseTimeMs, cell.baseArrival)) {
|
|
1117
|
+
const base = { voxelType: this.voxelTypeAt(chunk.coord, x, y, z) };
|
|
1118
|
+
const state = this.voxelStateAt(chunk.coord, x, y, z);
|
|
1119
|
+
if (state !== undefined)
|
|
1120
|
+
base.state = state;
|
|
1121
|
+
cell.base = base;
|
|
1122
|
+
const copyMs = copyTime(vk);
|
|
1123
|
+
if (copyMs > 0)
|
|
1124
|
+
cell.baseTimeMs = copyMs;
|
|
1125
|
+
cell.baseArrival = 0;
|
|
1126
|
+
}
|
|
1127
|
+
const shown = shownEdit(cell) ?? cell.base;
|
|
1128
|
+
this.writeVoxel(chunk, x, y, z, shown.voxelType, shown.state);
|
|
916
1129
|
}
|
|
917
1130
|
}
|
|
918
1131
|
/** Send queued edits while the pace and the in-flight limit allow. */
|
|
@@ -927,7 +1140,7 @@ export class ChunkStore {
|
|
|
927
1140
|
this.sendTokens >= 1) {
|
|
928
1141
|
const send = this.sendQueue.shift();
|
|
929
1142
|
// An edit undone while it waited (a pruned chunk's) is not sent.
|
|
930
|
-
if (!this.
|
|
1143
|
+
if (!this.pendingCells.get(send.key)?.edits.includes(send.edit)) {
|
|
931
1144
|
send.resolve(false);
|
|
932
1145
|
continue;
|
|
933
1146
|
}
|
|
@@ -977,10 +1190,10 @@ export class ChunkStore {
|
|
|
977
1190
|
}
|
|
978
1191
|
sent.then((accepted) => {
|
|
979
1192
|
if (!accepted)
|
|
980
|
-
this.
|
|
1193
|
+
this.finishEdit(key, edit, 'NOT_SENT');
|
|
981
1194
|
send.resolve(accepted);
|
|
982
1195
|
}, (error) => {
|
|
983
|
-
this.
|
|
1196
|
+
this.finishEdit(key, edit, 'SEND_FAILED');
|
|
984
1197
|
send.reject(error);
|
|
985
1198
|
});
|
|
986
1199
|
}
|
|
@@ -989,39 +1202,51 @@ export class ChunkStore {
|
|
|
989
1202
|
const cutoff = this.now() - this.editTimeoutMs;
|
|
990
1203
|
for (const { key, edit } of [...this.inFlightEdits.values()]) {
|
|
991
1204
|
if (edit.sentAt !== undefined && edit.sentAt < cutoff)
|
|
992
|
-
this.
|
|
1205
|
+
this.finishEdit(key, edit, 'TIMEOUT');
|
|
993
1206
|
}
|
|
994
1207
|
}
|
|
995
1208
|
/**
|
|
996
|
-
*
|
|
997
|
-
*
|
|
1209
|
+
* End an edit: echoed (`reason` null; at `echoTimeMs` it becomes the voxel's confirmed value,
|
|
1210
|
+
* unless the one held is newer), or one the server will not apply (reported). The voxel then
|
|
1211
|
+
* shows its next unanswered edit, or its confirmed value. Lets the next queued edit go.
|
|
998
1212
|
*/
|
|
999
|
-
|
|
1000
|
-
const
|
|
1001
|
-
const index = edits
|
|
1002
|
-
if (!
|
|
1213
|
+
finishEdit(key, edit, reason, echoTimeMs = 0) {
|
|
1214
|
+
const cell = this.pendingCells.get(key);
|
|
1215
|
+
const index = cell?.edits.indexOf(edit) ?? -1;
|
|
1216
|
+
if (!cell || index < 0)
|
|
1003
1217
|
return;
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
this.pendingEdits.delete(key);
|
|
1218
|
+
const wasShown = shownEdit(cell) === edit;
|
|
1219
|
+
cell.edits.splice(index, 1);
|
|
1007
1220
|
if (this.inFlightEdits.get(edit.sequenceNumber)?.edit === edit) {
|
|
1008
1221
|
this.inFlightEdits.delete(edit.sequenceNumber);
|
|
1009
1222
|
}
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
next.previous = edit.previous;
|
|
1223
|
+
if (reason === null) {
|
|
1224
|
+
const at = this.clampTime(echoTimeMs);
|
|
1225
|
+
if (at <= 0 || at >= cell.baseTimeMs) {
|
|
1226
|
+
cell.base = { voxelType: edit.voxelType, ...(edit.state !== undefined ? { state: edit.state } : {}) };
|
|
1227
|
+
if (at > 0)
|
|
1228
|
+
cell.baseTimeMs = at;
|
|
1229
|
+
cell.baseArrival = ++this.mergeSeq;
|
|
1018
1230
|
}
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1231
|
+
}
|
|
1232
|
+
const chunk = this.chunks.get(chunkKey(edit.coord));
|
|
1233
|
+
const { x, y, z } = edit.voxel;
|
|
1234
|
+
if (cell.edits.length === 0) {
|
|
1235
|
+
this.pendingCells.delete(key);
|
|
1236
|
+
if (chunk) {
|
|
1237
|
+
const vk = voxelKey(x, y, z);
|
|
1238
|
+
this.setTime(chunk, x, y, z, Math.max(cell.baseTimeMs, this.timeOf(chunk, vk)), cell.baseArrival);
|
|
1239
|
+
this.show(chunk, x, y, z, cell.base);
|
|
1023
1240
|
}
|
|
1024
1241
|
}
|
|
1242
|
+
else if (chunk) {
|
|
1243
|
+
this.show(chunk, x, y, z, shownEdit(cell) ?? cell.base);
|
|
1244
|
+
}
|
|
1245
|
+
if (reason === null) {
|
|
1246
|
+
this.pump();
|
|
1247
|
+
return;
|
|
1248
|
+
}
|
|
1249
|
+
const restored = wasShown && chunk !== undefined;
|
|
1025
1250
|
const refusal = {
|
|
1026
1251
|
coord: edit.coord,
|
|
1027
1252
|
voxel: edit.voxel,
|
|
@@ -1057,47 +1282,112 @@ export class ChunkStore {
|
|
|
1057
1282
|
this.seed(coord, generated.voxels, { writeBack: generated.writeBack ?? true });
|
|
1058
1283
|
}
|
|
1059
1284
|
}
|
|
1060
|
-
rememberEdit(chunk, voxel, edit) {
|
|
1061
|
-
const key = `${chunk.key}|${voxelKey(voxel.x, voxel.y, voxel.z)}`;
|
|
1062
|
-
const edits = this.livePendingEdits(key);
|
|
1063
|
-
edits.push(edit);
|
|
1064
|
-
this.pendingEdits.set(key, edits);
|
|
1065
|
-
return key;
|
|
1066
|
-
}
|
|
1067
|
-
/** The pending edits for one voxel, oldest first, after undoing the ones that timed out. */
|
|
1068
|
-
livePendingEdits(key) {
|
|
1069
|
-
this.expireEdits();
|
|
1070
|
-
return this.pendingEdits.get(key) ?? [];
|
|
1071
|
-
}
|
|
1072
1285
|
/**
|
|
1073
|
-
*
|
|
1074
|
-
*
|
|
1075
|
-
* applied optimistically is not applied again (no second change event), and an echo is never
|
|
1076
|
-
* applied over a newer local edit of the same voxel. Another client's edit is applied, and
|
|
1077
|
-
* marks the pending local edits of that voxel stale so their echoes restore them.
|
|
1286
|
+
* Whether a realtime voxel edit is the echo of one of this client's unanswered edits (sender uuid,
|
|
1287
|
+
* sequence number, voxel, type and state); one is ended as echoed at the notification's time.
|
|
1078
1288
|
*/
|
|
1079
|
-
takeEcho(chunk, notification) {
|
|
1289
|
+
takeEcho(chunk, notification, timeMs) {
|
|
1080
1290
|
const key = `${chunk.key}|${voxelKey(notification.voxelX, notification.voxelY, notification.voxelZ)}`;
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1291
|
+
this.expireEdits();
|
|
1292
|
+
const cell = this.pendingCells.get(key);
|
|
1293
|
+
if (!cell)
|
|
1294
|
+
return false;
|
|
1295
|
+
const echoed = cell.edits.find((edit) => edit.sentAt !== undefined &&
|
|
1085
1296
|
edit.uuid === notification.uuid &&
|
|
1086
|
-
edit.sequenceNumber === notification.sequenceNumber
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
return
|
|
1297
|
+
edit.sequenceNumber === notification.sequenceNumber &&
|
|
1298
|
+
edit.voxelType === notification.voxelType &&
|
|
1299
|
+
sameEncodedState(edit.encodedState, notification.voxelState));
|
|
1300
|
+
if (!echoed)
|
|
1301
|
+
return false;
|
|
1302
|
+
this.finishEdit(key, echoed, null, timeMs);
|
|
1303
|
+
return true;
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Merge a confirmed value at the server's time for it. Without one (0), it is new unless it came
|
|
1307
|
+
* from a read asked for at `readSeq` and the voxel changed after that. Under this client's
|
|
1308
|
+
* unanswered edits it becomes their confirmed value, and the voxel keeps showing the newest edit.
|
|
1309
|
+
*/
|
|
1310
|
+
confirm(chunk, x, y, z, voxelType, encodedState, timeMs, readSeq = 0) {
|
|
1311
|
+
const vk = voxelKey(x, y, z);
|
|
1312
|
+
const at = this.clampTime(timeMs);
|
|
1313
|
+
const older = (heldMs, heldArrival) => at > 0 ? at < heldMs : readSeq > 0 && heldArrival > readSeq;
|
|
1314
|
+
let state;
|
|
1315
|
+
if (encodedState) {
|
|
1316
|
+
try {
|
|
1317
|
+
state = this.voxelStateCodec.decode(encodedState);
|
|
1318
|
+
}
|
|
1319
|
+
catch {
|
|
1320
|
+
state = undefined;
|
|
1321
|
+
}
|
|
1091
1322
|
}
|
|
1092
|
-
const
|
|
1093
|
-
if (
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1323
|
+
const cell = this.pendingCells.get(`${chunk.key}|${vk}`);
|
|
1324
|
+
if (cell) {
|
|
1325
|
+
if (older(cell.baseTimeMs, cell.baseArrival))
|
|
1326
|
+
return;
|
|
1327
|
+
cell.base = { voxelType, ...(state !== undefined ? { state } : {}) };
|
|
1328
|
+
if (at > 0)
|
|
1329
|
+
cell.baseTimeMs = at;
|
|
1330
|
+
cell.baseArrival = ++this.mergeSeq;
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
const heldMs = this.timeOf(chunk, vk);
|
|
1334
|
+
if (older(heldMs, this.arrivalOf(chunk, vk)))
|
|
1335
|
+
return;
|
|
1336
|
+
this.setTime(chunk, x, y, z, at > 0 ? at : heldMs, ++this.mergeSeq);
|
|
1337
|
+
this.writeVoxel(chunk, x, y, z, voxelType, state);
|
|
1338
|
+
this.touch(chunk);
|
|
1339
|
+
}
|
|
1340
|
+
/** Show a value at a voxel; a change event only when it differs from what the voxel shows. */
|
|
1341
|
+
show(chunk, x, y, z, value) {
|
|
1342
|
+
if (this.voxelTypeAt(chunk.coord, x, y, z) === value.voxelType &&
|
|
1343
|
+
this.voxelStateAt(chunk.coord, x, y, z) === value.state) {
|
|
1344
|
+
return;
|
|
1098
1345
|
}
|
|
1099
|
-
|
|
1100
|
-
|
|
1346
|
+
this.writeVoxel(chunk, x, y, z, value.voxelType, value.state);
|
|
1347
|
+
this.touch(chunk);
|
|
1348
|
+
}
|
|
1349
|
+
/** A server time, no further past this machine's clock than {@link MAX_FUTURE_SKEW_MS}; 0 when unknown. */
|
|
1350
|
+
clampTime(timeMs) {
|
|
1351
|
+
if (!(timeMs > 0))
|
|
1352
|
+
return 0;
|
|
1353
|
+
return Math.min(timeMs, Date.now() + MAX_FUTURE_SKEW_MS);
|
|
1354
|
+
}
|
|
1355
|
+
/** The server's time for a voxel's value: its own, or the stored row's (0 when unknown). */
|
|
1356
|
+
timeOf(chunk, vk) {
|
|
1357
|
+
return this.cellTimes.get(chunk.key)?.get(vk) ?? chunk.storedAt ?? 0;
|
|
1358
|
+
}
|
|
1359
|
+
/** The merge a voxel's value arrived in (0: the stored copy). */
|
|
1360
|
+
arrivalOf(chunk, vk) {
|
|
1361
|
+
return this.arrivals.get(chunk.key)?.get(vk)?.seq ?? 0;
|
|
1362
|
+
}
|
|
1363
|
+
/** Record a voxel's server time and the merge it arrived in (0: the stored copy). */
|
|
1364
|
+
setTime(chunk, x, y, z, timeMs, arrival) {
|
|
1365
|
+
const vk = voxelKey(x, y, z);
|
|
1366
|
+
if (timeMs > (chunk.storedAt ?? 0))
|
|
1367
|
+
this.mapOf(this.cellTimes, chunk.key).set(vk, timeMs);
|
|
1368
|
+
else
|
|
1369
|
+
this.cellTimes.get(chunk.key)?.delete(vk);
|
|
1370
|
+
if (arrival > 0)
|
|
1371
|
+
this.mapOf(this.arrivals, chunk.key).set(vk, { x, y, z, seq: arrival });
|
|
1372
|
+
else
|
|
1373
|
+
this.arrivals.get(chunk.key)?.delete(vk);
|
|
1374
|
+
}
|
|
1375
|
+
mapOf(maps, key) {
|
|
1376
|
+
let map = maps.get(key);
|
|
1377
|
+
if (!map) {
|
|
1378
|
+
map = new Map();
|
|
1379
|
+
maps.set(key, map);
|
|
1380
|
+
}
|
|
1381
|
+
return map;
|
|
1382
|
+
}
|
|
1383
|
+
/** Stop tracking a chunk. A read of it that is out is not applied. */
|
|
1384
|
+
forget(key) {
|
|
1385
|
+
this.chunks.delete(key);
|
|
1386
|
+
this.inFlight.delete(key);
|
|
1387
|
+
this.hydrateFailures.delete(key);
|
|
1388
|
+
this.cellTimes.delete(key);
|
|
1389
|
+
this.arrivals.delete(key);
|
|
1390
|
+
this.revisionValue += 1;
|
|
1101
1391
|
}
|
|
1102
1392
|
applyVoxel(chunk, x, y, z, voxelType, encodedState, decodedState) {
|
|
1103
1393
|
let state = decodedState;
|