@caisual/kit 0.5.0 → 0.8.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 (54) hide show
  1. package/dist/kit.d.ts +2 -175
  2. package/dist/kit.js +1418 -48
  3. package/dist/node.d.ts +1 -88
  4. package/dist/node.js +128 -31
  5. package/dist/overlay.d.ts +1 -0
  6. package/dist/overlay.js +1846 -0
  7. package/dist/runtime.d.ts +1 -111
  8. package/dist/runtime.js +160 -32
  9. package/dist/server.d.ts +1 -71
  10. package/dist/server.js +1 -0
  11. package/dist/types/api.d.ts +25 -0
  12. package/dist/types/contracts/biglietto.d.ts +24 -0
  13. package/dist/types/contracts/device.d.ts +66 -0
  14. package/dist/types/contracts/firma.d.ts +21 -0
  15. package/dist/types/contracts/index.d.ts +10 -0
  16. package/dist/types/contracts/keys.d.ts +59 -0
  17. package/dist/types/contracts/manifest.d.ts +118 -0
  18. package/dist/types/contracts/overlay-boards.d.ts +3 -0
  19. package/dist/types/contracts/overlay.d.ts +215 -0
  20. package/dist/types/contracts/runtime.d.ts +70 -0
  21. package/dist/types/contracts/server-js.d.ts +11 -0
  22. package/dist/types/contracts/slug.d.ts +5 -0
  23. package/dist/types/contracts/token-crew.d.ts +21 -0
  24. package/dist/types/daily.d.ts +3 -0
  25. package/dist/types/errors.d.ts +6 -0
  26. package/dist/types/handshake.d.ts +26 -0
  27. package/dist/types/http.d.ts +3 -0
  28. package/dist/types/index.d.ts +217 -0
  29. package/dist/types/kit.d.ts +26 -0
  30. package/dist/types/node/index.d.ts +96 -0
  31. package/dist/types/node/websocket.d.ts +29 -0
  32. package/dist/types/overlay/boards.d.ts +48 -0
  33. package/dist/types/overlay/bridge.d.ts +3 -0
  34. package/dist/types/overlay/host-bridge.d.ts +58 -0
  35. package/dist/types/overlay/i18n.d.ts +122 -0
  36. package/dist/types/overlay/index.d.ts +9 -0
  37. package/dist/types/overlay/shortcut.d.ts +4 -0
  38. package/dist/types/overlay/styles.d.ts +1 -0
  39. package/dist/types/overlay/ui-model.d.ts +45 -0
  40. package/dist/types/overlay/ui.d.ts +80 -0
  41. package/dist/types/overlay/voice-panel.d.ts +13 -0
  42. package/dist/types/protocollo.d.ts +257 -0
  43. package/dist/types/runtime/index.d.ts +127 -0
  44. package/dist/types/server/index.d.ts +72 -0
  45. package/dist/types/session/index.d.ts +49 -0
  46. package/dist/types/session/resume.d.ts +13 -0
  47. package/dist/types/standalone.d.ts +14 -0
  48. package/dist/types/stanza/index.d.ts +3 -0
  49. package/dist/types/stanza/json.d.ts +18 -0
  50. package/dist/types/stanza/nucleo.d.ts +97 -0
  51. package/dist/types/stanza/tipi.d.ts +134 -0
  52. package/dist/types/stanza-client/index.d.ts +24 -0
  53. package/dist/types/voce/index.d.ts +124 -0
  54. package/package.json +10 -2
