@caisual/kit 0.24.0 → 0.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/runtime.js CHANGED
@@ -392,6 +392,7 @@ function seedStanza(roomId) {
392
392
 
393
393
  // src/stanza/nucleo.ts
394
394
  var CHIAVE_NUCLEO = "nucleo";
395
+ var VERSIONE_STATO = 2;
395
396
  var PREFISSO_SAVE = "save:";
396
397
  var LIMITE_FRAME = MASSIMO_BYTE_FRAME_STANZA;
397
398
  var LIMITE_STATO = 512 * 1024;
@@ -400,6 +401,7 @@ var GRAZIA_MS = 6e4;
400
401
  var STANZA_VUOTA_MS = 5 * 6e4;
401
402
  var ATTESA_RIVINCITA_MS = 2 * 6e4;
402
403
  var COUNTDOWN_MS = 3e3;
404
+ var INTERVALLO_SALVATAGGIO_MS = 5e3;
403
405
  var RIPOSO_TICK_MS = 3e4;
404
406
  var INATTIVITA_MS = 10 * 6e4;
405
407
  var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
@@ -451,9 +453,9 @@ var NucleoStanza = class _NucleoStanza {
451
453
  this.fineRichiesta = null;
452
454
  this.applicandoAzioni = false;
453
455
  this.ultimoStatoOsservato = "";
454
- this.tickRateSincronizzato = null;
455
- this.voceDaPersistire = false;
456
+ this.salvataggioImmediato = false;
456
457
  const nucleo = this;
458
+ this.tickRate = definizione.tickRate;
457
459
  const daily = {
458
460
  get day() {
459
461
  return nucleo.richiediDati().daily.day;
@@ -482,7 +484,7 @@ var NucleoStanza = class _NucleoStanza {
482
484
  return nucleo.richiediDati().tick;
483
485
  },
484
486
  get tickRate() {
485
- return nucleo.richiediDati().tickRate;
487
+ return nucleo.tickRate;
486
488
  },
487
489
  get result() {
488
490
  return nucleo.richiediDati().result;
@@ -549,22 +551,13 @@ var NucleoStanza = class _NucleoStanza {
549
551
  const nucleo = new _NucleoStanza(definizione, manifest, adattatore);
550
552
  const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
551
553
  if (salvato !== void 0) {
554
+ if (salvato.versione !== VERSIONE_STATO) throw new Error("The saved room format is unsupported. Create a new room.");
552
555
  if (modalitaLocale(manifest, salvato.mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
553
556
  nucleo.dati = salvato;
554
- nucleo.tickRateSincronizzato = salvato.tickRate;
555
- let daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0 || salvato.origin === void 0;
556
- salvato.origin ??= "player";
557
- if (salvato.origin === "matchmaking" && salvato.status === "lobby") {
558
- salvato.status = "waiting";
559
- daAggiornare = true;
560
- }
561
- salvato.daily ??= nucleo.contestoGiornaliero();
562
- salvato.ultimoInputAt ??= adattatore.ora();
563
- salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
564
557
  nucleo.ultimoStatoOsservato = JSON.stringify(salvato.state);
565
558
  await nucleo.riconciliaConnessioni();
566
559
  if (salvato.origin === "matchmaking") nucleo.rivalutaAvvio();
567
- if (daAggiornare) await nucleo.persisti();
560
+ if (salvato.status === "playing" && nucleo.tickRate > 0) nucleo.inviaSnapshotTutti();
568
561
  }
569
562
  await nucleo.aggiornaProgrammazione();
570
563
  return nucleo;
@@ -589,14 +582,13 @@ var NucleoStanza = class _NucleoStanza {
589
582
  if (modalitaLocale(this.manifest, mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
590
583
  const ora = this.adattatore.ora();
591
584
  this.dati = {
592
- versione: 1,
585
+ versione: VERSIONE_STATO,
593
586
  origin,
594
587
  daily: this.contestoGiornaliero(ora),
595
588
  id,
596
589
  mode,
597
590
  status: origin === "matchmaking" ? "waiting" : "lobby",
598
591
  tick: 0,
599
- tickRate: this.definizione.tickRate,
600
592
  ultimoInputAt: ora,
601
593
  ultimoCambioStatoAt: ora,
602
594
  state: {},
@@ -611,8 +603,6 @@ var NucleoStanza = class _NucleoStanza {
611
603
  cancellaDopoFlush: false,
612
604
  tickSincronizzato: 0,
613
605
  statoSincronizzato: {},
614
- durateCpu: [],
615
- cpuOltreCento: 0,
616
606
  voceGuadagni: {}
617
607
  };
618
608
  await this.chiama(this.definizione.onCreate, this.room);
@@ -753,7 +743,7 @@ var NucleoStanza = class _NucleoStanza {
753
743
  this.inviaGiocatori();
754
744
  this.rivalutaAvvio();
755
745
  }
756
- await this.persistiEProgramma();
746
+ await this.persistiEProgramma(true);
757
747
  return { ok: true };
758
748
  }
759
749
  async disconnetti(connessione) {
@@ -771,7 +761,7 @@ var NucleoStanza = class _NucleoStanza {
771
761
  await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
772
762
  await this.concludiEvento();
773
763
  this.inviaGiocatori();
774
- await this.persistiEProgramma();
764
+ await this.persistiEProgramma(true);
775
765
  }
776
766
  async ricevi(connessione, frame, limiteVerificato = false) {
777
767
  if (this.dati === null || this.dati.status === "ended") return;
@@ -909,7 +899,7 @@ var NucleoStanza = class _NucleoStanza {
909
899
  await this.messaggioErrato(player);
910
900
  }
911
901
  async eseguiTick() {
912
- if (this.dati === null || this.dati.status !== "playing" || this.dati.tickRate === 0) return;
902
+ if (this.dati === null || this.dati.status !== "playing" || this.tickRate === 0) return;
913
903
  if (await this.terminaSeInattiva()) return;
914
904
  if (!this.serveTick()) {
915
905
  await this.persistiEProgramma();
@@ -917,14 +907,11 @@ var NucleoStanza = class _NucleoStanza {
917
907
  }
918
908
  const dati = this.dati;
919
909
  dati.tick++;
920
- const inizio = this.adattatore.misuraCpu();
921
910
  await this.chiama(
922
911
  this.definizione.onTick,
923
912
  this.room,
924
- 1 / dati.tickRate
913
+ 1 / this.tickRate
925
914
  );
926
- const durata = Math.max(0, this.adattatore.misuraCpu() - inizio);
927
- this.registraCpu(durata);
928
915
  await this.applicaAzioni();
929
916
  if (dati.status === "ended") return;
930
917
  this.inviaGuadagniCambiati();
@@ -932,13 +919,14 @@ var NucleoStanza = class _NucleoStanza {
932
919
  if (stato === null || this.stanzaTerminata()) return;
933
920
  dati.state = stato.valore;
934
921
  this.inviaDiff();
935
- if (dati.tick % 100 === 0 || this.voceDaPersistire) await this.persisti();
936
- await this.aggiornaProgrammazione();
922
+ await this.persistiEProgramma();
937
923
  }
938
924
  async sveglia() {
939
925
  if (this.dati === null || this.dati.status === "ended") return;
926
+ if (await this.terminaSeVuota()) return;
940
927
  if (await this.terminaSeInattiva()) return;
941
928
  const ora = this.adattatore.ora();
929
+ const statoPrima = this.dati.status;
942
930
  const scaduti = this.dati.giocatori.filter(
943
931
  (player) => !player.connected && player.graziaFinoA !== null && player.graziaFinoA <= ora
944
932
  );
@@ -972,13 +960,10 @@ var NucleoStanza = class _NucleoStanza {
972
960
  }
973
961
  }
974
962
  await this.concludiEvento();
975
- if (!this.stanzaTerminata() && this.dati.giocatori.length === 0 && this.dati.vuotaDa !== null && this.dati.vuotaDa + STANZA_VUOTA_MS <= ora) {
976
- this.dati.cancellaDopoFlush = true;
977
- await this.terminaInterna(null);
978
- }
963
+ await this.terminaSeVuota();
979
964
  if (scaduti.length > 0) this.inviaGiocatori();
980
965
  await this.concludiEvento();
981
- await this.persistiEProgramma();
966
+ await this.persistiEProgramma(scaduti.length > 0 || this.dati.status !== statoPrima);
982
967
  }
983
968
  async preparaFlush() {
984
969
  const dati = this.richiediDati();
@@ -1038,7 +1023,7 @@ var NucleoStanza = class _NucleoStanza {
1038
1023
  }
1039
1024
  await this.concludiEvento();
1040
1025
  this.inviaGiocatori();
1041
- await this.persistiEProgramma();
1026
+ await this.persistiEProgramma(true);
1042
1027
  }
1043
1028
  }
1044
1029
  ruoloAutomatico() {
@@ -1093,6 +1078,7 @@ var NucleoStanza = class _NucleoStanza {
1093
1078
  if (player.team === null) player.team = this.squadraAutomatica();
1094
1079
  if (this.richiediDati().status === "lobby") player.ready = false;
1095
1080
  if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
1081
+ this.salvataggioImmediato = true;
1096
1082
  this.rivediVoce();
1097
1083
  }
1098
1084
  this.inviaGiocatori();
@@ -1106,7 +1092,10 @@ var NucleoStanza = class _NucleoStanza {
1106
1092
  const squadraPrecedente = player.team;
1107
1093
  player.team = team;
1108
1094
  if (this.richiediDati().status === "lobby") player.ready = false;
1109
- if (player.team !== squadraPrecedente) this.rivediVoce();
1095
+ if (player.team !== squadraPrecedente) {
1096
+ this.salvataggioImmediato = true;
1097
+ this.rivediVoce();
1098
+ }
1110
1099
  this.inviaGiocatori();
1111
1100
  this.rivalutaAvvio();
1112
1101
  return null;
@@ -1228,6 +1217,7 @@ var NucleoStanza = class _NucleoStanza {
1228
1217
  const indice = dati.giocatori.findIndex((item) => item.id === player.id);
1229
1218
  if (indice < 0) return;
1230
1219
  dati.giocatori.splice(indice, 1);
1220
+ this.salvataggioImmediato = true;
1231
1221
  if (player.connessione !== null) this.frequenza.delete(player.connessione);
1232
1222
  this.rivediVoce();
1233
1223
  this.pulisciGuadagni(player.id);
@@ -1379,15 +1369,13 @@ var NucleoStanza = class _NucleoStanza {
1379
1369
  rematch: this.rivincitaProtocollo(),
1380
1370
  mode: dati.mode,
1381
1371
  tick: dati.tick,
1382
- tickRate: dati.tickRate,
1372
+ tickRate: this.tickRate,
1383
1373
  serverTime: this.adattatore.ora(),
1384
1374
  countdownAt: dati.countdownAt,
1385
1375
  configuration: {
1386
1376
  players: { ...this.configurazione.players },
1387
1377
  persistent: this.manifest.persistent === true,
1388
- requestRole: this.definizione.onRoleRequest !== void 0,
1389
- // Campo tenuto per i giochi pubblicati prima della 0.23, che lo leggono per disegnare la sala d'attesa.
1390
- lobby: dati.origin === "player"
1378
+ requestRole: this.definizione.onRoleRequest !== void 0
1391
1379
  }
1392
1380
  };
1393
1381
  }
@@ -1408,12 +1396,11 @@ var NucleoStanza = class _NucleoStanza {
1408
1396
  inviaDiff() {
1409
1397
  const dati = this.richiediDati();
1410
1398
  const patch = creaDiff(dati.statoSincronizzato, dati.state);
1411
- if (patch.length === 0 && this.tickRateSincronizzato === dati.tickRate) return;
1412
- this.tickRateSincronizzato = dati.tickRate;
1399
+ if (patch.length === 0) return;
1413
1400
  this.broadcast({
1414
1401
  t: "state",
1415
1402
  tick: dati.tick,
1416
- tickRate: dati.tickRate,
1403
+ tickRate: this.tickRate,
1417
1404
  base: dati.tickSincronizzato,
1418
1405
  serverTime: this.adattatore.ora(),
1419
1406
  patch
@@ -1428,10 +1415,9 @@ var NucleoStanza = class _NucleoStanza {
1428
1415
  dati.state = stato.valore;
1429
1416
  dati.statoSincronizzato = copiaJson(stato.valore);
1430
1417
  dati.tickSincronizzato = dati.tick;
1431
- this.tickRateSincronizzato = dati.tickRate;
1432
1418
  this.broadcast({
1433
1419
  t: "snapshot",
1434
- tickRate: dati.tickRate,
1420
+ tickRate: this.tickRate,
1435
1421
  tick: dati.tick,
1436
1422
  serverTime: this.adattatore.ora(),
1437
1423
  state: stato.valore
@@ -1479,7 +1465,7 @@ var NucleoStanza = class _NucleoStanza {
1479
1465
  if (stato === null || this.stanzaTerminata()) return;
1480
1466
  const precedente = JSON.stringify(this.dati.statoSincronizzato);
1481
1467
  this.dati.state = stato.valore;
1482
- if ((this.dati.tickRate === 0 || this.dati.status !== "playing") && stato.testo !== precedente) {
1468
+ if ((this.tickRate === 0 || this.dati.status !== "playing") && stato.testo !== precedente) {
1483
1469
  this.dati.tick++;
1484
1470
  this.inviaDiff();
1485
1471
  }
@@ -1516,7 +1502,7 @@ var NucleoStanza = class _NucleoStanza {
1516
1502
  const precedente = dati.voceGuadagni[playerId]?.[altroId] ?? 1;
1517
1503
  if (precedente === valore) return;
1518
1504
  if (precedente > 0 && valore === 0) this.rivediVoce();
1519
- this.voceDaPersistire = true;
1505
+ this.salvataggioImmediato = true;
1520
1506
  if (valore === 1) {
1521
1507
  const riga = dati.voceGuadagni[playerId];
1522
1508
  if (riga !== void 0) {
@@ -1549,15 +1535,12 @@ var NucleoStanza = class _NucleoStanza {
1549
1535
  pulisciGuadagni(playerId) {
1550
1536
  const guadagni = this.richiediDati().voceGuadagni;
1551
1537
  if (guadagni === void 0) return;
1552
- let cambiato = Object.hasOwn(guadagni, playerId);
1553
1538
  delete guadagni[playerId];
1554
1539
  this.voceGuadagniCambiati.delete(playerId);
1555
1540
  for (const [altroId, riga] of Object.entries(guadagni)) {
1556
- cambiato = Object.hasOwn(riga, playerId) || cambiato;
1557
1541
  delete riga[playerId];
1558
1542
  if (Object.keys(riga).length === 0) delete guadagni[altroId];
1559
1543
  }
1560
- if (cambiato) this.voceDaPersistire = true;
1561
1544
  }
1562
1545
  async terminaPerStato(code) {
1563
1546
  const dati = this.richiediDati();
@@ -1568,7 +1551,8 @@ var NucleoStanza = class _NucleoStanza {
1568
1551
  const dati = this.richiediDati();
1569
1552
  if (dati.status === "ended") return;
1570
1553
  const giaFinita = dati.status === "finished";
1571
- if (dati.origin === "matchmaking" && record(result)?.error === void 0) rematch = true;
1554
+ if (dati.giocatori.length === 0) rematch = false;
1555
+ else if (dati.origin === "matchmaking" && record(result)?.error === void 0) rematch = true;
1572
1556
  dati.status = rematch ? "finished" : "ended";
1573
1557
  dati.rivincita = rematch ? { keepSetup: typeof rematch === "object" && rematch.keepSetup === true } : null;
1574
1558
  dati.prontiPrecedenti = rematch ? dati.giocatori.filter((p) => p.ready).map((p) => p.id) : [];
@@ -1611,41 +1595,34 @@ var NucleoStanza = class _NucleoStanza {
1611
1595
  }
1612
1596
  await this.persistiEProgramma();
1613
1597
  }
1614
- registraCpu(durata) {
1615
- const dati = this.richiediDati();
1616
- dati.durateCpu.push(durata);
1617
- if (dati.durateCpu.length > 50) dati.durateCpu.shift();
1618
- dati.cpuOltreCento = durata > 100 ? dati.cpuOltreCento + 1 : 0;
1619
- if (dati.cpuOltreCento >= 20) {
1620
- this.fineRichiesta = { result: { error: "cpu_budget" }, rematch: false };
1621
- return;
1622
- }
1623
- if (dati.durateCpu.length === 50) {
1624
- const media = dati.durateCpu.reduce((somma, value) => somma + value, 0) / 50;
1625
- if (media > 20 && dati.tickRate > 5) {
1626
- dati.tickRate = Math.max(5, Math.floor(dati.tickRate / 2));
1627
- dati.durateCpu = [];
1628
- this.broadcast({
1629
- t: "error",
1630
- code: "tick_rate_reduced",
1631
- message: `Tick rate reduced to ${dati.tickRate} because the game exceeded its CPU budget.`
1632
- });
1598
+ async persistiEProgramma(immediato = false) {
1599
+ const dati = this.dati;
1600
+ const ora = this.adattatore.ora();
1601
+ const tick = this.serveTick(ora);
1602
+ if (dati !== null && (!tick || immediato || this.salvataggioImmediato || ora >= (dati.prossimoSalvataggioAt ?? 0))) {
1603
+ const scadenzaPrima = dati.prossimoSalvataggioAt;
1604
+ dati.prossimoSalvataggioAt = ora + INTERVALLO_SALVATAGGIO_MS;
1605
+ try {
1606
+ await this.adattatore.storage.put(CHIAVE_NUCLEO, dati);
1607
+ } catch (errore) {
1608
+ dati.prossimoSalvataggioAt = scadenzaPrima;
1609
+ this.salvataggioImmediato = true;
1610
+ throw errore;
1633
1611
  }
1612
+ this.salvataggioImmediato = false;
1634
1613
  }
1614
+ await this.aggiornaProgrammazione(tick);
1635
1615
  }
1636
- async persisti() {
1637
- if (this.dati !== null) {
1638
- await this.adattatore.storage.put(CHIAVE_NUCLEO, this.dati);
1639
- this.voceDaPersistire = false;
1640
- }
1641
- }
1642
- async persistiEProgramma() {
1643
- await this.persisti();
1644
- await this.aggiornaProgrammazione();
1616
+ serveTick(ora = this.adattatore.ora()) {
1617
+ const dati = this.dati;
1618
+ return dati !== null && dati.status === "playing" && this.tickRate > 0 && dati.giocatori.some((player) => player.connected) && ora - Math.max(dati.ultimoInputAt, dati.ultimoCambioStatoAt) < RIPOSO_TICK_MS;
1645
1619
  }
1646
- serveTick() {
1620
+ async terminaSeVuota() {
1647
1621
  const dati = this.dati;
1648
- 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;
1622
+ if (dati === null || dati.status === "ended" || dati.giocatori.length > 0 || dati.vuotaDa === null || dati.vuotaDa + STANZA_VUOTA_MS > this.adattatore.ora()) return false;
1623
+ dati.cancellaDopoFlush = true;
1624
+ await this.terminaInterna(null);
1625
+ return true;
1649
1626
  }
1650
1627
  async terminaSeInattiva() {
1651
1628
  if (this.dati?.status === "finished") {
@@ -1666,15 +1643,13 @@ var NucleoStanza = class _NucleoStanza {
1666
1643
  await this.terminaInterna({ error: "idle" });
1667
1644
  return true;
1668
1645
  }
1669
- async aggiornaProgrammazione() {
1646
+ async aggiornaProgrammazione(tick = this.serveTick()) {
1670
1647
  if (this.dati === null || this.dati.status === "ended") {
1671
1648
  this.adattatore.programmaTick(null);
1672
1649
  await this.adattatore.programmaSveglia(null);
1673
1650
  return;
1674
1651
  }
1675
- this.adattatore.programmaTick(
1676
- this.serveTick() ? 1e3 / this.dati.tickRate : null
1677
- );
1652
+ this.adattatore.programmaTick(tick ? 1e3 / this.tickRate : null);
1678
1653
  const prossime = [];
1679
1654
  if (this.dati.status === "finished") prossime.push(this.dati.rivincitaFinoA ?? this.adattatore.ora());
1680
1655
  if (this.manifest.persistent === true) {
@@ -1708,7 +1683,7 @@ async function measureNetwork(definition, manifest) {
1708
1683
  const connections = /* @__PURE__ */ new Set();
1709
1684
  const meter = new NetworkMeter();
1710
1685
  let now = Date.now();
1711
- let interval = definition.tickRate > 0 ? 1e3 / definition.tickRate : 50;
1686
+ const interval = definition.tickRate > 0 ? 1e3 / definition.tickRate : 50;
1712
1687
  let error = null;
1713
1688
  let playing = false;
1714
1689
  const adapter = {
@@ -1729,18 +1704,16 @@ async function measureNetwork(definition, manifest) {
1729
1704
  invia(connection, message) {
1730
1705
  if (connections.has(connection)) meter.record(connection, JSON.stringify(message), now);
1731
1706
  if (message.t === "status" && message.status === "playing") playing = true;
1732
- if (message.t === "error" || message.t === "status" && message.result !== null && typeof message.result === "object" && !Array.isArray(message.result) && ["callback_error", "state_too_large", "state_invalid", "cpu_budget"].includes(String(message.result.error))) error = "The server reported an error during the publication probe.";
1707
+ if (message.t === "error" || message.t === "status" && message.result !== null && typeof message.result === "object" && !Array.isArray(message.result) && ["callback_error", "state_too_large", "state_invalid"].includes(String(message.result.error))) error = "The server reported an error during the publication probe.";
1733
1708
  },
1734
1709
  chiudi(connection) {
1735
1710
  connections.delete(connection);
1736
1711
  },
1737
1712
  connessioniAttive: () => [...connections],
1738
1713
  ora: () => now,
1739
- misuraCpu: () => performance.now(),
1740
1714
  rivediVoce() {
1741
1715
  },
1742
- programmaTick(ms) {
1743
- if (ms !== null) interval = ms;
1716
+ programmaTick() {
1744
1717
  },
1745
1718
  async programmaSveglia() {
1746
1719
  }
@@ -1792,8 +1765,8 @@ var ArchivioFacet = class {
1792
1765
  };
1793
1766
  var AdattatoreFacet = class {
1794
1767
  constructor(archivio, tickRate) {
1768
+ this.tickRate = tickRate;
1795
1769
  this.connessioni = /* @__PURE__ */ new Set();
1796
- this.cpuMessaggiOltreCento = 0;
1797
1770
  this.frames = [];
1798
1771
  this.chiusure = [];
1799
1772
  this.sveglia = null;
@@ -1801,22 +1774,15 @@ var AdattatoreFacet = class {
1801
1774
  this.voceRivedi = false;
1802
1775
  this.salvato = "";
1803
1776
  this.storage = new ArchivioFacet(archivio);
1804
- this.tickRate = tickRate;
1805
1777
  }
1806
1778
  async ripristina() {
1807
1779
  const dati = await this.storage.get(CHIAVE_ADATTATORE);
1808
1780
  if (dati === void 0) return;
1809
1781
  for (const connessione of dati.connessioni) this.connessioni.add(connessione);
1810
- this.tickRate = dati.tickRate;
1811
- this.cpuMessaggiOltreCento = dati.cpuMessaggiOltreCento;
1812
- this.salvato = JSON.stringify(dati);
1782
+ this.salvato = JSON.stringify({ connessioni: dati.connessioni });
1813
1783
  }
1814
1784
  async persisti() {
1815
- const dati = {
1816
- connessioni: [...this.connessioni],
1817
- tickRate: this.tickRate,
1818
- cpuMessaggiOltreCento: this.cpuMessaggiOltreCento
1819
- };
1785
+ const dati = { connessioni: [...this.connessioni] };
1820
1786
  const testo = JSON.stringify(dati);
1821
1787
  if (testo === this.salvato) return;
1822
1788
  await this.storage.put(CHIAVE_ADATTATORE, dati);
@@ -1835,15 +1801,11 @@ var AdattatoreFacet = class {
1835
1801
  ora() {
1836
1802
  return Date.now();
1837
1803
  }
1838
- misuraCpu() {
1839
- return performance.now();
1840
- }
1841
1804
  rivediVoce() {
1842
1805
  this.voceRivedi = true;
1843
1806
  }
1844
1807
  programmaTick(intervalloMs) {
1845
1808
  this.tick = intervalloMs !== null;
1846
- if (intervalloMs !== null) this.tickRate = 1e3 / intervalloMs;
1847
1809
  }
1848
1810
  async programmaSveglia(ora) {
1849
1811
  this.sveglia = ora;
@@ -1875,22 +1837,9 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
1875
1837
  this.#roomId = typeof stato.id === "string" ? stato.id : stato.id.name ?? stato.id.toString();
1876
1838
  const adattatore = new AdattatoreFacet(stato.storage, definizione.tickRate);
1877
1839
  this.#adattatore = adattatore;
1878
- const misurata = Object.create(definizione);
1879
- if (definizione.onMessage !== void 0) {
1880
- misurata.onMessage = async (room, player, messaggio) => {
1881
- const inizio = adattatore.misuraCpu();
1882
- try {
1883
- await definizione.onMessage?.(room, player, messaggio);
1884
- } finally {
1885
- const durata = Math.max(0, adattatore.misuraCpu() - inizio);
1886
- adattatore.cpuMessaggiOltreCento = durata > 100 ? adattatore.cpuMessaggiOltreCento + 1 : 0;
1887
- if (adattatore.cpuMessaggiOltreCento >= 20) room.end({ error: "cpu_budget" });
1888
- }
1889
- };
1890
- }
1891
1840
  this.#pronto = stato.blockConcurrencyWhile(async () => {
1892
1841
  await adattatore.ripristina();
1893
- return NucleoStanza.apri(misurata, manifest, adattatore);
1842
+ return NucleoStanza.apri(definizione, manifest, adattatore);
1894
1843
  });
1895
1844
  }
1896
1845
  // Le RPC possono intercalarsi durante un await del creatore: ogni Esito deve restare del suo evento.
@@ -3,7 +3,6 @@ export type TextDictionary = Record<string, string>;
3
3
  export declare function normalizeLanguage(value: unknown): string | null;
4
4
  export declare function manifestLanguages(manifest: {
5
5
  languages?: string[];
6
- language?: string;
7
6
  }): string[];
8
7
  export declare function languageFallbacks(language: string, defaultLanguage?: string): string[];
9
8
  export declare function resolveGameLanguage(preferences: readonly string[], languages?: readonly string[]): string;
@@ -24,7 +24,7 @@ export type VersionNumber = number & {
24
24
  * il numero di versione, questo URL potrebbe servire codice di un altro gioco
25
25
  * dentro l'origine della vittima:
26
26
  *
27
- * https://vittima.caisual.app/rt/v1/<n-di-un-altro-gioco>/index.html
27
+ * https://vittima.caisual.app/rt/v2/<n-di-un-altro-gioco>/index.html
28
28
  *
29
29
  * Con `games/<slug>/<n>/...`, `play` prende lo slug dall'hostname e cerca solo
30
30
  * sotto quel prefisso. I file dell'attaccante non esistono quindi nell'origine
@@ -53,8 +53,6 @@ export interface Manifest {
53
53
  screenshots: string[];
54
54
  tags: string[];
55
55
  languages: string[];
56
- /** @deprecated Usa languages; il primo elemento e' il default. */
57
- language: string;
58
56
  platform: Platform;
59
57
  orientation: Orientation;
60
58
  input: Input[];
@@ -68,8 +66,6 @@ export interface Manifest {
68
66
  voice: Voice;
69
67
  modes: ManifestMode[];
70
68
  }
71
- export declare const AVVISO_ISOLATED = "isolated has no effect: games run inside the portal page, which is not cross-origin isolated";
72
- export declare function avvisiManifest(value: unknown): string[];
73
69
  export type EsitoManifest = {
74
70
  ok: true;
75
71
  manifest: Manifest;
@@ -83,4 +79,3 @@ export declare const TETTO_GIOCATORI = 24;
83
79
  * errori. Un agente puo' cosi' correggere il file senza tentativi ripetuti.
84
80
  */
85
81
  export declare function validaManifest(valore: unknown): EsitoManifest;
86
- export declare function validaManifestPubblicazione(valore: unknown): EsitoManifest;
@@ -18,14 +18,14 @@
18
18
  * versione vecchia della trasformazione su un artefatto che non e' cambiato, e
19
19
  * non ci sarebbe modo di invalidarle se non a mano.
20
20
  *
21
- * Mettendo la versione nell'URL il problema sparisce: una trasformazione nuova
22
- * e' `rt/v2`, cioe' un URL nuovo, e le due non si incontrano mai. L'artefatto su
23
- * R2 resta intatto in entrambi i casi.
21
+ * Ogni trasformazione ha una versione nell'URL. Il passaggio a `rt/v2`
22
+ * rende disponibile il segnale di apertura anche con una copia v1 in cache.
23
+ * L'artefatto su R2 resta intatto.
24
24
  */
25
- /** Versioni di runtime che sappiamo servire. Non se ne toglie mai una. */
26
- declare const RUNTIME_VERSIONS: readonly ['v1'];
27
- export type RuntimeVersion = (typeof RUNTIME_VERSIONS)[number];
28
- export declare function isRuntimeVersion(value: string): value is RuntimeVersion;
25
+ /** Versione usata per i link generati oggi. */
26
+ declare const CURRENT_RUNTIME = "v2";
27
+ export type RuntimeVersion = typeof CURRENT_RUNTIME;
28
+ export declare const isRuntimeVersion: (value: string) => value is RuntimeVersion;
29
29
  /**
30
30
  * Path di un file di gioco sull'origine di `play`.
31
31
  * Forma: `/rt/{runtime}/{n}/{path}`.
@@ -84,6 +84,7 @@ export interface HostBridgeInput {
84
84
  onRoom(room: {
85
85
  code: string;
86
86
  } | null): void;
87
+ onApertura?(): void;
87
88
  crew?: CrewOspite;
88
89
  creaCanale?(): MessageChannel;
89
90
  }
@@ -8,7 +8,7 @@ export interface NodeWebSocket {
8
8
  send(message: string): void;
9
9
  close(code?: number, reason?: string): void;
10
10
  }
11
- export declare function acceptNodeWebSocket(request: IncomingMessage, socket: Duplex, head?: Buffer): NodeWebSocket;
11
+ export declare function acceptNodeWebSocket(request: IncomingMessage, socket: Duplex, head?: Buffer): Promise<NodeWebSocket>;
12
12
  export interface NodePlayerIdentity {
13
13
  id: string;
14
14
  name: string;
@@ -1,29 +1,17 @@
1
1
  import { EventEmitter } from 'node:events';
2
2
  import type { IncomingMessage } from 'node:http';
3
3
  import type { Duplex } from 'node:stream';
4
+ import WebSocket from 'ws';
4
5
  export declare interface NodeWebSocket {
5
6
  on(event: 'message', listener: (message: string) => void): this;
6
7
  on(event: 'close', listener: () => void): this;
7
8
  }
8
- /** WebSocket testuale minimo per server Node, senza estensioni negoziate. */
9
+ /** Mantiene il contratto testuale del kit; ws gestisce il protocollo di rete. */
9
10
  export declare class NodeWebSocket extends EventEmitter {
10
11
  private readonly socket;
11
- private readonly massimoMessaggio;
12
- private buffer;
13
- private frammenti;
14
- private byteFrammenti;
15
- private frammentando;
16
- private chiusuraInviata;
17
- private terminato;
18
- constructor(socket: Duplex, head: Buffer, massimoMessaggio?: number);
12
+ constructor(socket: WebSocket);
19
13
  send(message: string): void;
20
14
  close(code?: number, reason?: string): void;
21
- private termina;
22
- private erroreProtocollo;
23
- private aggiungi;
24
- private leggiFrame;
25
- private gestisciControllo;
26
- private gestisciDati;
27
15
  }
28
- /** Completa un upgrade HTTP gia autorizzato e restituisce il canale testuale. */
29
- export declare function acceptNodeWebSocket(request: IncomingMessage, socket: Duplex, head?: Buffer): NodeWebSocket;
16
+ /** Completa un upgrade HTTP gia' autorizzato. Nessuna compressione negoziata. */
17
+ export declare function acceptNodeWebSocket(request: IncomingMessage, socket: Duplex, head?: Buffer): Promise<NodeWebSocket>;
@@ -37,17 +37,16 @@ export interface RoomConfiguration {
37
37
  };
38
38
  persistent: boolean;
39
39
  requestRole: boolean;
40
- lobby: boolean;
41
40
  }
42
41
  export interface StanzaProtocollo {
43
42
  origin: 'player' | 'matchmaking';
44
- rematch?: {
43
+ rematch: {
45
44
  keepSetup: boolean;
46
45
  deadline: number;
47
46
  } | null;
48
- result?: JsonValue;
49
- countdownAt?: number | null;
50
- configuration?: RoomConfiguration;
47
+ result: JsonValue;
48
+ countdownAt: number | null;
49
+ configuration: RoomConfiguration;
51
50
  id: string;
52
51
  seed: number;
53
52
  status: RoomStatus;
@@ -65,8 +64,9 @@ export interface FotografiaStanza {
65
64
  /**
66
65
  * Operazioni voce del client. Ogni richiesta porta `r`, un intero crescente per
67
66
  * connessione: la risposta del server ripete lo stesso `r`. Gli op `session`,
68
- * `subscribe`, `answer` e `close` esistono solo con trasporto `sfu`; `publish` e
69
- * `signal` solo con trasporto `mesh` (sviluppo locale).
67
+ * `subscribe`, `answer` e `close` esistono solo con trasporto `sfu`; `signal` solo
68
+ * con trasporto `mesh` (sviluppo locale). `publish` annuncia il microfono agli altri
69
+ * in tutti e due: con `sfu` va mandato a connessione fatta.
70
70
  */
71
71
  export type MessaggioClientVoce = {
72
72
  t: 'voice';
@@ -220,24 +220,24 @@ export type MessaggioServerStanza = {
220
220
  code: string | null;
221
221
  } | {
222
222
  t: 'status';
223
- rematch?: {
223
+ rematch: {
224
224
  keepSetup: boolean;
225
225
  deadline: number;
226
226
  } | null;
227
227
  status: RoomStatus;
228
228
  at: number;
229
229
  result: JsonValue;
230
- countdownAt?: number | null;
230
+ countdownAt: number | null;
231
231
  } | {
232
232
  t: 'state';
233
- tickRate?: number;
233
+ tickRate: number;
234
234
  tick: number;
235
235
  serverTime: number;
236
236
  base: number;
237
237
  patch: OperazioneDiff[];
238
238
  } | {
239
239
  t: 'snapshot';
240
- tickRate?: number;
240
+ tickRate: number;
241
241
  tick: number;
242
242
  serverTime: number;
243
243
  state: JsonValue;