castle-web-cli 0.4.229 → 0.4.230
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/agent-prompts.d.ts +2 -0
- package/dist/agent-prompts.js +16 -8
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +21 -21
- package/dist/api.d.ts +28 -2
- package/dist/api.js +74 -16
- package/dist/castle-host/host.d.ts +91 -0
- package/dist/castle-host/host.js +779 -3
- package/dist/castleJson.d.ts +44 -0
- package/dist/castleJson.js +131 -0
- package/dist/check.js +27 -1
- package/dist/contentPack.d.ts +49 -0
- package/dist/contentPack.js +273 -0
- package/dist/devExperiments.d.ts +21 -0
- package/dist/devExperiments.js +139 -0
- package/dist/devHub.d.ts +55 -0
- package/dist/devHub.js +187 -0
- package/dist/devSessionChild.js +3 -1
- package/dist/devSessionServer.d.ts +14 -1
- package/dist/devSessionServer.js +229 -44
- package/dist/experimentAssignment.d.ts +15 -0
- package/dist/experimentAssignment.js +41 -0
- package/dist/importCheckHook.d.ts +40 -0
- package/dist/importCheckHook.js +171 -0
- package/dist/importRegression.d.ts +15 -0
- package/dist/importRegression.js +100 -0
- package/dist/importTrust.d.ts +2 -0
- package/dist/importTrust.js +27 -0
- package/dist/importUpdateHook.d.ts +18 -0
- package/dist/importUpdateHook.js +31 -8
- package/dist/importUpdateHookRunner.js +19 -7
- package/dist/imports.d.ts +16 -0
- package/dist/imports.js +141 -31
- package/dist/index.js +3 -1
- package/dist/init.d.ts +3 -0
- package/dist/init.js +125 -8
- package/dist/install.js +4 -1
- package/dist/localPaths.js +4 -0
- package/dist/login.js +2 -2
- package/dist/platformDoc.js +3 -8
- package/dist/playPagePolicy.js +3 -1
- package/dist/remix.js +5 -0
- package/dist/routerTaskReport.d.ts +28 -0
- package/dist/routerTaskReport.js +56 -0
- package/dist/save-deck.d.ts +18 -0
- package/dist/save-deck.js +142 -6
- package/dist/serve.d.ts +8 -0
- package/dist/serve.js +94 -13
- package/dist/serverBundle.d.ts +1 -1
- package/dist/serverBundle.js +5 -1
- package/dist/webSourceSync.d.ts +2 -0
- package/dist/webSourceSync.js +2 -0
- package/kits/base/CLAUDE.md +2 -3
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +258 -52
- package/kits/base/sdk/agent.js +4 -4
- package/kits/base/sdk/avatar.d.ts +12 -0
- package/kits/base/sdk/avatar.js +23 -0
- package/kits/base/sdk/castle.d.ts +5 -0
- package/kits/base/sdk/castle.js +3 -0
- package/kits/base/sdk/commands.d.ts +208 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +6 -0
- package/kits/base/sdk/experiments.d.ts +15 -0
- package/kits/base/sdk/experiments.js +83 -0
- package/kits/base/sdk/hub.d.ts +85 -0
- package/kits/base/sdk/hub.js +107 -0
- package/kits/base/sdk/multiplayer.d.ts +45 -1
- package/kits/base/sdk/multiplayer.js +106 -4
- package/kits/base/sdk/server/hub.d.ts +169 -0
- package/kits/base/sdk/server/hub.js +125 -0
- package/kits/base/sdk/server/platformHandle.d.ts +42 -1
- package/kits/base/sdk/server/storage.d.ts +55 -2
- package/kits/base/sdk/server/wrapper.d.ts +32 -2
- package/kits/base/sdk/server/wrapper.js +90 -11
- package/kits/base/sdk/transport.d.ts +2 -1
- package/kits/base/sdk/transport.js +13 -1
- package/kits/hub-game/.prettierrc +5 -0
- package/kits/hub-game/CLAUDE.md +146 -0
- package/kits/hub-game/blueprints/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
- package/kits/hub-game/castle.json +96 -0
- package/kits/hub-game/code/client/attention.js +133 -0
- package/kits/hub-game/code/client/avatar.js +288 -0
- package/kits/hub-game/code/client/cabinets.js +90 -0
- package/kits/hub-game/code/client/chat.js +135 -0
- package/kits/hub-game/code/client/chrome.js +1001 -0
- package/kits/hub-game/code/client/doorFrame.js +15 -0
- package/kits/hub-game/code/client/firstJoin.js +14 -0
- package/kits/hub-game/code/client/grab.js +241 -0
- package/kits/hub-game/code/client/hookLayer.js +230 -0
- package/kits/hub-game/code/client/idle.js +326 -0
- package/kits/hub-game/code/client/keys.js +48 -0
- package/kits/hub-game/code/client/pack.js +232 -0
- package/kits/hub-game/code/client/packFormat.js +397 -0
- package/kits/hub-game/code/client/say.js +127 -0
- package/kits/hub-game/code/client/sha256.js +80 -0
- package/kits/hub-game/code/client/sound.js +147 -0
- package/kits/hub-game/code/client/state.js +131 -0
- package/kits/hub-game/code/client/talk.js +259 -0
- package/kits/hub-game/code/client/together.js +365 -0
- package/kits/hub-game/code/client/wear.js +149 -0
- package/kits/hub-game/code/server/actors.js +627 -0
- package/kits/hub-game/code/server/blocks.js +103 -0
- package/kits/hub-game/code/server/cabinets.js +309 -0
- package/kits/hub-game/code/server/game.js +137 -0
- package/kits/hub-game/code/server/grab.js +247 -0
- package/kits/hub-game/code/server/history.js +96 -0
- package/kits/hub-game/code/server/hub.js +1904 -0
- package/kits/hub-game/code/server/idle.js +412 -0
- package/kits/hub-game/code/server/index.js +64 -0
- package/kits/hub-game/code/server/labels.js +59 -0
- package/kits/hub-game/code/server/manifest.js +527 -0
- package/kits/hub-game/code/server/npcs.js +745 -0
- package/kits/hub-game/code/server/party.js +381 -0
- package/kits/hub-game/code/server/place.js +43 -0
- package/kits/hub-game/code/server/say.js +83 -0
- package/kits/hub-game/code/server/wear.js +142 -0
- package/kits/hub-game/code/systems/hub.js +2230 -0
- package/kits/hub-game/docs/content-pack.md +151 -0
- package/kits/hub-game/docs/contract.md +1139 -0
- package/kits/hub-game/docs/style.md +137 -0
- package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
- package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
- package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
- package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
- package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
- package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
- package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
- package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
- package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
- package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
- package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
- package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
- package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
- package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
- package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
- package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
- package/kits/hub-game/eslint.config.js +20 -0
- package/kits/hub-game/fonts.generated.js +9 -0
- package/kits/hub-game/index.html +22 -0
- package/kits/hub-game/main.jsx +6 -0
- package/kits/hub-game/models/avatar-0.pxmodel +21 -0
- package/kits/hub-game/models/avatar-1.pxmodel +24 -0
- package/kits/hub-game/models/avatar-2.pxmodel +27 -0
- package/kits/hub-game/models/avatar-3.pxmodel +22 -0
- package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
- package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
- package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
- package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
- package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
- package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
- package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
- package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
- package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
- package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
- package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
- package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
- package/kits/hub-game/package-lock.json +365 -0
- package/kits/hub-game/package.json +32 -0
- package/kits/hub-game/scenes/main.scene +318 -0
- package/kits/hub-game/scripts/check.mjs +339 -0
- package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
- package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
- package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
- package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
- package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
- package/kits/hub-game/scripts/pack.mjs +192 -0
- package/kits/hub-game/scripts/test-actors.mjs +666 -0
- package/kits/hub-game/scripts/test-attention.mjs +103 -0
- package/kits/hub-game/scripts/test-avatar.mjs +472 -0
- package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
- package/kits/hub-game/scripts/test-blocks.mjs +351 -0
- package/kits/hub-game/scripts/test-chat.mjs +98 -0
- package/kits/hub-game/scripts/test-classic.mjs +590 -0
- package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
- package/kits/hub-game/scripts/test-door.mjs +56 -0
- package/kits/hub-game/scripts/test-grab.mjs +331 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
- package/kits/hub-game/scripts/test-idle.mjs +518 -0
- package/kits/hub-game/scripts/test-interest.mjs +413 -0
- package/kits/hub-game/scripts/test-keys.mjs +50 -0
- package/kits/hub-game/scripts/test-manifest.mjs +224 -0
- package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
- package/kits/hub-game/scripts/test-npcs.mjs +714 -0
- package/kits/hub-game/scripts/test-pack.mjs +424 -0
- package/kits/hub-game/scripts/test-party.mjs +286 -0
- package/kits/hub-game/scripts/test-say.mjs +149 -0
- package/kits/hub-game/scripts/test-together.mjs +183 -0
- package/kits/hub-game/theme.style +3 -0
- package/kits/multiplayer-3d/castle.json +5 -5
- package/kits/multiplayer-3d/code/client/avatars.js +101 -1
- package/kits/multiplayer-3d/code/server/players.js +4 -1
- package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
- package/kits/multiplayer-3d/code/server/world.js +20 -7
- package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/art.js +13 -0
- package/kits/physics-2d/engine/assets.js +4 -2
- package/kits/physics-2d/engine/scene.js +25 -5
- package/kits/physics-3d/CLAUDE.md +6 -1
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
- package/kits/physics-3d/engine3d/artobjects.js +2 -2
- package/kits/physics-3d/engine3d/materials.js +29 -1
- package/kits/physics-3d/engine3d/world3d.js +65 -1
- package/kits/physics-3d/systems/physics3d.js +5 -2
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/client/connection.js +77 -7
- package/kits/real-time/code/client/gameHooks.js +63 -3
- package/kits/real-time/code/client/messages.js +7 -0
- package/kits/real-time/code/server/gameHooks.js +24 -0
- package/kits/real-time/code/server/session.js +390 -22
- package/package.json +2 -2
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Jump-to-door (contract §5): a town tells its clients its doors (`hubDoors`)
|
|
2
|
+
// and puts a player who picks one in front of it, facing it, in the door's own
|
|
3
|
+
// frame; a game has no list, and a held player, an unknown door or a second
|
|
4
|
+
// jump within a second is ignored. `npm run check` runs it; it needs nothing
|
|
5
|
+
// installed.
|
|
6
|
+
|
|
7
|
+
import assert from 'node:assert/strict';
|
|
8
|
+
import { register } from 'node:module';
|
|
9
|
+
|
|
10
|
+
const sceneStub = 'export const readScene = () => globalThis.__hubTestActors ?? [];';
|
|
11
|
+
register(
|
|
12
|
+
`data:text/javascript,${encodeURIComponent(`
|
|
13
|
+
export async function resolve(specifier, context, next) {
|
|
14
|
+
if (specifier === '@imports/castle.multiplayer-3d/code/server/sceneBodies.js') {
|
|
15
|
+
return { url: 'data:text/javascript,${encodeURIComponent(sceneStub)}', shortCircuit: true };
|
|
16
|
+
}
|
|
17
|
+
return next(specifier, context);
|
|
18
|
+
}
|
|
19
|
+
`)}`,
|
|
20
|
+
);
|
|
21
|
+
const { layer } = await import('../code/server/hub.js');
|
|
22
|
+
const { doorPoint } = await import('../code/client/doorFrame.js');
|
|
23
|
+
|
|
24
|
+
globalThis.__hubTestActors = [
|
|
25
|
+
{ id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } },
|
|
26
|
+
// The west wall's door, turned to face +x (the square).
|
|
27
|
+
{ id: 'door-race', components: { HubDoor: { id: 'race', to: 'race' }, Transform: { x: -28, y: 1.4, z: 0, rotationY: 90 } } },
|
|
28
|
+
{ id: 'door-gems', components: { HubDoor: { id: 'gems', to: 'deck:gems', title: 'Gem Rush' }, Transform: { x: 0, y: 1.4, z: -28, rotationY: 0 } } },
|
|
29
|
+
];
|
|
30
|
+
const castleJson = {
|
|
31
|
+
server: { maxPlayers: 8 },
|
|
32
|
+
hub: {
|
|
33
|
+
contract: 1,
|
|
34
|
+
title: 'Town',
|
|
35
|
+
places: {
|
|
36
|
+
race: {
|
|
37
|
+
title: 'Foot Race',
|
|
38
|
+
scene: 'scenes/race.scene',
|
|
39
|
+
result: { outcomes: ['win', 'lose'], score: { unit: 'ms', better: 'lower', min: 0, max: 600000 } },
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
const sceneExists = (scene) => ['scenes/main.scene', 'scenes/race.scene'].includes(scene);
|
|
45
|
+
const PLAYERS = [{ playerId: 'p1', userId: '101', username: 'ada' }, { playerId: 'p2', userId: '102', username: 'bo' }];
|
|
46
|
+
|
|
47
|
+
function boot(sessionId = 'dev:test') {
|
|
48
|
+
const h = layer(castleJson, {}, {}, sceneExists);
|
|
49
|
+
const session = { sessionId, players: PLAYERS, send() {}, broadcast() {} };
|
|
50
|
+
h.sceneFor(session);
|
|
51
|
+
const players = new Map();
|
|
52
|
+
for (const { playerId } of PLAYERS) {
|
|
53
|
+
players.set(playerId, { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
|
|
54
|
+
}
|
|
55
|
+
const world = { timeMs: 0, players, bodies: new Map(), scenePath: 'scenes/main.scene' };
|
|
56
|
+
h.ready(world);
|
|
57
|
+
return { h, session, world };
|
|
58
|
+
}
|
|
59
|
+
const jump = (b, playerId, door) => b.h.message(b.session, { playerId }, { k: 'hub-door-jump', door });
|
|
60
|
+
|
|
61
|
+
let n = 0;
|
|
62
|
+
|
|
63
|
+
// The town lists its doors.
|
|
64
|
+
{
|
|
65
|
+
const b = boot();
|
|
66
|
+
assert.deepEqual(b.h.join().hubDoors, [
|
|
67
|
+
{ id: 'race', title: 'Foot Race' },
|
|
68
|
+
{ id: 'gems', title: 'Gem Rush' },
|
|
69
|
+
]);
|
|
70
|
+
n += 1;
|
|
71
|
+
|
|
72
|
+
// A jump puts the player 7 m in front of the turned door, facing it.
|
|
73
|
+
jump(b, 'p1', 'race');
|
|
74
|
+
const body = b.world.players.get('p1');
|
|
75
|
+
const front = doorPoint({ x: -28, z: 0, rotationY: 90 }, 0, 7);
|
|
76
|
+
assert.ok(Math.abs(body.at.x - front.x) < 1e-9, `x ${body.at.x}`);
|
|
77
|
+
assert.ok(Math.abs(body.at.z - front.z) <= 1, `z ${body.at.z} within the spread`);
|
|
78
|
+
assert.ok(Math.abs(body.facing - (270 * Math.PI) / 180) < 1e-9, 'faces the door (-x)');
|
|
79
|
+
assert.equal(body.clamped, true, 'reaches the client as a clamp');
|
|
80
|
+
n += 1;
|
|
81
|
+
|
|
82
|
+
// A second jump within a second is ignored.
|
|
83
|
+
jump(b, 'p1', 'gems');
|
|
84
|
+
assert.ok(Math.abs(body.at.x - front.x) < 1e-9, 'still at the race door');
|
|
85
|
+
n += 1;
|
|
86
|
+
|
|
87
|
+
// An unknown door is ignored.
|
|
88
|
+
jump(b, 'p2', 'nowhere');
|
|
89
|
+
assert.deepEqual(b.world.players.get('p2').at, { x: 0, y: 1, z: 0 });
|
|
90
|
+
n += 1;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A held player stays where the place holds them.
|
|
94
|
+
{
|
|
95
|
+
const b = boot();
|
|
96
|
+
let ctx = null;
|
|
97
|
+
const h = layer(castleJson, { ready: (hub) => (ctx = hub) }, {}, sceneExists);
|
|
98
|
+
h.sceneFor(b.session);
|
|
99
|
+
h.ready(b.world);
|
|
100
|
+
ctx.hold('p2', { x: 3, y: 1, z: 3 });
|
|
101
|
+
h.message(b.session, { playerId: 'p2' }, { k: 'hub-door-jump', door: 'gems' });
|
|
102
|
+
assert.equal(b.world.players.get('p2').at.z !== doorPoint({ x: 0, z: -28, rotationY: 0 }, 0, 7).z, true);
|
|
103
|
+
n += 1;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// A door the place hides leaves the list and takes no jumps; shown, it is back.
|
|
107
|
+
{
|
|
108
|
+
const b = boot();
|
|
109
|
+
let ctx = null;
|
|
110
|
+
const h = layer(castleJson, { ready: (hub) => (ctx = hub) }, {}, sceneExists);
|
|
111
|
+
h.sceneFor(b.session);
|
|
112
|
+
h.ready(b.world);
|
|
113
|
+
ctx.hide('door-race');
|
|
114
|
+
assert.deepEqual(h.join().hubDoors, [{ id: 'gems', title: 'Gem Rush' }]);
|
|
115
|
+
h.message(b.session, { playerId: 'p1' }, { k: 'hub-door-jump', door: 'race' });
|
|
116
|
+
assert.deepEqual(b.world.players.get('p1').at, { x: 0, y: 1, z: 0 }, 'no jump to a hidden door');
|
|
117
|
+
ctx.show('door-race');
|
|
118
|
+
assert.equal(h.join().hubDoors.length, 2);
|
|
119
|
+
n += 1;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// A game has no door list and no jumps.
|
|
123
|
+
{
|
|
124
|
+
const b = boot('dev:hub.race.abc123');
|
|
125
|
+
assert.deepEqual(b.h.join().hubDoors, []);
|
|
126
|
+
jump(b, 'p1', 'race');
|
|
127
|
+
assert.deepEqual(b.world.players.get('p1').at, { x: 0, y: 1, z: 0 });
|
|
128
|
+
n += 1;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
console.log(`test-door-jump: ${n} passed`);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// The door frame (code/client/doorFrame.js): a door's points land where the
|
|
2
|
+
// engine's own transform puts them for any rotationY, and a door facing +z
|
|
3
|
+
// swings exactly as it did before doors could face other ways.
|
|
4
|
+
// `npm run check` runs it.
|
|
5
|
+
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { doorPoint } from '../code/client/doorFrame.js';
|
|
9
|
+
|
|
10
|
+
const near = (a, b, what) => assert.ok(Math.abs(a - b) < 1e-9, `${what}: ${a} vs ${b}`);
|
|
11
|
+
|
|
12
|
+
// Where the renderer puts a door-local point: world3d.js sets a node's
|
|
13
|
+
// position and rotation.y (rotationY in radians) and three does the rest.
|
|
14
|
+
function engine(base, x, z) {
|
|
15
|
+
const node = new THREE.Object3D();
|
|
16
|
+
node.position.set(base.x, 0, base.z);
|
|
17
|
+
node.rotation.set(0, (base.rotationY * Math.PI) / 180, 0);
|
|
18
|
+
node.updateMatrixWorld();
|
|
19
|
+
const p = node.localToWorld(new THREE.Vector3(x, 0, z));
|
|
20
|
+
return { x: p.x, z: p.z };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let n = 0;
|
|
24
|
+
for (const rotationY of [0, 90, 180, 270, -45, 30]) {
|
|
25
|
+
const base = { x: 3, z: -7, rotationY };
|
|
26
|
+
for (const [along, out] of [[1.2, 0], [0, 0.3], [0, -6], [-1.2, 2]]) {
|
|
27
|
+
const got = doorPoint(base, along, out);
|
|
28
|
+
const want = engine(base, along, out);
|
|
29
|
+
near(got.x, want.x, `x r=${rotationY}`);
|
|
30
|
+
near(got.z, want.z, `z r=${rotationY}`);
|
|
31
|
+
n += 1;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Facing +z: in front is +z, beyond is -z, the hinge is the -x edge.
|
|
36
|
+
assert.deepEqual(doorPoint({ x: 0, z: 0, rotationY: 0 }, 0, 1), { x: 0, z: 1 });
|
|
37
|
+
// Facing +x (rotationY 90): in front is +x.
|
|
38
|
+
const east = doorPoint({ x: 0, z: 0, rotationY: 90 }, 0, 1);
|
|
39
|
+
near(east.x, 1, 'east x');
|
|
40
|
+
near(east.z, 0, 'east z');
|
|
41
|
+
n += 2;
|
|
42
|
+
|
|
43
|
+
// The swing at rotationY 0 is the one the town's north-wall doors always had.
|
|
44
|
+
const SWING = 100;
|
|
45
|
+
const half = 1.2;
|
|
46
|
+
for (const amount of [0, 0.3, 1]) {
|
|
47
|
+
const base = { x: 4, z: -10, rotationY: 0 };
|
|
48
|
+
const hinge = doorPoint(base, -half, 0);
|
|
49
|
+
const turned = ((base.rotationY + amount * SWING) * Math.PI) / 180;
|
|
50
|
+
const leaf = { x: hinge.x + Math.cos(turned) * half, z: hinge.z - Math.sin(turned) * half };
|
|
51
|
+
const theta = (amount * SWING * Math.PI) / 180;
|
|
52
|
+
near(leaf.x, base.x - half + Math.cos(theta) * half, 'old swing x');
|
|
53
|
+
near(leaf.z, base.z - Math.sin(theta) * half, 'old swing z');
|
|
54
|
+
n += 1;
|
|
55
|
+
}
|
|
56
|
+
console.log(`test-door: ${n} passed`);
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// Grabbing and throwing (contract §6, v1.3), the server's half: a client's
|
|
2
|
+
// `hub-grab` is checked against the simulation (a loose object of the place,
|
|
3
|
+
// near the player, this player's to simulate), offered to the place's `grab`
|
|
4
|
+
// hook, rate-limited and answered `hub-grab-deny` when refused; `hub.drop`
|
|
5
|
+
// tells a holder to let go; a thrown object is not slowed to a shove's speed
|
|
6
|
+
// while it flies. `npm run check` runs it; it needs nothing installed.
|
|
7
|
+
|
|
8
|
+
import assert from 'node:assert/strict';
|
|
9
|
+
import { register } from 'node:module';
|
|
10
|
+
|
|
11
|
+
const sceneStub = 'export const readScene = () => globalThis.__hubTestActors ?? [];';
|
|
12
|
+
register(
|
|
13
|
+
`data:text/javascript,${encodeURIComponent(`
|
|
14
|
+
export async function resolve(specifier, context, next) {
|
|
15
|
+
if (specifier === '@imports/castle.multiplayer-3d/code/server/sceneBodies.js') {
|
|
16
|
+
return { url: 'data:text/javascript,${encodeURIComponent(sceneStub)}', shortCircuit: true };
|
|
17
|
+
}
|
|
18
|
+
return next(specifier, context);
|
|
19
|
+
}
|
|
20
|
+
`)}`,
|
|
21
|
+
);
|
|
22
|
+
const { layer } = await import('../code/server/hub.js');
|
|
23
|
+
const Grab = await import('../code/server/grab.js');
|
|
24
|
+
|
|
25
|
+
const crateShape = { kind: 'box', width: 0.8, height: 0.8, depth: 0.8 };
|
|
26
|
+
globalThis.__hubTestActors = [
|
|
27
|
+
{ id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } },
|
|
28
|
+
{
|
|
29
|
+
id: 'crate',
|
|
30
|
+
components: { Transform: { x: 1, y: 0.4, z: 0 }, Shape: crateShape, Body: { type: 'dynamic' } },
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: 'barrel',
|
|
34
|
+
components: {
|
|
35
|
+
Transform: { x: -1, y: 0.4, z: 0 },
|
|
36
|
+
Shape: crateShape,
|
|
37
|
+
Body: { type: 'dynamic' },
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: 'far',
|
|
42
|
+
components: {
|
|
43
|
+
Transform: { x: 12, y: 0.4, z: 0 },
|
|
44
|
+
Shape: crateShape,
|
|
45
|
+
Body: { type: 'dynamic' },
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: 'gate',
|
|
50
|
+
components: { Transform: { x: 0, y: 1, z: 1 }, Shape: crateShape, Body: { type: 'kinematic' } },
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
function rigid(at) {
|
|
55
|
+
const state = { at: { ...at }, v: { x: 0, y: 0, z: 0 } };
|
|
56
|
+
return {
|
|
57
|
+
state,
|
|
58
|
+
translation: () => ({ ...state.at }),
|
|
59
|
+
linvel: () => ({ ...state.v }),
|
|
60
|
+
setLinvel: (v) => {
|
|
61
|
+
state.v = { ...v };
|
|
62
|
+
},
|
|
63
|
+
setEnabled() {},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function boot(options = {}) {
|
|
68
|
+
const grab = 'grab' in options ? options.grab : true;
|
|
69
|
+
const hooks = options.hooks ?? {};
|
|
70
|
+
const json = {
|
|
71
|
+
server: { maxPlayers: 8 },
|
|
72
|
+
hub: { contract: 1, title: 'Bumper', result: {}, ...(grab === undefined ? {} : { grab }) },
|
|
73
|
+
};
|
|
74
|
+
let hub = null;
|
|
75
|
+
const sent = [];
|
|
76
|
+
const h = layer(json, { ...hooks, ready: (x) => (hub = x) }, {}, () => true);
|
|
77
|
+
const players = [
|
|
78
|
+
{ playerId: 'p1', username: 'ada' },
|
|
79
|
+
{ playerId: 'p2', username: 'bo' },
|
|
80
|
+
];
|
|
81
|
+
const session = {
|
|
82
|
+
sessionId: 's-1',
|
|
83
|
+
players,
|
|
84
|
+
hub: null,
|
|
85
|
+
send: (id, d) => sent.push([id, d]),
|
|
86
|
+
broadcast() {},
|
|
87
|
+
};
|
|
88
|
+
h.sceneFor(session);
|
|
89
|
+
const body = () => ({ at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
|
|
90
|
+
const objects = new Map();
|
|
91
|
+
for (const actor of globalThis.__hubTestActors) {
|
|
92
|
+
const b = actor.components.Body;
|
|
93
|
+
if (b) {
|
|
94
|
+
objects.set(actor.id, {
|
|
95
|
+
body: rigid(actor.components.Transform),
|
|
96
|
+
claimable: b.type === 'dynamic',
|
|
97
|
+
fast: false,
|
|
98
|
+
owner: null,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const world = {
|
|
103
|
+
timeMs: 0,
|
|
104
|
+
players: new Map([
|
|
105
|
+
['p1', body()],
|
|
106
|
+
['p2', body()],
|
|
107
|
+
]),
|
|
108
|
+
bodies: new Map([...objects].map(([id, e]) => [id, e.body])),
|
|
109
|
+
objects,
|
|
110
|
+
scenePath: 'scenes/main.scene',
|
|
111
|
+
};
|
|
112
|
+
h.ready(world);
|
|
113
|
+
for (const p of players) {
|
|
114
|
+
h.message(session, p, { k: 'hub-hello', avatar: 0, party: 2 });
|
|
115
|
+
}
|
|
116
|
+
h.place(world);
|
|
117
|
+
// Everyone stands at the origin; p1 simulates the crate and the barrel.
|
|
118
|
+
for (const p of world.players.values()) {
|
|
119
|
+
p.at = { x: 0, y: 1, z: 0 };
|
|
120
|
+
}
|
|
121
|
+
objects.get('crate').owner = 'p1';
|
|
122
|
+
objects.get('barrel').owner = 'p1';
|
|
123
|
+
objects.get('far').owner = 'p1';
|
|
124
|
+
const say = (playerId, data) => h.message(session, { playerId }, { k: 'hub-grab', ...data });
|
|
125
|
+
const denies = () => sent.filter(([, d]) => d.k === 'hub-grab-deny');
|
|
126
|
+
return { h, hub, world, objects, sent, say, denies };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const warn = console.warn;
|
|
130
|
+
console.warn = () => {};
|
|
131
|
+
let n = 0;
|
|
132
|
+
function test(name, fn) {
|
|
133
|
+
fn();
|
|
134
|
+
n += 1;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
test('a grab of a loose object this player simulates is taken and offered to the hook', () => {
|
|
138
|
+
const calls = [];
|
|
139
|
+
const { hub, say, denies } = boot({ hooks: { grab: (x, ...args) => calls.push(args) } });
|
|
140
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
141
|
+
assert.deepEqual(calls, [['p1', 'crate', 'grab']]);
|
|
142
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
143
|
+
assert.deepEqual(denies(), []);
|
|
144
|
+
// One hold at a time: grabbing the barrel replaces it.
|
|
145
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
146
|
+
assert.equal(hub.holding('p1'), 'barrel');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('the hook vetoes a grab with false, and a hook that throws vetoes it too', () => {
|
|
150
|
+
const { hub, say, denies } = boot({
|
|
151
|
+
hooks: { grab: (x, playerId, actorId) => actorId !== 'crate' },
|
|
152
|
+
});
|
|
153
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
154
|
+
assert.deepEqual(denies(), [['p1', { k: 'hub-grab-deny', actor: 'crate' }]]);
|
|
155
|
+
assert.equal(hub.holding('p1'), null);
|
|
156
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
157
|
+
assert.equal(hub.holding('p1'), 'barrel');
|
|
158
|
+
const broken = boot({
|
|
159
|
+
hooks: {
|
|
160
|
+
grab: () => {
|
|
161
|
+
throw new Error('boom');
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
broken.say('p1', { actor: 'crate', op: 'grab' });
|
|
166
|
+
assert.equal(broken.denies().length, 1);
|
|
167
|
+
assert.equal(broken.hub.holding('p1'), null);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('refused: too far, not dynamic, unknown, hidden, someone else’s, or a held player', () => {
|
|
171
|
+
let asked = 0;
|
|
172
|
+
const { hub, say, denies, world, objects } = boot({ hooks: { grab: () => (asked += 1) } });
|
|
173
|
+
say('p1', { actor: 'far', op: 'grab' });
|
|
174
|
+
say('p1', { actor: 'gate', op: 'grab' });
|
|
175
|
+
world.timeMs += 1000;
|
|
176
|
+
say('p1', { actor: 'spawn', op: 'grab' });
|
|
177
|
+
say('p1', { actor: 'nobody', op: 'grab' });
|
|
178
|
+
// p2 stands beside the crate, but p1 simulates it.
|
|
179
|
+
say('p2', { actor: 'crate', op: 'grab' });
|
|
180
|
+
world.timeMs += 1000;
|
|
181
|
+
hub.hide('barrel');
|
|
182
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
183
|
+
hub.show('barrel');
|
|
184
|
+
hub.hold('p1');
|
|
185
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
186
|
+
hub.release('p1');
|
|
187
|
+
objects.get('crate').owner = null;
|
|
188
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
189
|
+
assert.equal(denies().length, 8);
|
|
190
|
+
assert.equal(asked, 0);
|
|
191
|
+
assert.equal(hub.holding('p1'), null);
|
|
192
|
+
assert.equal(hub.holding('p2'), null);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('a place without grab ignores the message entirely', () => {
|
|
196
|
+
let asked = 0;
|
|
197
|
+
for (const grab of [undefined, false]) {
|
|
198
|
+
const { hub, say, sent } = boot({ grab, hooks: { grab: () => (asked += 1) } });
|
|
199
|
+
const before = sent.length;
|
|
200
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
201
|
+
assert.equal(sent.length, before);
|
|
202
|
+
assert.equal(hub.holding('p1'), null);
|
|
203
|
+
assert.equal(hub.drop(), 0);
|
|
204
|
+
}
|
|
205
|
+
assert.equal(asked, 0);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('malformed messages are dropped without an answer', () => {
|
|
209
|
+
let asked = 0;
|
|
210
|
+
const { say, sent } = boot({ hooks: { grab: () => (asked += 1) } });
|
|
211
|
+
const before = sent.length;
|
|
212
|
+
say('p1', { actor: 'crate', op: 'yank' });
|
|
213
|
+
say('p1', { actor: 7, op: 'grab' });
|
|
214
|
+
say('p1', { op: 'grab' });
|
|
215
|
+
say('p1', { actor: 'x'.repeat(200), op: 'grab' });
|
|
216
|
+
say('p1', {});
|
|
217
|
+
assert.equal(sent.length, before);
|
|
218
|
+
assert.equal(asked, 0);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('hub-grab is rate-limited per player; a grab past the rate is denied unheard', () => {
|
|
222
|
+
let asked = 0;
|
|
223
|
+
const { say, denies, world } = boot({ hooks: { grab: () => (asked += 1) } });
|
|
224
|
+
for (let i = 0; i < Grab.GRAB_BURST + 3; i += 1) {
|
|
225
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
226
|
+
}
|
|
227
|
+
assert.equal(asked, Grab.GRAB_BURST);
|
|
228
|
+
assert.equal(denies().length, 3);
|
|
229
|
+
// Another player has their own budget.
|
|
230
|
+
say('p2', { actor: 'nobody', op: 'grab' });
|
|
231
|
+
assert.equal(denies().length, 4);
|
|
232
|
+
world.timeMs += 1000;
|
|
233
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
234
|
+
assert.equal(asked, Grab.GRAB_BURST + 1);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('a throw ends the hold, is told to the hook, and flies at its own cap, not a shove cap, until it lands', () => {
|
|
238
|
+
const calls = [];
|
|
239
|
+
const { h, hub, say, world, objects } = boot({
|
|
240
|
+
hooks: { grab: (x, ...args) => calls.push(args) },
|
|
241
|
+
});
|
|
242
|
+
// A throw of something not held is dropped.
|
|
243
|
+
say('p1', { actor: 'crate', op: 'throw' });
|
|
244
|
+
assert.deepEqual(calls, []);
|
|
245
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
246
|
+
say('p1', { actor: 'crate', op: 'throw' });
|
|
247
|
+
assert.deepEqual(calls, [
|
|
248
|
+
['p1', 'crate', 'grab'],
|
|
249
|
+
['p1', 'crate', 'throw'],
|
|
250
|
+
]);
|
|
251
|
+
assert.equal(hub.holding('p1'), null);
|
|
252
|
+
const crate = objects.get('crate');
|
|
253
|
+
assert.equal(typeof crate.speedCap, 'number');
|
|
254
|
+
// Still the thrower's: it flies.
|
|
255
|
+
h.place(world);
|
|
256
|
+
assert.equal(typeof crate.speedCap, 'number');
|
|
257
|
+
// Back to the server at speed: still flying.
|
|
258
|
+
crate.owner = null;
|
|
259
|
+
crate.body.state.v = { x: 10, y: 1, z: 0 };
|
|
260
|
+
h.place(world);
|
|
261
|
+
assert.equal(typeof crate.speedCap, 'number');
|
|
262
|
+
// Landed: the shove cap applies again.
|
|
263
|
+
crate.body.state.v = { x: 1, y: 0, z: 0 };
|
|
264
|
+
h.place(world);
|
|
265
|
+
assert.equal(crate.speedCap, undefined);
|
|
266
|
+
// A flight also runs out.
|
|
267
|
+
crate.owner = 'p1';
|
|
268
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
269
|
+
world.timeMs += 1000;
|
|
270
|
+
say('p1', { actor: 'crate', op: 'throw' });
|
|
271
|
+
assert.equal(typeof crate.speedCap, 'number');
|
|
272
|
+
world.timeMs += Grab.FLIGHT_MS;
|
|
273
|
+
h.place(world);
|
|
274
|
+
assert.equal(crate.speedCap, undefined);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("a flight in the server's hands never outruns the place's throw; a stale holder's throw does nothing", () => {
|
|
278
|
+
const { h, say, world, objects } = boot();
|
|
279
|
+
const crate = objects.get('crate');
|
|
280
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
281
|
+
// The thrower says it let go at 40 m/s.
|
|
282
|
+
crate.body.state.v = { x: 40, y: 0, z: 0 };
|
|
283
|
+
say('p1', { actor: 'crate', op: 'throw' });
|
|
284
|
+
const speed = () => Math.hypot(crate.body.state.v.x, crate.body.state.v.y, crate.body.state.v.z);
|
|
285
|
+
assert.ok(speed() <= 10 * Grab.THROW_SLACK + 0.5 + 1e-9);
|
|
286
|
+
// Handed back to the server later at speed: held to the throw again.
|
|
287
|
+
crate.owner = null;
|
|
288
|
+
crate.body.state.v = { x: 30, y: 0, z: 0 };
|
|
289
|
+
h.place(world);
|
|
290
|
+
assert.ok(speed() <= 10 * Grab.THROW_SLACK + 0.5 + 1e-9);
|
|
291
|
+
// A holder whose object went to someone else cannot set it flying.
|
|
292
|
+
delete crate.speedCap;
|
|
293
|
+
crate.owner = 'p1';
|
|
294
|
+
world.timeMs += 10000;
|
|
295
|
+
h.place(world);
|
|
296
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
297
|
+
crate.owner = 'p2';
|
|
298
|
+
say('p1', { actor: 'crate', op: 'throw' });
|
|
299
|
+
assert.equal(crate.speedCap, undefined);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('hub.drop tells a holder to let go, and only holders', () => {
|
|
303
|
+
const { hub, say, sent } = boot();
|
|
304
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
305
|
+
const drops = () => sent.filter(([, d]) => d.k === 'hub-drop');
|
|
306
|
+
assert.equal(hub.drop('p2'), 0);
|
|
307
|
+
assert.equal(hub.drop('p1'), 1);
|
|
308
|
+
assert.deepEqual(drops(), [['p1', { k: 'hub-drop', actor: 'crate' }]]);
|
|
309
|
+
assert.equal(hub.holding('p1'), null);
|
|
310
|
+
assert.equal(hub.drop(), 0);
|
|
311
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
312
|
+
assert.equal(hub.drop(), 1);
|
|
313
|
+
assert.equal(drops().length, 2);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test('a hold the multiplayer kit or the place ended is forgotten', () => {
|
|
317
|
+
const { h, hub, say, world, objects } = boot();
|
|
318
|
+
say('p1', { actor: 'crate', op: 'grab' });
|
|
319
|
+
h.place(world);
|
|
320
|
+
assert.equal(hub.holding('p1'), 'crate');
|
|
321
|
+
objects.get('crate').owner = 'p2';
|
|
322
|
+
h.place(world);
|
|
323
|
+
assert.equal(hub.holding('p1'), null);
|
|
324
|
+
say('p1', { actor: 'barrel', op: 'grab' });
|
|
325
|
+
hub.hide('barrel');
|
|
326
|
+
h.place(world);
|
|
327
|
+
assert.equal(hub.holding('p1'), null);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
console.warn = warn;
|
|
331
|
+
console.log(`test-grab: ${n} passed`);
|