package/dist/node.d.ts CHANGED
@@ -1,88 +1 @@
1
- import type { IncomingMessage } from 'node:http';
2
- import type { Duplex } from 'node:stream';
3
- import type { GameDefinition, JsonValue, RoomStatus, SharedGameStore } from './server.js';
4
- export interface NodeWebSocket {
5
- on(event: 'message', listener: (message: string) => void): this;
6
- on(event: 'close', listener: () => void): this;
7
- send(message: string): void;
8
- close(code?: number, reason?: string): void;
9
- }
10
- export declare function acceptNodeWebSocket(request: IncomingMessage, socket: Duplex, head?: Buffer): NodeWebSocket;
11
- export interface NodePlayerIdentity {
12
- id: string;
13
- name: string;
14
- guest: boolean;
15
- }
16
- export interface NodeRoomManifest {
17
- id: string;
18
- players: {
19
- min: number;
20
- max: number;
21
- };
22
- lobby: boolean;
23
- persistent?: boolean;
24
- spectators?: {
25
- delayMs: number;
26
- } | null;
27
- roles: Array<{
28
- id: string;
29
- min: number;
30
- max?: number;
31
- }>;
32
- teams: {
33
- min: number;
34
- max: number;
35
- } | null;
36
- modes: Array<{
37
- id: string;
38
- }>;
39
- voice?: 'none' | 'room' | 'team' | 'proximity';
40
- }
41
- export type NodeRoomJoinResult = {
42
- ok: true;
43
- } | {
44
- ok: false;
45
- code: 'room_not_found' | 'room_full' | 'room_ended' | 'room_playing';
46
- };
47
- export type NodeRoomWatchResult = {
48
- ok: true;
49
- } | {
50
- ok: false;
51
- code: 'room_not_found' | 'room_ended' | 'spectators_disabled' | 'spectators_full';
52
- };
53
- export interface NodeRoomInfo {
54
- roomId: string;
55
- status: RoomStatus;
56
- players: number;
57
- max: number;
58
- mode: string | null;
59
- }
60
- export interface NodeRoomFlush {
61
- scores: Array<{
62
- playerId: string;
63
- board: string;
64
- score: number;
65
- daily: boolean;
66
- }>;
67
- ended: {
68
- result: JsonValue;
69
- at: number;
70
- } | null;
71
- }
72
- export interface NodeRoomOptions {
73
- storageFile?: string;
74
- deposito?: SharedGameStore | null;
75
- }
76
- export interface NodeRoom {
77
- create(roomId: string, mode: string | null, creator: NodePlayerIdentity): Promise<boolean>;
78
- info(): Promise<NodeRoomInfo | null>;
79
- canJoin(identity: NodePlayerIdentity): Promise<NodeRoomJoinResult>;
80
- canWatch(): Promise<NodeRoomWatchResult>;
81
- connect(socket: NodeWebSocket, identity: NodePlayerIdentity): Promise<NodeRoomJoinResult>;
82
- watch(socket: NodeWebSocket, identity: NodePlayerIdentity): Promise<NodeRoomWatchResult>;
83
- flush(): Promise<NodeRoomFlush>;
84
- wake(): Promise<void>;
85
- close(): Promise<void>;
86
- }
87
- /** Crea una stanza Node usando memoria o un singolo file JSON persistente. */
88
- export declare function createNodeRoom(definition: GameDefinition, manifest: NodeRoomManifest, options?: NodeRoomOptions): Promise<NodeRoom>;
1
+ export * from './types/node/index.js';
package/dist/node.js CHANGED
@@ -40,6 +40,14 @@ var NOMI_RISERVATI = [
40
40
  var RISERVATI = new Set(NOMI_RISERVATI);
41
41
 
42
42
  // ../contracts/src/manifest.ts
43
+ function risolviModalita(manifest, mode) {
44
+ const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);
45
+ if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");
46
+ return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };
47
+ }
48
+ function modalitaLocale(manifest, mode) {
49
+ return mode !== null && manifest.modes.some((voce) => voce.id === mode && voce.execution === "local");
50
+ }
43
51
  var MASSIMO_SPETTATORI = 100;
44
52
  var RITARDO_SPETTATORI_MS = 3e3;
45
53
 
@@ -485,9 +493,8 @@ var NucleoStanza = class _NucleoStanza {
485
493
  }
