castle-web-cli 0.4.230 → 0.4.232
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/dist/castle-host/host.js +81 -0
- package/dist/devSaveStorage.d.ts +36 -0
- package/dist/devSaveStorage.js +469 -0
- package/dist/devSessionChild.js +22 -12
- package/dist/devSessionServer.js +8 -2
- package/dist/devSessionStorage.d.ts +9 -0
- package/dist/devSessionStorage.js +66 -1
- package/dist/init.js +1 -1
- package/dist/serve.js +5 -1
- package/dist/serverBundle.js +5 -3
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +148 -1
- package/kits/base/sdk/castle.d.ts +4 -0
- package/kits/base/sdk/castle.js +2 -0
- package/kits/base/sdk/commands.d.ts +15 -0
- package/kits/base/sdk/multiplayer.js +52 -4
- package/kits/base/sdk/save.d.ts +197 -0
- package/kits/base/sdk/save.js +525 -0
- package/kits/base/sdk/saveCoordinator.d.ts +39 -0
- package/kits/base/sdk/saveCoordinator.js +252 -0
- package/kits/base/sdk/server/storage.d.ts +3 -2
- package/kits/base/sdk/server/storage.js +25 -5
- package/kits/base/sdk/server/wrapper.d.ts +4 -0
- package/kits/base/sdk/server/wrapper.js +2 -0
- package/kits/base/sdk/store.d.ts +3 -2
- package/kits/base/sdk/store.js +5 -0
- package/kits/hub-game/CLAUDE.md +29 -4
- package/kits/hub-game/code/client/avatar.js +39 -0
- package/kits/hub-game/code/client/chat.js +12 -0
- package/kits/hub-game/code/client/chrome.js +237 -13
- package/kits/hub-game/code/client/grab.js +166 -7
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +36 -3
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/state.js +35 -1
- package/kits/hub-game/code/client/talk.js +10 -36
- package/kits/hub-game/code/client/tiers.js +505 -0
- package/kits/hub-game/code/client/together.js +144 -4
- package/kits/hub-game/code/client/wear.js +96 -14
- package/kits/hub-game/code/server/actors.js +5 -0
- package/kits/hub-game/code/server/grab.js +299 -13
- package/kits/hub-game/code/server/hub.js +162 -7
- package/kits/hub-game/code/server/manifest.js +80 -8
- package/kits/hub-game/code/server/npcs.js +21 -33
- package/kits/hub-game/code/server/party.js +75 -6
- package/kits/hub-game/code/server/say.js +8 -2
- package/kits/hub-game/code/server/wear.js +116 -11
- package/kits/hub-game/code/systems/hub.js +386 -21
- package/kits/hub-game/docs/contract.md +285 -19
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/test-chat.mjs +32 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +511 -0
- package/kits/hub-game/scripts/test-grab.mjs +857 -3
- package/kits/hub-game/scripts/test-hats.mjs +229 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +721 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
- package/kits/hub-game/scripts/test-manifest.mjs +122 -1
- package/kits/hub-game/scripts/test-npcs.mjs +34 -24
- package/kits/hub-game/scripts/test-party.mjs +108 -4
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-tiers.mjs +854 -0
- package/kits/hub-game/scripts/test-together.mjs +33 -1
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/physics-2d/CLAUDE.md +2 -0
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/scene.js +127 -0
- package/kits/physics-2d/package.json +1 -1
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +298 -0
- package/kits/physics-3d/CLAUDE.md +531 -0
- package/kits/physics-3d/behaviors/Player.jsx +437 -3
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/pxmodel-format.md +4 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -1
- package/kits/physics-3d/engine3d/actornode.js +253 -0
- package/kits/physics-3d/engine3d/nearshadows.js +600 -0
- package/kits/physics-3d/engine3d/optimize.js +49 -0
- package/kits/physics-3d/engine3d/orbitinput.js +277 -0
- package/kits/physics-3d/engine3d/unchanged.js +153 -0
- package/kits/physics-3d/engine3d/world3d.js +525 -11
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
- package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
- package/kits/physics-3d/scripts/test-camera-follow.mjs +543 -0
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
- package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +667 -0
- package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
- package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
- package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
- package/kits/physics-3d/systems/physics3d.js +273 -9
- package/package.json +1 -1
|
@@ -3,9 +3,13 @@
|
|
|
3
3
|
// near the player, this player's to simulate), offered to the place's `grab`
|
|
4
4
|
// hook, rate-limited and answered `hub-grab-deny` when refused; `hub.drop`
|
|
5
5
|
// tells a holder to let go; a thrown object is not slowed to a shove's speed
|
|
6
|
-
// while it flies. `
|
|
6
|
+
// while it flies. v1.27's `hub.keepFor` keeps an object for one player: anyone
|
|
7
|
+
// else's grab is refused before the hook, clients are told whose it is, and a
|
|
8
|
+
// place that never calls it is sent and asked exactly what it was.
|
|
9
|
+
// `npm run check` runs it; it needs nothing installed.
|
|
7
10
|
|
|
8
11
|
import assert from 'node:assert/strict';
|
|
12
|
+
import fs from 'node:fs';
|
|
9
13
|
import { register } from 'node:module';
|
|
10
14
|
|
|
11
15
|
const sceneStub = 'export const readScene = () => globalThis.__hubTestActors ?? [];';
|
|
@@ -60,6 +64,10 @@ function rigid(at) {
|
|
|
60
64
|
setLinvel: (v) => {
|
|
61
65
|
state.v = { ...v };
|
|
62
66
|
},
|
|
67
|
+
setTranslation: (at) => {
|
|
68
|
+
state.at = { ...at };
|
|
69
|
+
},
|
|
70
|
+
setRotation() {},
|
|
63
71
|
setEnabled() {},
|
|
64
72
|
};
|
|
65
73
|
}
|
|
@@ -73,7 +81,15 @@ function boot(options = {}) {
|
|
|
73
81
|
};
|
|
74
82
|
let hub = null;
|
|
75
83
|
const sent = [];
|
|
76
|
-
const h = layer(
|
|
84
|
+
const h = layer(
|
|
85
|
+
json,
|
|
86
|
+
{ ...hooks, ready: (x) => (hub = x) },
|
|
87
|
+
{},
|
|
88
|
+
() => true,
|
|
89
|
+
undefined,
|
|
90
|
+
null,
|
|
91
|
+
options.engine ?? null,
|
|
92
|
+
);
|
|
77
93
|
const players = [
|
|
78
94
|
{ playerId: 'p1', username: 'ada' },
|
|
79
95
|
{ playerId: 'p2', username: 'bo' },
|
|
@@ -93,6 +109,7 @@ function boot(options = {}) {
|
|
|
93
109
|
if (b) {
|
|
94
110
|
objects.set(actor.id, {
|
|
95
111
|
body: rigid(actor.components.Transform),
|
|
112
|
+
home: { ...actor.components.Transform },
|
|
96
113
|
claimable: b.type === 'dynamic',
|
|
97
114
|
fast: false,
|
|
98
115
|
owner: null,
|
|
@@ -123,7 +140,7 @@ function boot(options = {}) {
|
|
|
123
140
|
objects.get('far').owner = 'p1';
|
|
124
141
|
const say = (playerId, data) => h.message(session, { playerId }, { k: 'hub-grab', ...data });
|
|
125
142
|
const denies = () => sent.filter(([, d]) => d.k === 'hub-grab-deny');
|
|
126
|
-
return { h, hub, world, objects, sent, say, denies };
|
|
143
|
+
return { h, hub, world, objects, sent, say, denies, players, session };
|
|
127
144
|
}
|
|
128
145
|
|
|
129
146
|
const warn = console.warn;
|
|
@@ -327,5 +344,842 @@ test('a hold the multiplayer kit or the place ended is forgotten', () => {
|
|
|
327
344
|
assert.equal(hub.holding('p1'), null);
|
|
328
345
|
});
|
|
329
346
|
|
|
347
|
+
// --- v1.25 `grab.keepHeld`: a held object stays its holder's -----------------
|
|
348
|
+
|
|
349
|
+
// multiplayer-3d's `claim` (kits/multiplayer-3d/code/server/ownership.js), as
|
|
350
|
+
// far as it concerns the hub: an object that is not `claimable` is granted to
|
|
351
|
+
// nobody; any other goes to the claimant (who stands nearer, here).
|
|
352
|
+
function claim(objects, playerId, id) {
|
|
353
|
+
const entry = objects.get(id);
|
|
354
|
+
if (!entry?.claimable || entry.owner === playerId) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
entry.owner = playerId;
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
const mp3d = new URL('../../multiplayer-3d/code/server/ownership.js', import.meta.url);
|
|
361
|
+
if (fs.existsSync(mp3d)) {
|
|
362
|
+
// The stand-in above is only as good as the line it stands for.
|
|
363
|
+
assert.match(
|
|
364
|
+
fs.readFileSync(mp3d, 'utf8'),
|
|
365
|
+
/if \(!entry\?\.claimable \|\| entry\.owner === playerId\) \{\s+continue;/,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// No pin is left: every loose object of the scene is claimable again.
|
|
370
|
+
function allLoose(objects) {
|
|
371
|
+
for (const actor of globalThis.__hubTestActors) {
|
|
372
|
+
if (actor.components.Body?.type === 'dynamic') {
|
|
373
|
+
assert.equal(objects.get(actor.id).claimable, true, `${actor.id} is claimable`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
const KEEP = { keepHeld: true };
|
|
378
|
+
|
|
379
|
+
test('without keepHeld a hold changes nothing in the simulation: a nearer player is granted the object, as before', () => {
|
|
380
|
+
for (const grab of [true, { throwSpeed: 6 }, { steady: true, reach: 2.2 }, { keepHeld: false }]) {
|
|
381
|
+
const { h, hub, say, world, objects, sent } = boot({ grab });
|
|
382
|
+
const before = JSON.stringify([...objects].map(([id, e]) => [id, e.claimable, e.owner]));
|
|
383
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
384
|
+
assert.equal(JSON.stringify([...objects].map(([id, e]) => [id, e.claimable, e.owner])), before);
|
|
385
|
+
// `hold` is no message of such a place: dropped unheard, at no cost.
|
|
386
|
+
const n = sent.length;
|
|
387
|
+
for (let i = 0; i < 20; i += 1) {
|
|
388
|
+
say('p1', { actor: 'crate', op: 'hold' });
|
|
389
|
+
}
|
|
390
|
+
assert.equal(sent.length, n);
|
|
391
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
392
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
393
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
394
|
+
assert.equal(claim(objects, 'p2', 'crate'), true);
|
|
395
|
+
h.place(world);
|
|
396
|
+
assert.equal(hub.holding('p1'), null);
|
|
397
|
+
allLoose(objects);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
test('keepHeld: another player’s claim on a held object is refused, and the holder keeps it', () => {
|
|
402
|
+
const { h, hub, say, world, objects, denies } = boot({ grab: KEEP });
|
|
403
|
+
// Not held yet: anyone nearer may be handed it, as ever.
|
|
404
|
+
assert.equal(objects.get('crate').claimable, true);
|
|
405
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
406
|
+
assert.equal(objects.get('crate').claimable, false);
|
|
407
|
+
assert.equal(objects.get('barrel').claimable, true);
|
|
408
|
+
for (let i = 0; i < 5; i += 1) {
|
|
409
|
+
assert.equal(claim(objects, 'p2', 'crate'), false);
|
|
410
|
+
world.timeMs += 100;
|
|
411
|
+
h.place(world);
|
|
412
|
+
}
|
|
413
|
+
assert.equal(objects.get('crate').owner, 'p1');
|
|
414
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
415
|
+
// The other player cannot grab it either: it is not theirs to simulate.
|
|
416
|
+
say('p2', { actor: 'crate', op: 'grab' });
|
|
417
|
+
assert.equal(denies().length, 1);
|
|
418
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
419
|
+
assert.equal(objects.get('crate').claimable, false);
|
|
420
|
+
// An object nobody holds is still handed to whoever is nearer.
|
|
421
|
+
assert.equal(claim(objects, 'p2', 'barrel'), true);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
test('keepHeld: the pin lasts while the holder’s client says it holds, and lapses when it stops', () => {
|
|
425
|
+
const { h, say, world, objects, hub } = boot({ grab: KEEP });
|
|
426
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
427
|
+
for (let i = 0; i < 10; i += 1) {
|
|
428
|
+
world.timeMs += 1000;
|
|
429
|
+
say('p1', { actor: 'crate', op: 'hold' });
|
|
430
|
+
h.place(world);
|
|
431
|
+
assert.equal(objects.get('crate').claimable, false);
|
|
432
|
+
}
|
|
433
|
+
// A client that ended the hold by itself (a trip, the talk panel), or one
|
|
434
|
+
// from before v1.25, says nothing more: the object is anyone's again.
|
|
435
|
+
world.timeMs += Grab.KEEP_LEASE_MS;
|
|
436
|
+
h.place(world);
|
|
437
|
+
allLoose(objects);
|
|
438
|
+
// The hold itself is what it was before v1.24: known until the object moves on.
|
|
439
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
440
|
+
// A late word from a client still holding puts the pin back.
|
|
441
|
+
say('p1', { actor: 'crate', op: 'hold' });
|
|
442
|
+
assert.equal(objects.get('crate').claimable, false);
|
|
443
|
+
// Nobody else's word does, nor a word about another object.
|
|
444
|
+
world.timeMs += Grab.KEEP_LEASE_MS;
|
|
445
|
+
h.place(world);
|
|
446
|
+
say('p2', { actor: 'crate', op: 'hold' });
|
|
447
|
+
say('p1', { actor: 'barrel', op: 'hold' });
|
|
448
|
+
allLoose(objects);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
test('keepHeld: every end of a hold makes the object claimable again', () => {
|
|
452
|
+
const ends = {
|
|
453
|
+
throw: ({ say }) => say('p1', { actor: 'crate', op: 'throw' }),
|
|
454
|
+
'a throw the server does not let fly': ({ say, objects }) => {
|
|
455
|
+
objects.get('crate').body.state.at = { x: 50, y: 0, z: 0 };
|
|
456
|
+
say('p1', { actor: 'crate', op: 'throw' });
|
|
457
|
+
},
|
|
458
|
+
'hub.drop': ({ hub }) => assert.equal(hub.drop('p1'), 1),
|
|
459
|
+
'hub.drop for everyone': ({ hub }) => assert.equal(hub.drop(), 1),
|
|
460
|
+
'a refused grab of it': ({ say, refuse }) => {
|
|
461
|
+
refuse.on = true;
|
|
462
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
463
|
+
},
|
|
464
|
+
'a grab past the rate': ({ say }) => {
|
|
465
|
+
for (let i = 0; i < Grab.GRAB_BURST + 1; i += 1) {
|
|
466
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
467
|
+
}
|
|
468
|
+
},
|
|
469
|
+
'grabbing another object': ({ say, hub, objects }) => {
|
|
470
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
471
|
+
assert.equal(hub.holding('p1'), 'barrel');
|
|
472
|
+
assert.equal(objects.get('crate').claimable, true);
|
|
473
|
+
assert.equal(objects.get('barrel').claimable, false);
|
|
474
|
+
hub.drop();
|
|
475
|
+
},
|
|
476
|
+
'the place hides it': ({ hub, tick }) => {
|
|
477
|
+
hub.hide('crate');
|
|
478
|
+
tick();
|
|
479
|
+
hub.show('crate');
|
|
480
|
+
},
|
|
481
|
+
'the player is held still': ({ hub, tick }) => {
|
|
482
|
+
hub.hold('p1');
|
|
483
|
+
tick();
|
|
484
|
+
},
|
|
485
|
+
'the owner’s reports stop and the server takes it back': ({ objects, tick }) => {
|
|
486
|
+
objects.get('crate').owner = null;
|
|
487
|
+
tick();
|
|
488
|
+
},
|
|
489
|
+
'the holder’s body is gone': ({ world, tick }) => {
|
|
490
|
+
world.players.delete('p1');
|
|
491
|
+
tick();
|
|
492
|
+
},
|
|
493
|
+
'the holder leaves': ({ players, world, tick }) => {
|
|
494
|
+
players.splice(0, 1);
|
|
495
|
+
world.players.delete('p1');
|
|
496
|
+
tick();
|
|
497
|
+
},
|
|
498
|
+
'the game ends for the holder': ({ hub, tick }) => {
|
|
499
|
+
hub.end(['p1']);
|
|
500
|
+
tick();
|
|
501
|
+
},
|
|
502
|
+
'the lease runs out': ({ world, tick }) => {
|
|
503
|
+
world.timeMs += Grab.KEEP_LEASE_MS;
|
|
504
|
+
tick();
|
|
505
|
+
},
|
|
506
|
+
'hub.reset': ({ hub }) => assert.equal(hub.reset('crate'), true),
|
|
507
|
+
shutdown: ({ h }) => h.shutdown(),
|
|
508
|
+
};
|
|
509
|
+
for (const [name, end] of Object.entries(ends)) {
|
|
510
|
+
const refuse = { on: false };
|
|
511
|
+
const b = boot({ grab: KEEP, hooks: { grab: () => !refuse.on } });
|
|
512
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
513
|
+
assert.equal(b.objects.get('crate').claimable, false, name);
|
|
514
|
+
end({ ...b, refuse, tick: () => b.h.place(b.world) });
|
|
515
|
+
// A reset keeps its own short hold on the object; nothing else waits.
|
|
516
|
+
if (name === 'hub.reset') {
|
|
517
|
+
b.world.timeMs += 1000;
|
|
518
|
+
b.h.place(b.world);
|
|
519
|
+
}
|
|
520
|
+
try {
|
|
521
|
+
allLoose(b.objects);
|
|
522
|
+
} catch (err) {
|
|
523
|
+
console.error(`keepHeld end: ${name}`);
|
|
524
|
+
throw err;
|
|
525
|
+
}
|
|
526
|
+
// And it stays so: no later tick or late `hold` pins what is no longer held.
|
|
527
|
+
if (!['a refused grab of it', 'a grab past the rate', 'the lease runs out'].includes(name)) {
|
|
528
|
+
b.world.timeMs += 1000;
|
|
529
|
+
b.say('p1', { actor: 'crate', op: 'hold' });
|
|
530
|
+
b.h.place(b.world);
|
|
531
|
+
allLoose(b.objects);
|
|
532
|
+
}
|
|
533
|
+
assert.equal(claim(b.objects, 'p2', 'crate'), true, name);
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
test('keepHeld: a refused grab of the held object ends the hold, so `hold` cannot pin it again past the hook', () => {
|
|
538
|
+
const refuse = { on: false };
|
|
539
|
+
const asked = [];
|
|
540
|
+
const { h, hub, say, world, objects } = boot({
|
|
541
|
+
grab: KEEP,
|
|
542
|
+
hooks: {
|
|
543
|
+
grab: (x, ...args) => {
|
|
544
|
+
asked.push(args);
|
|
545
|
+
return !refuse.on;
|
|
546
|
+
},
|
|
547
|
+
},
|
|
548
|
+
});
|
|
549
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
550
|
+
refuse.on = true;
|
|
551
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
552
|
+
assert.equal(hub.holding('p1'), null);
|
|
553
|
+
allLoose(objects);
|
|
554
|
+
// A client that goes on saying `hold`, still the owner, still reporting.
|
|
555
|
+
for (let i = 0; i < 60; i += 1) {
|
|
556
|
+
world.timeMs += 1000;
|
|
557
|
+
say('p1', { actor: 'crate', op: 'hold' });
|
|
558
|
+
allLoose(objects);
|
|
559
|
+
h.place(world);
|
|
560
|
+
allLoose(objects);
|
|
561
|
+
}
|
|
562
|
+
assert.equal(asked.length, 2);
|
|
563
|
+
assert.equal(claim(objects, 'p2', 'crate'), true);
|
|
564
|
+
// A refusal past the rate ends it too.
|
|
565
|
+
const b = boot({ grab: KEEP });
|
|
566
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
567
|
+
for (let i = 0; i < Grab.GRAB_BURST; i += 1) {
|
|
568
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
569
|
+
}
|
|
570
|
+
assert.equal(b.hub.holding('p1'), null);
|
|
571
|
+
b.world.timeMs += 2000;
|
|
572
|
+
b.say('p1', { actor: 'crate', op: 'hold' });
|
|
573
|
+
allLoose(b.objects);
|
|
574
|
+
// Without keepHeld a refusal leaves the place's record as it always did.
|
|
575
|
+
const c = boot({ hooks: { grab: () => !refuse.on } });
|
|
576
|
+
refuse.on = false;
|
|
577
|
+
c.say('p1', { actor: 'crate', op: 'grab' });
|
|
578
|
+
refuse.on = true;
|
|
579
|
+
c.say('p1', { actor: 'crate', op: 'grab' });
|
|
580
|
+
assert.equal(c.hub.holding('p1'), 'crate');
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
test('keepHeld: a holder whose body is far from the object pins nothing, at a tick or by `hold`', () => {
|
|
584
|
+
// The holder walks off; its client keeps the object's pose and says `hold`.
|
|
585
|
+
const a = boot({ grab: KEEP });
|
|
586
|
+
a.say('p1', { actor: 'crate', op: 'grab' });
|
|
587
|
+
a.world.players.get('p1').at = { x: 99, y: 1, z: 0 };
|
|
588
|
+
a.say('p1', { actor: 'crate', op: 'hold' });
|
|
589
|
+
assert.equal(a.hub.holding('p1'), null);
|
|
590
|
+
allLoose(a.objects);
|
|
591
|
+
for (let i = 0; i < 10; i += 1) {
|
|
592
|
+
a.world.timeMs += 1000;
|
|
593
|
+
a.say('p1', { actor: 'crate', op: 'hold' });
|
|
594
|
+
a.h.place(a.world);
|
|
595
|
+
allLoose(a.objects);
|
|
596
|
+
}
|
|
597
|
+
assert.equal(claim(a.objects, 'p2', 'crate'), true);
|
|
598
|
+
// The same, caught by the tick before any `hold`.
|
|
599
|
+
const b = boot({ grab: KEEP });
|
|
600
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
601
|
+
b.world.players.get('p1').at = { x: Grab.KEEP_REACH + 1.5, y: 1, z: 0 };
|
|
602
|
+
b.h.place(b.world);
|
|
603
|
+
assert.equal(b.hub.holding('p1'), null);
|
|
604
|
+
allLoose(b.objects);
|
|
605
|
+
// Just inside the bound the hold goes on: a held object is within 4 m.
|
|
606
|
+
const c = boot({ grab: KEEP });
|
|
607
|
+
c.say('p1', { actor: 'crate', op: 'grab' });
|
|
608
|
+
c.objects.get('crate').body.state.at = { x: Grab.KEEP_REACH - 0.5, y: 1, z: 0 };
|
|
609
|
+
c.world.timeMs += 1000;
|
|
610
|
+
c.say('p1', { actor: 'crate', op: 'hold' });
|
|
611
|
+
c.h.place(c.world);
|
|
612
|
+
assert.equal(c.hub.holding('p1'), 'crate');
|
|
613
|
+
assert.equal(c.objects.get('crate').claimable, false);
|
|
614
|
+
// Back in range after a lapse without a hold in force: `hold` pins nothing.
|
|
615
|
+
a.world.players.get('p1').at = { x: 0, y: 1, z: 0 };
|
|
616
|
+
a.objects.get('crate').owner = 'p1';
|
|
617
|
+
a.say('p1', { actor: 'crate', op: 'hold' });
|
|
618
|
+
allLoose(a.objects);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test('keepHeld: `hold` pins only for a hold in force -- never a hidden object, a held, unarrived or ended player, or no grab at all', () => {
|
|
622
|
+
const fresh = () => {
|
|
623
|
+
const b = boot({ grab: KEEP });
|
|
624
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
625
|
+
// The lease lapses, so only a `hold` could pin again.
|
|
626
|
+
b.world.timeMs += Grab.KEEP_LEASE_MS;
|
|
627
|
+
b.h.place(b.world);
|
|
628
|
+
allLoose(b.objects);
|
|
629
|
+
return b;
|
|
630
|
+
};
|
|
631
|
+
const cases = {
|
|
632
|
+
'hidden object': (b) => b.hub.hide('crate'),
|
|
633
|
+
'player held still': (b) => b.hub.hold('p1'),
|
|
634
|
+
'game ended for them': (b) => b.hub.end(['p1']),
|
|
635
|
+
'object someone else’s': (b) => (b.objects.get('crate').owner = 'p2'),
|
|
636
|
+
'object the server’s': (b) => (b.objects.get('crate').owner = null),
|
|
637
|
+
'dropped by the place': (b) => b.hub.drop('p1'),
|
|
638
|
+
};
|
|
639
|
+
for (const [name, spoil] of Object.entries(cases)) {
|
|
640
|
+
const b = fresh();
|
|
641
|
+
spoil(b);
|
|
642
|
+
for (let i = 0; i < 3; i += 1) {
|
|
643
|
+
b.say('p1', { actor: 'crate', op: 'hold' });
|
|
644
|
+
assert.equal(b.objects.get('crate').claimable, true, name);
|
|
645
|
+
b.world.timeMs += 300;
|
|
646
|
+
b.h.place(b.world);
|
|
647
|
+
assert.equal(b.objects.get('crate').claimable, true, name);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
// No grab at all: `hold` alone, from the owner standing by it, pins nothing.
|
|
651
|
+
const none = boot({ grab: KEEP });
|
|
652
|
+
none.say('p1', { actor: 'crate', op: 'hold' });
|
|
653
|
+
none.say('p2', { actor: 'crate', op: 'hold' });
|
|
654
|
+
allLoose(none.objects);
|
|
655
|
+
// A control: an untouched lapsed hold is renewed.
|
|
656
|
+
const ok = fresh();
|
|
657
|
+
ok.say('p1', { actor: 'crate', op: 'hold' });
|
|
658
|
+
assert.equal(ok.objects.get('crate').claimable, false);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
test('keepHeld: a throw past the rate still ends the hold, so `hold` cannot keep the object', () => {
|
|
662
|
+
const run = (grab) => {
|
|
663
|
+
const b = boot({ grab });
|
|
664
|
+
for (let i = 0; i < Grab.GRAB_BURST; i += 1) {
|
|
665
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
666
|
+
}
|
|
667
|
+
assert.equal(b.hub.holding('p1'), 'crate');
|
|
668
|
+
b.say('p1', { actor: 'crate', op: 'throw' });
|
|
669
|
+
return b;
|
|
670
|
+
};
|
|
671
|
+
const b = run(KEEP);
|
|
672
|
+
assert.equal(b.hub.holding('p1'), null);
|
|
673
|
+
allLoose(b.objects);
|
|
674
|
+
// Unheard as a throw: it does not fly.
|
|
675
|
+
assert.equal(b.objects.get('crate').speedCap, undefined);
|
|
676
|
+
for (let i = 0; i < 60; i += 1) {
|
|
677
|
+
b.world.timeMs += 1000;
|
|
678
|
+
b.say('p1', { actor: 'crate', op: 'hold' });
|
|
679
|
+
allLoose(b.objects);
|
|
680
|
+
b.h.place(b.world);
|
|
681
|
+
allLoose(b.objects);
|
|
682
|
+
}
|
|
683
|
+
assert.equal(claim(b.objects, 'p2', 'crate'), true);
|
|
684
|
+
// A throw past the rate of something else ends nothing.
|
|
685
|
+
const c = boot({ grab: KEEP });
|
|
686
|
+
for (let i = 0; i < Grab.GRAB_BURST; i += 1) {
|
|
687
|
+
c.say('p1', { actor: 'crate', op: 'grab' });
|
|
688
|
+
}
|
|
689
|
+
c.say('p1', { actor: 'barrel', op: 'throw' });
|
|
690
|
+
assert.equal(c.hub.holding('p1'), 'crate');
|
|
691
|
+
// Without keepHeld a throw past the rate is dropped whole, as it always was.
|
|
692
|
+
assert.equal(run(true).hub.holding('p1'), 'crate');
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
test('keepHeld: a hold whose pin has lapsed is still checked each tick, and cannot be pinned again once it fails', () => {
|
|
696
|
+
const lapsed = () => {
|
|
697
|
+
const b = boot({ grab: KEEP });
|
|
698
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
699
|
+
b.world.timeMs += Grab.KEEP_LEASE_MS;
|
|
700
|
+
b.h.place(b.world);
|
|
701
|
+
assert.equal(b.hub.holding('p1'), 'crate');
|
|
702
|
+
allLoose(b.objects);
|
|
703
|
+
return b;
|
|
704
|
+
};
|
|
705
|
+
// The holder goes 99 m away, a tick passes, and they come back.
|
|
706
|
+
const a = lapsed();
|
|
707
|
+
a.world.players.get('p1').at = { x: 99, y: 1, z: 0 };
|
|
708
|
+
a.h.place(a.world);
|
|
709
|
+
assert.equal(a.hub.holding('p1'), null);
|
|
710
|
+
a.world.players.get('p1').at = { x: 0, y: 1, z: 0 };
|
|
711
|
+
a.say('p1', { actor: 'crate', op: 'hold' });
|
|
712
|
+
a.h.place(a.world);
|
|
713
|
+
allLoose(a.objects);
|
|
714
|
+
// The game ends for them while the pin is lapsed.
|
|
715
|
+
const b = lapsed();
|
|
716
|
+
b.hub.end(['p1']);
|
|
717
|
+
b.h.place(b.world);
|
|
718
|
+
assert.equal(b.hub.holding('p1'), null);
|
|
719
|
+
// Without keepHeld the record does not depend on distance, as before.
|
|
720
|
+
const c = boot();
|
|
721
|
+
c.say('p1', { actor: 'crate', op: 'grab' });
|
|
722
|
+
c.world.players.get('p1').at = { x: 5.5, y: 1, z: 0 };
|
|
723
|
+
c.h.place(c.world);
|
|
724
|
+
assert.equal(c.hub.holding('p1'), 'crate');
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
test('keepHeld: hidden and shown, or held still and released, inside one tick: the hold is over all the same', () => {
|
|
728
|
+
const a = boot({ grab: KEEP });
|
|
729
|
+
a.say('p1', { actor: 'crate', op: 'grab' });
|
|
730
|
+
a.hub.hide('crate');
|
|
731
|
+
a.hub.show('crate');
|
|
732
|
+
assert.equal(a.hub.holding('p1'), null);
|
|
733
|
+
a.say('p1', { actor: 'crate', op: 'hold' });
|
|
734
|
+
a.h.place(a.world);
|
|
735
|
+
allLoose(a.objects);
|
|
736
|
+
const b = boot({ grab: KEEP });
|
|
737
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
738
|
+
b.hub.hold('p1');
|
|
739
|
+
b.hub.release('p1');
|
|
740
|
+
assert.equal(b.hub.holding('p1'), null);
|
|
741
|
+
b.say('p1', { actor: 'crate', op: 'hold' });
|
|
742
|
+
allLoose(b.objects);
|
|
743
|
+
// hub.reset too: the hold is over before the next tick.
|
|
744
|
+
const r = boot({ grab: KEEP });
|
|
745
|
+
r.say('p1', { actor: 'crate', op: 'grab' });
|
|
746
|
+
r.hub.reset('crate');
|
|
747
|
+
assert.equal(r.hub.holding('p1'), null);
|
|
748
|
+
// Without keepHeld each is seen only by a tick, as before.
|
|
749
|
+
const c = boot();
|
|
750
|
+
c.say('p1', { actor: 'crate', op: 'grab' });
|
|
751
|
+
c.hub.hide('crate');
|
|
752
|
+
c.hub.show('crate');
|
|
753
|
+
c.hub.hold('p1');
|
|
754
|
+
c.hub.release('p1');
|
|
755
|
+
c.h.place(c.world);
|
|
756
|
+
assert.equal(c.hub.holding('p1'), 'crate');
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
test('a player who switches objects keeps their place among the holders: hub.drop tells them in the same order, with or without keepHeld', () => {
|
|
760
|
+
for (const grab of [true, KEEP]) {
|
|
761
|
+
const { hub, say, objects, sent } = boot({ grab });
|
|
762
|
+
objects.get('barrel').owner = 'p2';
|
|
763
|
+
objects.get('far').body.state.at = { x: 0, y: 0.4, z: 1 };
|
|
764
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
765
|
+
say('p2', { actor: 'barrel', op: 'grab' });
|
|
766
|
+
say('p1', { actor: 'far', op: 'grab' });
|
|
767
|
+
assert.equal(hub.holding('p1'), 'far');
|
|
768
|
+
assert.equal(hub.drop(), 2);
|
|
769
|
+
assert.deepEqual(
|
|
770
|
+
sent.filter(([, d]) => d.k === 'hub-drop'),
|
|
771
|
+
[
|
|
772
|
+
['p1', { k: 'hub-drop', actor: 'far' }],
|
|
773
|
+
['p2', { k: 'hub-drop', actor: 'barrel' }],
|
|
774
|
+
],
|
|
775
|
+
);
|
|
776
|
+
allLoose(objects);
|
|
777
|
+
}
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
test('keepHeld: a hold the place’s hook itself ended is not recorded or pinned when the hook returns', () => {
|
|
781
|
+
const acts = {
|
|
782
|
+
'hub.drop(player)': (hub) => hub.drop('p1'),
|
|
783
|
+
'hub.drop()': (hub) => hub.drop(),
|
|
784
|
+
'hub.hide then hub.show': (hub) => {
|
|
785
|
+
hub.hide('crate');
|
|
786
|
+
hub.show('crate');
|
|
787
|
+
},
|
|
788
|
+
'hub.hold then hub.release': (hub) => {
|
|
789
|
+
hub.hold('p1');
|
|
790
|
+
hub.release('p1');
|
|
791
|
+
},
|
|
792
|
+
'hub.reset': (hub) => hub.reset('crate'),
|
|
793
|
+
};
|
|
794
|
+
for (const [name, act] of Object.entries(acts)) {
|
|
795
|
+
// Both while already holding the object and on a first grab of it.
|
|
796
|
+
for (const holding of [true, false]) {
|
|
797
|
+
const on = { act: false };
|
|
798
|
+
const b = boot({
|
|
799
|
+
grab: KEEP,
|
|
800
|
+
hooks: {
|
|
801
|
+
grab: (hub, playerId, actorId, op) => {
|
|
802
|
+
if (on.act && op === 'grab') {
|
|
803
|
+
act(hub);
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
},
|
|
807
|
+
});
|
|
808
|
+
if (holding) {
|
|
809
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
810
|
+
assert.equal(b.objects.get('crate').claimable, false);
|
|
811
|
+
}
|
|
812
|
+
on.act = true;
|
|
813
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
814
|
+
on.act = false;
|
|
815
|
+
const label = `${name}, ${holding ? 'holding' : 'first grab'}`;
|
|
816
|
+
assert.equal(b.hub.holding('p1'), null, label);
|
|
817
|
+
// The client is told it does not hold it.
|
|
818
|
+
assert.ok(
|
|
819
|
+
b.sent.some(([id, d]) => id === 'p1' && d.k === 'hub-grab-deny' && d.actor === 'crate'),
|
|
820
|
+
label,
|
|
821
|
+
);
|
|
822
|
+
// And a client that goes on saying `hold` pins nothing, for a minute.
|
|
823
|
+
for (let i = 0; i < 60; i += 1) {
|
|
824
|
+
b.world.timeMs += 1000;
|
|
825
|
+
b.say('p1', { actor: 'crate', op: 'hold' });
|
|
826
|
+
b.h.place(b.world);
|
|
827
|
+
allLoose(b.objects);
|
|
828
|
+
}
|
|
829
|
+
// A later grab the hook lets be is taken as ever.
|
|
830
|
+
b.objects.get('crate').owner = 'p1';
|
|
831
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
832
|
+
assert.equal(b.hub.holding('p1'), 'crate', label);
|
|
833
|
+
assert.equal(b.objects.get('crate').claimable, false, label);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
// A hook that ends someone else's hold, or hides another object, refuses nothing.
|
|
837
|
+
const other = boot({
|
|
838
|
+
grab: KEEP,
|
|
839
|
+
hooks: {
|
|
840
|
+
grab: (hub) => {
|
|
841
|
+
hub.drop('p2');
|
|
842
|
+
hub.hide('gate');
|
|
843
|
+
hub.show('gate');
|
|
844
|
+
},
|
|
845
|
+
},
|
|
846
|
+
});
|
|
847
|
+
other.say('p1', { actor: 'crate', op: 'grab' });
|
|
848
|
+
assert.equal(other.hub.holding('p1'), 'crate');
|
|
849
|
+
assert.deepEqual(other.denies(), []);
|
|
850
|
+
// Without keepHeld the grab is recorded after the hook whatever it did, as before.
|
|
851
|
+
const plain = boot({ hooks: { grab: (hub) => void hub.drop('p1') } });
|
|
852
|
+
plain.say('p1', { actor: 'crate', op: 'grab' });
|
|
853
|
+
plain.say('p1', { actor: 'crate', op: 'grab' });
|
|
854
|
+
assert.equal(plain.hub.holding('p1'), 'crate');
|
|
855
|
+
assert.deepEqual(plain.denies(), []);
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
test('keepHeld: a grab is checked again after the place’s hook, which may have ended the game, moved the player or removed their body', () => {
|
|
859
|
+
const acts = {
|
|
860
|
+
'hub.end': (b) => b.hub.end(['p1']),
|
|
861
|
+
'a teleport out of reach': (b) => b.hub.placeAt('p1', { x: 60, y: 1, z: 0 }),
|
|
862
|
+
'their body removed': (b) => b.world.players.delete('p1'),
|
|
863
|
+
'the object handed to another': (b) => (b.objects.get('crate').owner = 'p2'),
|
|
864
|
+
};
|
|
865
|
+
for (const [name, act] of Object.entries(acts)) {
|
|
866
|
+
for (const holding of [true, false]) {
|
|
867
|
+
const label = `${name}, ${holding ? 'holding' : 'first grab'}`;
|
|
868
|
+
const on = { act: false };
|
|
869
|
+
const ref = {};
|
|
870
|
+
const b = boot({ grab: KEEP, hooks: { grab: () => void (on.act && act(ref.b)) } });
|
|
871
|
+
ref.b = b;
|
|
872
|
+
if (holding) {
|
|
873
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
874
|
+
}
|
|
875
|
+
on.act = true;
|
|
876
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
877
|
+
on.act = false;
|
|
878
|
+
assert.equal(b.hub.holding('p1'), null, label);
|
|
879
|
+
assert.ok(
|
|
880
|
+
b.sent.some(([id, d]) => id === 'p1' && d.k === 'hub-grab-deny' && d.actor === 'crate'),
|
|
881
|
+
label,
|
|
882
|
+
);
|
|
883
|
+
// Nothing pinned, before any tick: another player's claim in the same tick is granted.
|
|
884
|
+
allLoose(b.objects);
|
|
885
|
+
if (name !== 'the object handed to another') {
|
|
886
|
+
assert.equal(claim(b.objects, 'p2', 'crate'), true, label);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
// Without keepHeld the grab is recorded after its hook, as before, and no deny is sent.
|
|
890
|
+
const on = { act: false };
|
|
891
|
+
const ref = {};
|
|
892
|
+
const plain = boot({ hooks: { grab: () => void (on.act && act(ref.b)) } });
|
|
893
|
+
ref.b = plain;
|
|
894
|
+
on.act = true;
|
|
895
|
+
plain.say('p1', { actor: 'crate', op: 'grab' });
|
|
896
|
+
assert.equal(plain.hub.holding('p1'), 'crate', name);
|
|
897
|
+
assert.deepEqual(plain.denies(), [], name);
|
|
898
|
+
}
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
test('keepHeld and hub.reset: each keeps its own hold on claimable, in either order', () => {
|
|
902
|
+
// multiplayer-3d's release: the object is the server's again.
|
|
903
|
+
const engine = {
|
|
904
|
+
release(sim, owner, ids, rows) {
|
|
905
|
+
for (const id of ids) {
|
|
906
|
+
sim.objects.get(id).owner = null;
|
|
907
|
+
rows.push([id, 0, 0, 0, 0, 0, 0, 0, 1]);
|
|
908
|
+
}
|
|
909
|
+
},
|
|
910
|
+
};
|
|
911
|
+
for (const withRelease of [true, false]) {
|
|
912
|
+
// Held, then reset: the reset's hold is the only one left, and it ends.
|
|
913
|
+
const a = boot({ grab: KEEP, engine: withRelease ? engine : null });
|
|
914
|
+
a.say('p1', { actor: 'crate', op: 'grab' });
|
|
915
|
+
assert.equal(a.hub.reset('crate'), true);
|
|
916
|
+
assert.equal(a.objects.get('crate').claimable, false);
|
|
917
|
+
// A `hold` from the old holder does not pin a reset object.
|
|
918
|
+
a.say('p1', { actor: 'crate', op: 'hold' });
|
|
919
|
+
a.h.place(a.world);
|
|
920
|
+
assert.equal(a.hub.holding('p1'), null);
|
|
921
|
+
assert.equal(a.objects.get('crate').claimable, false);
|
|
922
|
+
a.world.timeMs += 1000;
|
|
923
|
+
a.say('p1', { actor: 'crate', op: 'hold' });
|
|
924
|
+
a.h.place(a.world);
|
|
925
|
+
allLoose(a.objects);
|
|
926
|
+
// Reset, then a grab inside the reset's hold: refused as before, nothing pinned.
|
|
927
|
+
const b = boot({ grab: KEEP, engine: withRelease ? engine : null });
|
|
928
|
+
assert.equal(b.hub.reset('crate'), true);
|
|
929
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
930
|
+
assert.equal(b.denies().length, 1);
|
|
931
|
+
assert.equal(b.hub.holding('p1'), null);
|
|
932
|
+
b.world.timeMs += 1000;
|
|
933
|
+
b.h.place(b.world);
|
|
934
|
+
allLoose(b.objects);
|
|
935
|
+
// Then held again, and reset twice running: still one hold each.
|
|
936
|
+
b.objects.get('crate').owner = 'p1';
|
|
937
|
+
b.say('p1', { actor: 'crate', op: 'grab' });
|
|
938
|
+
assert.equal(b.objects.get('crate').claimable, false);
|
|
939
|
+
assert.equal(b.hub.reset('crate'), true);
|
|
940
|
+
assert.equal(b.hub.reset('crate'), true);
|
|
941
|
+
b.world.timeMs += 1000;
|
|
942
|
+
b.h.place(b.world);
|
|
943
|
+
allLoose(b.objects);
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
|
|
947
|
+
test('keepHeld: a pinned object is thrown like any other, and flies at the place’s cap', () => {
|
|
948
|
+
const calls = [];
|
|
949
|
+
const { say, objects, hub } = boot({
|
|
950
|
+
grab: KEEP,
|
|
951
|
+
hooks: { grab: (x, ...args) => calls.push(args) },
|
|
952
|
+
});
|
|
953
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
954
|
+
say('p1', { actor: 'crate', op: 'throw' });
|
|
955
|
+
assert.deepEqual(calls, [
|
|
956
|
+
['p1', 'crate', 'grab'],
|
|
957
|
+
['p1', 'crate', 'throw'],
|
|
958
|
+
]);
|
|
959
|
+
assert.equal(typeof objects.get('crate').speedCap, 'number');
|
|
960
|
+
assert.equal(hub.holding('p1'), null);
|
|
961
|
+
allLoose(objects);
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
// --- v1.27 `hub.keepFor`: an object only one player may grab -----------------
|
|
965
|
+
|
|
966
|
+
const GRAB_OP = (actor) => ({ actor, op: 'grab' });
|
|
967
|
+
|
|
968
|
+
test('keepFor: a grab by anyone else is refused before the hook is asked; the keeper’s grab is what it always was', () => {
|
|
969
|
+
const calls = [];
|
|
970
|
+
const { hub, say, denies, objects } = boot({ hooks: { grab: (x, ...args) => calls.push(args) } });
|
|
971
|
+
assert.equal(hub.keepFor('crate', 'p2'), true);
|
|
972
|
+
// p1 simulates the crate and stands by it: only the mark refuses them.
|
|
973
|
+
say('p1', GRAB_OP('crate'));
|
|
974
|
+
assert.deepEqual(calls, []);
|
|
975
|
+
assert.deepEqual(denies(), [['p1', { k: 'hub-grab-deny', actor: 'crate' }]]);
|
|
976
|
+
assert.equal(hub.holding('p1'), null);
|
|
977
|
+
// An object kept for nobody is grabbed as ever.
|
|
978
|
+
say('p1', GRAB_OP('barrel'));
|
|
979
|
+
assert.deepEqual(calls, [['p1', 'barrel', 'grab']]);
|
|
980
|
+
// The keeper's grab is checked as any grab is (theirs to simulate)...
|
|
981
|
+
say('p2', GRAB_OP('crate'));
|
|
982
|
+
assert.equal(denies().length, 2);
|
|
983
|
+
assert.deepEqual(calls, [['p1', 'barrel', 'grab']]);
|
|
984
|
+
// ...and then heard by the hook, recorded, and thrown like any other.
|
|
985
|
+
objects.get('crate').owner = 'p2';
|
|
986
|
+
say('p2', GRAB_OP('crate'));
|
|
987
|
+
assert.equal(hub.holding('p2'), 'crate');
|
|
988
|
+
say('p2', { actor: 'crate', op: 'throw' });
|
|
989
|
+
assert.deepEqual(calls, [
|
|
990
|
+
['p1', 'barrel', 'grab'],
|
|
991
|
+
['p2', 'crate', 'grab'],
|
|
992
|
+
['p2', 'crate', 'throw'],
|
|
993
|
+
]);
|
|
994
|
+
assert.equal(denies().length, 2);
|
|
995
|
+
// The hook may still refuse the keeper.
|
|
996
|
+
const strict = boot({ hooks: { grab: () => false } });
|
|
997
|
+
strict.hub.keepFor('crate', 'p1');
|
|
998
|
+
strict.say('p1', GRAB_OP('crate'));
|
|
999
|
+
assert.equal(strict.denies().length, 1);
|
|
1000
|
+
assert.equal(strict.hub.holding('p1'), null);
|
|
1001
|
+
});
|
|
1002
|
+
|
|
1003
|
+
test('keepFor: whoever else holds the object lets go; a hook that keeps it for someone else refuses its own grab', () => {
|
|
1004
|
+
for (const grab of [true, KEEP]) {
|
|
1005
|
+
const { hub, say, sent, objects } = boot({ grab });
|
|
1006
|
+
say('p1', GRAB_OP('crate'));
|
|
1007
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
1008
|
+
// Kept for its holder: nothing happens to the hold.
|
|
1009
|
+
assert.equal(hub.keepFor('crate', 'p1'), true);
|
|
1010
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
1011
|
+
assert.deepEqual(sent.filter(([, d]) => d.k === 'hub-drop'), []);
|
|
1012
|
+
assert.equal(hub.keepFor('crate', 'p2'), true);
|
|
1013
|
+
assert.equal(hub.holding('p1'), null);
|
|
1014
|
+
assert.deepEqual(sent.filter(([, d]) => d.k === 'hub-drop'), [['p1', { k: 'hub-drop', actor: 'crate' }]]);
|
|
1015
|
+
allLoose(objects);
|
|
1016
|
+
const inHook = boot({ grab, hooks: { grab: (x, playerId, actorId) => void x.keepFor(actorId, 'p2') } });
|
|
1017
|
+
inHook.say('p1', GRAB_OP('crate'));
|
|
1018
|
+
assert.equal(inHook.hub.holding('p1'), null);
|
|
1019
|
+
assert.deepEqual(inHook.denies(), [['p1', { k: 'hub-grab-deny', actor: 'crate' }]]);
|
|
1020
|
+
allLoose(inHook.objects);
|
|
1021
|
+
}
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
test('keepFor: only a loose object of a place with grab, for a player who is here; anything else changes nothing', () => {
|
|
1025
|
+
const { h, hub, say, denies } = boot();
|
|
1026
|
+
assert.equal(hub.keepFor('gate', 'p1'), false);
|
|
1027
|
+
assert.equal(hub.keepFor('spawn', 'p1'), false);
|
|
1028
|
+
assert.equal(hub.keepFor('nobody', 'p1'), false);
|
|
1029
|
+
assert.equal(hub.keepFor(7, 'p1'), false);
|
|
1030
|
+
assert.equal(hub.keepFor('crate', 'p9'), false);
|
|
1031
|
+
assert.equal(h.join().hubKept, undefined);
|
|
1032
|
+
// A refused call leaves the mark that stood.
|
|
1033
|
+
assert.equal(hub.keepFor('crate', 'p1'), true);
|
|
1034
|
+
assert.equal(hub.keepFor('crate', 'p9'), false);
|
|
1035
|
+
assert.deepEqual({ ...h.join().hubKept }, { crate: 'p1' });
|
|
1036
|
+
say('p1', GRAB_OP('crate'));
|
|
1037
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
1038
|
+
assert.deepEqual(denies(), []);
|
|
1039
|
+
// Clearing what was never kept is fine.
|
|
1040
|
+
assert.equal(hub.keepFor('barrel', null), true);
|
|
1041
|
+
const none = boot({ grab: undefined });
|
|
1042
|
+
assert.equal(none.hub.keepFor('crate', 'p1'), false);
|
|
1043
|
+
assert.equal(none.h.join().hubKept, undefined);
|
|
1044
|
+
});
|
|
1045
|
+
|
|
1046
|
+
test('keepFor: ends with null, with hub.hide and with the player’s leaving; hub.reset leaves it', () => {
|
|
1047
|
+
const calls = [];
|
|
1048
|
+
const { h, hub, say, world, players, denies } = boot({ hooks: { grab: (x, ...args) => calls.push(args) } });
|
|
1049
|
+
const kept = () => ({ ...h.join().hubKept });
|
|
1050
|
+
hub.keepFor('crate', 'p2');
|
|
1051
|
+
hub.keepFor('barrel', 'p2');
|
|
1052
|
+
hub.keepFor('far', 'p2');
|
|
1053
|
+
// A reset, and the half second after it: still p2's.
|
|
1054
|
+
assert.equal(hub.reset('crate'), true);
|
|
1055
|
+
world.timeMs += 1000;
|
|
1056
|
+
h.place(world);
|
|
1057
|
+
assert.deepEqual(kept(), { crate: 'p2', barrel: 'p2', far: 'p2' });
|
|
1058
|
+
say('p1', GRAB_OP('crate'));
|
|
1059
|
+
assert.equal(denies().length, 1);
|
|
1060
|
+
assert.deepEqual(calls, []);
|
|
1061
|
+
// null: anyone's again, and the hook hears the grab.
|
|
1062
|
+
hub.keepFor('crate', null);
|
|
1063
|
+
assert.deepEqual(kept(), { barrel: 'p2', far: 'p2' });
|
|
1064
|
+
say('p1', GRAB_OP('crate'));
|
|
1065
|
+
assert.deepEqual(calls, [['p1', 'crate', 'grab']]);
|
|
1066
|
+
// Hidden: kept for nobody, and still nobody's when shown again.
|
|
1067
|
+
hub.hide('barrel');
|
|
1068
|
+
assert.deepEqual(kept(), { far: 'p2' });
|
|
1069
|
+
hub.show('barrel');
|
|
1070
|
+
assert.deepEqual(kept(), { far: 'p2' });
|
|
1071
|
+
// Its player leaves: the last mark goes, and the snapshot carries none.
|
|
1072
|
+
players.splice(players.findIndex((p) => p.playerId === 'p2'), 1);
|
|
1073
|
+
world.players.delete('p2');
|
|
1074
|
+
h.place(world);
|
|
1075
|
+
assert.equal(h.join().hubKept, undefined);
|
|
1076
|
+
});
|
|
1077
|
+
|
|
1078
|
+
test('keepFor: hiding an object again ends a keeping made while it was hidden; a missing player id frees nothing', () => {
|
|
1079
|
+
const { hub, say, denies } = boot();
|
|
1080
|
+
hub.hide('crate');
|
|
1081
|
+
assert.equal(hub.keepFor('crate', 'p2'), true);
|
|
1082
|
+
hub.hide('crate');
|
|
1083
|
+
hub.show('crate');
|
|
1084
|
+
say('p1', GRAB_OP('crate'));
|
|
1085
|
+
assert.equal(denies().length, 0);
|
|
1086
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
1087
|
+
// Only null clears: undefined is a player who is not here.
|
|
1088
|
+
const other = boot();
|
|
1089
|
+
assert.equal(other.hub.keepFor('crate', 'p2'), true);
|
|
1090
|
+
assert.equal(other.hub.keepFor('crate', undefined), false);
|
|
1091
|
+
other.say('p1', GRAB_OP('crate'));
|
|
1092
|
+
assert.deepEqual(other.denies(), [['p1', { k: 'hub-grab-deny', actor: 'crate' }]]);
|
|
1093
|
+
assert.equal(other.hub.keepFor('crate', null), true);
|
|
1094
|
+
other.say('p1', GRAB_OP('crate'));
|
|
1095
|
+
assert.equal(other.hub.holding('p1'), 'crate');
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
test('keepFor: every client is told whose an object is, a late joiner too, and each change once', () => {
|
|
1099
|
+
const { h, hub } = boot();
|
|
1100
|
+
h.delta();
|
|
1101
|
+
assert.equal(h.delta(), null);
|
|
1102
|
+
hub.keepFor('crate', 'p2');
|
|
1103
|
+
assert.deepEqual(JSON.parse(JSON.stringify(h.delta())), { hubKept: { crate: 'p2' } });
|
|
1104
|
+
assert.equal(h.delta(), null);
|
|
1105
|
+
// The snapshot a joiner is sent says it too.
|
|
1106
|
+
assert.deepEqual({ ...h.join().hubKept }, { crate: 'p2' });
|
|
1107
|
+
hub.keepFor('crate', 'p2');
|
|
1108
|
+
assert.equal(h.delta(), null);
|
|
1109
|
+
hub.keepFor('barrel', 'p1');
|
|
1110
|
+
assert.deepEqual(JSON.parse(JSON.stringify(h.delta())), { hubKept: { crate: 'p2', barrel: 'p1' } });
|
|
1111
|
+
// The last mark gone: clients are told once that nothing is kept.
|
|
1112
|
+
hub.keepFor('crate', null);
|
|
1113
|
+
hub.keepFor('barrel', null);
|
|
1114
|
+
assert.deepEqual(JSON.parse(JSON.stringify(h.delta())), { hubKept: {} });
|
|
1115
|
+
assert.equal(h.delta(), null);
|
|
1116
|
+
assert.equal('hubKept' in h.join(), false);
|
|
1117
|
+
});
|
|
1118
|
+
|
|
1119
|
+
// Everything a place and its clients see of a session: the messages, the
|
|
1120
|
+
// hook's calls, the snapshot and each tick's delta.
|
|
1121
|
+
function story(b, calls) {
|
|
1122
|
+
const log = [];
|
|
1123
|
+
const tick = (ms) => {
|
|
1124
|
+
b.world.timeMs += ms;
|
|
1125
|
+
b.h.place(b.world);
|
|
1126
|
+
log.push(['delta', JSON.stringify(b.h.delta())]);
|
|
1127
|
+
};
|
|
1128
|
+
log.push(['join', JSON.stringify(b.h.join())]);
|
|
1129
|
+
b.say('p1', GRAB_OP('crate'));
|
|
1130
|
+
tick(100);
|
|
1131
|
+
b.say('p2', GRAB_OP('crate'));
|
|
1132
|
+
b.say('p1', { actor: 'crate', op: 'hold' });
|
|
1133
|
+
tick(1000);
|
|
1134
|
+
b.say('p1', { actor: 'crate', op: 'throw' });
|
|
1135
|
+
tick(100);
|
|
1136
|
+
b.objects.get('barrel').owner = 'p2';
|
|
1137
|
+
b.say('p2', GRAB_OP('barrel'));
|
|
1138
|
+
b.say('p1', GRAB_OP('far'));
|
|
1139
|
+
tick(500);
|
|
1140
|
+
b.hub.hide('barrel');
|
|
1141
|
+
tick(100);
|
|
1142
|
+
b.hub.show('barrel');
|
|
1143
|
+
b.hub.reset('crate');
|
|
1144
|
+
tick(1000);
|
|
1145
|
+
b.say('p1', GRAB_OP('crate'));
|
|
1146
|
+
log.push(['dropped', b.hub.drop()]);
|
|
1147
|
+
for (let i = 0; i < 6; i += 1) {
|
|
1148
|
+
b.say('p1', GRAB_OP('barrel'));
|
|
1149
|
+
}
|
|
1150
|
+
tick(100);
|
|
1151
|
+
b.players.splice(1, 1);
|
|
1152
|
+
b.world.players.delete('p2');
|
|
1153
|
+
tick(100);
|
|
1154
|
+
log.push(['join', JSON.stringify(b.h.join())]);
|
|
1155
|
+
log.push(['sent', JSON.stringify(b.sent)], ['calls', JSON.stringify(calls)]);
|
|
1156
|
+
return log;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
test('keepFor never called: nothing new is sent, asked or kept, with or without keepHeld', () => {
|
|
1160
|
+
for (const grab of [true, KEEP, { steady: true, reach: 2.2 }]) {
|
|
1161
|
+
const calls = [];
|
|
1162
|
+
const plain = boot({ grab, hooks: { grab: (x, ...args) => calls.push(args) } });
|
|
1163
|
+
const told = story(plain, calls);
|
|
1164
|
+
assert.ok(told.every(([, text]) => !String(text).includes('hubKept')));
|
|
1165
|
+
assert.ok(calls.length >= 3, 'the story is heard by the hook');
|
|
1166
|
+
// A place that used it and put everything back is that same place again:
|
|
1167
|
+
// message for message, call for call.
|
|
1168
|
+
const calls2 = [];
|
|
1169
|
+
const used = boot({ grab, hooks: { grab: (x, ...args) => calls2.push(args) } });
|
|
1170
|
+
used.h.delta();
|
|
1171
|
+
used.hub.keepFor('crate', 'p2');
|
|
1172
|
+
used.hub.keepFor('far', 'p1');
|
|
1173
|
+
assert.deepEqual(JSON.parse(JSON.stringify(used.h.delta())), { hubKept: { crate: 'p2', far: 'p1' } });
|
|
1174
|
+
used.hub.keepFor('crate', null);
|
|
1175
|
+
used.hub.keepFor('far', null);
|
|
1176
|
+
assert.deepEqual(JSON.parse(JSON.stringify(used.h.delta())), { hubKept: {} });
|
|
1177
|
+
const fresh = boot({ grab, hooks: { grab: (x, ...args) => calls.push(args) } });
|
|
1178
|
+
calls.length = 0;
|
|
1179
|
+
fresh.h.delta();
|
|
1180
|
+
assert.deepEqual(story(used, calls2), story(fresh, calls));
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
|
|
330
1184
|
console.warn = warn;
|
|
331
1185
|
console.log(`test-grab: ${n} passed`);
|