castle-web-cli 0.4.231 → 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/serverBundle.js +5 -3
- package/kits/hub-game/CLAUDE.md +26 -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 +194 -4
- package/kits/hub-game/code/client/grab.js +31 -7
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +30 -3
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/state.js +22 -0
- package/kits/hub-game/code/client/tiers.js +31 -6
- package/kits/hub-game/code/client/together.js +66 -1
- 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 +296 -11
- package/kits/hub-game/code/server/hub.js +63 -2
- package/kits/hub-game/code/server/manifest.js +48 -3
- package/kits/hub-game/code/server/party.js +11 -0
- 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 +253 -5
- package/kits/hub-game/docs/contract.md +204 -8
- 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 +186 -3
- 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 +513 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
- package/kits/hub-game/scripts/test-manifest.mjs +75 -0
- package/kits/hub-game/scripts/test-party.mjs +22 -1
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-tiers.mjs +190 -2
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/physics-2d/castle.json +1 -1
- package/kits/physics-2d/engine/scene.js +35 -0
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +44 -0
- package/kits/physics-3d/CLAUDE.md +252 -0
- package/kits/physics-3d/behaviors/Player.jsx +363 -2
- package/kits/physics-3d/castle.json +2 -2
- package/kits/physics-3d/docs/pxmodel-format.md +4 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -0
- package/kits/physics-3d/engine3d/optimize.js +49 -0
- package/kits/physics-3d/engine3d/orbitinput.js +277 -0
- package/kits/physics-3d/engine3d/world3d.js +81 -25
- 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 +12 -2
- 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-static-actors.mjs +6 -1
- package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
- package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
- package/kits/physics-3d/systems/physics3d.js +91 -6
- package/package.json +1 -1
|
@@ -0,0 +1,940 @@
|
|
|
1
|
+
// Try-ons (contract §11, v1.28): `hub.tryOn(playerId, spec | null)` shows a
|
|
2
|
+
// wearable on a player in this place only, instead of the one they own, until
|
|
3
|
+
// it is cleared, they leave or the place ends -- and never touches the one
|
|
4
|
+
// they own (code/server/wear.js, hub.js; code/client/state.js, hookLayer.js).
|
|
5
|
+
//
|
|
6
|
+
// The server is the kit's layer on a fake session, as test-hub-ops runs it;
|
|
7
|
+
// the client is the kit's real hook layer (its `import.meta.glob` and two
|
|
8
|
+
// imports stubbed) fed the messages that server wrote. One scripted session,
|
|
9
|
+
// `story`, is run with and without its `hub.tryOn` calls and everything it
|
|
10
|
+
// wrote is compared as text: a place that never calls it writes what it
|
|
11
|
+
// always did.
|
|
12
|
+
//
|
|
13
|
+
// `HUB_TRY_BASELINE=<dir>` points at another copy of this kit's `code/` (say
|
|
14
|
+
// `git archive <commit> kits/hub-game/code | tar -x -C <dir>` and the path to
|
|
15
|
+
// `kits/hub-game` in it): the same story is then run on that copy's server
|
|
16
|
+
// and client too, and compared byte for byte. `npm run check` runs this
|
|
17
|
+
// without one; it needs nothing installed.
|
|
18
|
+
|
|
19
|
+
import assert from 'node:assert/strict';
|
|
20
|
+
import { register } from 'node:module';
|
|
21
|
+
import { pathToFileURL } from 'node:url';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
|
|
24
|
+
const sceneStub = 'export const readScene = () => globalThis.__hubTestActors ?? [];';
|
|
25
|
+
const avatarsStub = 'export function dropMissing() {}';
|
|
26
|
+
register(
|
|
27
|
+
`data:text/javascript,${encodeURIComponent(`
|
|
28
|
+
import { readFile } from 'node:fs/promises';
|
|
29
|
+
import { fileURLToPath } from 'node:url';
|
|
30
|
+
const stubs = {
|
|
31
|
+
'@imports/castle.multiplayer-3d/code/server/sceneBodies.js': 'data:text/javascript,${encodeURIComponent(sceneStub)}',
|
|
32
|
+
'@imports/castle.multiplayer-3d/code/client/avatars.js': 'data:text/javascript,${encodeURIComponent(avatarsStub)}',
|
|
33
|
+
'castle-web-sdk': ${JSON.stringify(new URL('./fixtures/sdk-stub.mjs', import.meta.url).href)},
|
|
34
|
+
};
|
|
35
|
+
export async function resolve(specifier, context, next) {
|
|
36
|
+
if (stubs[specifier]) {
|
|
37
|
+
return { url: stubs[specifier], shortCircuit: true };
|
|
38
|
+
}
|
|
39
|
+
return next(specifier, context);
|
|
40
|
+
}
|
|
41
|
+
// The hook layer reads the deck's castle.json through the bundler.
|
|
42
|
+
export async function load(url, context, next) {
|
|
43
|
+
if (url.endsWith('/code/client/hookLayer.js')) {
|
|
44
|
+
const source = await readFile(fileURLToPath(url), 'utf8');
|
|
45
|
+
return { format: 'module', source: source.replace('import.meta.glob(', '(() => ({}))('), shortCircuit: true };
|
|
46
|
+
}
|
|
47
|
+
return next(url, context);
|
|
48
|
+
}
|
|
49
|
+
`)}`,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
async function kit(root) {
|
|
53
|
+
const at = (file) => pathToFileURL(path.join(root, file)).href;
|
|
54
|
+
return {
|
|
55
|
+
layer: (await import(at('code/server/hub.js'))).layer,
|
|
56
|
+
hookLayer: (await import(at('code/client/hookLayer.js'))).default,
|
|
57
|
+
State: await import(at('code/client/state.js')),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const here = await kit(path.join(import.meta.dirname, '..'));
|
|
62
|
+
const baseline = process.env.HUB_TRY_BASELINE ? await kit(path.resolve(process.env.HUB_TRY_BASELINE)) : null;
|
|
63
|
+
const Wear = await import('../code/server/wear.js');
|
|
64
|
+
const { makeWear } = await import('../code/client/wear.js');
|
|
65
|
+
const { HATS } = await import('../code/client/hats.js');
|
|
66
|
+
|
|
67
|
+
globalThis.__hubTestActors = [
|
|
68
|
+
{ id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } },
|
|
69
|
+
{ id: 'sign', components: { Transform: { x: 2, y: 1, z: 0 }, Shape: { kind: 'box', width: 1, height: 1, depth: 1, color: '#808080' } } },
|
|
70
|
+
];
|
|
71
|
+
const castleJson = { server: { maxPlayers: 8 }, hub: { contract: 1, title: 'Hat Shop', result: {} } };
|
|
72
|
+
const sceneExists = () => true;
|
|
73
|
+
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
|
74
|
+
|
|
75
|
+
const ROSTER = [
|
|
76
|
+
{ playerId: 'p1', userId: '101', username: 'ada', isAnonymous: false },
|
|
77
|
+
{ playerId: 'p2', userId: '102', username: 'bo', isAnonymous: false },
|
|
78
|
+
{ playerId: 'p3', userId: 'anon-1', username: 'guest', isAnonymous: true },
|
|
79
|
+
];
|
|
80
|
+
const halo = { kind: 'halo', color: '#ffd700', rarity: 4 };
|
|
81
|
+
const aura = { kind: 'aura', color: '#00ff00', rarity: 9 };
|
|
82
|
+
const witch = { kind: 'hat-witch', color: '#3a2a4a', rarity: 0 };
|
|
83
|
+
const straw = { kind: 'hat-straw', color: '#e8c96a', rarity: 0 };
|
|
84
|
+
const cap = { kind: 'hat-cap', color: '#d1453b', rarity: 2 };
|
|
85
|
+
|
|
86
|
+
// A place on a fake session, with everything it writes kept in `log` in the
|
|
87
|
+
// order it wrote it: snapshots, deltas, messages to one player, ghost calls,
|
|
88
|
+
// the deck's hooks and warnings. `hides(x, y)` is the host's block answer.
|
|
89
|
+
function room(layer, { hides = null, saved = { 101: halo, 102: null }, roster = ROSTER } = {}) {
|
|
90
|
+
const log = [];
|
|
91
|
+
const calls = [];
|
|
92
|
+
const players = roster.map((p) => ({ ...p }));
|
|
93
|
+
const platform = {
|
|
94
|
+
place: 'main',
|
|
95
|
+
title: 'Town',
|
|
96
|
+
result: null,
|
|
97
|
+
arrival: () => null,
|
|
98
|
+
async reportResult() {
|
|
99
|
+
return { recorded: true };
|
|
100
|
+
},
|
|
101
|
+
async updatePad() {
|
|
102
|
+
return { status: 'gathering', members: [] };
|
|
103
|
+
},
|
|
104
|
+
async readResults(input) {
|
|
105
|
+
calls.push(['read', input]);
|
|
106
|
+
log.push(['ghost-read', JSON.stringify(input)]);
|
|
107
|
+
const out = {};
|
|
108
|
+
for (const id of input.userIds ?? []) {
|
|
109
|
+
out[id] = { avatar: 1, recent: [], records: [], wearable: saved[id] ?? null };
|
|
110
|
+
}
|
|
111
|
+
return { players: out, boards: {} };
|
|
112
|
+
},
|
|
113
|
+
async wear(playerId, wearable) {
|
|
114
|
+
calls.push(['wear', playerId, wearable]);
|
|
115
|
+
log.push(['ghost-wear', playerId, JSON.stringify(wearable)]);
|
|
116
|
+
return { wearable };
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
if (hides) {
|
|
120
|
+
platform.hiddenPair = hides;
|
|
121
|
+
}
|
|
122
|
+
const session = {
|
|
123
|
+
sessionId: 's-1',
|
|
124
|
+
place: null,
|
|
125
|
+
players,
|
|
126
|
+
hub: platform,
|
|
127
|
+
send: (playerId, data) => log.push(['send', playerId, JSON.stringify(data)]),
|
|
128
|
+
broadcast: (data) => log.push(['broadcast', JSON.stringify(data)]),
|
|
129
|
+
};
|
|
130
|
+
let ctx = null;
|
|
131
|
+
const deck = {
|
|
132
|
+
ready: (hub) => (ctx = hub),
|
|
133
|
+
arrive: (hub, group) => log.push(['arrive', JSON.stringify(group)]),
|
|
134
|
+
leave: (hub, playerId) => log.push(['leave', playerId]),
|
|
135
|
+
step: () => log.push(['step']),
|
|
136
|
+
shutdown: () => log.push(['shutdown']),
|
|
137
|
+
};
|
|
138
|
+
const h = layer(castleJson, deck, {}, sceneExists);
|
|
139
|
+
h.sceneFor(session);
|
|
140
|
+
const world = { timeMs: 0, players: new Map(), bodies: new Map(), scenePath: 'scenes/main.scene' };
|
|
141
|
+
const body = () => ({ at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
|
|
142
|
+
h.ready(world);
|
|
143
|
+
return {
|
|
144
|
+
h,
|
|
145
|
+
log,
|
|
146
|
+
calls,
|
|
147
|
+
world,
|
|
148
|
+
hub: () => ctx,
|
|
149
|
+
// The WORLD snapshot a joiner gets, and the delta everyone does.
|
|
150
|
+
join() {
|
|
151
|
+
const snapshot = h.join();
|
|
152
|
+
log.push(['join', JSON.stringify(snapshot)]);
|
|
153
|
+
return snapshot;
|
|
154
|
+
},
|
|
155
|
+
delta() {
|
|
156
|
+
const delta = h.delta();
|
|
157
|
+
log.push(['delta', JSON.stringify(delta)]);
|
|
158
|
+
return delta;
|
|
159
|
+
},
|
|
160
|
+
async tick(ms = 50) {
|
|
161
|
+
world.timeMs += ms;
|
|
162
|
+
h.place(world);
|
|
163
|
+
// Ghost's arrival read answers, and the next tick places who it was for.
|
|
164
|
+
await flush();
|
|
165
|
+
world.timeMs += ms;
|
|
166
|
+
h.place(world);
|
|
167
|
+
h.step(world);
|
|
168
|
+
},
|
|
169
|
+
enter(playerId) {
|
|
170
|
+
this.body(playerId);
|
|
171
|
+
this.hello(playerId);
|
|
172
|
+
},
|
|
173
|
+
// The two halves of coming in: a body in the simulation (and a place in
|
|
174
|
+
// the roster, for one who went), and the client's hello.
|
|
175
|
+
body(playerId) {
|
|
176
|
+
world.players.set(playerId, body());
|
|
177
|
+
if (!players.some((p) => p.playerId === playerId)) {
|
|
178
|
+
players.push({ ...(roster.find((p) => p.playerId === playerId) ?? { playerId, userId: null, username: playerId, isAnonymous: true }) });
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
hello(playerId, extra = {}) {
|
|
182
|
+
h.message(session, { playerId }, { k: 'hub-hello', avatar: 1, from: null, party: 1, ...extra });
|
|
183
|
+
},
|
|
184
|
+
exit(playerId) {
|
|
185
|
+
world.players.delete(playerId);
|
|
186
|
+
const at = players.findIndex((p) => p.playerId === playerId);
|
|
187
|
+
if (at >= 0) {
|
|
188
|
+
players.splice(at, 1);
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
shutdown: () => h.shutdown(),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// One visit to a place. `tries` false leaves every `hub.tryOn` out; `before`
|
|
196
|
+
// runs first, for a place that tried things on and cleared them all.
|
|
197
|
+
async function story(layer, { tries, before = null, hides = null }) {
|
|
198
|
+
const r = room(layer, { hides });
|
|
199
|
+
const tryOn = (...args) => (tries ? r.hub().tryOn(...args) : undefined);
|
|
200
|
+
r.join();
|
|
201
|
+
r.delta();
|
|
202
|
+
r.enter('p1');
|
|
203
|
+
r.enter('p2');
|
|
204
|
+
await r.tick();
|
|
205
|
+
r.join();
|
|
206
|
+
r.delta();
|
|
207
|
+
if (before) {
|
|
208
|
+
await before(r);
|
|
209
|
+
}
|
|
210
|
+
const mark = r.log.length;
|
|
211
|
+
// A try-on over nothing, and one over a saved wearable.
|
|
212
|
+
tryOn('p2', witch);
|
|
213
|
+
r.delta();
|
|
214
|
+
await r.tick();
|
|
215
|
+
r.join();
|
|
216
|
+
tryOn('p1', straw);
|
|
217
|
+
r.delta();
|
|
218
|
+
// The place saves a wearable while a try-on is up; a mark and a label too.
|
|
219
|
+
r.hub().wear('p2', aura);
|
|
220
|
+
r.hub().mark('p1', { text: 'RED', color: '#d1453b' });
|
|
221
|
+
r.hub().label('sign', 'Hats');
|
|
222
|
+
await r.tick();
|
|
223
|
+
r.delta();
|
|
224
|
+
// A late joiner.
|
|
225
|
+
r.enter('p3');
|
|
226
|
+
await r.tick();
|
|
227
|
+
r.join();
|
|
228
|
+
r.delta();
|
|
229
|
+
tryOn('p3', cap);
|
|
230
|
+
r.hub().wear('p3', halo);
|
|
231
|
+
r.delta();
|
|
232
|
+
// One comes off; one leaves with theirs on; the place ends with one up.
|
|
233
|
+
tryOn('p1', null);
|
|
234
|
+
await r.tick();
|
|
235
|
+
r.delta();
|
|
236
|
+
r.exit('p2');
|
|
237
|
+
await r.tick();
|
|
238
|
+
r.join();
|
|
239
|
+
r.delta();
|
|
240
|
+
r.hub().wear('p1', null);
|
|
241
|
+
await r.tick();
|
|
242
|
+
r.delta();
|
|
243
|
+
await r.shutdown();
|
|
244
|
+
r.delta();
|
|
245
|
+
r.join();
|
|
246
|
+
return { ...r, main: r.log.slice(mark) };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// What a client that knows nothing of try-ons is left with: `hubTry` out of
|
|
250
|
+
// every snapshot and delta, and no `hub-try` message.
|
|
251
|
+
function withoutTry(log) {
|
|
252
|
+
const out = [];
|
|
253
|
+
for (const entry of log) {
|
|
254
|
+
if (entry[0] === 'send' && JSON.parse(entry[2]).k === 'hub-try') {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (entry[0] === 'join' || entry[0] === 'delta') {
|
|
258
|
+
const value = JSON.parse(entry[1]);
|
|
259
|
+
if (value && 'hubTry' in value) {
|
|
260
|
+
delete value.hubTry;
|
|
261
|
+
out.push([entry[0], JSON.stringify(entry[0] === 'delta' && Object.keys(value).length === 0 ? null : value)]);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
out.push(entry);
|
|
266
|
+
}
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const text = (log) => log.map((entry) => entry.join(' ')).join('\n');
|
|
271
|
+
const bytes = (log) => Buffer.byteLength(text(log));
|
|
272
|
+
|
|
273
|
+
// A client: the kit's hook layer fed a server's log as `viewer` would get it.
|
|
274
|
+
function client({ hookLayer, State }, viewer) {
|
|
275
|
+
State.clear();
|
|
276
|
+
const scene = { files: null };
|
|
277
|
+
return {
|
|
278
|
+
hub: State.hub,
|
|
279
|
+
State,
|
|
280
|
+
feed(log) {
|
|
281
|
+
for (const entry of log) {
|
|
282
|
+
if (entry[0] === 'join' || entry[0] === 'delta') {
|
|
283
|
+
const msg = JSON.parse(entry[1]);
|
|
284
|
+
if (msg) {
|
|
285
|
+
hookLayer.state(scene, msg, null);
|
|
286
|
+
}
|
|
287
|
+
} else if (entry[0] === 'send' && entry[1] === viewer) {
|
|
288
|
+
hookLayer.message(scene, JSON.parse(entry[2]));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return Object.fromEntries(State.hub.wearing);
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const warn = console.warn;
|
|
297
|
+
const info = console.log;
|
|
298
|
+
const warned = [];
|
|
299
|
+
console.warn = (line) => warned.push(String(line));
|
|
300
|
+
console.log = () => {};
|
|
301
|
+
let n = 0;
|
|
302
|
+
async function test(name, fn) {
|
|
303
|
+
await fn();
|
|
304
|
+
n += 1;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const never = await story(here.layer, { tries: false });
|
|
308
|
+
const used = await story(here.layer, { tries: true });
|
|
309
|
+
|
|
310
|
+
await test('never called: no snapshot, delta or message names a try-on, and no branch for one is taken', async () => {
|
|
311
|
+
assert.ok(!text(never.log).includes('hubTry') && !text(never.log).includes('hub-try'));
|
|
312
|
+
// The state behind it, on the module itself: empty and unused until a call.
|
|
313
|
+
const h = { ...Wear.newWearState(), players: new Map([['a', { hello: true }]]), sim: { players: new Map([['a', {}]]) }, warned: new Set() };
|
|
314
|
+
assert.equal(Wear.tryOnUsed(h), false);
|
|
315
|
+
assert.deepEqual(Wear.tryingOf(h, null), {});
|
|
316
|
+
assert.equal(Wear.tryOn(h, 'a', cap), true);
|
|
317
|
+
assert.equal(Wear.tryOnUsed(h), true);
|
|
318
|
+
assert.deepEqual(Wear.tryingOf(h, null), { a: cap });
|
|
319
|
+
assert.equal(h.wearing.size + h.wearRev.size + h.wearSent.size, 0, 'nothing of the saved wearable is written');
|
|
320
|
+
Wear.forget(h, 'a');
|
|
321
|
+
assert.equal(h.trying.size + h.sentTryMe.size, 0);
|
|
322
|
+
assert.equal(Wear.tryOnUsed(h), false);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
await test('used: everything else the place writes is what it writes when never called, in the same order', async () => {
|
|
326
|
+
assert.ok(text(used.log).includes('hubTry'));
|
|
327
|
+
assert.equal(text(withoutTry(used.log)), text(never.log));
|
|
328
|
+
// Ghost and the deck's hooks heard exactly the same.
|
|
329
|
+
assert.deepEqual(used.calls, never.calls);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
await test('used and all cleared: from then on the place writes byte for byte what a fresh one does', async () => {
|
|
333
|
+
// Both run the same ticks first; only one of them tries anything on.
|
|
334
|
+
const prelude = (tries) => async (r) => {
|
|
335
|
+
if (tries) {
|
|
336
|
+
assert.equal(r.hub().tryOn('p1', straw), true);
|
|
337
|
+
assert.equal(r.hub().tryOn('p2', cap), true);
|
|
338
|
+
}
|
|
339
|
+
r.delta();
|
|
340
|
+
await r.tick();
|
|
341
|
+
r.join();
|
|
342
|
+
if (tries) {
|
|
343
|
+
assert.equal(r.hub().tryOn('p1', null), true);
|
|
344
|
+
assert.equal(r.hub().tryOn('p2', null), true);
|
|
345
|
+
}
|
|
346
|
+
r.delta();
|
|
347
|
+
await r.tick();
|
|
348
|
+
};
|
|
349
|
+
const fresh = await story(here.layer, { tries: false, before: prelude(false) });
|
|
350
|
+
const cleared = await story(here.layer, { tries: false, before: prelude(true) });
|
|
351
|
+
assert.ok(text(cleared.log).includes('"hubTry":{}'), 'the empty map went out once');
|
|
352
|
+
assert.equal(text(cleared.log).split('"hubTry":{}').length, 2);
|
|
353
|
+
assert.equal(text(cleared.main), text(fresh.main));
|
|
354
|
+
assert.ok(bytes(fresh.main) > 2000);
|
|
355
|
+
assert.equal(text(withoutTry(cleared.log)), text(fresh.log));
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
await test('shown to everyone and to a late joiner; each change once; `{}` once when the last goes', async () => {
|
|
359
|
+
const r = room(here.layer);
|
|
360
|
+
r.enter('p1');
|
|
361
|
+
r.enter('p2');
|
|
362
|
+
await r.tick();
|
|
363
|
+
r.delta();
|
|
364
|
+
assert.equal('hubTry' in r.join(), false);
|
|
365
|
+
assert.equal(r.hub().tryOn('p2', { kind: 'hat-witch', color: '#3A2A4A', rarity: 0 }), true);
|
|
366
|
+
assert.deepEqual(r.delta(), { hubTry: { p2: witch } });
|
|
367
|
+
assert.equal(r.delta(), null, 'latched: sent only on change');
|
|
368
|
+
// The same again changes nothing.
|
|
369
|
+
assert.equal(r.hub().tryOn('p2', witch), true);
|
|
370
|
+
assert.equal(r.delta(), null);
|
|
371
|
+
// A late joiner's snapshot has it, and they are told nothing twice.
|
|
372
|
+
r.enter('p3');
|
|
373
|
+
await r.tick();
|
|
374
|
+
assert.deepEqual(r.join().hubTry, { p2: witch });
|
|
375
|
+
assert.equal(r.delta()?.hubTry, undefined);
|
|
376
|
+
// Latest wins; a second player's rides the same map.
|
|
377
|
+
r.hub().tryOn('p2', cap);
|
|
378
|
+
r.hub().tryOn('p3', straw);
|
|
379
|
+
assert.deepEqual(r.delta().hubTry, { p2: cap, p3: straw });
|
|
380
|
+
// The saved wearables were never part of it.
|
|
381
|
+
assert.deepEqual(r.join().hubWear, { p1: halo });
|
|
382
|
+
r.hub().tryOn('p2', null);
|
|
383
|
+
assert.deepEqual(r.delta().hubTry, { p3: straw });
|
|
384
|
+
r.hub().tryOn('p3', null);
|
|
385
|
+
assert.deepEqual(r.delta(), { hubTry: {} });
|
|
386
|
+
assert.equal(r.delta(), null);
|
|
387
|
+
assert.equal('hubTry' in r.join(), false);
|
|
388
|
+
// A kind the kit does not draw is kept, as `hub.wear` keeps one.
|
|
389
|
+
assert.equal(r.hub().tryOn('p1', { kind: 'crown', color: '#00ff00', rarity: 9 }), true);
|
|
390
|
+
assert.deepEqual(r.delta().hubTry, { p1: { kind: 'crown', color: '#00ff00', rarity: 9 } });
|
|
391
|
+
// A player who has not said hello is not shown yet.
|
|
392
|
+
const early = room(here.layer);
|
|
393
|
+
early.world.players.set('p1', {});
|
|
394
|
+
assert.equal(early.hub().tryOn('p1', cap), true);
|
|
395
|
+
assert.equal('hubTry' in early.join(), false);
|
|
396
|
+
assert.equal(early.delta()?.hubTry, undefined);
|
|
397
|
+
early.enter('p1');
|
|
398
|
+
assert.deepEqual(early.join().hubTry, { p1: cap });
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
await test('cleared by null, by the player leaving and by the place ending; the map never outgrows the players here', async () => {
|
|
402
|
+
const r = room(here.layer);
|
|
403
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
404
|
+
r.enter(id);
|
|
405
|
+
}
|
|
406
|
+
await r.tick();
|
|
407
|
+
r.delta();
|
|
408
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
409
|
+
r.hub().tryOn(id, cap);
|
|
410
|
+
}
|
|
411
|
+
assert.deepEqual(Object.keys(r.delta().hubTry), ['p1', 'p2', 'p3']);
|
|
412
|
+
r.hub().tryOn('p1', null);
|
|
413
|
+
assert.deepEqual(Object.keys(r.delta().hubTry), ['p2', 'p3']);
|
|
414
|
+
// p2 leaves: theirs goes with them, without a word from the place.
|
|
415
|
+
r.exit('p2');
|
|
416
|
+
await r.tick();
|
|
417
|
+
assert.deepEqual(r.log.filter((e) => e[0] === 'leave').at(-1), ['leave', 'p2']);
|
|
418
|
+
assert.deepEqual(r.delta().hubTry, { p3: cap });
|
|
419
|
+
assert.deepEqual(r.join().hubTry, { p3: cap });
|
|
420
|
+
// They are not here to be dressed any more.
|
|
421
|
+
assert.equal(r.hub().tryOn('p2', cap), false);
|
|
422
|
+
assert.deepEqual(r.join().hubTry, { p3: cap });
|
|
423
|
+
// The place ends: nothing is left.
|
|
424
|
+
await r.shutdown();
|
|
425
|
+
assert.equal('hubTry' in r.join(), false);
|
|
426
|
+
assert.deepEqual(r.delta().hubTry, {});
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
await test('the saved wearable is untouched: ghost is never called, and what is kept and sent as `hubWear` is the same', async () => {
|
|
430
|
+
const r = room(here.layer);
|
|
431
|
+
r.enter('p1');
|
|
432
|
+
r.enter('p2');
|
|
433
|
+
r.enter('p3');
|
|
434
|
+
await r.tick();
|
|
435
|
+
r.delta();
|
|
436
|
+
const wears = () => r.calls.filter((c) => c[0] === 'wear');
|
|
437
|
+
const reads = r.calls.filter((c) => c[0] === 'read').length;
|
|
438
|
+
const hubWear = JSON.stringify(r.join().hubWear);
|
|
439
|
+
assert.equal(hubWear, JSON.stringify({ p1: halo }));
|
|
440
|
+
// On a signed-in player with a saved wearable, one without, and a guest.
|
|
441
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
442
|
+
assert.equal(r.hub().tryOn(id, straw), true);
|
|
443
|
+
assert.equal(r.hub().tryOn(id, cap), true);
|
|
444
|
+
}
|
|
445
|
+
await r.tick();
|
|
446
|
+
await flush();
|
|
447
|
+
assert.deepEqual(r.delta(), { hubTry: { p1: cap, p2: cap, p3: cap } }, 'no `hubWear` goes out');
|
|
448
|
+
assert.equal(JSON.stringify(r.join().hubWear), hubWear);
|
|
449
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
450
|
+
assert.equal(r.hub().tryOn(id, null), true);
|
|
451
|
+
}
|
|
452
|
+
await r.tick();
|
|
453
|
+
await flush();
|
|
454
|
+
assert.deepEqual(wears(), [], 'hub.tryOn never reaches ghost');
|
|
455
|
+
assert.equal(r.calls.filter((c) => c[0] === 'read').length, reads);
|
|
456
|
+
assert.equal(JSON.stringify(r.join().hubWear), hubWear);
|
|
457
|
+
// `hub.wear` still does: the stub is the one `hub.wear` writes to. What
|
|
458
|
+
// ghost already holds (p1's halo) is still known as held, so it is not sent.
|
|
459
|
+
assert.equal(r.hub().wear('p1', halo), true);
|
|
460
|
+
assert.deepEqual(wears(), []);
|
|
461
|
+
assert.equal(r.hub().wear('p2', aura), true);
|
|
462
|
+
assert.deepEqual(wears(), [['wear', 'p2', aura]]);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
await test('hub.wear during a try-on saves as it always did and shows once the try-on clears', async () => {
|
|
466
|
+
const server = room(here.layer);
|
|
467
|
+
server.enter('p1');
|
|
468
|
+
server.enter('p2');
|
|
469
|
+
await server.tick();
|
|
470
|
+
server.delta();
|
|
471
|
+
const c = client(here, 'p2');
|
|
472
|
+
assert.deepEqual(c.feed(server.log), { p1: halo });
|
|
473
|
+
server.log.length = 0;
|
|
474
|
+
server.hub().tryOn('p1', witch);
|
|
475
|
+
server.delta();
|
|
476
|
+
assert.deepEqual(c.feed(server.log.splice(0)), { p1: witch });
|
|
477
|
+
// Saved now, to ghost and in `hubWear`, and still not what is drawn.
|
|
478
|
+
assert.equal(server.hub().wear('p1', aura), true);
|
|
479
|
+
assert.deepEqual(server.calls.filter((call) => call[0] === 'wear'), [['wear', 'p1', aura]]);
|
|
480
|
+
assert.deepEqual(server.delta(), { hubWear: { p1: aura } });
|
|
481
|
+
assert.deepEqual(c.feed(server.log.splice(0)), { p1: witch });
|
|
482
|
+
// The try-on goes: the newly saved one is drawn.
|
|
483
|
+
server.hub().tryOn('p1', null);
|
|
484
|
+
assert.deepEqual(server.delta(), { hubTry: {} });
|
|
485
|
+
assert.deepEqual(c.feed(server.log.splice(0)), { p1: aura });
|
|
486
|
+
// And a saved one taken off under a try-on leaves nothing when it clears.
|
|
487
|
+
server.hub().tryOn('p1', cap);
|
|
488
|
+
server.hub().wear('p1', null);
|
|
489
|
+
server.delta();
|
|
490
|
+
assert.deepEqual(c.feed(server.log.splice(0)), { p1: cap });
|
|
491
|
+
server.hub().tryOn('p1', null);
|
|
492
|
+
server.delta();
|
|
493
|
+
assert.deepEqual(c.feed(server.log.splice(0)), {});
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
await test('validation is hub.wear’s: a player who is not here and a spec that is not a wearable are refused and logged once', async () => {
|
|
497
|
+
const r = room(here.layer);
|
|
498
|
+
r.enter('p1');
|
|
499
|
+
await r.tick();
|
|
500
|
+
r.hub().tryOn('p1', cap);
|
|
501
|
+
r.delta();
|
|
502
|
+
warned.length = 0;
|
|
503
|
+
const bad = [
|
|
504
|
+
{ kind: 'halo', color: 'gold', rarity: 1 },
|
|
505
|
+
{ kind: 'Halo', color: '#ffd700', rarity: 1 },
|
|
506
|
+
{ kind: 'halo', color: '#ffd700', rarity: 10 },
|
|
507
|
+
{ kind: 'halo', color: '#ffd700', rarity: 1.5 },
|
|
508
|
+
{ kind: 'halo', color: '#ffd700', rarity: 1, text: 'free text' },
|
|
509
|
+
{ kind: 'halo', color: '#ffd700' },
|
|
510
|
+
'halo',
|
|
511
|
+
[],
|
|
512
|
+
7,
|
|
513
|
+
];
|
|
514
|
+
for (const spec of bad) {
|
|
515
|
+
assert.equal(r.hub().tryOn('p1', spec), r.hub().wear('p1', spec), JSON.stringify(spec));
|
|
516
|
+
assert.equal(r.hub().tryOn('p1', spec), false, JSON.stringify(spec));
|
|
517
|
+
}
|
|
518
|
+
for (const who of ['nobody', undefined, null, 7]) {
|
|
519
|
+
assert.equal(r.hub().tryOn(who, cap), false);
|
|
520
|
+
assert.equal(r.hub().wear(who, cap), false);
|
|
521
|
+
}
|
|
522
|
+
// A refused one changes nothing.
|
|
523
|
+
assert.equal(r.delta(), null);
|
|
524
|
+
assert.deepEqual(r.join().hubTry, { p1: cap });
|
|
525
|
+
// Each API warns in its own name, once per kind of refusal.
|
|
526
|
+
const lines = (api) => warned.filter((line) => line.includes(`hub.${api}:`));
|
|
527
|
+
assert.equal(lines('tryOn').length, lines('wear').length);
|
|
528
|
+
assert.deepEqual(
|
|
529
|
+
lines('tryOn').map((line) => line.replaceAll('tryOn', 'wear')),
|
|
530
|
+
lines('wear'),
|
|
531
|
+
);
|
|
532
|
+
assert.ok(lines('tryOn').some((line) => line.includes('is not a player here')));
|
|
533
|
+
assert.ok(lines('tryOn').some((line) => line.includes('a wearable is { kind, color: "#rrggbb", rarity: 0-9 } or null')));
|
|
534
|
+
// Like `hub.wear`, no spec at all clears.
|
|
535
|
+
assert.equal(r.hub().tryOn('p1'), true);
|
|
536
|
+
assert.deepEqual(r.delta(), { hubTry: {} });
|
|
537
|
+
// Every hat the kit lists is one a place may try on.
|
|
538
|
+
for (const id of HATS) {
|
|
539
|
+
assert.equal(r.hub().tryOn('p1', { kind: `hat-${id}`, color: '#123456', rarity: 0 }), true, id);
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
await test('a block hides a try-on as it hides the wearable: out of the shared map, sent alone to who may see it', async () => {
|
|
544
|
+
// p1 and p2 hide each other; p3 sees both.
|
|
545
|
+
const hides = (x, y) => [x, y].sort().join() === 'p1,p2';
|
|
546
|
+
const r = room(here.layer, { hides });
|
|
547
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
548
|
+
r.enter(id);
|
|
549
|
+
}
|
|
550
|
+
await r.tick();
|
|
551
|
+
r.delta();
|
|
552
|
+
r.log.length = 0;
|
|
553
|
+
r.hub().tryOn('p1', witch);
|
|
554
|
+
r.hub().tryOn('p3', cap);
|
|
555
|
+
await r.tick();
|
|
556
|
+
assert.deepEqual(r.delta().hubTry, { p3: cap });
|
|
557
|
+
assert.deepEqual(r.join().hubTry, { p3: cap });
|
|
558
|
+
const alone = () => r.log.filter((e) => e[0] === 'send' && JSON.parse(e[2]).k === 'hub-try').map((e) => [e[1], JSON.parse(e[2]).hubTry]);
|
|
559
|
+
assert.deepEqual(alone(), [
|
|
560
|
+
['p1', { p1: witch }],
|
|
561
|
+
['p3', { p1: witch }],
|
|
562
|
+
]);
|
|
563
|
+
// Each client draws what it may see.
|
|
564
|
+
assert.deepEqual(client(here, 'p3').feed(r.log).p1, witch);
|
|
565
|
+
assert.equal('p1' in client(here, 'p2').feed(r.log), false);
|
|
566
|
+
await r.tick();
|
|
567
|
+
assert.equal(alone().length, 2, 'sent only on change');
|
|
568
|
+
r.hub().tryOn('p1', null);
|
|
569
|
+
await r.tick();
|
|
570
|
+
assert.deepEqual(alone().slice(2), [
|
|
571
|
+
['p1', {}],
|
|
572
|
+
['p3', {}],
|
|
573
|
+
]);
|
|
574
|
+
// Blocks and nobody trying anything on: the same story writes nothing new.
|
|
575
|
+
const quiet = await story(here.layer, { tries: false, hides });
|
|
576
|
+
assert.ok(!text(quiet.log).includes('hubTry') && !text(quiet.log).includes('hub-try'));
|
|
577
|
+
const loud = await story(here.layer, { tries: true, hides });
|
|
578
|
+
assert.ok(text(loud.log).includes('hub-try'));
|
|
579
|
+
assert.equal(text(withoutTry(loud.log)), text(quiet.log));
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
await test('the client draws the try-on instead of the saved wearable, and the saved one again when it goes', async () => {
|
|
583
|
+
const c = client(here, 'p3');
|
|
584
|
+
const halfway = used.log.findIndex((e) => e[0] === 'delta' && e[1].includes('"hubTry":{"p2"') && e[1].includes('"p1"'));
|
|
585
|
+
assert.ok(halfway > 0);
|
|
586
|
+
// p1 owns a halo and has a straw hat tried on; p2 owns nothing and has a witch's.
|
|
587
|
+
assert.deepEqual(c.feed(used.log.slice(0, halfway + 1)), { p1: straw, p2: witch });
|
|
588
|
+
assert.deepEqual(Object.fromEntries(c.hub.wearAll), { p1: halo }, 'the saved map is the server’s, unchanged');
|
|
589
|
+
// What is drawn: the hat's pieces over p1, not the halo's one.
|
|
590
|
+
const local = { id: 'me-body', components: { Transform: { x: 0, y: 1, z: 0, rotationY: 0 } } };
|
|
591
|
+
const actors = new Map([
|
|
592
|
+
['me-body', local],
|
|
593
|
+
['body-p1', { id: 'body-p1', components: { Transform: { x: 3, y: 1, z: 0, rotationY: 0 } } }],
|
|
594
|
+
]);
|
|
595
|
+
const scene = {
|
|
596
|
+
multiplayer: { net: { selfId: () => 'p3' }, avatars: { actors: new Map([['p1', 'body-p1']]), retired: new Set() } },
|
|
597
|
+
getActor: (id) => actors.get(id) ?? null,
|
|
598
|
+
spawnActor: (actor) => actors.set(actor.id, { id: actor.id, components: { ...actor.components, Transform: { ...actor.components.Transform } } }),
|
|
599
|
+
despawnActor: (id) => actors.delete(id),
|
|
600
|
+
};
|
|
601
|
+
const fit = null;
|
|
602
|
+
const wear = makeWear();
|
|
603
|
+
const worn = () => [...actors.keys()].filter((id) => id.startsWith('wear:p1:')).map((id) => actors.get(id).components.Shape);
|
|
604
|
+
wear.frame(scene, local, c.hub.wearing, () => 1.66, 0, () => fit);
|
|
605
|
+
const hat = worn();
|
|
606
|
+
assert.ok(hat.length >= 1 && hat.every((shape) => shape.emissive === undefined), 'a matte hat, no glow');
|
|
607
|
+
assert.equal(hat[0].color, straw.color);
|
|
608
|
+
// The same client with the saved look alone, for what a halo is drawn as.
|
|
609
|
+
const plain = makeWear();
|
|
610
|
+
const before = new Map(actors);
|
|
611
|
+
wear.clear(scene);
|
|
612
|
+
plain.frame(scene, local, new Map([['p1', halo]]), () => 1.66, 0, () => fit);
|
|
613
|
+
const haloShapes = JSON.stringify(worn());
|
|
614
|
+
plain.clear(scene);
|
|
615
|
+
assert.equal(actors.size, before.size - hat.length);
|
|
616
|
+
// The rest of the visit: p1's comes off (nothing saved by then but what the
|
|
617
|
+
// place saved since), p2 leaves, the place ends.
|
|
618
|
+
const end = c.feed(used.log.slice(halfway + 1));
|
|
619
|
+
assert.deepEqual(end, client(here, 'p3').feed(never.log), 'at the end, exactly what a place that never tried anything on leaves drawn');
|
|
620
|
+
// Cleared with the saved halo still there: the halo is drawn again as it was.
|
|
621
|
+
const s = room(here.layer);
|
|
622
|
+
s.enter('p1');
|
|
623
|
+
await s.tick();
|
|
624
|
+
s.hub().tryOn('p1', straw);
|
|
625
|
+
s.delta();
|
|
626
|
+
const again = client(here, 'p3');
|
|
627
|
+
again.feed(s.log.splice(0));
|
|
628
|
+
wear.frame(scene, local, again.hub.wearing, () => 1.66, 16, () => fit);
|
|
629
|
+
assert.equal(worn()[0].color, straw.color);
|
|
630
|
+
s.hub().tryOn('p1', null);
|
|
631
|
+
s.delta();
|
|
632
|
+
again.feed(s.log.splice(0));
|
|
633
|
+
wear.frame(scene, local, again.hub.wearing, () => 1.66, 32, () => fit);
|
|
634
|
+
assert.equal(JSON.stringify(worn()), haloShapes);
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
await test('leaving the place clears the overlay on the client: none in the town, none in the next place', async () => {
|
|
638
|
+
const c = client(here, 'p3');
|
|
639
|
+
const halfway = used.log.findIndex((e) => e[0] === 'delta' && e[1].includes('"hubTry":{"p2"') && e[1].includes('"p1"'));
|
|
640
|
+
c.feed(used.log.slice(0, halfway + 1));
|
|
641
|
+
assert.equal(c.hub.tryAll.size, 2);
|
|
642
|
+
// A session switch (through a door, back to the town) clears the place's state.
|
|
643
|
+
c.State.clear();
|
|
644
|
+
assert.equal(c.hub.tryAll.size + c.hub.tryMe.size + c.hub.wearing.size, 0);
|
|
645
|
+
// The next place, which never tries anything on, draws only what is saved.
|
|
646
|
+
assert.deepEqual(c.feed(never.log.slice(0, 1 + never.log.findIndex((e) => e[0] === 'join' && e[1].includes('"hubWear"')))), { p1: halo });
|
|
647
|
+
// A snapshot with no `hubTry` means none is up, even without the switch
|
|
648
|
+
// (a client that rejoined the same place after its try-ons were cleared).
|
|
649
|
+
const r = client(here, 'p3');
|
|
650
|
+
r.feed(used.log.slice(0, halfway + 1));
|
|
651
|
+
assert.equal(r.hub.tryAll.size, 2);
|
|
652
|
+
assert.deepEqual(r.feed([never.log.find((e) => e[0] === 'join' && e[1].includes('"hubWear"'))]), { p1: halo });
|
|
653
|
+
assert.equal(r.hub.tryAll.size, 0);
|
|
654
|
+
// A delta without it changes nothing: it is sent only on change.
|
|
655
|
+
const d = client(here, 'p3');
|
|
656
|
+
d.feed(used.log.slice(0, halfway + 1));
|
|
657
|
+
d.feed([['delta', JSON.stringify({ marks: {} })]]);
|
|
658
|
+
assert.equal(d.hub.tryAll.size, 2);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
await test('a client that never heard of try-ons: the new keys change nothing it reads', async () => {
|
|
662
|
+
// Fed everything, and fed the log without `hubTry` and `hub-try`: the maps
|
|
663
|
+
// an older hook layer reads (`hubWear`, `hub-wear`) come out the same, so
|
|
664
|
+
// it keeps drawing the saved look.
|
|
665
|
+
for (const viewer of ['p1', 'p3']) {
|
|
666
|
+
const full = client(here, viewer);
|
|
667
|
+
full.feed(used.log);
|
|
668
|
+
const saved = JSON.stringify([[...full.hub.wearAll], [...full.hub.wearMe], [...full.hub.avatars], full.hub.marks, full.hub.labels]);
|
|
669
|
+
const old = client(here, viewer);
|
|
670
|
+
old.feed(withoutTry(used.log));
|
|
671
|
+
assert.equal(JSON.stringify([[...old.hub.wearAll], [...old.hub.wearMe], [...old.hub.avatars], old.hub.marks, old.hub.labels]), saved);
|
|
672
|
+
assert.equal(old.hub.tryAll.size, 0);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
const aloneIn = (log) => log.filter((e) => e[0] === 'send' && JSON.parse(e[2]).k === 'hub-try').map((e) => [e[1], JSON.parse(e[2]).hubTry]);
|
|
677
|
+
const p1p2 = (x, y) => [x, y].sort().join() === 'p1,p2';
|
|
678
|
+
|
|
679
|
+
await test('a private try-on after a rejoin is what the server holds now: none left behind, none missed', async () => {
|
|
680
|
+
// p1 and p2 hide each other; p3 sees p1's try-on through `hub-try`.
|
|
681
|
+
const r = room(here.layer, { hides: p1p2 });
|
|
682
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
683
|
+
r.enter(id);
|
|
684
|
+
}
|
|
685
|
+
await r.tick();
|
|
686
|
+
r.delta();
|
|
687
|
+
r.hub().tryOn('p1', witch);
|
|
688
|
+
await r.tick();
|
|
689
|
+
r.delta();
|
|
690
|
+
r.join();
|
|
691
|
+
const page = client(here, 'p3');
|
|
692
|
+
assert.deepEqual(page.feed(r.log.splice(0)).p1, witch);
|
|
693
|
+
// p3 drops out, p1's comes off, and p3 is back on the same page, with what
|
|
694
|
+
// it knew: it is told the private map again, empty as it is.
|
|
695
|
+
r.exit('p3');
|
|
696
|
+
await r.tick();
|
|
697
|
+
r.hub().tryOn('p1', null);
|
|
698
|
+
await r.tick();
|
|
699
|
+
r.delta();
|
|
700
|
+
r.body('p3');
|
|
701
|
+
r.hello('p3');
|
|
702
|
+
await r.tick();
|
|
703
|
+
r.join();
|
|
704
|
+
r.delta();
|
|
705
|
+
assert.deepEqual(aloneIn(r.log).filter(([to]) => to === 'p3'), [['p3', {}]]);
|
|
706
|
+
page.feed(r.log.splice(0));
|
|
707
|
+
assert.deepEqual(Object.fromEntries(page.hub.wearing), { p1: halo }, 'the saved halo, not the costume that came off');
|
|
708
|
+
assert.equal(page.hub.tryMe.size, 0);
|
|
709
|
+
// A fresh page that resumes its player id while one IS up (the place never
|
|
710
|
+
// saw it go): its hello has the private map sent again.
|
|
711
|
+
r.hub().tryOn('p1', cap);
|
|
712
|
+
await r.tick();
|
|
713
|
+
r.delta();
|
|
714
|
+
assert.deepEqual(aloneIn(r.log.splice(0)), [
|
|
715
|
+
['p1', { p1: cap }],
|
|
716
|
+
['p3', { p1: cap }],
|
|
717
|
+
]);
|
|
718
|
+
r.hello('p3');
|
|
719
|
+
await r.tick();
|
|
720
|
+
r.join();
|
|
721
|
+
r.delta();
|
|
722
|
+
assert.deepEqual(aloneIn(r.log), [['p3', { p1: cap }]]);
|
|
723
|
+
assert.deepEqual(client(here, 'p3').feed(r.log.splice(0)).p1, cap);
|
|
724
|
+
// A hello that is only an avatar change is the same page: nothing is sent again.
|
|
725
|
+
r.hello('p3', { again: true });
|
|
726
|
+
await r.tick();
|
|
727
|
+
assert.deepEqual(aloneIn(r.log.splice(0)), []);
|
|
728
|
+
// A hello the place hears before the player has a body: the private map is
|
|
729
|
+
// sent on the tick they get one, which is before their snapshot, and the
|
|
730
|
+
// snapshot does not take it away.
|
|
731
|
+
r.exit('p3');
|
|
732
|
+
await r.tick();
|
|
733
|
+
r.log.length = 0;
|
|
734
|
+
r.body('p3');
|
|
735
|
+
r.world.players.delete('p3');
|
|
736
|
+
r.hello('p3');
|
|
737
|
+
await r.tick();
|
|
738
|
+
assert.deepEqual(aloneIn(r.log), []);
|
|
739
|
+
r.world.players.set('p3', { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
|
|
740
|
+
await r.tick();
|
|
741
|
+
r.join();
|
|
742
|
+
const order = r.log.map((e) => (e[0] === 'send' ? JSON.parse(e[2]).k : e[0]));
|
|
743
|
+
assert.ok(order.indexOf('hub-try') >= 0 && order.indexOf('hub-try') < order.indexOf('join'));
|
|
744
|
+
assert.deepEqual(client(here, 'p3').feed(r.log.splice(0)).p1, cap);
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
await test('a rejoin sends nothing new where no private try-on was ever sent', async () => {
|
|
748
|
+
// Blocks and no try-on; try-ons and no blocks: a hello after the first is
|
|
749
|
+
// answered with exactly what it was.
|
|
750
|
+
for (const [hides, tries] of [
|
|
751
|
+
[p1p2, false],
|
|
752
|
+
[null, true],
|
|
753
|
+
]) {
|
|
754
|
+
const r = room(here.layer, { hides });
|
|
755
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
756
|
+
r.enter(id);
|
|
757
|
+
}
|
|
758
|
+
await r.tick();
|
|
759
|
+
if (tries) {
|
|
760
|
+
r.hub().tryOn('p1', witch);
|
|
761
|
+
r.hub().tryOn('p3', cap);
|
|
762
|
+
}
|
|
763
|
+
await r.tick();
|
|
764
|
+
r.delta();
|
|
765
|
+
r.log.length = 0;
|
|
766
|
+
r.hello('p3');
|
|
767
|
+
await r.tick();
|
|
768
|
+
r.exit('p3');
|
|
769
|
+
await r.tick();
|
|
770
|
+
r.body('p3');
|
|
771
|
+
r.hello('p3');
|
|
772
|
+
await r.tick();
|
|
773
|
+
assert.deepEqual(aloneIn(r.log), []);
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
await test('a try-on made before the player’s hello goes when they do: none comes back with their id, and the place’s end leaves none', async () => {
|
|
778
|
+
const r = room(here.layer);
|
|
779
|
+
r.enter('p2');
|
|
780
|
+
await r.tick();
|
|
781
|
+
r.delta();
|
|
782
|
+
// p1 has a body and has not said hello: taken, as `hub.wear` takes it, and not shown yet.
|
|
783
|
+
r.body('p1');
|
|
784
|
+
assert.equal(r.hub().tryOn('p1', cap), true);
|
|
785
|
+
await r.tick();
|
|
786
|
+
assert.equal('hubTry' in r.join(), false);
|
|
787
|
+
// They go without ever saying it, and come back with the same id.
|
|
788
|
+
r.exit('p1');
|
|
789
|
+
await r.tick();
|
|
790
|
+
r.enter('p1');
|
|
791
|
+
await r.tick();
|
|
792
|
+
assert.equal('hubTry' in r.join(), false, 'the old try-on is not theirs any more');
|
|
793
|
+
assert.equal(r.delta()?.hubTry, undefined);
|
|
794
|
+
// Many come and go before their hello: nothing of them is kept.
|
|
795
|
+
const ids = Array.from({ length: 40 }, (_, k) => `g${k}`);
|
|
796
|
+
for (const id of ids) {
|
|
797
|
+
r.body(id);
|
|
798
|
+
assert.equal(r.hub().tryOn(id, cap), true);
|
|
799
|
+
await r.tick(10);
|
|
800
|
+
r.exit(id);
|
|
801
|
+
}
|
|
802
|
+
await r.tick();
|
|
803
|
+
for (const id of ids.slice(0, 5)) {
|
|
804
|
+
r.enter(id);
|
|
805
|
+
}
|
|
806
|
+
await r.tick();
|
|
807
|
+
assert.equal('hubTry' in r.join(), false);
|
|
808
|
+
// One who does say hello afterwards is shown, as before.
|
|
809
|
+
r.body('p3');
|
|
810
|
+
r.hub().tryOn('p3', straw);
|
|
811
|
+
r.hello('p3');
|
|
812
|
+
await r.tick();
|
|
813
|
+
assert.deepEqual(r.join().hubTry, { p3: straw });
|
|
814
|
+
// The place ends with a try-on on a player who never said hello.
|
|
815
|
+
const last = room(here.layer);
|
|
816
|
+
last.enter('p2');
|
|
817
|
+
last.body('p1');
|
|
818
|
+
await last.tick();
|
|
819
|
+
last.hub().tryOn('p1', cap);
|
|
820
|
+
await last.shutdown();
|
|
821
|
+
last.hello('p1');
|
|
822
|
+
assert.equal('hubTry' in last.join(), false);
|
|
823
|
+
// The module's own rule: a try-on is kept only for a player who is here.
|
|
824
|
+
const h = { ...Wear.newWearState(), players: new Map([['a', { hello: true }]]), sim: { players: new Map([['b', {}]]) }, warned: new Set() };
|
|
825
|
+
for (const id of ['a', 'b']) {
|
|
826
|
+
assert.equal(Wear.tryOn(h, id, cap), true);
|
|
827
|
+
}
|
|
828
|
+
h.sentTryMe.set('b', '{}');
|
|
829
|
+
h.sentTryMe.set('gone', '{}');
|
|
830
|
+
h.trying.set('gone', cap);
|
|
831
|
+
Wear.pruneTries(h);
|
|
832
|
+
assert.deepEqual([...h.trying.keys()], ['a', 'b']);
|
|
833
|
+
assert.deepEqual([...h.sentTryMe.keys()], ['b']);
|
|
834
|
+
h.sim.players.delete('b');
|
|
835
|
+
Wear.pruneTries(h);
|
|
836
|
+
assert.deepEqual([...h.trying.keys()], ['a']);
|
|
837
|
+
assert.equal(h.sentTryMe.size, 0);
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
await test('the same try-on asked for every tick is sent once: no delta, no `hub-try`, nothing else', async () => {
|
|
841
|
+
for (const hides of [null, p1p2]) {
|
|
842
|
+
const run = async (everyTick) => {
|
|
843
|
+
const r = room(here.layer, { hides });
|
|
844
|
+
for (const id of ['p1', 'p2', 'p3']) {
|
|
845
|
+
r.enter(id);
|
|
846
|
+
}
|
|
847
|
+
await r.tick();
|
|
848
|
+
r.delta();
|
|
849
|
+
r.log.length = 0;
|
|
850
|
+
for (let k = 0; k < 25; k += 1) {
|
|
851
|
+
if (k === 0 || everyTick) {
|
|
852
|
+
// A fresh object each time, as a shop's step hook would build it.
|
|
853
|
+
assert.equal(r.hub().tryOn('p1', { ...witch }), true);
|
|
854
|
+
assert.equal(r.hub().tryOn('p3', { kind: 'hat-cap', color: '#D1453B', rarity: 2 }), true);
|
|
855
|
+
}
|
|
856
|
+
await r.tick();
|
|
857
|
+
r.delta();
|
|
858
|
+
}
|
|
859
|
+
return r.log;
|
|
860
|
+
};
|
|
861
|
+
const once = await run(false);
|
|
862
|
+
const every = await run(true);
|
|
863
|
+
assert.equal(text(every), text(once));
|
|
864
|
+
assert.equal(every.filter((e) => e[0] === 'delta' && e[1] !== 'null').length, 1);
|
|
865
|
+
assert.equal(text(every).split('hubTry').length - 1, hides ? 3 : 1);
|
|
866
|
+
assert.equal(aloneIn(every).length, hides ? 2 : 0);
|
|
867
|
+
}
|
|
868
|
+
});
|
|
869
|
+
|
|
870
|
+
await test('a guest is dressed like anyone else, and a player with no user id at all', async () => {
|
|
871
|
+
const roster = [...ROSTER, { playerId: 'p4', username: 'nobody' }];
|
|
872
|
+
const r = room(here.layer, { roster });
|
|
873
|
+
for (const id of ['p1', 'p3', 'p4']) {
|
|
874
|
+
r.enter(id);
|
|
875
|
+
}
|
|
876
|
+
await r.tick();
|
|
877
|
+
r.delta();
|
|
878
|
+
assert.equal(r.hub().tryOn('p3', witch), true);
|
|
879
|
+
assert.equal(r.hub().tryOn('p4', cap), true);
|
|
880
|
+
assert.deepEqual(r.delta(), { hubTry: { p3: witch, p4: cap } });
|
|
881
|
+
assert.deepEqual(r.join().hubTry, { p3: witch, p4: cap });
|
|
882
|
+
assert.deepEqual(client(here, 'p1').feed(r.log), { p1: halo, p3: witch, p4: cap });
|
|
883
|
+
assert.deepEqual(r.calls.filter((c) => c[0] === 'wear'), []);
|
|
884
|
+
assert.equal(r.hub().tryOn('p3', null), true);
|
|
885
|
+
r.exit('p4');
|
|
886
|
+
await r.tick();
|
|
887
|
+
assert.deepEqual(r.delta().hubTry, {});
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
// The same story on another copy of the kit (see the top of this file).
|
|
891
|
+
if (baseline) {
|
|
892
|
+
await test('baseline: the never-called story is byte for byte the other copy’s, server and client', async () => {
|
|
893
|
+
const theirs = await story(baseline.layer, { tries: false });
|
|
894
|
+
assert.equal(text(never.log), text(theirs.log));
|
|
895
|
+
for (const viewer of ['p1', 'p2', 'p3']) {
|
|
896
|
+
const mine = client(here, viewer);
|
|
897
|
+
const old = client(baseline, viewer);
|
|
898
|
+
for (let i = 1; i <= never.log.length; i += 1) {
|
|
899
|
+
mine.feed(never.log.slice(i - 1, i));
|
|
900
|
+
old.feed(theirs.log.slice(i - 1, i));
|
|
901
|
+
assert.equal(JSON.stringify([...mine.hub.wearing]), JSON.stringify([...old.hub.wearing]), `${viewer} at ${i}`);
|
|
902
|
+
}
|
|
903
|
+
const { tryAll: _a, tryMe: _b, ...mineRest } = mine.hub;
|
|
904
|
+
assert.equal(JSON.stringify(mineRest, replacer), JSON.stringify(old.hub, replacer), `${viewer}: every other field of the client's state`);
|
|
905
|
+
}
|
|
906
|
+
// The other copy's client fed a place that DOES try things on keeps
|
|
907
|
+
// drawing the saved look, as it does where nothing is tried on.
|
|
908
|
+
for (const viewer of ['p1', 'p2', 'p3']) {
|
|
909
|
+
const old = client(baseline, viewer);
|
|
910
|
+
const plain = client(baseline, viewer);
|
|
911
|
+
const oldUsed = [];
|
|
912
|
+
const oldNever = [];
|
|
913
|
+
used.log.forEach((entry) => oldUsed.push(JSON.stringify(old.feed([entry]))));
|
|
914
|
+
withoutTry(used.log).forEach((entry) => oldNever.push(JSON.stringify(plain.feed([entry]))));
|
|
915
|
+
assert.equal(JSON.stringify(old.hub, replacer), JSON.stringify(plain.hub, replacer));
|
|
916
|
+
assert.ok(!oldUsed.join().includes('hat-'), 'no try-on is ever drawn by it');
|
|
917
|
+
assert.equal(oldUsed.at(-1), oldNever.at(-1));
|
|
918
|
+
}
|
|
919
|
+
info(
|
|
920
|
+
`test-try-on: baseline ${process.env.HUB_TRY_BASELINE}: never-called log ${bytes(never.log)} bytes in ${never.log.length} entries, identical; with try-ons ${bytes(used.log)} bytes in ${used.log.length} entries`,
|
|
921
|
+
);
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
function replacer(key, value) {
|
|
926
|
+
if (value instanceof Map) {
|
|
927
|
+
return { map: [...value] };
|
|
928
|
+
}
|
|
929
|
+
if (value instanceof Set) {
|
|
930
|
+
return { set: [...value] };
|
|
931
|
+
}
|
|
932
|
+
return key === 'lastTAt' || key === 'whereAtAt' ? 0 : value;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
console.warn = warn;
|
|
936
|
+
if (process.env.HUB_TRY_DUMP) {
|
|
937
|
+
info(text(used.log));
|
|
938
|
+
}
|
|
939
|
+
console.log = info;
|
|
940
|
+
console.log(`test-try-on: ${n} ok (${bytes(never.log)} bytes never called, ${bytes(used.log)} with try-ons)`);
|