486
494
  async crea(id, mode, _creator) {
487
495
  if (this.dati !== null) return false;
488
- if (mode !== null && !this.manifest.modes.some((item) => item.id === mode)) {
489
- throw new Error("The selected game mode does not exist.");
490
- }
496
+ risolviModalita(this.manifest, mode);
497
+ if (modalitaLocale(this.manifest, mode)) throw new Error("Local modes cannot create rooms.");
491
498
  const ora = this.adattatore.ora();
492
499
  this.dati = {
493
500
  versione: 1,
@@ -532,6 +539,9 @@ var NucleoStanza = class _NucleoStanza {
532
539
  await this.persistiEProgramma();
533
540
  return true;
534
541
  }
542
+ get configurazione() {
543
+ return risolviModalita(this.manifest, this.dati?.mode ?? null);
544
+ }
535
545
  info(playerId) {
536
546
  if (this.dati === null) return null;
537
547
  return {
@@ -540,7 +550,7 @@ var NucleoStanza = class _NucleoStanza {
540
550
  players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
541
551
  (player) => player.connected || player.graziaFinoA !== null
542
552
  ).length,
543
- max: this.manifest.players.max,
553
+ max: this.configurazione.players.max,
544
554
  mode: this.dati.mode,
545
555
  member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
546
556
  };
@@ -559,15 +569,34 @@ var NucleoStanza = class _NucleoStanza {
559
569
  );
560
570
  return player === void 0 ? null : copiaGiocatore(player);
561
571
  }
572
+ puoAscoltare(listener, speakers) {
573
+ const mode = this.manifest.voice ?? "none";
574
+ if (mode === "none") return speakers.map(() => ({ ok: false, reason: "mode" }));
575
+ const dati = this.richiediDati();
576
+ const ascoltatore = dati.giocatori.find((player) => player.id === listener);
577
+ return speakers.map((speakerId) => {
578
+ const speaker = dati.giocatori.find((player) => player.id === speakerId);
579
+ if (ascoltatore === void 0 || speaker === void 0 || listener === speakerId) {
580
+ return { ok: false, reason: "unknown" };
581
+ }
582
+ if (mode === "team" && ascoltatore.role !== "spectator" && ascoltatore.team !== speaker.team) {
583
+ return { ok: false, reason: "team" };
584
+ }
585
+ if ((dati.voceGuadagni?.[listener]?.[speakerId] ?? 1) <= 0) {
586
+ return { ok: false, reason: "gain" };
587
+ }
588
+ return { ok: true };
589
+ });
590
+ }
562
591
  puoEntrare(identity) {
563
592
  if (this.dati === null) return { ok: false, code: "room_not_found" };
564
593
  if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
565
594
  const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
566
595
  if (esistente !== void 0) return { ok: true };
567
- if (this.manifest.lobby && this.dati.status !== "lobby") {
596
+ if (this.configurazione.lobby && this.dati.status !== "lobby") {
568
597
  return { ok: false, code: "room_playing" };
569
598
  }
570
- if (this.dati.giocatori.length >= this.manifest.players.max) {
599
+ if (this.dati.giocatori.length >= this.configurazione.players.max) {
571
600
  return { ok: false, code: "room_full" };
572
601
  }
573
602
  return { ok: true };
@@ -610,7 +639,7 @@ var NucleoStanza = class _NucleoStanza {
610
639
  dati.hostId ??= player.id;
611
640
  dati.vuotaDa = null;
612
641
  dati.ultimoInputAt = ora;
613
- const primaConnessione = !this.manifest.lobby && dati.status === "lobby";
642
+ const primaConnessione = !this.configurazione.lobby && dati.status === "lobby";
614
643
  if (primaConnessione) dati.status = "playing";
615
644
  if (nuovo) {
616
645
  await this.chiama(
@@ -716,6 +745,26 @@ var NucleoStanza = class _NucleoStanza {
716
745
  await this.persistiEProgramma();
717
746
  return;
718
747
  }
748
+ if (message.t === "request-role") {
749
+ if (!Number.isSafeInteger(message.r) || message.r < 1 || typeof message.role !== "string") return this.messaggioErrato(player);
750
+ let code = null;
751
+ if (this.dati.status !== "playing" || this.definizione.onRoleRequest === void 0) code = "role_change_unavailable";
752
+ else if (!this.manifest.roles.some((role) => role.id === message.role)) code = "invalid_role";
753
+ else {
754
+ this.dati.ultimoInputAt = ora;
755
+ await this.chiama(this.definizione.onRoleRequest, this.room, copiaGiocatore(player), message.role);
756
+ await this.concludiEvento();
757
+ if (player.role !== message.role) code = "role_change_refused";
758
+ }
759
+ if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
760
+ t: "role-result",
761
+ r: message.r,
762
+ ok: code === null,
763
+ code
764
+ });
765
+ await this.persistiEProgramma();
766
+ return;
767
+ }
719
768
  if (message.t === "team") {
720
769
  if (!Number.isInteger(message.team)) return this.messaggioErrato(player);
721
770
  if (!this.inLobby(player)) return;
@@ -784,23 +833,8 @@ var NucleoStanza = class _NucleoStanza {
784
833
  this.inviaGuadagniCambiati();
785
834
  const stato = await this.verificaStato();
786
835
  if (stato === null || this.stanzaTerminata()) return;
787
- const cambiato = stato.testo !== JSON.stringify(dati.statoSincronizzato);
788
- if (dati.tick % 100 === 0) {
789
- dati.state = stato.valore;
790
- this.inviaSnapshotTutti();
791
- } else if (cambiato) {
792
- const base = dati.tickSincronizzato;
793
- this.broadcast({
794
- t: "state",
795
- tick: dati.tick,
796
- serverTime: this.adattatore.ora(),
797
- base,
798
- patch: creaDiff(dati.statoSincronizzato, stato.valore)
799
- });
800
- dati.state = stato.valore;
801
- dati.statoSincronizzato = copiaJson(stato.valore);
802
- dati.tickSincronizzato = dati.tick;
803
- }
836
+ dati.state = stato.valore;
837
+ this.inviaDiff();
804
838
  if (dati.tick % 100 === 0 || this.voceDaPersistire) await this.persisti();
805
839
  await this.aggiornaProgrammazione();
806
840
  }
@@ -929,10 +963,15 @@ var NucleoStanza = class _NucleoStanza {
929
963
  if (ruolo?.max !== void 0 && occupati >= ruolo.max) {
930
964
  return { code: "role_full", message: "This role is full." };
931
965
  }
966
+ const ruoloPrecedente = player.role;
967
+ const squadraPrecedente = player.team;
932
968
  player.role = roleId;
933
969
  if (roleId === "spectator") player.team = null;
934
970
  else if (player.team === null) player.team = this.squadraAutomatica();
935
971
  if (this.richiediDati().status === "lobby") player.ready = false;
972
+ if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
973
+ this.rivediVoce();
974
+ }
936
975
  this.inviaGiocatori();
937
976
  return null;
938
977
  }
@@ -943,8 +982,10 @@ var NucleoStanza = class _NucleoStanza {
943
982
  if (player.role === "spectator") {
944
983
  return { code: "spectator", message: "Spectators cannot join a team." };
945
984
  }
985
+ const squadraPrecedente = player.team;
946
986
  player.team = team;
947
987
  if (this.richiediDati().status === "lobby") player.ready = false;
988
+ if (player.team !== squadraPrecedente) this.rivediVoce();
948
989
  this.inviaGiocatori();
949
990
  return null;
950
991
  }
@@ -968,7 +1009,7 @@ var NucleoStanza = class _NucleoStanza {
968
1009
  erroreMinimi() {
969
1010
  const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
970
1011
  const attivi = connessi.filter((player) => player.role !== "spectator");
971
- if (attivi.length < this.manifest.players.min) {
1012
+ if (attivi.length < this.configurazione.players.min) {
972
1013
  return { code: "not_enough_players", message: "The room does not have enough players." };
973
1014
  }
974
1015
  if (connessi.some((player) => !player.ready)) {
@@ -1023,6 +1064,7 @@ var NucleoStanza = class _NucleoStanza {
1023
1064
  const indice = dati.giocatori.findIndex((item) => item.id === player.id);
1024
1065
  if (indice < 0) return;
1025
1066
  dati.giocatori.splice(indice, 1);
1067
+ this.rivediVoce();
1026
1068
  this.pulisciGuadagni(player.id);
1027
1069
  if (dati.hostId === player.id) this.assegnaHost();
1028
1070
  if (dati.giocatori.length === 0) dati.vuotaDa = this.adattatore.ora();
@@ -1069,7 +1111,7 @@ var NucleoStanza = class _NucleoStanza {
1069
1111
  });
1070
1112
  }
1071
1113
  accodaPunteggio(playerId, board, score, daily) {
1072
- if (!this.richiediDati().giocatori.some((player) => player.id === playerId)) {
1114
+ if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
1073
1115
  throw new Error("Player not found.");
1074
1116
  }
1075
1117
  if (!CHIAVE.test(board)) {
@@ -1078,7 +1120,20 @@ var NucleoStanza = class _NucleoStanza {
1078
1120
  if (!Number.isSafeInteger(score) || score < 0) {
1079
1121
  throw new TypeError("Score must be a non-negative safe integer.");
1080
1122
  }
1081
- this.richiediDati().punteggi.push({ playerId, board, score, daily });
1123
+ const submittedAt = this.adattatore.ora();
1124
+ this.richiediDati().punteggi.push({
1125
+ playerId,
1126
+ board,
1127
+ score,
1128
+ daily,
1129
+ submittedAt,
1130
+ day: daily ? giornoUtc(submittedAt) : null
1131
+ });
1132
+ const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
1133
+ if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
1134
+ t: "score-queued",
1135
+ score: { player: playerId, board, score, day: daily ? giornoUtc(submittedAt) : null, submittedAt }
1136
+ });
1082
1137
  this.broadcast({ t: "flush" });
1083
1138
  }
1084
1139
  richiediFine(result) {
@@ -1255,21 +1310,44 @@ var NucleoStanza = class _NucleoStanza {
1255
1310
  tick: dati.tick,
1256
1311
  tickRate: dati.tickRate,
1257
1312
  serverTime: this.adattatore.ora(),
1258
- host: dati.hostId
1313
+ host: dati.hostId,
1314
+ countdownAt: dati.countdownAt,
1315
+ configuration: {
1316
+ players: { ...this.configurazione.players },
1317
+ lobby: this.configurazione.lobby,
1318
+ persistent: this.manifest.persistent === true,
1319
+ requestRole: this.definizione.onRoleRequest !== void 0
1320
+ }
1259
1321
  };
1260
1322
  }
1261
1323
  inviaGiocatori() {
1262
- this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
1324
+ this.broadcast({ t: "players", players: this.giocatoriProtocollo(), host: this.richiediDati().hostId });
1263
1325
  }
1264
1326
  inviaStatus(at) {
1265
1327
  const dati = this.richiediDati();
1266
1328
  this.broadcast({
1267
1329
  t: "status",
1268
1330
  status: dati.status,
1331
+ host: dati.hostId,
1332
+ countdownAt: dati.countdownAt,
1269
1333
  at,
1270
1334
  result: dati.status === "ended" ? dati.result : null
1271
1335
  });
1272
1336
  }
1337
+ inviaDiff() {
1338
+ const dati = this.richiediDati();
1339
+ const patch = creaDiff(dati.statoSincronizzato, dati.state);
1340
+ if (patch.length === 0) return;
1341
+ this.broadcast({
1342
+ t: "state",
1343
+ tick: dati.tick,
1344
+ base: dati.tickSincronizzato,
1345
+ serverTime: this.adattatore.ora(),
1346
+ patch
1347
+ });
1348
+ dati.statoSincronizzato = copiaJson(dati.state);
1349
+ dati.tickSincronizzato = dati.tick;
1350
+ }
1273
1351
  inviaSnapshotTutti() {
1274
1352
  const dati = this.richiediDati();
1275
1353
  const stato = analizzaJson(dati.state);
@@ -1324,7 +1402,7 @@ var NucleoStanza = class _NucleoStanza {
1324
1402
  this.dati.state = stato.valore;
1325
1403
  if ((this.dati.tickRate === 0 || this.dati.status !== "playing") && stato.testo !== precedente) {
1326
1404
  this.dati.tick++;
1327
- this.inviaSnapshotTutti();
1405
+ this.inviaDiff();
1328
1406
  }
1329
1407
  }
1330
1408
  async verificaStato() {
@@ -1358,6 +1436,7 @@ var NucleoStanza = class _NucleoStanza {
1358
1436
  dati.voceGuadagni ??= {};
1359
1437
  const precedente = dati.voceGuadagni[playerId]?.[altroId] ?? 1;
1360
1438
  if (precedente === valore) return;
1439
+ if (precedente > 0 && valore === 0) this.rivediVoce();
1361
1440
  this.voceDaPersistire = true;
1362
1441
  if (valore === 1) {
1363
1442
  const riga = dati.voceGuadagni[playerId];
@@ -1375,6 +1454,9 @@ var NucleoStanza = class _NucleoStanza {
1375
1454
  this.voceGuadagniCambiati.set(playerId, cambi);
1376
1455
  return cambi;
1377
1456
  }
1457
+ rivediVoce() {
1458
+ if ((this.manifest.voice ?? "none") !== "none") this.adattatore.rivediVoce();
1459
+ }
1378
1460
  inviaGuadagniCambiati() {
1379
1461
  if (this.dati === null || this.voceGuadagniCambiati.size === 0) return;
1380
1462
  for (const [playerId, gains] of this.voceGuadagniCambiati) {
@@ -1422,7 +1504,7 @@ var NucleoStanza = class _NucleoStanza {
1422
1504
  } else if (stato.testo !== JSON.stringify(dati.statoSincronizzato)) {
1423
1505
  dati.tick++;
1424
1506
  dati.state = stato.valore;
1425
- this.inviaSnapshotTutti();
1507
+ this.inviaDiff();
1426
1508
  }
1427
1509
  const fine = { result: dati.result, at: dati.resultAt };
1428
1510
  dati.fineInCoda = fine;
@@ -1878,6 +1960,8 @@ var AdattatoreNode = class {
1878
1960
  misuraCpu() {
1879
1961
  return performance.now();
1880
1962
  }
1963
+ rivediVoce() {
1964
+ }
1881
1965
  programmaTick(intervalloMs) {
1882
1966
  if (this.tickIntervallo === intervalloMs) return;
1883
1967
  this.tickIntervallo = intervalloMs;
@@ -2203,6 +2287,19 @@ var StanzaNode = class {
2203
2287
  }
2204
2288
  if (richiesta.op === "signal") {
2205
2289
  const destinazione = this.giocatoriConnessioni.get(richiesta.to);
2290
+ const autorizzazioni = [
2291
+ ...this.nucleo.puoAscoltare(richiesta.to, [playerId]),
2292
+ ...this.nucleo.puoAscoltare(playerId, [richiesta.to])
2293
+ ];
2294
+ if (autorizzazioni.some((esito) => !esito.ok)) {
2295
+ this.inviaErroreVoce(
2296
+ connessione,
2297
+ richiesta,
2298
+ "not_allowed",
2299
+ "The player is not allowed to hear this participant."
2300
+ );
2301
+ return;
2302
+ }
2206
2303
  if (destinazione !== void 0) {
2207
2304
  this.adattatore.invia(destinazione, {
2208
2305
  t: "voice",
@@ -0,0 +1 @@
1
+ export * from './types/overlay/index.js';