castle-web-cli 0.4.157 → 0.4.158
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-failures.d.ts +1 -0
- package/dist/agent-failures.js +6 -4
- package/dist/agent-prompts.js +4 -0
- package/dist/agent.d.ts +11 -0
- package/dist/agent.js +175 -15
- package/dist/bundle.js +8 -2
- package/dist/castle-host/host.js +4 -2
- package/dist/castleJson.js +6 -1
- package/dist/check.d.ts +1 -0
- package/dist/check.js +25 -0
- package/dist/devSessionServer.js +31 -7
- package/dist/imports.js +1 -19
- package/dist/index.js +15 -1
- package/dist/init.d.ts +2 -2
- package/dist/init.js +78 -42
- package/dist/mcpPlaytest.d.ts +1 -0
- package/dist/mcpPlaytest.js +182 -0
- package/dist/metering.d.ts +24 -0
- package/dist/metering.js +87 -0
- package/dist/sdkResolve.d.ts +3 -0
- package/dist/sdkResolve.js +55 -0
- package/dist/serve.js +50 -4
- package/dist/serverBundle.js +8 -3
- package/dist/shell/assets/{index-DsDkYFvP.js → index-BuSYLy-C.js} +64 -64
- package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
- package/dist/shell/index.html +2 -2
- package/dist/vitePlugins.d.ts +1 -0
- package/dist/vitePlugins.js +20 -0
- package/kits/base/CLAUDE.md +196 -0
- package/kits/{blank → base}/castle.json +5 -2
- package/kits/base/editorHost.js +172 -0
- package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
- package/kits/{blank → base}/package-lock.json +7 -4
- package/kits/{blank → base}/package.json +4 -2
- package/kits/base/sdk/README.md +569 -0
- package/kits/base/sdk/castle.d.ts +27 -0
- package/kits/base/sdk/castle.js +16 -0
- package/kits/base/sdk/commands.d.ts +181 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +17 -0
- package/kits/base/sdk/context.js +15 -0
- package/kits/base/sdk/errors.d.ts +22 -0
- package/kits/base/sdk/errors.js +16 -0
- package/kits/base/sdk/files.d.ts +15 -0
- package/kits/base/sdk/files.js +52 -0
- package/kits/base/sdk/haptics.d.ts +6 -0
- package/kits/base/sdk/haptics.js +36 -0
- package/kits/base/sdk/leaderboard.d.ts +26 -0
- package/kits/base/sdk/leaderboard.js +296 -0
- package/kits/base/sdk/lifecycle.d.ts +4 -0
- package/kits/base/sdk/lifecycle.js +13 -0
- package/kits/base/sdk/multiplayer.d.ts +43 -0
- package/kits/base/sdk/multiplayer.js +464 -0
- package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
- package/kits/base/sdk/multiplayerProtocol.js +5 -0
- package/kits/base/sdk/passes.d.ts +7 -0
- package/kits/base/sdk/passes.js +84 -0
- package/kits/base/sdk/portal.d.ts +7 -0
- package/kits/base/sdk/portal.js +35 -0
- package/kits/base/sdk/runtime.d.ts +62 -0
- package/kits/base/sdk/runtime.js +970 -0
- package/kits/base/sdk/saveQueue.d.ts +27 -0
- package/kits/base/sdk/saveQueue.js +205 -0
- package/kits/base/sdk/server/platformHandle.d.ts +25 -0
- package/kits/base/sdk/server/platformHandle.js +1 -0
- package/kits/base/sdk/server/wrapper.d.ts +28 -0
- package/kits/base/sdk/server/wrapper.js +109 -0
- package/kits/base/sdk/storage.d.ts +16 -0
- package/kits/base/sdk/storage.js +323 -0
- package/kits/base/sdk/time.d.ts +17 -0
- package/kits/base/sdk/time.js +120 -0
- package/kits/base/sdk/transport.d.ts +17 -0
- package/kits/base/sdk/transport.js +141 -0
- package/kits/base/sdk/types.d.ts +3 -0
- package/kits/base/sdk/types.js +1 -0
- package/kits/base/sdk/user.d.ts +10 -0
- package/kits/base/sdk/user.js +45 -0
- package/kits/multiplayer-2d/.prettierrc +5 -0
- package/kits/multiplayer-2d/CLAUDE.md +129 -0
- package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
- package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
- package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
- package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
- package/kits/multiplayer-2d/blueprints/player.scene +15 -0
- package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
- package/kits/multiplayer-2d/castle.json +39 -0
- package/kits/multiplayer-2d/code/client/avatars.js +79 -0
- package/kits/multiplayer-2d/code/client/poses.js +168 -0
- package/kits/multiplayer-2d/code/server/index.js +26 -0
- package/kits/multiplayer-2d/code/server/limits.js +99 -0
- package/kits/multiplayer-2d/code/server/ownership.js +177 -0
- package/kits/multiplayer-2d/code/server/players.js +127 -0
- package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
- package/kits/multiplayer-2d/code/server/world.js +337 -0
- package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
- package/kits/multiplayer-2d/eslint.config.js +21 -0
- package/kits/multiplayer-2d/fonts.generated.js +9 -0
- package/kits/multiplayer-2d/index.html +25 -0
- package/kits/multiplayer-2d/main.jsx +10 -0
- package/kits/multiplayer-2d/package-lock.json +351 -0
- package/kits/multiplayer-2d/package.json +30 -0
- package/kits/multiplayer-2d/scenes/main.scene +20 -0
- package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
- package/kits/multiplayer-2d/theme.style +4 -0
- package/kits/multiplayer-3d/.prettierrc +5 -0
- package/kits/multiplayer-3d/CLAUDE.md +215 -0
- package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
- package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
- package/kits/multiplayer-3d/castle.json +43 -0
- package/kits/multiplayer-3d/code/client/avatars.js +136 -0
- package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
- package/kits/multiplayer-3d/code/client/poses.js +220 -0
- package/kits/multiplayer-3d/code/server/index.js +23 -0
- package/kits/multiplayer-3d/code/server/limits.js +107 -0
- package/kits/multiplayer-3d/code/server/ownership.js +157 -0
- package/kits/multiplayer-3d/code/server/players.js +114 -0
- package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
- package/kits/multiplayer-3d/code/server/world.js +306 -0
- package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
- package/kits/multiplayer-3d/eslint.config.js +20 -0
- package/kits/multiplayer-3d/fonts.generated.js +9 -0
- package/kits/multiplayer-3d/index.html +22 -0
- package/kits/multiplayer-3d/main.jsx +9 -0
- package/kits/multiplayer-3d/package-lock.json +368 -0
- package/kits/multiplayer-3d/package.json +32 -0
- package/kits/multiplayer-3d/scenes/main.scene +409 -0
- package/kits/multiplayer-3d/theme.style +3 -0
- package/kits/physics-2d/CLAUDE.md +6 -0
- package/kits/physics-2d/castle.json +7 -1
- package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
- package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
- package/kits/physics-2d/engine/files.js +9 -5
- package/kits/physics-2d/engine/systemRegistry.js +2 -0
- package/kits/physics-2d/engine/ui.module.css +11 -0
- package/kits/physics-2d/package-lock.json +8 -2
- package/kits/physics-2d/package.json +2 -2
- package/kits/physics-3d/CLAUDE.md +4 -4
- package/kits/physics-3d/behaviors/Shape.jsx +1 -1
- package/kits/physics-3d/blueprints/crate.scene +1 -1
- package/kits/physics-3d/blueprints/door.scene +4 -4
- package/kits/physics-3d/blueprints/pillar.scene +1 -1
- package/kits/physics-3d/blueprints/platform.scene +1 -1
- package/kits/physics-3d/blueprints/statue.scene +1 -1
- package/kits/physics-3d/blueprints/wall.scene +1 -1
- package/kits/physics-3d/castle.json +8 -11
- package/kits/physics-3d/docs/pxmodel-format.md +1 -1
- package/kits/physics-3d/engine3d/materials.js +3 -3
- package/kits/physics-3d/engine3d/modelFiles.js +4 -2
- package/kits/physics-3d/engine3d/world3d.js +1 -1
- package/kits/physics-3d/fonts.generated.js +9 -0
- package/kits/physics-3d/main.jsx +1 -1
- package/kits/physics-3d/package-lock.json +85 -2
- package/kits/physics-3d/package.json +6 -2
- package/kits/physics-3d/scenes/main.scene +1 -1
- package/kits/physics-3d/scenes/model-lab.scene +1 -1
- package/kits/real-time/.prettierrc +5 -0
- package/kits/real-time/CLAUDE.md +60 -0
- package/kits/real-time/castle.json +19 -0
- package/kits/real-time/code/client/clock.js +68 -0
- package/kits/real-time/code/client/connection.js +134 -0
- package/kits/real-time/code/client/gameHooks.js +50 -0
- package/kits/real-time/code/client/interpolate.js +82 -0
- package/kits/real-time/code/client/messages.js +55 -0
- package/kits/real-time/code/client/ownership.js +94 -0
- package/kits/real-time/code/client/smooth.js +54 -0
- package/kits/real-time/code/server/blockDelta.js +48 -0
- package/kits/real-time/code/server/gameHooks.js +47 -0
- package/kits/real-time/code/server/limits.js +35 -0
- package/kits/real-time/code/server/session.js +251 -0
- package/kits/real-time/eslint.config.js +22 -0
- package/kits/real-time/index.html +45 -0
- package/kits/real-time/package.json +14 -0
- package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
- package/kits/turn-based/blueprints/board.scene +15 -0
- package/kits/turn-based/blueprints/square.scene +25 -0
- package/kits/turn-based/castle.json +37 -0
- package/kits/turn-based/code/behaviors/Board.jsx +169 -0
- package/kits/turn-based/code/behaviors/Square.jsx +26 -0
- package/kits/turn-based/code/server/index.js +152 -0
- package/kits/turn-based/drawings/square.sprite +25 -0
- package/kits/turn-based/eslint.config.js +12 -0
- package/kits/turn-based/fonts.generated.js +9 -0
- package/kits/{tabletop → turn-based}/index.html +5 -3
- package/kits/{tabletop → turn-based}/main.jsx +2 -2
- package/kits/{tabletop → turn-based}/package-lock.json +17 -4
- package/kits/{tabletop → turn-based}/package.json +5 -2
- package/kits/turn-based/room.js +289 -0
- package/kits/turn-based/scenes/main.scene +227 -0
- package/kits/{tabletop → turn-based}/testing.js +131 -99
- package/kits/turn-based/theme.style +3 -0
- package/package.json +1 -1
- package/kits/blank/CLAUDE.md +0 -198
- package/kits/tabletop/castle.json +0 -34
- package/kits/tabletop/room.js +0 -269
- /package/kits/{blank → base}/index.html +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
- /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
- /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
- /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
- /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
- /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
- /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
- /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Client behavior for the starter claim-squares game.
|
|
2
|
+
// Board supplies the scene update and React overlay hooks. The helpers join a
|
|
3
|
+
// multiplayer session, send square claims, tint actors and format status text.
|
|
4
|
+
|
|
5
|
+
import { Multiplayer } from 'castle-web-sdk';
|
|
6
|
+
|
|
7
|
+
// Sprite tint multiplies these seat colors over each square's own artwork.
|
|
8
|
+
const SEAT_TINT = { A: '#ea323cff', B: '#0cf1ffff' };
|
|
9
|
+
const UNCLAIMED_TINT = '#ffffffff';
|
|
10
|
+
|
|
11
|
+
// A public join can return a solo fallback when the player is signed out, the
|
|
12
|
+
// session is full or the deck is private. retryJoin repeats the public request.
|
|
13
|
+
const RETRY_SECONDS = 6;
|
|
14
|
+
|
|
15
|
+
// The status overlay sits above the grid, which starts at y=170 on the card.
|
|
16
|
+
const STATUS_STYLE = {
|
|
17
|
+
position: 'absolute',
|
|
18
|
+
left: 0,
|
|
19
|
+
top: 96,
|
|
20
|
+
width: '100%',
|
|
21
|
+
textAlign: 'center',
|
|
22
|
+
fontFamily: 'system-ui, sans-serif',
|
|
23
|
+
fontSize: 22,
|
|
24
|
+
color: '#c7cfdd',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// The scene attaches Board to its board actor. The behavior reads input and
|
|
28
|
+
// server state while the actor supplies storage that lasts across frames.
|
|
29
|
+
export class Board {
|
|
30
|
+
// The behavior registry uses this name to match the Board component in a scene.
|
|
31
|
+
static behaviorName = 'Board';
|
|
32
|
+
static defaultProps = {};
|
|
33
|
+
|
|
34
|
+
constructor(props) {
|
|
35
|
+
// The engine constructs a behavior instance from the actor's component props.
|
|
36
|
+
this.props = props;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
update(actor, scene) {
|
|
40
|
+
// The engine calls update once per frame for the actor carrying Board.
|
|
41
|
+
// clientState starts the multiplayer join on the first update or UI render.
|
|
42
|
+
const game = clientState(actor);
|
|
43
|
+
// A state snapshot supplies both the square ownership and the player's seat.
|
|
44
|
+
if (!game.state) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
// Taps send claims; painting continues to use the latest server snapshot.
|
|
48
|
+
readTaps(scene, game);
|
|
49
|
+
paintSquares(scene, game.state);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The engine calls ui with the same actor and renders the returned React node
|
|
53
|
+
// over the canvas. This keeps status text separate from scene actors.
|
|
54
|
+
ui(actor) {
|
|
55
|
+
return <div style={STATUS_STYLE}>{statusLine(clientState(actor))}</div>;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Return this board actor's client state and start its first join when needed.
|
|
60
|
+
function clientState(actor) {
|
|
61
|
+
// actor.runtime survives frames but is omitted from serialized scene data.
|
|
62
|
+
// `connection` stays null until join succeeds, and `state` stays null until a
|
|
63
|
+
// server snapshot arrives.
|
|
64
|
+
actor.runtime.game ??= { connection: null, state: null, waiting: null };
|
|
65
|
+
const game = actor.runtime.game;
|
|
66
|
+
// Both update and ui call this helper, so `waiting` prevents concurrent joins.
|
|
67
|
+
if (!game.connection && !game.waiting) {
|
|
68
|
+
game.waiting = 'joining';
|
|
69
|
+
joinSession(game);
|
|
70
|
+
}
|
|
71
|
+
return game;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Join a public session and subscribe this board actor to server snapshots.
|
|
75
|
+
function joinSession(game) {
|
|
76
|
+
// Multiplayer.join may resolve to either a network connection or the SDK's
|
|
77
|
+
// local solo fallback, so the handler checks isSolo before storing the result.
|
|
78
|
+
Multiplayer.join({ mode: 'public' })
|
|
79
|
+
.then((connection) => {
|
|
80
|
+
// Claims require authoritative validation, so the local solo fallback
|
|
81
|
+
// cannot run this game. Start another public join after a delay.
|
|
82
|
+
if (connection.isSolo) {
|
|
83
|
+
return retryJoin(game);
|
|
84
|
+
}
|
|
85
|
+
// The connection remains on actor.runtime for later tap messages.
|
|
86
|
+
game.connection = connection;
|
|
87
|
+
game.waiting = null;
|
|
88
|
+
// The server sends complete snapshots, so the latest state replaces the
|
|
89
|
+
// previous one without a client-side merge.
|
|
90
|
+
connection.onMessage((data) => {
|
|
91
|
+
if (data?.type === 'state') {
|
|
92
|
+
game.state = data;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
})
|
|
96
|
+
// Failed joins use the same delayed path as solo fallback sessions.
|
|
97
|
+
.catch(() => retryJoin(game));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Show the retry status and defer the next public join by a fixed interval.
|
|
101
|
+
function retryJoin(game) {
|
|
102
|
+
// `statusLine` displays `waiting` while the timer runs.
|
|
103
|
+
game.waiting = 'no table to join - trying again in a moment';
|
|
104
|
+
setTimeout(() => joinSession(game), RETRY_SECONDS * 1000);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Forward every square tap to the authoritative rules in code/server/index.js.
|
|
108
|
+
// The server validates turn, seat, index, occupancy and game status.
|
|
109
|
+
function readTaps(scene, game) {
|
|
110
|
+
// The physics kit tracks active pointers and marks the frame each press starts.
|
|
111
|
+
for (const pointer of scene.pointers.values()) {
|
|
112
|
+
if (!pointer.justPressed) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// actorAtPoint tests Collider components and returns the topmost hit actor.
|
|
116
|
+
const hit = scene.physics.actorAtPoint(pointer.x, pointer.y);
|
|
117
|
+
// Only actors carrying Square can produce a claim message.
|
|
118
|
+
const square = hit?.components?.Square;
|
|
119
|
+
if (square) {
|
|
120
|
+
// The message carries the public square index; the server derives the seat
|
|
121
|
+
// from the authenticated player and validates the claim.
|
|
122
|
+
game.connection?.send({ type: 'claim', square: square.index });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Paint every actor with a Square behavior from the server's ownership list.
|
|
128
|
+
function paintSquares(scene, state) {
|
|
129
|
+
// actorsWith returns actors whose named behavior is currently enabled.
|
|
130
|
+
for (const squareActor of scene.actorsWith('Square')) {
|
|
131
|
+
// Square.index selects the same slot in the server's sixteen-element array.
|
|
132
|
+
const seat = state.squares[squareActor.components.Square.index];
|
|
133
|
+
// Sprite tint multiplies the editable `drawings/square.sprite` pixels.
|
|
134
|
+
// White preserves their colors; a seat tint displays the square's owner.
|
|
135
|
+
// A game with separate pieces can spawn piece actors over these square actors.
|
|
136
|
+
squareActor.components.Sprite.tint = seat ? SEAT_TINT[seat] : UNCLAIMED_TINT;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Convert connection and server state into the Board behavior's status text.
|
|
141
|
+
function statusLine(game) {
|
|
142
|
+
// Retry text takes precedence during the delay before the next join.
|
|
143
|
+
if (game.waiting) {
|
|
144
|
+
return game.waiting;
|
|
145
|
+
}
|
|
146
|
+
const state = game.state;
|
|
147
|
+
// The first server snapshot supplies all display state, so the initial
|
|
148
|
+
// overlay is empty.
|
|
149
|
+
if (!state) {
|
|
150
|
+
return '';
|
|
151
|
+
}
|
|
152
|
+
// The server refuses claims until both seats are taken, so a lone player is
|
|
153
|
+
// told to wait rather than shown a turn whose taps would do nothing.
|
|
154
|
+
if (Object.values(state.names).some((name) => !name)) {
|
|
155
|
+
return 'waiting for another player';
|
|
156
|
+
}
|
|
157
|
+
// The server uses `draw` as a sentinel; any other winner value is a seat id.
|
|
158
|
+
if (state.winner === 'draw') {
|
|
159
|
+
return 'a draw - tap to play again';
|
|
160
|
+
}
|
|
161
|
+
if (state.winner) {
|
|
162
|
+
return `${state.winner} wins - tap to play again`;
|
|
163
|
+
}
|
|
164
|
+
// The server sends a null `you` field to spectators and still reports the turn.
|
|
165
|
+
if (!state.you) {
|
|
166
|
+
return `watching - ${state.turn} to play`;
|
|
167
|
+
}
|
|
168
|
+
return state.turn === state.you ? 'your turn' : 'their turn';
|
|
169
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Behavior for one square in the starter game's board.
|
|
2
|
+
// It exposes the server index and declares the component used for hit testing.
|
|
3
|
+
|
|
4
|
+
// Decks attach Square to each cell actor so Board can find and address the cell.
|
|
5
|
+
export class Square {
|
|
6
|
+
// The behavior registry uses this name to match Square components in scene data.
|
|
7
|
+
static behaviorName = 'Square';
|
|
8
|
+
|
|
9
|
+
// defaultProps makes index an editable field in the automatic inspector.
|
|
10
|
+
static defaultProps = { index: 0 };
|
|
11
|
+
// The range matches the sixteen slots in the starter game's server state.
|
|
12
|
+
static propertyMeta = { index: { min: 0, max: 15, step: 1 } };
|
|
13
|
+
|
|
14
|
+
// Board uses scene.physics.actorAtPoint, which tests a Collider on this actor.
|
|
15
|
+
// When the editor adds Square, expectedSiblings adds a missing Collider first.
|
|
16
|
+
// This rule runs during editor creation; handwritten scene data must include
|
|
17
|
+
// its Collider explicitly.
|
|
18
|
+
static expectedSiblings = ['Collider'];
|
|
19
|
+
|
|
20
|
+
// The actor's Sprite component renders `drawings/square.sprite`, which stays
|
|
21
|
+
// editable in the sprite editor while this behavior stores the game index.
|
|
22
|
+
constructor(props) {
|
|
23
|
+
// The engine constructs behavior instances from the actor's component props.
|
|
24
|
+
this.props = props;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// Server entry point for the kit's starter claim-squares game.
|
|
2
|
+
// The game state and rules come first, followed by the session callbacks.
|
|
3
|
+
|
|
4
|
+
// Relative, not `@imports/castle.turn-based/...`: this file is served both as
|
|
5
|
+
// the kit's own deck and from inside an importing deck, and only a relative path
|
|
6
|
+
// finds room.js in both.
|
|
7
|
+
import * as Room from '../../room.js';
|
|
8
|
+
|
|
9
|
+
const SQUARES = 16;
|
|
10
|
+
const SEATS = ['A', 'B'];
|
|
11
|
+
|
|
12
|
+
// Room.join uses this idle interval when testing whether to clear game state.
|
|
13
|
+
const FRESH_AFTER_IDLE_MS = 3 * 60 * 1000;
|
|
14
|
+
|
|
15
|
+
// A departure record can restore its seat during this reconnect interval.
|
|
16
|
+
const RECONNECT_GRACE_MS = 45 * 1000;
|
|
17
|
+
|
|
18
|
+
// The room stores session tables, seats, spectators, reconnect records and idle
|
|
19
|
+
// clearing. The deck supplies the game stored on each table.
|
|
20
|
+
const room = Room.createRoom({
|
|
21
|
+
seats: SEATS,
|
|
22
|
+
idleMs: FRESH_AFTER_IDLE_MS,
|
|
23
|
+
graceMs: RECONNECT_GRACE_MS,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Create the deck-owned object stored in `table.game`. Two players alternate
|
|
27
|
+
// claiming sixteen squares, and the full board is scored by claim count.
|
|
28
|
+
// A room clear assigns null to `table.game`; the join callback creates this value.
|
|
29
|
+
function freshGame() {
|
|
30
|
+
return { squares: Array(SQUARES).fill(null), turn: SEATS[0], winner: null };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Return null while play remains, then the seat with more squares or 'draw'.
|
|
34
|
+
// A deck with different scoring can replace this function with its own rule.
|
|
35
|
+
function winnerOf(squares) {
|
|
36
|
+
if (squares.some((seat) => !seat)) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const counts = SEATS.map((seat) => squares.filter((held) => held === seat).length);
|
|
40
|
+
if (counts[0] === counts[1]) {
|
|
41
|
+
return 'draw';
|
|
42
|
+
}
|
|
43
|
+
return counts[0] > counts[1] ? SEATS[0] : SEATS[1];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Apply one claim after deriving the seat from the authenticated player.
|
|
47
|
+
// A deck with different moves can replace this function and keep the room flow.
|
|
48
|
+
function claim(table, player, index) {
|
|
49
|
+
const seat = table.seatOf(player.playerId);
|
|
50
|
+
const game = table.game;
|
|
51
|
+
|
|
52
|
+
// Claims require a seated player, both seats occupied and an unfinished game.
|
|
53
|
+
if (!seat || Object.values(table.seats).filter(Boolean).length < 2 || game.winner) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
// The message supplies an index; the server supplies and validates the seat.
|
|
57
|
+
if (seat !== game.turn || !Number.isInteger(index) || index < 0 || index >= SQUARES) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Existing ownership makes the square unavailable for another claim.
|
|
61
|
+
if (game.squares[index]) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// An accepted claim records ownership, advances the turn and checks the score.
|
|
66
|
+
game.squares[index] = seat;
|
|
67
|
+
game.turn = seat === SEATS[0] ? SEATS[1] : SEATS[0];
|
|
68
|
+
game.winner = winnerOf(game.squares);
|
|
69
|
+
|
|
70
|
+
// Only an accepted move updates the room's idle clock. Rejected messages do
|
|
71
|
+
// not extend the lifetime of an inactive table.
|
|
72
|
+
table.keepAlive();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Start a rematch requested by a seated player after the current game ends.
|
|
76
|
+
function rematch(table, player) {
|
|
77
|
+
if (!table.seatOf(player.playerId) || !table.game.winner) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
// A rematch replaces deck state and counts as accepted table activity.
|
|
81
|
+
table.game = freshGame();
|
|
82
|
+
table.keepAlive();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Build one player's view of the public board and their current seat.
|
|
86
|
+
// `you` varies by recipient, so the server creates this object per player.
|
|
87
|
+
function stateFor(session, table, playerId) {
|
|
88
|
+
return {
|
|
89
|
+
type: 'state',
|
|
90
|
+
squares: table.game.squares,
|
|
91
|
+
turn: table.game.turn,
|
|
92
|
+
winner: table.game.winner,
|
|
93
|
+
names: table.seatNames(),
|
|
94
|
+
watching: table.spectatorCount(session),
|
|
95
|
+
you: table.seatOf(playerId),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Send each connected player a state object containing their own `you` value.
|
|
100
|
+
function pushState(session, table) {
|
|
101
|
+
for (const player of session.players) {
|
|
102
|
+
session.send(player.playerId, stateFor(session, table, player.playerId));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Castle calls these handlers for the lifetime of each multiplayer session.
|
|
107
|
+
export default {
|
|
108
|
+
onStart(session) {
|
|
109
|
+
// Eager creation gives the session a table before its first player arrives.
|
|
110
|
+
room.table(session);
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
onPlayerJoin(session, player) {
|
|
114
|
+
// Room.join restores or assigns a seat and may clear an idle table.
|
|
115
|
+
const { table } = room.join(session, player);
|
|
116
|
+
|
|
117
|
+
// New and cleared tables have null game state for the deck to initialize.
|
|
118
|
+
if (!table.game) {
|
|
119
|
+
table.game = freshGame();
|
|
120
|
+
}
|
|
121
|
+
pushState(session, table);
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
onPlayerLeave(session, player) {
|
|
125
|
+
// Room.leave releases the seat and may promote a spectator. It preserves
|
|
126
|
+
// `table.game`, so remaining players continue the same game.
|
|
127
|
+
const { table } = room.leave(session, player);
|
|
128
|
+
pushState(session, table);
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
onMessage(session, player, data) {
|
|
132
|
+
// Room.table makes the table on demand, so no null check is needed here.
|
|
133
|
+
const table = room.table(session);
|
|
134
|
+
// A sync request returns only to its sender and does not update activity.
|
|
135
|
+
if (data?.type === 'sync') {
|
|
136
|
+
session.send(player.playerId, stateFor(session, table, player.playerId));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
// Recognized commands validate and mutate through the rule functions above.
|
|
140
|
+
if (data?.type === 'claim') {
|
|
141
|
+
claim(table, player, data.square);
|
|
142
|
+
} else if (data?.type === 'rematch') {
|
|
143
|
+
rematch(table, player);
|
|
144
|
+
} else {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// Every recognized command ends with a state push, accepted or refused, so
|
|
148
|
+
// a client whose claim was refused is corrected. Only the accepted path
|
|
149
|
+
// called keepAlive above, so a refusal does not extend the idle clock.
|
|
150
|
+
pushState(session, table);
|
|
151
|
+
},
|
|
152
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"format": "compact",
|
|
3
|
+
"palette": {
|
|
4
|
+
"a": "#3b4363",
|
|
5
|
+
"b": "#1e1d38"
|
|
6
|
+
},
|
|
7
|
+
"grid": [
|
|
8
|
+
"................",
|
|
9
|
+
".aaaaaaaaaaaaaa.",
|
|
10
|
+
".abbbbbbbbbbbba.",
|
|
11
|
+
".abbbbbbbbbbbba.",
|
|
12
|
+
".abbbbbbbbbbbba.",
|
|
13
|
+
".abbbbbbbbbbbba.",
|
|
14
|
+
".abbbbbbbbbbbba.",
|
|
15
|
+
".abbbbbbbbbbbba.",
|
|
16
|
+
".abbbbbbbbbbbba.",
|
|
17
|
+
".abbbbbbbbbbbba.",
|
|
18
|
+
".abbbbbbbbbbbba.",
|
|
19
|
+
".abbbbbbbbbbbba.",
|
|
20
|
+
".abbbbbbbbbbbba.",
|
|
21
|
+
".abbbbbbbbbbbba.",
|
|
22
|
+
".aaaaaaaaaaaaaa.",
|
|
23
|
+
"................"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export default [
|
|
2
|
+
{ ignores: ['node_modules/**', 'imports/**', '.castle/**', 'dist/**'] },
|
|
3
|
+
{
|
|
4
|
+
files: ['*.js'],
|
|
5
|
+
languageOptions: {
|
|
6
|
+
ecmaVersion: 'latest',
|
|
7
|
+
sourceType: 'module',
|
|
8
|
+
parserOptions: { ecmaFeatures: { jsx: true } },
|
|
9
|
+
},
|
|
10
|
+
rules: { curly: ['error', 'all'] },
|
|
11
|
+
},
|
|
12
|
+
];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by the Style editor. Do not edit -- picking a font rewrites it.
|
|
2
|
+
//
|
|
3
|
+
// `theme.style` records that this deck's font is DMSans; this module is what
|
|
4
|
+
// puts DMSans's bytes in the published bundle. It has to name the face in a
|
|
5
|
+
// static import: reaching it through the name in `theme.style` instead would
|
|
6
|
+
// inline all nine Castle faces into the deck, since no bundler can tell which
|
|
7
|
+
// one a runtime lookup reads.
|
|
8
|
+
export { installDMSans as installDeckFont } from 'castle-web-fonts';
|
|
9
|
+
export const deckFont = 'DMSans';
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
|
|
5
5
|
<meta name="color-scheme" content="dark" />
|
|
6
|
-
<title>
|
|
7
|
-
<!--
|
|
8
|
-
|
|
6
|
+
<title>turn-based</title>
|
|
7
|
+
<!-- Declare the dark scheme before the JS-imported CSS loads, so the frame
|
|
8
|
+
never paints light first (Safari won't repaint a composited iframe on a
|
|
9
|
+
color-scheme change). -->
|
|
9
10
|
<style>
|
|
10
11
|
html {
|
|
11
12
|
color-scheme: dark;
|
|
@@ -15,6 +16,7 @@
|
|
|
15
16
|
</head>
|
|
16
17
|
<body>
|
|
17
18
|
<div id="root"></div>
|
|
19
|
+
<!-- The kit's own entry, so playing this kit plays its starter game. -->
|
|
18
20
|
<script type="module" src="./main.jsx"></script>
|
|
19
21
|
</body>
|
|
20
22
|
</html>
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
// The
|
|
1
|
+
// The turn-based kit adds no engine and no editors -- it is server-side room
|
|
2
2
|
// bookkeeping and a test double. Playing and editing are the physics-2d kit's,
|
|
3
3
|
// unchanged, so this entry is that entry.
|
|
4
4
|
//
|
|
5
5
|
// It exists so a deck's `index.html` can point at ONE import
|
|
6
|
-
// (`/imports/castle.
|
|
6
|
+
// (`/imports/castle.turn-based/main.jsx`) and not have to know that physics-2d is
|
|
7
7
|
// underneath, and so `castle.json`'s `main` has something to name.
|
|
8
8
|
import '@imports/castle.physics-2d/main.jsx';
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "
|
|
2
|
+
"name": "turn-based",
|
|
3
3
|
"lockfileVersion": 3,
|
|
4
4
|
"requires": true,
|
|
5
5
|
"packages": {
|
|
6
6
|
"": {
|
|
7
|
-
"name": "
|
|
7
|
+
"name": "turn-based",
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@codemirror/commands": "^6.10.3",
|
|
10
10
|
"@codemirror/lang-javascript": "^6.2.5",
|
|
@@ -17,16 +17,20 @@
|
|
|
17
17
|
"@dnd-kit/utilities": "^3.2.2",
|
|
18
18
|
"@fortawesome/free-solid-svg-icons": "^5.15.4",
|
|
19
19
|
"@lezer/highlight": "^1.2.3",
|
|
20
|
-
"castle-web-
|
|
20
|
+
"castle-web-fonts": "^1.0.0",
|
|
21
21
|
"codemirror": "^6.0.2",
|
|
22
22
|
"matter-js": "^0.20.0",
|
|
23
23
|
"react": "^19.2.4",
|
|
24
24
|
"react-dom": "^19.2.4"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"castle-web-sdk": "file:../../sdk"
|
|
25
28
|
}
|
|
26
29
|
},
|
|
27
30
|
"../../sdk": {
|
|
28
31
|
"name": "castle-web-sdk",
|
|
29
|
-
"version": "0.4.
|
|
32
|
+
"version": "0.4.23",
|
|
33
|
+
"dev": true,
|
|
30
34
|
"devDependencies": {
|
|
31
35
|
"eslint": "^9.0.0",
|
|
32
36
|
"jscpd": "^4.0.5",
|
|
@@ -147,6 +151,7 @@
|
|
|
147
151
|
"resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
|
|
148
152
|
"integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
|
|
149
153
|
"license": "MIT",
|
|
154
|
+
"peer": true,
|
|
150
155
|
"dependencies": {
|
|
151
156
|
"@dnd-kit/accessibility": "^3.1.1",
|
|
152
157
|
"@dnd-kit/utilities": "^3.2.2",
|
|
@@ -261,6 +266,12 @@
|
|
|
261
266
|
"integrity": "sha512-Wy0V7+SGUjnF9/TkiM1hKVDPj7jKXduPNboMVtHTA8dySMURWqfg/JZ9E2Sq8JgSJmkl7k7Qe9FLeMSrSraWmQ==",
|
|
262
267
|
"license": "MIT"
|
|
263
268
|
},
|
|
269
|
+
"node_modules/castle-web-fonts": {
|
|
270
|
+
"version": "1.0.0",
|
|
271
|
+
"resolved": "https://registry.npmjs.org/castle-web-fonts/-/castle-web-fonts-1.0.0.tgz",
|
|
272
|
+
"integrity": "sha512-amFts4vVJjaXKNQkh2DMMmxYHafCYczAOvme70n+0WX8fsTqJACh6r6MzND94l89Q6LFoIyrCm+cLh3S1Usy0w==",
|
|
273
|
+
"license": "SEE LICENSE IN licenses/FONTS.md"
|
|
274
|
+
},
|
|
264
275
|
"node_modules/castle-web-sdk": {
|
|
265
276
|
"resolved": "../../sdk",
|
|
266
277
|
"link": true
|
|
@@ -297,6 +308,7 @@
|
|
|
297
308
|
"resolved": "https://registry.npmjs.org/react/-/react-19.2.8.tgz",
|
|
298
309
|
"integrity": "sha512-PWaYA1L/q9u2u7xYQi+Y3L3Yfnie7XyLeaJICV1MGD6LprsBxcAqGjYyr0eY3p+QdsA+x/Irkt4Qif8D63+Sbw==",
|
|
299
310
|
"license": "MIT",
|
|
311
|
+
"peer": true,
|
|
300
312
|
"engines": {
|
|
301
313
|
"node": ">=0.10.0"
|
|
302
314
|
}
|
|
@@ -306,6 +318,7 @@
|
|
|
306
318
|
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.8.tgz",
|
|
307
319
|
"integrity": "sha512-rVprimfGBG3DR+Tq0IQG2DT5PxKth1WIGDmj5yPmlzr4YBe7uyE+Du4oVqTDXZSHGGGXRtTJEGSSePyQCMBglQ==",
|
|
308
320
|
"license": "MIT",
|
|
321
|
+
"peer": true,
|
|
309
322
|
"dependencies": {
|
|
310
323
|
"scheduler": "^0.27.0"
|
|
311
324
|
},
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "
|
|
2
|
+
"name": "turn-based",
|
|
3
3
|
"private": true,
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
@@ -20,10 +20,13 @@
|
|
|
20
20
|
"@dnd-kit/utilities": "^3.2.2",
|
|
21
21
|
"@fortawesome/free-solid-svg-icons": "^5.15.4",
|
|
22
22
|
"@lezer/highlight": "^1.2.3",
|
|
23
|
-
"castle-web-
|
|
23
|
+
"castle-web-fonts": "^1.0.0",
|
|
24
24
|
"codemirror": "^6.0.2",
|
|
25
25
|
"matter-js": "^0.20.0",
|
|
26
26
|
"react": "^19.2.4",
|
|
27
27
|
"react-dom": "^19.2.4"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"castle-web-sdk": "file:../../sdk"
|
|
28
31
|
}
|
|
29
32
|
}
|