@caisual/kit 0.4.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 -157
  2. package/dist/kit.js +1539 -59
  3. package/dist/node.d.ts +1 -77
  4. package/dist/node.js +341 -42
  5. package/dist/overlay.d.ts +1 -0
  6. package/dist/overlay.js +1846 -0
  7. package/dist/runtime.d.ts +1 -108
  8. package/dist/runtime.js +191 -41
  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/runtime.d.ts CHANGED
@@ -1,108 +1 @@
1
- import type { GameDefinition, JsonValue, RoomStatus } from './server.js';
2
- export interface RoomManifest {
3
- id: string;
4
- players: {
5
- min: number;
6
- max: number;
7
- };
8
- lobby: boolean;
9
- persistent?: boolean;
10
- roles: Array<{
11
- id: string;
12
- min: number;
13
- max?: number;
14
- }>;
15
- teams: {
16
- min: number;
17
- max: number;
18
- } | null;
19
- modes: Array<{
20
- id: string;
21
- }>;
22
- voice?: 'none' | 'room' | 'team' | 'proximity';
23
- }
24
- export interface IdentitaGiocatore {
25
- id: string;
26
- name: string;
27
- guest: boolean;
28
- }
29
- export interface Esito {
30
- frames: Array<{
31
- connessione: string;
32
- testo: string;
33
- }>;
34
- chiusure: Array<{
35
- connessione: string;
36
- codice: number;
37
- motivo: string;
38
- }>;
39
- sveglia: number | null;
40
- tick: boolean;
41
- tickRate: number;
42
- status: RoomStatus | null;
43
- }
44
- export interface InfoStanza {
45
- roomId: string;
46
- status: RoomStatus;
47
- players: number;
48
- max: number;
49
- mode: string | null;
50
- member: boolean;
51
- }
52
- export type EsitoIngresso = {
53
- ok: true;
54
- } | {
55
- ok: false;
56
- code: 'room_not_found' | 'room_full' | 'room_ended' | 'room_playing';
57
- };
58
- export interface EsitoFlush {
59
- scores: Array<{
60
- playerId: string;
61
- board: string;
62
- score: number;
63
- daily: boolean;
64
- }>;
65
- ended: {
66
- result: JsonValue;
67
- at: number;
68
- } | null;
69
- }
70
- export interface FacetStanza {
71
- crea(input: {
72
- mode: string | null;
73
- creator: IdentitaGiocatore;
74
- }): Promise<Esito & {
75
- ok: boolean;
76
- }>;
77
- info(playerId?: string): Promise<InfoStanza | null>;
78
- puoEntrare(identita: IdentitaGiocatore): Promise<EsitoIngresso>;
79
- entra(identita: IdentitaGiocatore, connessione: string): Promise<Esito>;
80
- ricevi(connessione: string, testo: string): Promise<Esito>;
81
- disconnetti(connessione: string): Promise<Esito>;
82
- tick(): Promise<Esito>;
83
- sveglia(): Promise<Esito>;
84
- flush(): Promise<EsitoFlush>;
85
- }
86
- export interface StorageFacet {
87
- get<T>(chiave: string): Promise<T | undefined>;
88
- put(chiave: string, valore: unknown): Promise<void>;
89
- delete(chiave: string): Promise<boolean>;
90
- list<T>(opzioni?: {
91
- prefix?: string;
92
- }): Promise<Map<string, T>>;
93
- }
94
- export interface StatoFacet {
95
- readonly id: string | {
96
- readonly name?: string;
97
- toString(): string;
98
- };
99
- readonly storage: StorageFacet;
100
- blockConcurrencyWhile<T>(callback: () => Promise<T>): Promise<T>;
101
- }
102
- export interface ClasseBaseFacet {
103
- new (stato: StatoFacet, ambiente: unknown): object;
104
- }
105
- export interface ClasseFacetStanza {
106
- new (stato: StatoFacet, ambiente: unknown): FacetStanza;
107
- }
108
- export declare function creaFacetStanza(DurableObject: ClasseBaseFacet, definizione: GameDefinition, manifest: RoomManifest): ClasseFacetStanza;
1
+ export * from './types/runtime/index.js';
package/dist/runtime.js CHANGED
@@ -1,3 +1,48 @@
1
+ // ../contracts/src/slug.ts
2
+ var NOMI_RISERVATI = [
3
+ "www",
4
+ "api",
5
+ "app",
6
+ "play",
7
+ "live",
8
+ "multi",
9
+ "cdn",
10
+ "assets",
11
+ "static",
12
+ "mail",
13
+ "mx",
14
+ "ns1",
15
+ "ns2",
16
+ "autodiscover",
17
+ "_dmarc",
18
+ "admin",
19
+ "login",
20
+ "account",
21
+ "auth",
22
+ "pay",
23
+ "secure",
24
+ "support",
25
+ "help",
26
+ "blog",
27
+ "status",
28
+ "dev",
29
+ "staging",
30
+ "test",
31
+ "caisual",
32
+ "shipz"
33
+ ];
34
+ var RISERVATI = new Set(NOMI_RISERVATI);
35
+
36
+ // ../contracts/src/manifest.ts
37
+ function risolviModalita(manifest, mode) {
38
+ const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);
39
+ if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");
40
+ return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };
41
+ }
42
+ function modalitaLocale(manifest, mode) {
43
+ return mode !== null && manifest.modes.some((voce) => voce.id === mode && voce.execution === "local");
44
+ }
45
+
1
46
  // src/stanza/json.ts
