@caisual/kit 0.5.0 → 0.10.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 (58) hide show
  1. package/dist/kit.d.ts +2 -175
  2. package/dist/kit.js +1808 -76
  3. package/dist/node.d.ts +1 -88
  4. package/dist/node.js +354 -96
  5. package/dist/overlay.d.ts +1 -0
  6. package/dist/overlay.js +2074 -0
  7. package/dist/runtime.d.ts +1 -111
  8. package/dist/runtime.js +335 -67
  9. package/dist/server.d.ts +1 -71
  10. package/dist/server.js +3 -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/i18n.d.ts +12 -0
  16. package/dist/types/contracts/index.d.ts +13 -0
  17. package/dist/types/contracts/keys.d.ts +59 -0
  18. package/dist/types/contracts/manifest.d.ts +123 -0
  19. package/dist/types/contracts/overlay-boards.d.ts +3 -0
  20. package/dist/types/contracts/overlay.d.ts +223 -0
  21. package/dist/types/contracts/player.d.ts +1 -0
  22. package/dist/types/contracts/room-limits.d.ts +13 -0
  23. package/dist/types/contracts/runtime.d.ts +70 -0
  24. package/dist/types/contracts/server-js.d.ts +11 -0
  25. package/dist/types/contracts/slug.d.ts +5 -0
  26. package/dist/types/contracts/token-crew.d.ts +21 -0
  27. package/dist/types/daily.d.ts +3 -0
  28. package/dist/types/errors.d.ts +6 -0
  29. package/dist/types/handshake.d.ts +30 -0
  30. package/dist/types/http.d.ts +3 -0
  31. package/dist/types/index.d.ts +231 -0
  32. package/dist/types/kit.d.ts +28 -0
  33. package/dist/types/node/index.d.ts +97 -0
  34. package/dist/types/node/websocket.d.ts +29 -0
  35. package/dist/types/overlay/boards.d.ts +48 -0
  36. package/dist/types/overlay/bridge.d.ts +3 -0
  37. package/dist/types/overlay/host-bridge.d.ts +74 -0
  38. package/dist/types/overlay/i18n.d.ts +128 -0
  39. package/dist/types/overlay/index.d.ts +10 -0
  40. package/dist/types/overlay/shortcut.d.ts +4 -0
  41. package/dist/types/overlay/styles.d.ts +1 -0
  42. package/dist/types/overlay/ui-model.d.ts +45 -0
  43. package/dist/types/overlay/ui.d.ts +80 -0
  44. package/dist/types/overlay/voice-panel.d.ts +13 -0
  45. package/dist/types/protocollo.d.ts +262 -0
  46. package/dist/types/runtime/index.d.ts +128 -0
  47. package/dist/types/server/index.d.ts +76 -0
  48. package/dist/types/session/index.d.ts +49 -0
  49. package/dist/types/session/resume.d.ts +13 -0
  50. package/dist/types/standalone.d.ts +14 -0
  51. package/dist/types/stanza/index.d.ts +3 -0
  52. package/dist/types/stanza/json.d.ts +18 -0
  53. package/dist/types/stanza/nucleo.d.ts +100 -0
  54. package/dist/types/stanza/tipi.d.ts +136 -0
  55. package/dist/types/stanza-client/index.d.ts +24 -0
  56. package/dist/types/text.d.ts +2 -0
  57. package/dist/types/voce/index.d.ts +124 -0
  58. package/package.json +10 -2
package/dist/node.js CHANGED
@@ -1,9 +1,3 @@
1
- // src/node/index.ts
2
- import { randomUUID } from "node:crypto";
3
- import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
4
- import { dirname } from "node:path";
5
- import { performance } from "node:perf_hooks";
6
-
7
1
  // ../contracts/src/slug.ts
