@caisual/kit 0.21.0 → 0.23.0

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.
Files changed (44) hide show
  1. package/README.md +12 -9
  2. package/dist/host.d.ts +1 -0
  3. package/dist/host.js +169 -0
  4. package/dist/kit.js +2 -4
  5. package/dist/kit.js.map +4 -4
  6. package/dist/node.js +193 -427
  7. package/dist/runtime.js +241 -269
  8. package/dist/types/api.d.ts +1 -1
  9. package/dist/types/contracts/connessione.d.ts +1 -0
  10. package/dist/types/contracts/giornata.d.ts +1 -0
  11. package/dist/types/contracts/index.d.ts +3 -2
  12. package/dist/types/contracts/manifest.d.ts +7 -50
  13. package/dist/types/contracts/network-budget.d.ts +53 -0
  14. package/dist/types/contracts/versioni.d.ts +0 -1
  15. package/dist/types/crew/index.d.ts +4 -0
  16. package/dist/types/handshake.d.ts +0 -4
  17. package/dist/types/{overlay/host-bridge.d.ts → host/index.d.ts} +60 -37
  18. package/dist/types/index.d.ts +66 -57
  19. package/dist/types/node/index.d.ts +5 -25
  20. package/dist/types/protocollo.d.ts +2 -21
  21. package/dist/types/runtime/index.d.ts +3 -13
  22. package/dist/types/runtime/network-probe.d.ts +4 -0
  23. package/dist/types/server/index.d.ts +1 -11
  24. package/dist/types/stanza/nucleo.d.ts +8 -16
  25. package/dist/types/stanza/tipi.d.ts +3 -21
  26. package/dist/types/voce/index.d.ts +0 -1
  27. package/package.json +15 -17
  28. package/dist/overlay.d.ts +0 -1
  29. package/dist/overlay.js +0 -2157
  30. package/dist/overlay.js.map +0 -7
  31. package/dist/types/contracts/overlay.d.ts +0 -235
  32. package/dist/types/contracts/replay.d.ts +0 -83
  33. package/dist/types/overlay/bridge.d.ts +0 -3
  34. package/dist/types/overlay/i18n.d.ts +0 -128
  35. package/dist/types/overlay/index.d.ts +0 -9
  36. package/dist/types/overlay/locale.d.ts +0 -4
  37. package/dist/types/overlay/shortcut.d.ts +0 -4
  38. package/dist/types/overlay/styles.d.ts +0 -1
  39. package/dist/types/overlay/ui-model.d.ts +0 -53
  40. package/dist/types/overlay/ui.d.ts +0 -81
  41. package/dist/types/overlay/voice-panel.d.ts +0 -13
  42. package/dist/types/replay/index.d.ts +0 -65
  43. package/dist/types/session/index.d.ts +0 -49
  44. package/dist/types/session/resume.d.ts +0 -13
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  The game kit for [Caisual](https://caisual.com), a free home for small browser games with multiplayer built in.
4
4
 
5
- The kit gives a published game a stable player identity, cloud saves, a daily challenge seed, and rooms with server-owned state, voice, matchmaking and spectators. It is one JavaScript module with no dependencies and never touches the page: the game keeps its own rendering, input and UI. Menus, lobby, invites, friends and results are drawn by the platform's standard overlay.
5
+ The kit gives a published game a stable player identity, cloud saves, a daily challenge seed, rooms with server-owned state, voice, matchmaking, and friends. It is one JavaScript module with no dependencies and never touches the page: the game fills the window and draws its own menu, lobby and results.
6
6
 
7
7
  ## Client
8
8
 
@@ -14,14 +14,11 @@ npm install @caisual/kit
14
14
  import { caisual } from '@caisual/kit';
15
15
 
16
16
  const c = await caisual.connect();
17
- console.log(c.player.name);
17
+ console.log(c.player.name, c.player.language);
18
18
 
19
- c.session.onChange((session) => {
20
- if (session.kind === 'room') {
21
- session.room.onState((state) => draw(state));
22
- }
23
- });
24
- c.session.ready();
19
+ const room = await c.room.create({ mode: null });
20
+ room.onState((state) => draw(state));
21
+ room.ready(true);
25
22
  ```
26
23
 
27
24
  A published game can also load the same module from its own origin, without a bundler: `import { caisual } from '/__caisual/kit/v1.js'`.
@@ -45,7 +42,13 @@ export default defineGame({
45
42
 
46
43
  - [Kit reference](https://caisual.com/kit.md): every call, event and limit.
47
44
  - [Publishing guide](https://caisual.com/publish.md): the folder, the manifest and the CLI.
48
- - [Guides](https://caisual.com/docs): rooms, voice, matchmaking, spectators, saves, versions, limits.
45
+ - [Guides](https://caisual.com/docs): rooms, voice, matchmaking, friends, saves, versions, limits.
49
46
  - [llms.txt](https://caisual.com/llms.txt): the index of every guide for coding agents.
50
47
 
51
48
  Publish with [`@caisual/cli`](https://www.npmjs.com/package/@caisual/cli). Publishing is free; games run on caisual.com.
49
+
50
+ ## Instant matchmaking
51
+
52
+ Enable a mode with `"matchmaking": true`, then call `await c.room.match({ mode: 'arena' })`. It enters a room immediately. `room.origin` is `matchmaking`; player-created rooms use `player` and always have a lobby.
53
+
54
+ Matchmaking has no ready button or setup choices. The server assigns roles and teams. Below `players.min`, draw `waiting` as a playable practice scene with a short notice. At the minimum, the room starts a three-second countdown. Players can join during countdown and play up to `players.max`. Normal rounds restart automatically after eight seconds. The local dev server follows these same rules.
package/dist/host.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './types/host/index.js';
package/dist/host.js ADDED
@@ -0,0 +1,169 @@
1
+ // src/errors.ts
2
+ function creaErrore(code, message, version = {}) {
3
+ return Object.assign(new Error(message), { name: "CaisualError", code, ...version });
4
+ }
5
+
6
+ // src/host/index.ts
7
+ function record(valore) {
8
+ return typeof valore === "object" && valore !== null && !Array.isArray(valore) ? valore : null;
9
+ }
10
+ var CODICE = /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/;
11
+ function eMessaggioReady(value) {
12
+ return record(value)?.type === "caisual:ready";
13
+ }
14
+ function eRichiestaBiglietto(value) {
15
+ const data = record(value);
16
+ return data?.type === "caisual:ticket" && (data.aud === void 0 || data.aud === "portal" || data.aud === "live");
17
+ }
18
+ function stanzaDaMessaggio(value) {
19
+ const data = record(value);
20
+ if (data?.type !== "caisual:room") return void 0;
21
+ if (data.room === null) return null;
22
+ const room = record(data.room);
23
+ return typeof room?.code === "string" && CODICE.test(room.code) ? { code: room.code } : void 0;
24
+ }
25
+ function creaPonteOspite(input) {
26
+ let port = null, instance = null;
27
+ let disposed = false, legacyReady = true;
28
+ let polling = null, pollingEnd = null;
29
+ let fermaCrew = null;
30
+ const stopPolling = () => {
31
+ if (polling !== null) input.finestra.clearInterval(polling);
32
+ if (pollingEnd !== null) input.finestra.clearTimeout(pollingEnd);
33
+ polling = pollingEnd = null;
34
+ };
35
+ const askReady = () => {
36
+ if (!disposed && input.frame.src !== "") input.frame.contentWindow?.postMessage({ type: "caisual:ready?" }, input.origineGioco);
37
+ };
38
+ const poll = () => {
39
+ stopPolling();
40
+ polling = input.finestra.setInterval(askReady, 500);
41
+ pollingEnd = input.finestra.setTimeout(stopPolling, 1e4);
42
+ askReady();
43
+ };
44
+ const loaded = () => {
45
+ legacyReady = true;
46
+ poll();
47
+ };
48
+ const listen = (event) => {
49
+ if (disposed || event.origin !== input.origineGioco || event.source !== input.frame.contentWindow || !eMessaggioReady(event.data)) return;
50
+ const data = record(event.data);
51
+ const nextInstance = typeof data.instance === "string" && data.instance.length <= 128 ? data.instance : null;
52
+ if (port && (nextInstance !== null ? nextInstance === instance : !legacyReady)) return;
53
+ stopPolling();
54
+ legacyReady = false;
55
+ instance = nextInstance;
56
+ fermaCrew?.();
57
+ fermaCrew = null;
58
+ port?.close();
59
+ input.onRoom(null);
60
+ const channel = input.creaCanale?.() ?? new MessageChannel();
61
+ const currentPort = channel.port1;
62
+ port = currentPort;
63
+ const current = () => !disposed && port === currentPort;
64
+ currentPort.onmessage = (event2) => {
65
+ if (!current()) return;
66
+ const data2 = record(event2.data);
67
+ if (eRichiestaBiglietto(data2)) {
68
+ const aud = data2?.aud === "live" ? "live" : "portal";
69
+ void input.rinnova(aud).then((ticket) => {
70
+ if (current()) currentPort.postMessage({ type: "caisual:ticket", aud, ticket });
71
+ }).catch(() => {
72
+ if (current()) currentPort.postMessage({ type: "caisual:ticket", aud, error: "offline" });
73
+ });
74
+ return;
75
+ }
76
+ if (data2?.type === "caisual:reload") {
77
+ const target = record(data2.target);
78
+ const code = typeof target?.code === "string" && CODICE.test(target.code) ? target.code : void 0;
79
+ const roomId = typeof target?.roomId === "string" && /^g[1-9][0-9]*-[1-9][0-9]*\.[a-z0-9]{16}$/.test(target.roomId) ? target.roomId : void 0;
80
+ input.reload?.(code || roomId ? { code, roomId } : void 0);
81
+ return;
82
+ }
83
+ const room = stanzaDaMessaggio(data2);
84
+ if (room !== void 0) {
85
+ input.onRoom(room);
86
+ return;
87
+ }
88
+ if (data2?.type === "caisual:crew") gestisciCrew(data2, currentPort, current);
89
+ };
90
+ currentPort.start();
91
+ input.frame.contentWindow?.postMessage({
92
+ type: "caisual:hello",
93
+ n: input.n,
94
+ ticket: input.ticket,
95
+ live: input.origineLive,
96
+ invite: input.invite,
97
+ ...input.language ? { language: input.language } : {},
98
+ ...input.languagePreferences ? { languagePreferences: [...input.languagePreferences] } : {},
99
+ ...input.gameLanguages ? { gameLanguages: [...input.gameLanguages] } : {}
100
+ }, input.origineGioco, [channel.port2]);
101
+ };
102
+ function gestisciCrew(data, porta, current) {
103
+ const crew = input.crew;
104
+ if (!crew) {
105
+ if (data.op === "subscribe") porta.postMessage({ type: "caisual:crew-state", state: null });
106
+ return;
107
+ }
108
+ if (data.op === "subscribe") {
109
+ const invia = () => {
110
+ if (current()) porta.postMessage({ type: "caisual:crew-state", state: crew.getSnapshot() });
111
+ };
112
+ fermaCrew?.();
113
+ fermaCrew = crew.subscribe(invia);
114
+ invia();
115
+ return;
116
+ }
117
+ const player = typeof data.player === "string" ? data.player : null;
118
+ const party = typeof data.party === "string" ? data.party : null;
119
+ if (data.op === "party") {
120
+ if (data.action === "create") crew.party.create();
121
+ else if (data.action === "leave") crew.party.leave();
122
+ else if (data.action === "invite" && player) crew.party.invite(player);
123
+ else if (data.action === "kick" && player) crew.party.kick(player);
124
+ else if (data.action === "accept" && party) crew.party.accept(party);
125
+ else if (data.action === "decline" && party) crew.party.decline(party);
126
+ return;
127
+ }
128
+ if (data.op === "follow" && typeof data.slug === "string" && typeof data.code === "string" && CODICE.test(data.code)) {
129
+ crew.follow(data.slug, data.code);
130
+ }
131
+ }
132
+ input.finestra.addEventListener("message", listen);
133
+ input.frame.addEventListener?.("load", loaded);
134
+ poll();
135
+ return {
136
+ reload() {
137
+ input.reload?.();
138
+ },
139
+ /** Il gioco e' collegato: serve al sito per sapere se il ponte ha agganciato il documento. */
140
+ get collegato() {
141
+ return port !== null;
142
+ },
143
+ invia(message) {
144
+ if (!port || disposed) throw creaErrore("offline", "The game bridge is not connected.");
145
+ port.postMessage(message);
146
+ },
147
+ dispose() {
148
+ disposed = true;
149
+ stopPolling();
150
+ fermaCrew?.();
151
+ fermaCrew = null;
152
+ port?.close();
153
+ port = null;
154
+ input.finestra.removeEventListener("message", listen);
155
+ input.frame.removeEventListener?.("load", loaded);
156
+ }
157
+ };
158
+ }
159
+ function avviaHandshake(input) {
160
+ const bridge = creaPonteOspite(input);
161
+ return () => bridge.dispose();
162
+ }
163
+ export {
164
+ avviaHandshake,
165
+ creaPonteOspite,
166
+ eMessaggioReady,
167
+ eRichiestaBiglietto,
168
+ stanzaDaMessaggio
169
+ };