2
47
  function isPlainObject(value) {
3
48
  const prototype = Object.getPrototypeOf(value);
@@ -440,9 +485,8 @@ var NucleoStanza = class _NucleoStanza {
440
485
  }
441
486
  async crea(id, mode, _creator) {
442
487
  if (this.dati !== null) return false;
443
- if (mode !== null && !this.manifest.modes.some((item) => item.id === mode)) {
444
- throw new Error("The selected game mode does not exist.");
445
- }
488
+ risolviModalita(this.manifest, mode);
489
+ if (modalitaLocale(this.manifest, mode)) throw new Error("Local modes cannot create rooms.");
446
490
  const ora = this.adattatore.ora();
447
491
  this.dati = {
448
492
  versione: 1,
@@ -487,6 +531,9 @@ var NucleoStanza = class _NucleoStanza {
487
531
  await this.persistiEProgramma();
488
532
  return true;
489
533
  }
534
+ get configurazione() {
535
+ return risolviModalita(this.manifest, this.dati?.mode ?? null);
536
+ }
490
537
  info(playerId) {
491
538
  if (this.dati === null) return null;
492
539
  return {
@@ -495,26 +542,53 @@ var NucleoStanza = class _NucleoStanza {
495
542
  players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
496
543
  (player) => player.connected || player.graziaFinoA !== null
497
544
  ).length,
498
- max: this.manifest.players.max,
545
+ max: this.configurazione.players.max,
499
546
  mode: this.dati.mode,
500
547
  member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
501
548
  };
502
549
  }
550
+ fotografia() {
551
+ if (this.dati === null) return null;
552
+ return {
553
+ room: this.stanzaProtocollo(),
554
+ players: this.giocatoriProtocollo(),
555
+ state: this.dati.state
556
+ };
557
+ }
503
558
  giocatoreConnesso(connessione) {
504
559
  const player = this.dati?.giocatori.find(
505
560
  (item) => item.connected && item.connessione === connessione
506
561
  );
507
562
  return player === void 0 ? null : copiaGiocatore(player);
508
563
  }
564
+ puoAscoltare(listener, speakers) {
565
+ const mode = this.manifest.voice ?? "none";
566
+ if (mode === "none") return speakers.map(() => ({ ok: false, reason: "mode" }));
567
+ const dati = this.richiediDati();
568
+ const ascoltatore = dati.giocatori.find((player) => player.id === listener);
569
+ return speakers.map((speakerId) => {
570
+ const speaker = dati.giocatori.find((player) => player.id === speakerId);
571
+ if (ascoltatore === void 0 || speaker === void 0 || listener === speakerId) {
572
+ return { ok: false, reason: "unknown" };
573
+ }
574
+ if (mode === "team" && ascoltatore.role !== "spectator" && ascoltatore.team !== speaker.team) {
575
+ return { ok: false, reason: "team" };
576
+ }
577
+ if ((dati.voceGuadagni?.[listener]?.[speakerId] ?? 1) <= 0) {
578
+ return { ok: false, reason: "gain" };
579
+ }
580
+ return { ok: true };
581
+ });
582
+ }
509
583
  puoEntrare(identity) {
510
584
  if (this.dati === null) return { ok: false, code: "room_not_found" };
511
585
  if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
512
586
  const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
513
587
  if (esistente !== void 0) return { ok: true };
514
- if (this.manifest.lobby && this.dati.status !== "lobby") {
588
+ if (this.configurazione.lobby && this.dati.status !== "lobby") {
515
589
  return { ok: false, code: "room_playing" };
516
590
  }
517
- if (this.dati.giocatori.length >= this.manifest.players.max) {
591
+ if (this.dati.giocatori.length >= this.configurazione.players.max) {
518
592
  return { ok: false, code: "room_full" };
519
593
  }
520
594
  return { ok: true };
@@ -557,7 +631,7 @@ var NucleoStanza = class _NucleoStanza {
557
631
  dati.hostId ??= player.id;
558
632
  dati.vuotaDa = null;
559
633
  dati.ultimoInputAt = ora;
560
- const primaConnessione = !this.manifest.lobby && dati.status === "lobby";
634
+ const primaConnessione = !this.configurazione.lobby && dati.status === "lobby";
561
635
  if (primaConnessione) dati.status = "playing";
562
636
  if (nuovo) {
563
637
  await this.chiama(
@@ -663,6 +737,26 @@ var NucleoStanza = class _NucleoStanza {
663
737
  await this.persistiEProgramma();
664
738
  return;
665
739
  }
740
+ if (message.t === "request-role") {
741
+ if (!Number.isSafeInteger(message.r) || message.r < 1 || typeof message.role !== "string") return this.messaggioErrato(player);
742
+ let code = null;
743
+ if (this.dati.status !== "playing" || this.definizione.onRoleRequest === void 0) code = "role_change_unavailable";
744
+ else if (!this.manifest.roles.some((role) => role.id === message.role)) code = "invalid_role";
745
+ else {
746
+ this.dati.ultimoInputAt = ora;
747
+ await this.chiama(this.definizione.onRoleRequest, this.room, copiaGiocatore(player), message.role);
748
+ await this.concludiEvento();
749
+ if (player.role !== message.role) code = "role_change_refused";
750
+ }
751
+ if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
752
+ t: "role-result",
753
+ r: message.r,
754
+ ok: code === null,
755
+ code
756
+ });
757
+ await this.persistiEProgramma();
758
+ return;
759
+ }
666
760
  if (message.t === "team") {
667
761
  if (!Number.isInteger(message.team)) return this.messaggioErrato(player);
668
762
  if (!this.inLobby(player)) return;
@@ -731,23 +825,8 @@ var NucleoStanza = class _NucleoStanza {
731
825
  this.inviaGuadagniCambiati();
732
826
  const stato = await this.verificaStato();
733
827
  if (stato === null || this.stanzaTerminata()) return;
734
- const cambiato = stato.testo !== JSON.stringify(dati.statoSincronizzato);
735
- if (dati.tick % 100 === 0) {
736
- dati.state = stato.valore;
737
- this.inviaSnapshotTutti();
738
- } else if (cambiato) {
739
- const base = dati.tickSincronizzato;
740
- this.broadcast({
741
- t: "state",
742
- tick: dati.tick,
743
- serverTime: this.adattatore.ora(),
744
- base,
745
- patch: creaDiff(dati.statoSincronizzato, stato.valore)
746
- });
747
- dati.state = stato.valore;
748
- dati.statoSincronizzato = copiaJson(stato.valore);
749
- dati.tickSincronizzato = dati.tick;
750
- }
828
+ dati.state = stato.valore;
829
+ this.inviaDiff();
751
830
  if (dati.tick % 100 === 0 || this.voceDaPersistire) await this.persisti();
752
831
  await this.aggiornaProgrammazione();
753
832
  }
@@ -876,10 +955,15 @@ var NucleoStanza = class _NucleoStanza {
876
955
  if (ruolo?.max !== void 0 && occupati >= ruolo.max) {
877
956
  return { code: "role_full", message: "This role is full." };
878
957
  }
958
+ const ruoloPrecedente = player.role;
959
+ const squadraPrecedente = player.team;
879
960
  player.role = roleId;
880
961
  if (roleId === "spectator") player.team = null;
881
962
  else if (player.team === null) player.team = this.squadraAutomatica();
882
963
  if (this.richiediDati().status === "lobby") player.ready = false;
964
+ if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
965
+ this.rivediVoce();
966
+ }
883
967
  this.inviaGiocatori();
884
968
  return null;
885
969
  }
@@ -890,8 +974,10 @@ var NucleoStanza = class _NucleoStanza {
890
974
  if (player.role === "spectator") {
891
975
  return { code: "spectator", message: "Spectators cannot join a team." };
892
976
  }
977
+ const squadraPrecedente = player.team;
893
978
  player.team = team;
894
979
  if (this.richiediDati().status === "lobby") player.ready = false;
980
+ if (player.team !== squadraPrecedente) this.rivediVoce();
895
981
  this.inviaGiocatori();
896
982
  return null;
897
983
  }
@@ -915,7 +1001,7 @@ var NucleoStanza = class _NucleoStanza {
915
1001
  erroreMinimi() {
916
1002
  const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
917
1003
  const attivi = connessi.filter((player) => player.role !== "spectator");
918
- if (attivi.length < this.manifest.players.min) {
1004
+ if (attivi.length < this.configurazione.players.min) {
919
1005
  return { code: "not_enough_players", message: "The room does not have enough players." };
920
1006
  }
921
1007
  if (connessi.some((player) => !player.ready)) {
@@ -970,6 +1056,7 @@ var NucleoStanza = class _NucleoStanza {
970
1056
  const indice = dati.giocatori.findIndex((item) => item.id === player.id);
971
1057
  if (indice < 0) return;
972
1058
  dati.giocatori.splice(indice, 1);
1059
+ this.rivediVoce();
973
1060
  this.pulisciGuadagni(player.id);
974
1061
  if (dati.hostId === player.id) this.assegnaHost();
975
1062
  if (dati.giocatori.length === 0) dati.vuotaDa = this.adattatore.ora();
@@ -1016,7 +1103,7 @@ var NucleoStanza = class _NucleoStanza {
1016
1103
  });
1017
1104
  }
1018
1105
  accodaPunteggio(playerId, board, score, daily) {
1019
- if (!this.richiediDati().giocatori.some((player) => player.id === playerId)) {
1106
+ if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
1020
1107
  throw new Error("Player not found.");
1021
1108
  }
1022
1109
  if (!CHIAVE.test(board)) {
@@ -1025,7 +1112,20 @@ var NucleoStanza = class _NucleoStanza {
1025
1112
  if (!Number.isSafeInteger(score) || score < 0) {
1026
1113
  throw new TypeError("Score must be a non-negative safe integer.");
1027
1114
  }
1028
- this.richiediDati().punteggi.push({ playerId, board, score, daily });
1115
+ const submittedAt = this.adattatore.ora();
1116
+ this.richiediDati().punteggi.push({
1117
+ playerId,
1118
+ board,
1119
+ score,
1120
+ daily,
1121
+ submittedAt,
1122
+ day: daily ? giornoUtc(submittedAt) : null
1123
+ });
1124
+ const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
1125
+ if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
1126
+ t: "score-queued",
1127
+ score: { player: playerId, board, score, day: daily ? giornoUtc(submittedAt) : null, submittedAt }
1128
+ });
1029
1129
  this.broadcast({ t: "flush" });
1030
1130
  }
1031
1131
  richiediFine(result) {
@@ -1159,6 +1259,7 @@ var NucleoStanza = class _NucleoStanza {
1159
1259
  this.adattatore.invia(player.connessione, message);
1160
1260
  }
1161
1261
  }
1262
+ if (message.t !== "flush") this.adattatore.pubblica(message);
1162
1263
  }
1163
1264
  inviaErrore(player, code, message) {
1164
1265
  if (player.connected && player.connessione !== null) {
@@ -1182,16 +1283,7 @@ var NucleoStanza = class _NucleoStanza {
1182
1283
  this.adattatore.invia(player.connessione, {
1183
1284
  t: "welcome",
1184
1285
  you: player.id,
1185
- room: {
1186
- id: dati.id,
1187
- seed: seedStanza(dati.id),
1188
- status: dati.status,
1189
- mode: dati.mode,
1190
- tick: dati.tick,
1191
- tickRate: dati.tickRate,
1192
- serverTime: this.adattatore.ora(),
1193
- host: dati.hostId
1194
- },
1286
+ room: this.stanzaProtocollo(),
1195
1287
  players: this.giocatoriProtocollo(),
1196
1288
  state: dati.state
1197
1289
  });
@@ -1200,18 +1292,54 @@ var NucleoStanza = class _NucleoStanza {
1200
1292
  this.adattatore.invia(player.connessione, { t: "voice", op: "gain", gains: { ...gains } });
1201
1293
  }
1202
1294
  }
1295
+ stanzaProtocollo() {
1296
+ const dati = this.richiediDati();
1297
+ return {
1298
+ id: dati.id,
1299
+ seed: seedStanza(dati.id),
1300
+ status: dati.status,
1301
+ mode: dati.mode,
1302
+ tick: dati.tick,
1303
+ tickRate: dati.tickRate,
1304
+ serverTime: this.adattatore.ora(),
1305
+ host: dati.hostId,
1306
+ countdownAt: dati.countdownAt,
1307
+ configuration: {
1308
+ players: { ...this.configurazione.players },
1309
+ lobby: this.configurazione.lobby,
1310
+ persistent: this.manifest.persistent === true,
1311
+ requestRole: this.definizione.onRoleRequest !== void 0
1312
+ }
1313
+ };
1314
+ }
1203
1315
  inviaGiocatori() {
1204
- this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
1316
+ this.broadcast({ t: "players", players: this.giocatoriProtocollo(), host: this.richiediDati().hostId });
1205
1317
  }
1206
1318
  inviaStatus(at) {
1207
1319
  const dati = this.richiediDati();
1208
1320
  this.broadcast({
1209
1321
  t: "status",
1210
1322
  status: dati.status,
1323
+ host: dati.hostId,
1324
+ countdownAt: dati.countdownAt,
1211
1325
  at,
1212
1326
  result: dati.status === "ended" ? dati.result : null
1213
1327
  });
1214
1328
  }
1329
+ inviaDiff() {
1330
+ const dati = this.richiediDati();
1331
+ const patch = creaDiff(dati.statoSincronizzato, dati.state);
1332
+ if (patch.length === 0) return;
1333
+ this.broadcast({
1334
+ t: "state",
1335
+ tick: dati.tick,
1336
+ base: dati.tickSincronizzato,
1337
+ serverTime: this.adattatore.ora(),
1338
+ patch
1339
+ });
1340
+ dati.statoSincronizzato = copiaJson(dati.state);
1341
+ dati.tickSincronizzato = dati.tick;
1342
+ }
1215
1343
  inviaSnapshotTutti() {
1216
1344
  const dati = this.richiediDati();
1217
1345
  const stato = analizzaJson(dati.state);
@@ -1266,7 +1394,7 @@ var NucleoStanza = class _NucleoStanza {
1266
1394
  this.dati.state = stato.valore;
1267
1395
  if ((this.dati.tickRate === 0 || this.dati.status !== "playing") && stato.testo !== precedente) {
1268
1396
  this.dati.tick++;
1269
- this.inviaSnapshotTutti();
1397
+ this.inviaDiff();
1270
1398
  }
1271
1399
  }
1272
1400
  async verificaStato() {
@@ -1300,6 +1428,7 @@ var NucleoStanza = class _NucleoStanza {
1300
1428
  dati.voceGuadagni ??= {};
1301
1429
  const precedente = dati.voceGuadagni[playerId]?.[altroId] ?? 1;
1302
1430
  if (precedente === valore) return;
1431
+ if (precedente > 0 && valore === 0) this.rivediVoce();
1303
1432
  this.voceDaPersistire = true;
1304
1433
  if (valore === 1) {
1305
1434
  const riga = dati.voceGuadagni[playerId];
@@ -1317,6 +1446,9 @@ var NucleoStanza = class _NucleoStanza {
1317
1446
  this.voceGuadagniCambiati.set(playerId, cambi);
1318
1447
  return cambi;
1319
1448
  }
1449
+ rivediVoce() {
1450
+ if ((this.manifest.voice ?? "none") !== "none") this.adattatore.rivediVoce();
1451
+ }
1320
1452
  inviaGuadagniCambiati() {
1321
1453
  if (this.dati === null || this.voceGuadagniCambiati.size === 0) return;
1322
1454
  for (const [playerId, gains] of this.voceGuadagniCambiati) {
@@ -1364,7 +1496,7 @@ var NucleoStanza = class _NucleoStanza {
1364
1496
  } else if (stato.testo !== JSON.stringify(dati.statoSincronizzato)) {
1365
1497
  dati.tick++;
1366
1498
  dati.state = stato.valore;
1367
- this.inviaSnapshotTutti();
1499
+ this.inviaDiff();
1368
1500
  }
1369
1501
  const fine = { result: dati.result, at: dati.resultAt };
1370
1502
  dati.fineInCoda = fine;
@@ -1479,9 +1611,11 @@ var AdattatoreFacet = class {
1479
1611
  this.connessioni = /* @__PURE__ */ new Set();
1480
1612
  this.cpuMessaggiOltreCento = 0;
1481
1613
  this.frames = [];
1614
+ this.pubblici = [];
1482
1615
  this.chiusure = [];
1483
1616
  this.sveglia = null;
1484
1617
  this.tick = false;
1618
+ this.voceRivedi = false;
1485
1619
  this.salvato = "";
1486
1620
  this.storage = new ArchivioFacet(archivio);
1487
1621
  this.tickRate = tickRate;
@@ -1509,6 +1643,9 @@ var AdattatoreFacet = class {
1509
1643
  invia(connessione, messaggio) {
1510
1644
  this.frames.push({ connessione, testo: JSON.stringify(messaggio) });
1511
1645
  }
1646
+ pubblica(messaggio) {
1647
+ this.pubblici.push(JSON.stringify(messaggio));
1648
+ }
1512
1649
  chiudi(connessione, codice, motivo) {
1513
1650
  this.chiusure.push({ connessione, codice, motivo });
1514
1651
  this.connessioni.delete(connessione);
@@ -1522,6 +1659,9 @@ var AdattatoreFacet = class {
1522
1659
  misuraCpu() {
1523
1660
  return performance.now();
1524
1661
  }
1662
+ rivediVoce() {
1663
+ this.voceRivedi = true;
1664
+ }
1525
1665
  programmaTick(intervalloMs) {
1526
1666
  this.tick = intervalloMs !== null;
1527
1667
  if (intervalloMs !== null) this.tickRate = 1e3 / intervalloMs;
@@ -1532,14 +1672,18 @@ var AdattatoreFacet = class {
1532
1672
  esito(status) {
1533
1673
  const risultato = {
1534
1674
  frames: this.frames,
1675
+ pubblici: this.pubblici,
1535
1676
  chiusure: this.chiusure,
1536
1677
  sveglia: this.sveglia,
1537
1678
  tick: this.tick,
1538
1679
  tickRate: this.tickRate,
1539
- status
1680
+ status,
1681
+ voceRivedi: this.voceRivedi
1540
1682
  };
1541
1683
  this.frames = [];
1684
+ this.pubblici = [];
1542
1685
  this.chiusure = [];
1686
+ this.voceRivedi = false;
1543
1687
  return risultato;
1544
1688
  }
1545
1689
  };
@@ -1599,9 +1743,15 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
1599
1743
  info(playerId) {
1600
1744
  return this.#serializza((nucleo) => nucleo.info(playerId));
1601
1745
  }
1746
+ fotografia() {
1747
+ return this.#serializza((nucleo) => nucleo.fotografia());
1748
+ }
1602
1749
  puoEntrare(identita) {
1603
1750
  return this.#serializza((nucleo) => nucleo.puoEntrare(identita));
1604
1751
  }
1752
+ puoAscoltare(listener, speakers) {
1753
+ return this.#serializza((nucleo) => nucleo.puoAscoltare(listener, speakers));
1754
+ }
1605
1755
  entra(identita, connessione) {
1606
1756
  return this.#esegui(async (nucleo) => {
1607
1757
  this.#adattatore.connessioni.add(connessione);
package/dist/server.d.ts CHANGED
@@ -1,71 +1 @@
1
- export type JsonValue = null | boolean | number | string | JsonValue[] | {
2
- [key: string]: JsonValue;
3
- };
4
- export type RoomStatus = 'lobby' | 'countdown' | 'playing' | 'ended';
5
- export type VoiceMode = 'none' | 'room' | 'team' | 'proximity';
6
- export type LeaveReason = 'left' | 'timeout' | 'kicked';
7
- export interface SharedGameStore {
8
- get(key: string): Promise<JsonValue | null>;
9
- set(key: string, value: JsonValue): Promise<void>;
10
- delete(key: string): Promise<void>;
11
- list(prefix?: string): Promise<string[]>;
12
- increment(key: string, amount?: number): Promise<number>;
13
- }
14
- export interface Player {
15
- readonly id: string;
16
- readonly name: string;
17
- readonly guest: boolean;
18
- readonly role: string | null;
19
- readonly team: number | null;
20
- readonly connected: boolean;
21
- }
22
- export interface Room<State extends JsonValue = JsonValue> {
23
- readonly id: string;
24
- readonly seed: number;
25
- readonly mode: string | null;
26
- readonly status: RoomStatus;
27
- readonly tick: number;
28
- readonly tickRate: number;
29
- readonly result: JsonValue | null;
30
- state: State;
31
- readonly players: readonly Player[];
32
- readonly host: Player | null;
33
- broadcast(message: JsonValue): void;
34
- send(player: Player | string, message: JsonValue): void;
35
- kick(player: Player | string): void;
36
- setRole(player: Player | string, role: string): void;
37
- setTeam(player: Player | string, team: number): void;
38
- end(result: JsonValue): void;
39
- save(key: string, value: JsonValue): Promise<void>;
40
- load(key: string): Promise<JsonValue | null>;
41
- readonly shared: SharedGameStore;
42
- schedule(milliseconds: number, handler: string, payload: JsonValue): void;
43
- readonly board: {
44
- submit(player: Player | string, board: string, score: number, options?: {
45
- daily?: boolean;
46
- }): void;
47
- };
48
- readonly daily: {
49
- readonly day: string;
50
- readonly seed: number;
51
- };
52
- readonly time: {
53
- now(): number;
54
- };
55
- readonly voice: {
56
- readonly mode: VoiceMode;
57
- setGain(listener: Player | string, speaker: Player | string, gain: number): void;
58
- setProximity(a: Player | string, b: Player | string, gain: number): void;
59
- };
60
- }
61
- export interface GameDefinition<State extends JsonValue = JsonValue> {
62
- tickRate: number;
63
- onCreate?(room: Room<State>): void | Promise<void>;
64
- onStart?(room: Room<State>): void | Promise<void>;
65
- onJoin?(room: Room<State>, player: Player): void | Promise<void>;
66
- onLeave?(room: Room<State>, player: Player, reason: LeaveReason): void | Promise<void>;
67
- onMessage?(room: Room<State>, player: Player, message: JsonValue): void | Promise<void>;
68
- onTick?(room: Room<State>, deltaSeconds: number): void | Promise<void>;
69
- onEnd?(room: Room<State>): void | Promise<void>;
70
- }
71
- export declare function defineGame<State extends JsonValue = JsonValue, const Definition extends GameDefinition<State> = GameDefinition<State>>(definition: Definition): Definition;
1
+ export * from './types/server/index.js';
package/dist/server.js CHANGED
@@ -6,6 +6,7 @@ var CALLBACKS = [
6
6
  "onJoin",
7
7
  "onLeave",
8
8
  "onMessage",
9
+ "onRoleRequest",
9
10
  "onTick",
10
11
  "onEnd"
11
12
  ];
@@ -0,0 +1,25 @@
1
+ import type { BoardResult, BoardTop, JsonValue, Player, SaveInfo } from './index.js';
2
+ import type { GestoreBiglietto } from './handshake.js';
3
+ export interface RispostaMe {
4
+ player: Player;
5
+ game: {
6
+ slug: string;
7
+ };
8
+ day: string;
9
+ seed: number;
10
+ serverTime: number;
11
+ }
12
+ export declare function creaClienteApi(appOrigin: string, fetcher: typeof fetch, biglietto: GestoreBiglietto): {
13
+ me: () => Promise<RispostaMe>;
14
+ saveSet: (key: string, value: JsonValue) => Promise<SaveInfo>;
15
+ saveGet(key: string): Promise<JsonValue | null>;
16
+ saveRemove(key: string): Promise<void>;
17
+ saveList(): Promise<SaveInfo[]>;
18
+ boardSubmit(board: string, score: number, daily: boolean): Promise<BoardResult>;
19
+ boardTop(board: string, opzioni: {
20
+ day?: string;
21
+ daily?: boolean;
22
+ limit?: number;
23
+ guests?: boolean;
24
+ }): Promise<BoardTop>;
25
+ };
@@ -0,0 +1,24 @@
1
+ export type DestinatarioBiglietto = 'portal' | 'live';
2
+ export interface Biglietto {
3
+ sub: string;
4
+ game: string;
5
+ name: string;
6
+ guest: boolean;
7
+ aud: DestinatarioBiglietto;
8
+ iat: number;
9
+ exp: number;
10
+ }
11
+ export interface DatiBiglietto {
12
+ sub: string;
13
+ game: string;
14
+ name: string;
15
+ guest: boolean;
16
+ aud: DestinatarioBiglietto;
17
+ }
18
+ export declare function firmaBiglietto(dati: DatiBiglietto, secret: string, now?: Date): Promise<string>;
19
+ /** Restituisce null senza distinguere quale controllo ha rifiutato il gettone. */
20
+ export declare function verificaBiglietto(token: string, secret: string, atteso: {
21
+ aud: DestinatarioBiglietto;
22
+ game: string;
23
+ now?: Date;
24
+ }): Promise<Biglietto | null>;