8
2
  var NOMI_RISERVATI = [
9
3
  "www",
@@ -40,9 +34,76 @@ var NOMI_RISERVATI = [
40
34
  var RISERVATI = new Set(NOMI_RISERVATI);
41
35
 
42
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
+ }
43
45
  var MASSIMO_SPETTATORI = 100;
44
46
  var RITARDO_SPETTATORI_MS = 3e3;
45
47
 
48
+ // ../contracts/src/overlay.ts
49
+ function validBoardDay(value) {
50
+ if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
51
+ const at = Date.parse(`${value}T00:00:00Z`);
52
+ return Number.isFinite(at) && new Date(at).toISOString().slice(0, 10) === value;
53
+ }
54
+
55
+ // ../contracts/src/room-limits.ts
56
+ var MESSAGGI_GIOCO_AL_SECONDO = 20;
57
+ var MESSAGGI_SERVIZIO_AL_SECONDO = 20;
58
+ var LimiteMessaggiStanza = class {
59
+ connessioni = /* @__PURE__ */ new Map();
60
+ delete(connessione) {
61
+ this.connessioni.delete(connessione);
62
+ }
63
+ clear() {
64
+ this.connessioni.clear();
65
+ }
66
+ controlla(connessione, gioco, ora) {
67
+ let stato = this.connessioni.get(connessione);
68
+ if (stato === void 0) {
69
+ const budget2 = () => ({ accettati: [], finestra: ora, tentativi: 0, finestreAbusive: 0 });
70
+ stato = { gioco: budget2(), servizio: budget2(), ultimoErrore: -Infinity };
71
+ this.connessioni.set(connessione, stato);
72
+ }
73
+ const budget = gioco ? stato.gioco : stato.servizio;
74
+ const limite = gioco ? MESSAGGI_GIOCO_AL_SECONDO : MESSAGGI_SERVIZIO_AL_SECONDO;
75
+ if (ora - budget.finestra >= 1e3) {
76
+ budget.finestreAbusive = ora - budget.finestra < 2e3 && budget.tentativi > limite * 5 ? budget.finestreAbusive + 1 : 0;
77
+ budget.finestra += Math.floor((ora - budget.finestra) / 1e3) * 1e3;
78
+ budget.tentativi = 0;
79
+ }
80
+ budget.tentativi = Math.min(budget.tentativi + 1, limite * 5 + 1);
81
+ budget.accettati = budget.accettati.filter((at) => ora - at < 1e3);
82
+ if (budget.accettati.length < limite) {
83
+ budget.accettati.push(ora);
84
+ return { accetta: true, avvisa: false, chiudi: false };
85
+ }
86
+ const avvisa = ora - stato.ultimoErrore >= 1e3;
87
+ if (avvisa) stato.ultimoErrore = ora;
88
+ return {
89
+ accetta: false,
90
+ avvisa,
91
+ chiudi: budget.finestreAbusive >= 2 && budget.tentativi > limite * 5
92
+ };
93
+ }
94
+ };
95
+
96
+ // src/daily.ts
97
+ function giornoUtc(ora) {
98
+ return new Date(ora).toISOString().slice(0, 10);
99
+ }
100
+
101
+ // src/node/index.ts
102
+ import { randomUUID } from "node:crypto";
103
+ import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
104
+ import { dirname } from "node:path";
105
+ import { performance } from "node:perf_hooks";
106
+
46
107
  // src/stanza/json.ts
47
108
  function isPlainObject(value) {
48
109
  const prototype = Object.getPrototypeOf(value);
@@ -125,7 +186,7 @@ function creaDiff(prima, dopo) {
125
186
  visitaDiff(prima, dopo, [], patch);
126
187
  return patch;
127
188
  }
128
- function giornoUtc(ora) {
189
+ function giornoUtc2(ora) {
129
190
  return new Date(ora).toISOString().slice(0, 10);
130
191
  }
131
192
  var COSTANTI_SHA256 = [
@@ -278,6 +339,7 @@ var LIMITE_DEPOSITO = 64 * 1024;
278
339
  var LIMITE_OPERAZIONI_DEPOSITO = 120;
279
340
  var GRAZIA_MS = 6e4;
280
341
  var STANZA_VUOTA_MS = 5 * 6e4;
342
+ var ATTESA_RIVINCITA_MS = 2 * 6e4;
281
343
  var COUNTDOWN_MS = 3e3;
282
344
  var RIPOSO_TICK_MS = 3e4;
283
345
  var INATTIVITA_MS = 10 * 6e4;
@@ -329,21 +391,22 @@ var NucleoStanza = class _NucleoStanza {
329
391
  this.manifest = manifest;
330
392
  this.adattatore = adattatore;
331
393
  this.dati = null;
332
- this.frequenza = /* @__PURE__ */ new Map();
394
+ this.frequenza = new LimiteMessaggiStanza();
333
395
  this.frequenzaDeposito = [];
334
396
  this.kickRichiesti = /* @__PURE__ */ new Set();
335
397
  this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
336
398
  this.fineRichiesta = null;
337
399
  this.applicandoAzioni = false;
338
400
  this.ultimoStatoOsservato = "";
401
+ this.tickRateSincronizzato = null;
339
402
  this.voceDaPersistire = false;
340
403
  const nucleo = this;
341
404
  const daily = {
342
405
  get day() {
343
- return giornoUtc(nucleo.adattatore.ora());
406
+ return nucleo.giornata();
344
407
  },
345
408
  get seed() {
346
- return seedGiornata(nucleo.manifest.id, giornoUtc(nucleo.adattatore.ora()));
409
+ return seedGiornata(nucleo.manifest.id, nucleo.giornata());
347
410
  }
348
411
  };
349
412
  this.room = {
@@ -397,8 +460,8 @@ var NucleoStanza = class _NucleoStanza {
397
460
  setTeam(player, team) {
398
461
  nucleo.impostaSquadraDalServer(idGiocatore(player), team);
399
462
  },
400
- end(result) {
401
- nucleo.richiediFine(result);
463
+ end(result, options) {
464
+ nucleo.richiediFine(result, options?.rematch === true);
402
465
  },
403
466
  save(key, value) {
404
467
  return nucleo.salva(key, value);
@@ -458,6 +521,7 @@ var NucleoStanza = class _NucleoStanza {
458
521
  const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
459
522
  if (salvato !== void 0) {
460
523
  nucleo.dati = salvato;
524
+ nucleo.tickRateSincronizzato = salvato.tickRate;
461
525
  const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0;
462
526
  salvato.ultimoInputAt ??= adattatore.ora();
463
527
  salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
@@ -485,9 +549,8 @@ var NucleoStanza = class _NucleoStanza {
485
549
  }
486
550
  async crea(id, mode, _creator) {
487
551
  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
- }
552
+ risolviModalita(this.manifest, mode);
553
+ if (modalitaLocale(this.manifest, mode)) throw new Error("Local modes cannot create rooms.");
491
554
  const ora = this.adattatore.ora();
492
555
  this.dati = {
493
556
  versione: 1,
@@ -532,6 +595,9 @@ var NucleoStanza = class _NucleoStanza {
532
595
  await this.persistiEProgramma();
533
596
  return true;
534
597
  }
598
+ get configurazione() {
599
+ return risolviModalita(this.manifest, this.dati?.mode ?? null);
600
+ }
535
601
  info(playerId) {
536
602
  if (this.dati === null) return null;
537
603
  return {
@@ -540,7 +606,7 @@ var NucleoStanza = class _NucleoStanza {
540
606
  players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
541
607
  (player) => player.connected || player.graziaFinoA !== null
542
608
  ).length,
543
- max: this.manifest.players.max,
609
+ max: this.configurazione.players.max,
544
610
  mode: this.dati.mode,
545
611
  member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
546
612
  };
@@ -559,15 +625,34 @@ var NucleoStanza = class _NucleoStanza {
559
625
  );
560
626
  return player === void 0 ? null : copiaGiocatore(player);
561
627
  }
628
+ puoAscoltare(listener, speakers) {
629
+ const mode = this.manifest.voice ?? "none";
630
+ if (mode === "none") return speakers.map(() => ({ ok: false, reason: "mode" }));
631
+ const dati = this.richiediDati();
632
+ const ascoltatore = dati.giocatori.find((player) => player.id === listener);
633
+ return speakers.map((speakerId) => {
634
+ const speaker = dati.giocatori.find((player) => player.id === speakerId);
635
+ if (ascoltatore === void 0 || speaker === void 0 || listener === speakerId) {
636
+ return { ok: false, reason: "unknown" };
637
+ }
638
+ if (mode === "team" && ascoltatore.role !== "spectator" && ascoltatore.team !== speaker.team) {
639
+ return { ok: false, reason: "team" };
640
+ }
641
+ if ((dati.voceGuadagni?.[listener]?.[speakerId] ?? 1) <= 0) {
642
+ return { ok: false, reason: "gain" };
643
+ }
644
+ return { ok: true };
645
+ });
646
+ }
562
647
  puoEntrare(identity) {
563
648
  if (this.dati === null) return { ok: false, code: "room_not_found" };
564
649
  if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
565
650
  const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
566
651
  if (esistente !== void 0) return { ok: true };
567
- if (this.manifest.lobby && this.dati.status !== "lobby") {
652
+ if (this.configurazione.lobby && !["lobby", "finished"].includes(this.dati.status)) {
568
653
  return { ok: false, code: "room_playing" };
569
654
  }
570
- if (this.dati.giocatori.length >= this.manifest.players.max) {
655
+ if (this.dati.giocatori.length >= this.configurazione.players.max) {
571
656
  return { ok: false, code: "room_full" };
572
657
  }
573
658
  return { ok: true };
@@ -583,6 +668,7 @@ var NucleoStanza = class _NucleoStanza {
583
668
  const ora = this.adattatore.ora();
584
669
  let player = dati.giocatori.find((item) => item.id === identity.id);
585
670
  const nuovo = player === void 0;
671
+ const riconnesso = player !== void 0 && !player.connected;
586
672
  if (player === void 0) {
587
673
  player = {
588
674
  ...identity,
@@ -598,6 +684,7 @@ var NucleoStanza = class _NucleoStanza {
598
684
  dati.giocatori.push(player);
599
685
  } else {
600
686
  if (player.connected && player.connessione !== null && player.connessione !== connessione) {
687
+ this.frequenza.delete(player.connessione);
601
688
  this.adattatore.chiudi(player.connessione, 4006, "replaced");
602
689
  }
603
690
  player.name = identity.name;
@@ -610,7 +697,7 @@ var NucleoStanza = class _NucleoStanza {
610
697
  dati.hostId ??= player.id;
611
698
  dati.vuotaDa = null;
612
699
  dati.ultimoInputAt = ora;
613
- const primaConnessione = !this.manifest.lobby && dati.status === "lobby";
700
+ const primaConnessione = !this.configurazione.lobby && dati.status === "lobby";
614
701
  if (primaConnessione) dati.status = "playing";
615
702
  if (nuovo) {
616
703
  await this.chiama(
@@ -619,6 +706,9 @@ var NucleoStanza = class _NucleoStanza {
619
706
  copiaGiocatore(player)
620
707
  );
621
708
  }
709
+ if (riconnesso) {
710
+ await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), true);
711
+ }
622
712
  if (primaConnessione) {
623
713
  await this.chiama(this.definizione.onStart, this.room);
624
714
  this.inviaStatus(ora);
@@ -641,14 +731,16 @@ var NucleoStanza = class _NucleoStanza {
641
731
  player.connected = false;
642
732
  player.connessione = null;
643
733
  player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
644
- if (this.manifest.persistent === true && this.dati.status === "lobby") player.ready = false;
734
+ if (this.dati.status === "finished" || this.manifest.persistent === true && this.dati.status === "lobby") player.ready = false;
645
735
  this.frequenza.delete(connessione);
646
736
  if (this.dati.hostId === player.id) this.assegnaHost();
647
737
  this.verificaCountdown();
738
+ await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
739
+ await this.concludiEvento();
648
740
  this.inviaGiocatori();
649
741
  await this.persistiEProgramma();
650
742
  }
651
- async ricevi(connessione, frame) {
743
+ async ricevi(connessione, frame, limiteVerificato = false) {
652
744
  if (this.dati === null || this.dati.status === "ended") return;
653
745
  if (await this.terminaSeInattiva()) return;
654
746
  const player = this.dati.giocatori.find(
@@ -656,17 +748,10 @@ var NucleoStanza = class _NucleoStanza {
656
748
  );
657
749
  if (player === void 0) return;
658
750
  if (new TextEncoder().encode(frame).byteLength > LIMITE_FRAME) {
659
- await this.chiudiConnessione(player, 4009, "bad_message");
751
+ await this.chiudiConnessione(player, 4009, "message_too_large");
660
752
  return;
661
753
  }
662
754
  const ora = this.adattatore.ora();
663
- const recenti = (this.frequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
664
- if (recenti.length >= 20) {
665
- await this.chiudiConnessione(player, 4008, "rate_limited");
666
- return;
667
- }
668
- recenti.push(ora);
669
- this.frequenza.set(connessione, recenti);
670
755
  let message = null;
671
756
  try {
672
757
  message = record(JSON.parse(frame));
@@ -676,6 +761,14 @@ var NucleoStanza = class _NucleoStanza {
676
761
  await this.chiudiConnessione(player, 4009, "bad_message");
677
762
  return;
678
763
  }
764
+ if (!limiteVerificato) {
765
+ const limite = this.frequenza.controlla(connessione, message.t === "msg", ora);
766
+ if (!limite.accetta) {
767
+ if (limite.avvisa) this.inviaErrore(player, "rate_limited", "Too many room messages. Excess messages are dropped.");
768
+ if (limite.chiudi) await this.chiudiConnessione(player, 4008, "rate_limited");
769
+ return;
770
+ }
771
+ }
679
772
  if (message.t === "ping") {
680
773
  if (typeof message.c !== "number" || !Number.isFinite(message.c)) {
681
774
  await this.chiudiConnessione(player, 4009, "bad_message");
@@ -698,6 +791,10 @@ var NucleoStanza = class _NucleoStanza {
698
791
  await this.persistiEProgramma();
699
792
  return;
700
793
  }
794
+ if (message.t === "restart") {
795
+ await this.rivincita(player);
796
+ return;
797
+ }
701
798
  if (message.t === "ready") {
702
799
  if (typeof message.ready !== "boolean") return this.messaggioErrato(player);
703
800
  if (!this.inLobby(player)) return;
@@ -716,6 +813,26 @@ var NucleoStanza = class _NucleoStanza {
716
813
  await this.persistiEProgramma();
717
814
  return;
718
815
  }
816
+ if (message.t === "request-role") {
817
+ if (!Number.isSafeInteger(message.r) || message.r < 1 || typeof message.role !== "string") return this.messaggioErrato(player);
818
+ let code = null;
819
+ if (this.dati.status !== "playing" || this.definizione.onRoleRequest === void 0) code = "role_change_unavailable";
820
+ else if (!this.manifest.roles.some((role) => role.id === message.role)) code = "invalid_role";
821
+ else {
822
+ this.dati.ultimoInputAt = ora;
823
+ await this.chiama(this.definizione.onRoleRequest, this.room, copiaGiocatore(player), message.role);
824
+ await this.concludiEvento();
825
+ if (player.role !== message.role) code = "role_change_refused";
826
+ }
827
+ if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
828
+ t: "role-result",
829
+ r: message.r,
830
+ ok: code === null,
831
+ code
832
+ });
833
+ await this.persistiEProgramma();
834
+ return;
835
+ }
719
836
  if (message.t === "team") {
720
837
  if (!Number.isInteger(message.team)) return this.messaggioErrato(player);
721
838
  if (!this.inLobby(player)) return;
@@ -749,6 +866,7 @@ var NucleoStanza = class _NucleoStanza {
749
866
  await this.persistiEProgramma();
750
867
  return;
751
868
  }
869
+ if (this.dati.status === "finished") return;
752
870
  this.dati.ultimoInputAt = ora;
753
871
  await this.chiama(
754
872
  this.definizione.onMessage,
@@ -784,23 +902,8 @@ var NucleoStanza = class _NucleoStanza {
784
902
  this.inviaGuadagniCambiati();
785
903
  const stato = await this.verificaStato();
786
904
  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
- }
905
+ dati.state = stato.valore;
906
+ this.inviaDiff();
804
907
  if (dati.tick % 100 === 0 || this.voceDaPersistire) await this.persisti();
805
908
  await this.aggiornaProgrammazione();
806
909
  }
@@ -834,7 +937,11 @@ var NucleoStanza = class _NucleoStanza {
834
937
  if (dovuti.length > 0) {
835
938
  const ids = new Set(dovuti.map((timer) => timer.id));
836
939
  this.dati.timer = this.dati.timer.filter((timer) => !ids.has(timer.id));
837
- for (const timer of dovuti) await this.eseguiTimer(timer);
940
+ for (const timer of dovuti) {
941
+ await this.eseguiTimer(timer);
942
+ await this.applicaAzioni();
943
+ if (this.dati.status === "finished" || this.stanzaTerminata()) break;
944
+ }
838
945
  }
839
946
  await this.concludiEvento();
840
947
  if (!this.stanzaTerminata() && this.dati.giocatori.length === 0 && this.dati.vuotaDa !== null && this.dati.vuotaDa + STANZA_VUOTA_MS <= ora) {
@@ -865,22 +972,30 @@ var NucleoStanza = class _NucleoStanza {
865
972
  return esito;
866
973
  }
867
974
  async riconciliaConnessioni() {
975
+ if (this.dati === null || this.dati.status === "ended") return;
868
976
  const dati = this.richiediDati();
869
977
  const attive = new Set(this.adattatore.connessioniAttive());
870
978
  const ora = this.adattatore.ora();
871
- let cambiato = false;
979
+ const disconnessi = [];
872
980
  for (const player of dati.giocatori) {
873
981
  if (player.connected && (player.connessione === null || !attive.has(player.connessione))) {
874
982
  player.connected = false;
983
+ if (player.connessione !== null) this.frequenza.delete(player.connessione);
875
984
  player.connessione = null;
876
985
  player.graziaFinoA = ora + GRAZIA_MS;
877
- if (this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
878
- cambiato = true;
986
+ if (dati.status === "finished" || this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
987
+ disconnessi.push(player);
879
988
  }
880
989
  }
881
- if (cambiato) {
990
+ if (disconnessi.length > 0) {
882
991
  this.assegnaHost();
883
- await this.persisti();
992
+ this.verificaCountdown();
993
+ for (const player of disconnessi) {
994
+ await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
995
+ }
996
+ await this.concludiEvento();
997
+ this.inviaGiocatori();
998
+ await this.persistiEProgramma();
884
999
  }
885
1000
  }
886
1001
  ruoloAutomatico() {
@@ -929,10 +1044,15 @@ var NucleoStanza = class _NucleoStanza {
929
1044
  if (ruolo?.max !== void 0 && occupati >= ruolo.max) {
930
1045
  return { code: "role_full", message: "This role is full." };
931
1046
  }
1047
+ const ruoloPrecedente = player.role;
1048
+ const squadraPrecedente = player.team;
932
1049
  player.role = roleId;
933
1050
  if (roleId === "spectator") player.team = null;
934
1051
  else if (player.team === null) player.team = this.squadraAutomatica();
935
1052
  if (this.richiediDati().status === "lobby") player.ready = false;
1053
+ if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
1054
+ this.rivediVoce();
1055
+ }
936
1056
  this.inviaGiocatori();
937
1057
  return null;
938
1058
  }
@@ -943,8 +1063,10 @@ var NucleoStanza = class _NucleoStanza {
943
1063
  if (player.role === "spectator") {
944
1064
  return { code: "spectator", message: "Spectators cannot join a team." };
945
1065
  }
1066
+ const squadraPrecedente = player.team;
946
1067
  player.team = team;
947
1068
  if (this.richiediDati().status === "lobby") player.ready = false;
1069
+ if (player.team !== squadraPrecedente) this.rivediVoce();
948
1070
  this.inviaGiocatori();
949
1071
  return null;
950
1072
  }
@@ -968,7 +1090,7 @@ var NucleoStanza = class _NucleoStanza {
968
1090
  erroreMinimi() {
969
1091
  const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
970
1092
  const attivi = connessi.filter((player) => player.role !== "spectator");
971
- if (attivi.length < this.manifest.players.min) {
1093
+ if (attivi.length < this.configurazione.players.min) {
972
1094
  return { code: "not_enough_players", message: "The room does not have enough players." };
973
1095
  }
974
1096
  if (connessi.some((player) => !player.ready)) {
@@ -987,6 +1109,43 @@ var NucleoStanza = class _NucleoStanza {
987
1109
  }
988
1110
  return null;
989
1111
  }
1112
+ async rivincita(player) {
1113
+ const dati = this.richiediDati();
1114
+ if (dati.status !== "finished") {
1115
+ this.inviaErrore(player, "rematch_unavailable", "This room is not waiting for a rematch.");
1116
+ return;
1117
+ }
1118
+ if (player.role !== "spectator" && !player.ready) {
1119
+ player.ready = true;
1120
+ this.inviaGiocatori();
1121
+ await this.persistiEProgramma();
1122
+ return;
1123
+ }
1124
+ if (dati.hostId !== player.id) return;
1125
+ const attivi = dati.giocatori.filter((item) => item.connected && item.role !== "spectator");
1126
+ if (attivi.length < this.configurazione.players.min || attivi.some((item) => !item.ready)) {
1127
+ this.inviaErrore(player, "players_not_ready", "Enough connected players must be ready for the rematch.");
1128
+ return;
1129
+ }
1130
+ dati.status = this.configurazione.lobby ? "lobby" : "playing";
1131
+ dati.rivincitaFinoA = null;
1132
+ dati.result = null;
1133
+ dati.resultAt = null;
1134
+ dati.ultimoInputAt = this.adattatore.ora();
1135
+ for (const item of dati.giocatori) item.ready = false;
1136
+ await this.chiama(this.definizione.onRestart, this.room);
1137
+ await this.applicaAzioni();
1138
+ if (dati.status === "playing") {
1139
+ await this.chiama(this.definizione.onStart, this.room);
1140
+ }
1141
+ await this.concludiEvento();
1142
+ if (dati.status === "lobby" || dati.status === "playing") {
1143
+ this.inviaSnapshotTutti();
1144
+ this.inviaGiocatori();
1145
+ this.inviaStatus(this.adattatore.ora());
1146
+ }
1147
+ await this.persistiEProgramma();
1148
+ }
990
1149
  async avviaCountdown(player) {
991
1150
  const dati = this.richiediDati();
992
1151
  if (dati.hostId !== player.id) {
@@ -1023,6 +1182,8 @@ var NucleoStanza = class _NucleoStanza {
1023
1182
  const indice = dati.giocatori.findIndex((item) => item.id === player.id);
1024
1183
  if (indice < 0) return;
1025
1184
  dati.giocatori.splice(indice, 1);
1185
+ if (player.connessione !== null) this.frequenza.delete(player.connessione);
1186
+ this.rivediVoce();
1026
1187
  this.pulisciGuadagni(player.id);
1027
1188
  if (dati.hostId === player.id) this.assegnaHost();
1028
1189
  if (dati.giocatori.length === 0) dati.vuotaDa = this.adattatore.ora();
@@ -1061,6 +1222,7 @@ var NucleoStanza = class _NucleoStanza {
1061
1222
  const json = analizzaJson(payload);
1062
1223
  if (!json.ok) throw new TypeError("Schedule payload must be valid JSON.");
1063
1224
  const dati = this.richiediDati();
1225
+ if (dati.status === "finished" || dati.status === "ended") return;
1064
1226
  dati.timer.push({
1065
1227
  id: dati.prossimoTimerId++,
1066
1228
  at: this.adattatore.ora() + milliseconds,
@@ -1068,8 +1230,11 @@ var NucleoStanza = class _NucleoStanza {
1068
1230
  payload: json.valore
1069
1231
  });
1070
1232
  }
1233
+ giornata(ora = this.adattatore.ora()) {
1234
+ return this.adattatore.giorno?.(ora) ?? giornoUtc2(ora);
1235
+ }
1071
1236
  accodaPunteggio(playerId, board, score, daily) {
1072
- if (!this.richiediDati().giocatori.some((player) => player.id === playerId)) {
1237
+ if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
1073
1238
  throw new Error("Player not found.");
1074
1239
  }
1075
1240
  if (!CHIAVE.test(board)) {
@@ -1078,13 +1243,28 @@ var NucleoStanza = class _NucleoStanza {
1078
1243
  if (!Number.isSafeInteger(score) || score < 0) {
1079
1244
  throw new TypeError("Score must be a non-negative safe integer.");
1080
1245
  }
1081
- this.richiediDati().punteggi.push({ playerId, board, score, daily });
1246
+ const submittedAt = this.adattatore.ora();
1247
+ const day = daily ? this.giornata(submittedAt) : null;
1248
+ this.richiediDati().punteggi.push({
1249
+ playerId,
1250
+ board,
1251
+ score,
1252
+ daily,
1253
+ submittedAt,
1254
+ day
1255
+ });
1256
+ const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
1257
+ if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
1258
+ t: "score-queued",
1259
+ score: { player: playerId, board, score, day, submittedAt }
1260
+ });
1082
1261
  this.broadcast({ t: "flush" });
1083
1262
  }
1084
- richiediFine(result) {
1263
+ richiediFine(result, rematch = false) {
1085
1264
  const json = analizzaJson(result);
1086
1265
  if (!json.ok) throw new TypeError("Game result must be valid JSON.");
1087
- this.fineRichiesta = json.valore;
1266
+ if (this.dati?.status === "ended" || this.dati?.status === "finished" && rematch) return;
1267
+ this.fineRichiesta = { result: json.valore, rematch };
1088
1268
  }
1089
1269
  async salva(key, value) {
1090
1270
  if (!CHIAVE.test(key)) {
@@ -1251,25 +1431,51 @@ var NucleoStanza = class _NucleoStanza {
1251
1431
  id: dati.id,
1252
1432
  seed: seedStanza(dati.id),
1253
1433
  status: dati.status,
1434
+ result: dati.result,
1254
1435
  mode: dati.mode,
1255
1436
  tick: dati.tick,
1256
1437
  tickRate: dati.tickRate,
1257
1438
  serverTime: this.adattatore.ora(),
1258
- host: dati.hostId
1439
+ host: dati.hostId,
1440
+ countdownAt: dati.countdownAt,
1441
+ configuration: {
1442
+ players: { ...this.configurazione.players },
1443
+ lobby: this.configurazione.lobby,
1444
+ persistent: this.manifest.persistent === true,
1445
+ requestRole: this.definizione.onRoleRequest !== void 0
1446
+ }
1259
1447
  };
1260
1448
  }
1261
1449
  inviaGiocatori() {
1262
- this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
1450
+ this.broadcast({ t: "players", players: this.giocatoriProtocollo(), host: this.richiediDati().hostId });
1263
1451
  }
1264
1452
  inviaStatus(at) {
1265
1453
  const dati = this.richiediDati();
1266
1454
  this.broadcast({
1267
1455
  t: "status",
1268
1456
  status: dati.status,
1457
+ host: dati.hostId,
1458
+ countdownAt: dati.countdownAt,
1269
1459
  at,
1270
- result: dati.status === "ended" ? dati.result : null
1460
+ result: dati.status === "ended" || dati.status === "finished" ? dati.result : null
1271
1461
  });
1272
1462
  }
1463
+ inviaDiff() {
1464
+ const dati = this.richiediDati();
1465
+ const patch = creaDiff(dati.statoSincronizzato, dati.state);
1466
+ if (patch.length === 0 && this.tickRateSincronizzato === dati.tickRate) return;
1467
+ this.tickRateSincronizzato = dati.tickRate;
1468
+ this.broadcast({
1469
+ t: "state",
1470
+ tick: dati.tick,
1471
+ tickRate: dati.tickRate,
1472
+ base: dati.tickSincronizzato,
1473
+ serverTime: this.adattatore.ora(),
1474
+ patch
1475
+ });
1476
+ dati.statoSincronizzato = copiaJson(dati.state);
1477
+ dati.tickSincronizzato = dati.tick;
1478
+ }
1273
1479
  inviaSnapshotTutti() {
1274
1480
  const dati = this.richiediDati();
1275
1481
  const stato = analizzaJson(dati.state);
@@ -1277,8 +1483,10 @@ var NucleoStanza = class _NucleoStanza {
1277
1483
  dati.state = stato.valore;
1278
1484
  dati.statoSincronizzato = copiaJson(stato.valore);
1279
1485
  dati.tickSincronizzato = dati.tick;
1486
+ this.tickRateSincronizzato = dati.tickRate;
1280
1487
  this.broadcast({
1281
1488
  t: "snapshot",
1489
+ tickRate: dati.tickRate,
1282
1490
  tick: dati.tick,
1283
1491
  serverTime: this.adattatore.ora(),
1284
1492
  state: stato.valore
@@ -1289,7 +1497,7 @@ var NucleoStanza = class _NucleoStanza {
1289
1497
  try {
1290
1498
  await callback(...args);
1291
1499
  } catch {
1292
- this.fineRichiesta = { error: "callback_error" };
1500
+ this.fineRichiesta = { result: { error: "callback_error" }, rematch: false };
1293
1501
  }
1294
1502
  }
1295
1503
  async applicaAzioni() {
@@ -1308,7 +1516,7 @@ var NucleoStanza = class _NucleoStanza {
1308
1516
  if (this.fineRichiesta !== null && this.dati.status !== "ended") {
1309
1517
  const result = this.fineRichiesta;
1310
1518
  this.fineRichiesta = null;
1311
- await this.terminaInterna(result);
1519
+ await this.terminaInterna(result.result, result.rematch);
1312
1520
  }
1313
1521
  } finally {
1314
1522
  this.applicandoAzioni = false;
@@ -1324,7 +1532,7 @@ var NucleoStanza = class _NucleoStanza {
1324
1532
  this.dati.state = stato.valore;
1325
1533
  if ((this.dati.tickRate === 0 || this.dati.status !== "playing") && stato.testo !== precedente) {
1326
1534
  this.dati.tick++;
1327
- this.inviaSnapshotTutti();
1535
+ this.inviaDiff();
1328
1536
  }
1329
1537
  }
1330
1538
  async verificaStato() {
@@ -1358,6 +1566,7 @@ var NucleoStanza = class _NucleoStanza {
1358
1566
  dati.voceGuadagni ??= {};
1359
1567
  const precedente = dati.voceGuadagni[playerId]?.[altroId] ?? 1;
1360
1568
  if (precedente === valore) return;
1569
+ if (precedente > 0 && valore === 0) this.rivediVoce();
1361
1570
  this.voceDaPersistire = true;
1362
1571
  if (valore === 1) {
1363
1572
  const riga = dati.voceGuadagni[playerId];
@@ -1375,6 +1584,9 @@ var NucleoStanza = class _NucleoStanza {
1375
1584
  this.voceGuadagniCambiati.set(playerId, cambi);
1376
1585
  return cambi;
1377
1586
  }
1587
+ rivediVoce() {
1588
+ if ((this.manifest.voice ?? "none") !== "none") this.adattatore.rivediVoce();
1589
+ }
1378
1590
  inviaGuadagniCambiati() {
1379
1591
  if (this.dati === null || this.voceGuadagniCambiati.size === 0) return;
1380
1592
  for (const [playerId, gains] of this.voceGuadagniCambiati) {
@@ -1403,33 +1615,42 @@ var NucleoStanza = class _NucleoStanza {
1403
1615
  dati.state = dati.statoSincronizzato;
1404
1616
  await this.terminaInterna({ error: code });
1405
1617
  }
1406
- async terminaInterna(result) {
1618
+ async terminaInterna(result, rematch = false) {
1407
1619
  const dati = this.richiediDati();
1408
1620
  if (dati.status === "ended") return;
1409
- dati.status = "ended";
1621
+ const giaFinita = dati.status === "finished";
1622
+ dati.status = rematch ? "finished" : "ended";
1623
+ dati.rivincitaFinoA = rematch ? this.adattatore.ora() + ATTESA_RIVINCITA_MS : null;
1624
+ dati.timer = [];
1625
+ if (rematch) for (const player of dati.giocatori) player.ready = false;
1410
1626
  dati.countdownAt = null;
1411
1627
  dati.result = result;
1412
1628
  dati.resultAt = this.adattatore.ora();
1413
1629
  try {
1414
- await this.definizione.onEnd?.(this.room);
1630
+ if (!giaFinita) await this.definizione.onEnd?.(this.room);
1415
1631
  } catch {
1416
1632
  if (record(result)?.error === void 0) dati.result = { error: "callback_error" };
1633
+ dati.status = "ended";
1634
+ dati.rivincitaFinoA = null;
1417
1635
  }
1418
1636
  const stato = analizzaJson(dati.state);
1419
1637
  if (!stato.ok || stato.bytes > LIMITE_STATO) {
1420
1638
  dati.state = dati.statoSincronizzato;
1421
1639
  dati.result = { error: stato.ok ? "state_too_large" : "state_invalid" };
1640
+ dati.status = "ended";
1641
+ dati.rivincitaFinoA = null;
1422
1642
  } else if (stato.testo !== JSON.stringify(dati.statoSincronizzato)) {
1423
1643
  dati.tick++;
1424
1644
  dati.state = stato.valore;
1425
- this.inviaSnapshotTutti();
1645
+ this.inviaDiff();
1426
1646
  }
1427
1647
  const fine = { result: dati.result, at: dati.resultAt };
1428
- dati.fineInCoda = fine;
1648
+ if (dati.status === "ended") dati.fineInCoda = fine;
1649
+ if (dati.status === "finished") this.inviaGiocatori();
1429
1650
  this.inviaStatus(dati.resultAt);
1430
1651
  this.broadcast({ t: "flush" });
1431
1652
  for (const player of dati.giocatori) {
1432
- if (player.connected && player.connessione !== null) {
1653
+ if (dati.status === "ended" && player.connected && player.connessione !== null) {
1433
1654
  this.adattatore.chiudi(player.connessione, 4004, "room_ended");
1434
1655
  player.connected = false;
1435
1656
  player.connessione = null;
@@ -1444,7 +1665,7 @@ var NucleoStanza = class _NucleoStanza {
1444
1665
  if (dati.durateCpu.length > 50) dati.durateCpu.shift();
1445
1666
  dati.cpuOltreCento = durata > 100 ? dati.cpuOltreCento + 1 : 0;
1446
1667
  if (dati.cpuOltreCento >= 20) {
1447
- this.fineRichiesta = { error: "cpu_budget" };
1668
+ this.fineRichiesta = { result: { error: "cpu_budget" }, rematch: false };
1448
1669
  return;
1449
1670
  }
1450
1671
  if (dati.durateCpu.length === 50) {
@@ -1475,6 +1696,11 @@ var NucleoStanza = class _NucleoStanza {
1475
1696
  return dati !== null && dati.status === "playing" && dati.tickRate > 0 && dati.giocatori.some((player) => player.connected) && this.adattatore.ora() - Math.max(dati.ultimoInputAt, dati.ultimoCambioStatoAt) < RIPOSO_TICK_MS;
1476
1697
  }
1477
1698
  async terminaSeInattiva() {
1699
+ if (this.dati?.status === "finished") {
1700
+ if (this.adattatore.ora() < (this.dati.rivincitaFinoA ?? 0)) return false;
1701
+ await this.terminaInterna(this.dati.result);
1702
+ return true;
1703
+ }
1478
1704
  if (this.manifest.persistent === true) {
1479
1705
  if (this.dati?.status === "ended" || this.dati === null || this.adattatore.ora() < Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS) return false;
1480
1706
  await this.terminaInterna({ error: "expired" });
@@ -1494,6 +1720,7 @@ var NucleoStanza = class _NucleoStanza {
1494
1720
  this.serveTick() ? 1e3 / this.dati.tickRate : null
1495
1721
  );
1496
1722
  const prossime = [];
1723
+ if (this.dati.status === "finished") prossime.push(this.dati.rivincitaFinoA ?? this.adattatore.ora());
1497
1724
  if (this.manifest.persistent === true) {
1498
1725
  prossime.push(
1499
1726
  Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
@@ -1781,10 +2008,11 @@ var ArchivioNode = class _ArchivioNode {
1781
2008
  }
1782
2009
  };
1783
2010
  var AdattatoreNode = class {
1784
- constructor(storage, deposito, ritardoSpettatori) {
2011
+ constructor(storage, deposito, ritardoSpettatori, dailyDay) {
1785
2012
  this.storage = storage;
1786
2013
  this.deposito = deposito;
1787
2014
  this.ritardoSpettatori = ritardoSpettatori;
2015
+ this.dailyDay = dailyDay;
1788
2016
  this.connessioni = /* @__PURE__ */ new Map();
1789
2017
  this.spettatori = /* @__PURE__ */ new Map();
1790
2018
  this.timerSpettatori = /* @__PURE__ */ new Set();
@@ -1875,9 +2103,15 @@ var AdattatoreNode = class {
1875
2103
  ora() {
1876
2104
  return Date.now();
1877
2105
  }
2106
+ // La giornata di prova non deve spostare scadenze, timer o misure della connessione.
2107
+ giorno(ora) {
2108
+ return this.dailyDay ?? giornoUtc(ora);
2109
+ }
1878
2110
  misuraCpu() {
1879
2111
  return performance.now();
1880
2112
  }
2113
+ rivediVoce() {
2114
+ }
1881
2115
  programmaTick(intervalloMs) {
1882
2116
  if (this.tickIntervallo === intervalloMs) return;
1883
2117
  this.tickIntervallo = intervalloMs;
@@ -1941,7 +2175,7 @@ var StanzaNode = class {
1941
2175
  this.voceListeners = /* @__PURE__ */ new Map();
1942
2176
  this.voceFrequenza = /* @__PURE__ */ new Map();
1943
2177
  this.voceUltimaRichiesta = /* @__PURE__ */ new Map();
1944
- this.frameFrequenza = /* @__PURE__ */ new Map();
2178
+ this.frameFrequenza = new LimiteMessaggiStanza();
1945
2179
  this.connessioniGiocatori = /* @__PURE__ */ new Map();
1946
2180
  this.giocatoriConnessioni = /* @__PURE__ */ new Map();
1947
2181
  adattatore.collega({
@@ -2042,15 +2276,6 @@ var StanzaNode = class {
2042
2276
  const player = this.nucleo.giocatoreConnesso(connessione);
2043
2277
  if (player === null) return;
2044
2278
  const ora = Date.now();
2045
- const frames = (this.frameFrequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
2046
- if (frames.length >= 20) {
2047
- this.adattatore.chiudi(connessione, 4008, "rate_limited");
2048
- this.rimuoviConnessione(connessione);
2049
- await this.nucleo.disconnetti(connessione);
2050
- return;
2051
- }
2052
- frames.push(ora);
2053
- this.frameFrequenza.set(connessione, frames);
2054
2279
  let value;
2055
2280
  try {
2056
2281
  value = JSON.parse(frame);
@@ -2059,17 +2284,31 @@ var StanzaNode = class {
2059
2284
  return;
2060
2285
  }
2061
2286
  const message = typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
2062
- if (message?.t !== "voice") {
2063
- await this.nucleo.ricevi(connessione, frame);
2064
- this.riconciliaRoster();
2065
- return;
2066
- }
2067
- if (Buffer.byteLength(frame, "utf8") > 64 * 1024) {
2068
- this.adattatore.chiudi(connessione, 4009, "bad_message");
2287
+ if (Buffer.byteLength(frame, "utf8") > (message?.t === "voice" ? 64 : 16) * 1024) {
2288
+ this.adattatore.chiudi(connessione, 4009, "message_too_large");
2069
2289
  this.rimuoviConnessione(connessione);
2070
2290
  await this.nucleo.disconnetti(connessione);
2071
2291
  return;
2072
2292
  }
2293
+ const limite = this.frameFrequenza.controlla(connessione, message?.t === "msg", ora);
2294
+ if (!limite.accetta) {
2295
+ if (limite.avvisa) this.adattatore.invia(connessione, {
2296
+ t: "error",
2297
+ code: "rate_limited",
2298
+ message: "Too many room messages. Excess messages are dropped."
2299
+ });
2300
+ if (limite.chiudi) {
2301
+ this.adattatore.chiudi(connessione, 4008, "rate_limited");
2302
+ this.rimuoviConnessione(connessione);
2303
+ await this.nucleo.disconnetti(connessione);
2304
+ }
2305
+ return;
2306
+ }
2307
+ if (message?.t !== "voice") {
2308
+ await this.nucleo.ricevi(connessione, frame, true);
2309
+ this.riconciliaRoster();
2310
+ return;
2311
+ }
2073
2312
  await this.riceviVoce(connessione, player.id, player.role, message);
2074
2313
  }
2075
2314
  permessoSpettatore() {
@@ -2093,19 +2332,24 @@ var StanzaNode = class {
2093
2332
  }
2094
2333
  async riceviSpettatore(connessione, frame) {
2095
2334
  if (Buffer.byteLength(frame, "utf8") > 16 * 1024) {
2096
- this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
2335
+ this.adattatore.chiudiSpettatore(connessione, 4009, "message_too_large");
2097
2336
  this.frameFrequenza.delete(connessione);
2098
2337
  return;
2099
2338
  }
2100
2339
  const ora = Date.now();
2101
- const frames = (this.frameFrequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
2102
- if (frames.length >= 20) {
2103
- this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
2104
- this.frameFrequenza.delete(connessione);
2340
+ const limite = this.frameFrequenza.controlla(connessione, false, ora);
2341
+ if (!limite.accetta) {
2342
+ if (limite.avvisa) this.adattatore.inviaSpettatoreSubito(connessione, JSON.stringify({
2343
+ t: "error",
2344
+ code: "rate_limited",
2345
+ message: "Too many room messages. Excess messages are dropped."
2346
+ }));
2347
+ if (limite.chiudi) {
2348
+ this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
2349
+ this.frameFrequenza.delete(connessione);
2350
+ }
2105
2351
  return;
2106
2352
  }
2107
- frames.push(ora);
2108
- this.frameFrequenza.set(connessione, frames);
2109
2353
  let message = null;
2110
2354
  try {
2111
2355
  const value = JSON.parse(frame);
@@ -2203,6 +2447,19 @@ var StanzaNode = class {
2203
2447
  }
2204
2448
  if (richiesta.op === "signal") {
2205
2449
  const destinazione = this.giocatoriConnessioni.get(richiesta.to);
2450
+ const autorizzazioni = [
2451
+ ...this.nucleo.puoAscoltare(richiesta.to, [playerId]),
2452
+ ...this.nucleo.puoAscoltare(playerId, [richiesta.to])
2453
+ ];
2454
+ if (autorizzazioni.some((esito) => !esito.ok)) {
2455
+ this.inviaErroreVoce(
2456
+ connessione,
2457
+ richiesta,
2458
+ "not_allowed",
2459
+ "The player is not allowed to hear this participant."
2460
+ );
2461
+ return;
2462
+ }
2206
2463
  if (destinazione !== void 0) {
2207
2464
  this.adattatore.invia(destinazione, {
2208
2465
  t: "voice",
@@ -2321,9 +2578,10 @@ var StanzaNode = class {
2321
2578
  }
2322
2579
  };
2323
2580
  async function createNodeRoom(definition, manifest, options = {}) {
2581
+ if (options.dailyDay !== void 0 && !validBoardDay(options.dailyDay)) throw new TypeError("dailyDay must be a real UTC date in YYYY-MM-DD format.");
2324
2582
  const storage = await ArchivioNode.apri(options.storageFile ?? null);
2325
2583
  const ritardoSpettatori = manifest.spectators === null ? null : manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS;
2326
- const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori);
2584
+ const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori, options.dailyDay);
2327
2585
  const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
2328
2586
  return new StanzaNode(nucleo, adattatore, manifest);
2329
2587
  }