@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/README.md +1 -1
- package/dist/host.js +12 -8
- package/dist/kit.js +2 -2
- package/dist/kit.js.map +3 -3
- package/dist/node.js +123 -264
- package/dist/runtime.js +65 -116
- package/dist/types/contracts/i18n.d.ts +0 -1
- package/dist/types/contracts/keys.d.ts +1 -1
- package/dist/types/contracts/manifest.d.ts +0 -5
- package/dist/types/contracts/runtime.d.ts +7 -7
- package/dist/types/host/index.d.ts +1 -0
- package/dist/types/node/index.d.ts +1 -1
- package/dist/types/node/websocket.d.ts +5 -17
- package/dist/types/protocollo.d.ts +11 -11
- package/dist/types/stanza/nucleo.d.ts +3 -4
- package/dist/types/stanza/tipi.d.ts +3 -6
- package/dist/types/voce/index.d.ts +3 -0
- package/package.json +7 -2
package/dist/node.js
CHANGED
|
@@ -411,6 +411,7 @@ function seedStanza(roomId) {
|
|
|
411
411
|
|
|
412
412
|
// src/stanza/nucleo.ts
|
|
413
413
|
var CHIAVE_NUCLEO = "nucleo";
|
|
414
|
+
var VERSIONE_STATO = 2;
|
|
414
415
|
var PREFISSO_SAVE = "save:";
|
|
415
416
|
var LIMITE_FRAME = MASSIMO_BYTE_FRAME_STANZA;
|
|
416
417
|
var LIMITE_STATO = 512 * 1024;
|
|
@@ -419,6 +420,7 @@ var GRAZIA_MS = 6e4;
|
|
|
419
420
|
var STANZA_VUOTA_MS = 5 * 6e4;
|
|
420
421
|
var ATTESA_RIVINCITA_MS = 2 * 6e4;
|
|
421
422
|
var COUNTDOWN_MS = 3e3;
|
|
423
|
+
var INTERVALLO_SALVATAGGIO_MS = 5e3;
|
|
422
424
|
var RIPOSO_TICK_MS = 3e4;
|
|
423
425
|
var INATTIVITA_MS = 10 * 6e4;
|
|
424
426
|
var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
|
|
@@ -470,9 +472,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
470
472
|
this.fineRichiesta = null;
|
|
471
473
|
this.applicandoAzioni = false;
|
|
472
474
|
this.ultimoStatoOsservato = "";
|
|
473
|
-
this.
|
|
474
|
-
this.voceDaPersistire = false;
|
|
475
|
+
this.salvataggioImmediato = false;
|
|
475
476
|
const nucleo = this;
|
|
477
|
+
this.tickRate = definizione.tickRate;
|
|
476
478
|
const daily = {
|
|
477
479
|
get day() {
|
|
478
480
|
return nucleo.richiediDati().daily.day;
|
|
@@ -501,7 +503,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
501
503
|
return nucleo.richiediDati().tick;
|
|
502
504
|
},
|
|
503
505
|
get tickRate() {
|
|
504
|
-
return nucleo.
|
|
506
|
+
return nucleo.tickRate;
|
|
505
507
|
},
|
|
506
508
|
get result() {
|
|
507
509
|
return nucleo.richiediDati().result;
|
|
@@ -568,22 +570,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
568
570
|
const nucleo = new _NucleoStanza(definizione, manifest, adattatore);
|
|
569
571
|
const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
|
|
570
572
|
if (salvato !== void 0) {
|
|
573
|
+
if (salvato.versione !== VERSIONE_STATO) throw new Error("The saved room format is unsupported. Create a new room.");
|
|
571
574
|
if (modalitaLocale(manifest, salvato.mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
572
575
|
nucleo.dati = salvato;
|
|
573
|
-
nucleo.tickRateSincronizzato = salvato.tickRate;
|
|
574
|
-
let daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0 || salvato.origin === void 0;
|
|
575
|
-
salvato.origin ??= "player";
|
|
576
|
-
if (salvato.origin === "matchmaking" && salvato.status === "lobby") {
|
|
577
|
-
salvato.status = "waiting";
|
|
578
|
-
daAggiornare = true;
|
|
579
|
-
}
|
|
580
|
-
salvato.daily ??= nucleo.contestoGiornaliero();
|
|
581
|
-
salvato.ultimoInputAt ??= adattatore.ora();
|
|
582
|
-
salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
|
|
583
576
|
nucleo.ultimoStatoOsservato = JSON.stringify(salvato.state);
|
|
584
577
|
await nucleo.riconciliaConnessioni();
|
|
585
578
|
if (salvato.origin === "matchmaking") nucleo.rivalutaAvvio();
|
|
586
|
-
if (
|
|
579
|
+
if (salvato.status === "playing" && nucleo.tickRate > 0) nucleo.inviaSnapshotTutti();
|
|
587
580
|
}
|
|
588
581
|
await nucleo.aggiornaProgrammazione();
|
|
589
582
|
return nucleo;
|
|
@@ -608,14 +601,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
608
601
|
if (modalitaLocale(this.manifest, mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
609
602
|
const ora = this.adattatore.ora();
|
|
610
603
|
this.dati = {
|
|
611
|
-
versione:
|
|
604
|
+
versione: VERSIONE_STATO,
|
|
612
605
|
origin,
|
|
613
606
|
daily: this.contestoGiornaliero(ora),
|
|
614
607
|
id,
|
|
615
608
|
mode,
|
|
616
609
|
status: origin === "matchmaking" ? "waiting" : "lobby",
|
|
617
610
|
tick: 0,
|
|
618
|
-
tickRate: this.definizione.tickRate,
|
|
619
611
|
ultimoInputAt: ora,
|
|
620
612
|
ultimoCambioStatoAt: ora,
|
|
621
613
|
state: {},
|
|
@@ -630,8 +622,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
630
622
|
cancellaDopoFlush: false,
|
|
631
623
|
tickSincronizzato: 0,
|
|
632
624
|
statoSincronizzato: {},
|
|
633
|
-
durateCpu: [],
|
|
634
|
-
cpuOltreCento: 0,
|
|
635
625
|
voceGuadagni: {}
|
|
636
626
|
};
|
|
637
627
|
await this.chiama(this.definizione.onCreate, this.room);
|
|
@@ -772,7 +762,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
772
762
|
this.inviaGiocatori();
|
|
773
763
|
this.rivalutaAvvio();
|
|
774
764
|
}
|
|
775
|
-
await this.persistiEProgramma();
|
|
765
|
+
await this.persistiEProgramma(true);
|
|
776
766
|
return { ok: true };
|
|
777
767
|
}
|
|
778
768
|
async disconnetti(connessione) {
|
|
@@ -790,7 +780,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
790
780
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
791
781
|
await this.concludiEvento();
|
|
792
782
|
this.inviaGiocatori();
|
|
793
|
-
await this.persistiEProgramma();
|
|
783
|
+
await this.persistiEProgramma(true);
|
|
794
784
|
}
|
|
795
785
|
async ricevi(connessione, frame, limiteVerificato = false) {
|
|
796
786
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
@@ -928,7 +918,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
928
918
|
await this.messaggioErrato(player);
|
|
929
919
|
}
|
|
930
920
|
async eseguiTick() {
|
|
931
|
-
if (this.dati === null || this.dati.status !== "playing" || this.
|
|
921
|
+
if (this.dati === null || this.dati.status !== "playing" || this.tickRate === 0) return;
|
|
932
922
|
if (await this.terminaSeInattiva()) return;
|
|
933
923
|
if (!this.serveTick()) {
|
|
934
924
|
await this.persistiEProgramma();
|
|
@@ -936,14 +926,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
936
926
|
}
|
|
937
927
|
const dati = this.dati;
|
|
938
928
|
dati.tick++;
|
|
939
|
-
const inizio = this.adattatore.misuraCpu();
|
|
940
929
|
await this.chiama(
|
|
941
930
|
this.definizione.onTick,
|
|
942
931
|
this.room,
|
|
943
|
-
1 /
|
|
932
|
+
1 / this.tickRate
|
|
944
933
|
);
|
|
945
|
-
const durata = Math.max(0, this.adattatore.misuraCpu() - inizio);
|
|
946
|
-
this.registraCpu(durata);
|
|
947
934
|
await this.applicaAzioni();
|
|
948
935
|
if (dati.status === "ended") return;
|
|
949
936
|
this.inviaGuadagniCambiati();
|
|
@@ -951,13 +938,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
951
938
|
if (stato === null || this.stanzaTerminata()) return;
|
|
952
939
|
dati.state = stato.valore;
|
|
953
940
|
this.inviaDiff();
|
|
954
|
-
|
|
955
|
-
await this.aggiornaProgrammazione();
|
|
941
|
+
await this.persistiEProgramma();
|
|
956
942
|
}
|
|
957
943
|
async sveglia() {
|
|
958
944
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
945
|
+
if (await this.terminaSeVuota()) return;
|
|
959
946
|
if (await this.terminaSeInattiva()) return;
|
|
960
947
|
const ora = this.adattatore.ora();
|
|
948
|
+
const statoPrima = this.dati.status;
|
|
961
949
|
const scaduti = this.dati.giocatori.filter(
|
|
962
950
|
(player) => !player.connected && player.graziaFinoA !== null && player.graziaFinoA <= ora
|
|
963
951
|
);
|
|
@@ -991,13 +979,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
991
979
|
}
|
|
992
980
|
}
|
|
993
981
|
await this.concludiEvento();
|
|
994
|
-
|
|
995
|
-
this.dati.cancellaDopoFlush = true;
|
|
996
|
-
await this.terminaInterna(null);
|
|
997
|
-
}
|
|
982
|
+
await this.terminaSeVuota();
|
|
998
983
|
if (scaduti.length > 0) this.inviaGiocatori();
|
|
999
984
|
await this.concludiEvento();
|
|
1000
|
-
await this.persistiEProgramma();
|
|
985
|
+
await this.persistiEProgramma(scaduti.length > 0 || this.dati.status !== statoPrima);
|
|
1001
986
|
}
|
|
1002
987
|
async preparaFlush() {
|
|
1003
988
|
const dati = this.richiediDati();
|
|
@@ -1057,7 +1042,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1057
1042
|
}
|
|
1058
1043
|
await this.concludiEvento();
|
|
1059
1044
|
this.inviaGiocatori();
|
|
1060
|
-
await this.persistiEProgramma();
|
|
1045
|
+
await this.persistiEProgramma(true);
|
|
1061
1046
|
}
|
|
1062
1047
|
}
|
|
1063
1048
|
ruoloAutomatico() {
|
|
@@ -1112,6 +1097,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1112
1097
|
if (player.team === null) player.team = this.squadraAutomatica();
|
|
1113
1098
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1114
1099
|
if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
|
|
1100
|
+
this.salvataggioImmediato = true;
|
|
1115
1101
|
this.rivediVoce();
|
|
1116
1102
|
}
|
|
1117
1103
|
this.inviaGiocatori();
|
|
@@ -1125,7 +1111,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1125
1111
|
const squadraPrecedente = player.team;
|
|
1126
1112
|
player.team = team;
|
|
1127
1113
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1128
|
-
if (player.team !== squadraPrecedente)
|
|
1114
|
+
if (player.team !== squadraPrecedente) {
|
|
1115
|
+
this.salvataggioImmediato = true;
|
|
1116
|
+
this.rivediVoce();
|
|
1117
|
+
}
|
|
1129
1118
|
this.inviaGiocatori();
|
|
1130
1119
|
this.rivalutaAvvio();
|
|
1131
1120
|
return null;
|
|
@@ -1247,6 +1236,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1247
1236
|
const indice = dati.giocatori.findIndex((item) => item.id === player.id);
|
|
1248
1237
|
if (indice < 0) return;
|
|
1249
1238
|
dati.giocatori.splice(indice, 1);
|
|
1239
|
+
this.salvataggioImmediato = true;
|
|
1250
1240
|
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1251
1241
|
this.rivediVoce();
|
|
1252
1242
|
this.pulisciGuadagni(player.id);
|
|
@@ -1398,15 +1388,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1398
1388
|
rematch: this.rivincitaProtocollo(),
|
|
1399
1389
|
mode: dati.mode,
|
|
1400
1390
|
tick: dati.tick,
|
|
1401
|
-
tickRate:
|
|
1391
|
+
tickRate: this.tickRate,
|
|
1402
1392
|
serverTime: this.adattatore.ora(),
|
|
1403
1393
|
countdownAt: dati.countdownAt,
|
|
1404
1394
|
configuration: {
|
|
1405
1395
|
players: { ...this.configurazione.players },
|
|
1406
1396
|
persistent: this.manifest.persistent === true,
|
|
1407
|
-
requestRole: this.definizione.onRoleRequest !== void 0
|
|
1408
|
-
// Campo tenuto per i giochi pubblicati prima della 0.23, che lo leggono per disegnare la sala d'attesa.
|
|
1409
|
-
lobby: dati.origin === "player"
|
|
1397
|
+
requestRole: this.definizione.onRoleRequest !== void 0
|
|
1410
1398
|
}
|
|
1411
1399
|
};
|
|
1412
1400
|
}
|
|
@@ -1427,12 +1415,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1427
1415
|
inviaDiff() {
|
|
1428
1416
|
const dati = this.richiediDati();
|
|
1429
1417
|
const patch = creaDiff(dati.statoSincronizzato, dati.state);
|
|
1430
|
-
if (patch.length === 0
|
|
1431
|
-
this.tickRateSincronizzato = dati.tickRate;
|
|
1418
|
+
if (patch.length === 0) return;
|
|
1432
1419
|
this.broadcast({
|
|
1433
1420
|
t: "state",
|
|
1434
1421
|
tick: dati.tick,
|
|
1435
|
-
tickRate:
|
|
1422
|
+
tickRate: this.tickRate,
|
|
1436
1423
|
base: dati.tickSincronizzato,
|
|
1437
1424
|
serverTime: this.adattatore.ora(),
|
|
1438
1425
|
patch
|
|
@@ -1447,10 +1434,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1447
1434
|
dati.state = stato.valore;
|
|
1448
1435
|
dati.statoSincronizzato = copiaJson(stato.valore);
|
|
1449
1436
|
dati.tickSincronizzato = dati.tick;
|
|
1450
|
-
this.tickRateSincronizzato = dati.tickRate;
|
|
1451
1437
|
this.broadcast({
|
|
1452
1438
|
t: "snapshot",
|
|
1453
|
-
tickRate:
|
|
1439
|
+
tickRate: this.tickRate,
|
|
1454
1440
|
tick: dati.tick,
|
|
1455
1441
|
serverTime: this.adattatore.ora(),
|
|
1456
1442
|
state: stato.valore
|
|
@@ -1498,7 +1484,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1498
1484
|
if (stato === null || this.stanzaTerminata()) return;
|
|
1499
1485
|
const precedente = JSON.stringify(this.dati.statoSincronizzato);
|
|
1500
1486
|
this.dati.state = stato.valore;
|
|
1501
|
-
if ((this.
|
|
1487
|
+
if ((this.tickRate === 0 || this.dati.status !== "playing") && stato.testo !== precedente) {
|
|
1502
1488
|
this.dati.tick++;
|
|
1503
1489
|
this.inviaDiff();
|
|
1504
1490
|
}
|
|
@@ -1535,7 +1521,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1535
1521
|
const precedente = dati.voceGuadagni[playerId]?.[altroId] ?? 1;
|
|
1536
1522
|
if (precedente === valore) return;
|
|
1537
1523
|
if (precedente > 0 && valore === 0) this.rivediVoce();
|
|
1538
|
-
this.
|
|
1524
|
+
this.salvataggioImmediato = true;
|
|
1539
1525
|
if (valore === 1) {
|
|
1540
1526
|
const riga = dati.voceGuadagni[playerId];
|
|
1541
1527
|
if (riga !== void 0) {
|
|
@@ -1568,15 +1554,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1568
1554
|
pulisciGuadagni(playerId) {
|
|
1569
1555
|
const guadagni = this.richiediDati().voceGuadagni;
|
|
1570
1556
|
if (guadagni === void 0) return;
|
|
1571
|
-
let cambiato = Object.hasOwn(guadagni, playerId);
|
|
1572
1557
|
delete guadagni[playerId];
|
|
1573
1558
|
this.voceGuadagniCambiati.delete(playerId);
|
|
1574
1559
|
for (const [altroId, riga] of Object.entries(guadagni)) {
|
|
1575
|
-
cambiato = Object.hasOwn(riga, playerId) || cambiato;
|
|
1576
1560
|
delete riga[playerId];
|
|
1577
1561
|
if (Object.keys(riga).length === 0) delete guadagni[altroId];
|
|
1578
1562
|
}
|
|
1579
|
-
if (cambiato) this.voceDaPersistire = true;
|
|
1580
1563
|
}
|
|
1581
1564
|
async terminaPerStato(code) {
|
|
1582
1565
|
const dati = this.richiediDati();
|
|
@@ -1587,7 +1570,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1587
1570
|
const dati = this.richiediDati();
|
|
1588
1571
|
if (dati.status === "ended") return;
|
|
1589
1572
|
const giaFinita = dati.status === "finished";
|
|
1590
|
-
if (dati.
|
|
1573
|
+
if (dati.giocatori.length === 0) rematch = false;
|
|
1574
|
+
else if (dati.origin === "matchmaking" && record(result)?.error === void 0) rematch = true;
|
|
1591
1575
|
dati.status = rematch ? "finished" : "ended";
|
|
1592
1576
|
dati.rivincita = rematch ? { keepSetup: typeof rematch === "object" && rematch.keepSetup === true } : null;
|
|
1593
1577
|
dati.prontiPrecedenti = rematch ? dati.giocatori.filter((p) => p.ready).map((p) => p.id) : [];
|
|
@@ -1630,41 +1614,34 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1630
1614
|
}
|
|
1631
1615
|
await this.persistiEProgramma();
|
|
1632
1616
|
}
|
|
1633
|
-
|
|
1634
|
-
const dati = this.
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
dati
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
dati.durateCpu = [];
|
|
1647
|
-
this.broadcast({
|
|
1648
|
-
t: "error",
|
|
1649
|
-
code: "tick_rate_reduced",
|
|
1650
|
-
message: `Tick rate reduced to ${dati.tickRate} because the game exceeded its CPU budget.`
|
|
1651
|
-
});
|
|
1617
|
+
async persistiEProgramma(immediato = false) {
|
|
1618
|
+
const dati = this.dati;
|
|
1619
|
+
const ora = this.adattatore.ora();
|
|
1620
|
+
const tick = this.serveTick(ora);
|
|
1621
|
+
if (dati !== null && (!tick || immediato || this.salvataggioImmediato || ora >= (dati.prossimoSalvataggioAt ?? 0))) {
|
|
1622
|
+
const scadenzaPrima = dati.prossimoSalvataggioAt;
|
|
1623
|
+
dati.prossimoSalvataggioAt = ora + INTERVALLO_SALVATAGGIO_MS;
|
|
1624
|
+
try {
|
|
1625
|
+
await this.adattatore.storage.put(CHIAVE_NUCLEO, dati);
|
|
1626
|
+
} catch (errore) {
|
|
1627
|
+
dati.prossimoSalvataggioAt = scadenzaPrima;
|
|
1628
|
+
this.salvataggioImmediato = true;
|
|
1629
|
+
throw errore;
|
|
1652
1630
|
}
|
|
1631
|
+
this.salvataggioImmediato = false;
|
|
1653
1632
|
}
|
|
1633
|
+
await this.aggiornaProgrammazione(tick);
|
|
1654
1634
|
}
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
this.voceDaPersistire = false;
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
async persistiEProgramma() {
|
|
1662
|
-
await this.persisti();
|
|
1663
|
-
await this.aggiornaProgrammazione();
|
|
1635
|
+
serveTick(ora = this.adattatore.ora()) {
|
|
1636
|
+
const dati = this.dati;
|
|
1637
|
+
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;
|
|
1664
1638
|
}
|
|
1665
|
-
|
|
1639
|
+
async terminaSeVuota() {
|
|
1666
1640
|
const dati = this.dati;
|
|
1667
|
-
|
|
1641
|
+
if (dati === null || dati.status === "ended" || dati.giocatori.length > 0 || dati.vuotaDa === null || dati.vuotaDa + STANZA_VUOTA_MS > this.adattatore.ora()) return false;
|
|
1642
|
+
dati.cancellaDopoFlush = true;
|
|
1643
|
+
await this.terminaInterna(null);
|
|
1644
|
+
return true;
|
|
1668
1645
|
}
|
|
1669
1646
|
async terminaSeInattiva() {
|
|
1670
1647
|
if (this.dati?.status === "finished") {
|
|
@@ -1685,15 +1662,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1685
1662
|
await this.terminaInterna({ error: "idle" });
|
|
1686
1663
|
return true;
|
|
1687
1664
|
}
|
|
1688
|
-
async aggiornaProgrammazione() {
|
|
1665
|
+
async aggiornaProgrammazione(tick = this.serveTick()) {
|
|
1689
1666
|
if (this.dati === null || this.dati.status === "ended") {
|
|
1690
1667
|
this.adattatore.programmaTick(null);
|
|
1691
1668
|
await this.adattatore.programmaSveglia(null);
|
|
1692
1669
|
return;
|
|
1693
1670
|
}
|
|
1694
|
-
this.adattatore.programmaTick(
|
|
1695
|
-
this.serveTick() ? 1e3 / this.dati.tickRate : null
|
|
1696
|
-
);
|
|
1671
|
+
this.adattatore.programmaTick(tick ? 1e3 / this.tickRate : null);
|
|
1697
1672
|
const prossime = [];
|
|
1698
1673
|
if (this.dati.status === "finished") prossime.push(this.dati.rivincitaFinoA ?? this.adattatore.ora());
|
|
1699
1674
|
if (this.manifest.persistent === true) {
|
|
@@ -1719,194 +1694,61 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1719
1694
|
};
|
|
1720
1695
|
|
|
1721
1696
|
// src/node/websocket.ts
|
|
1722
|
-
import { createHash } from "node:crypto";
|
|
1723
1697
|
import { EventEmitter } from "node:events";
|
|
1724
|
-
|
|
1725
|
-
var MASSIMO_MESSAGGIO = 1e6;
|
|
1726
|
-
function contieneToken(value, token) {
|
|
1727
|
-
return value?.split(",").some((parte) => parte.trim().toLowerCase() === token) === true;
|
|
1728
|
-
}
|
|
1729
|
-
function chiaveValida(value) {
|
|
1730
|
-
if (value === void 0 || !/^[A-Za-z0-9+/]{22}==$/.test(value)) return false;
|
|
1731
|
-
return Buffer.from(value, "base64").byteLength === 16;
|
|
1732
|
-
}
|
|
1733
|
-
function creaFrame(opcode, payload) {
|
|
1734
|
-
const primo = Buffer.from([128 | opcode]);
|
|
1735
|
-
if (payload.byteLength < 126) {
|
|
1736
|
-
return Buffer.concat([primo, Buffer.from([payload.byteLength]), payload]);
|
|
1737
|
-
}
|
|
1738
|
-
if (payload.byteLength <= 65535) {
|
|
1739
|
-
const header2 = Buffer.allocUnsafe(3);
|
|
1740
|
-
header2[0] = 126;
|
|
1741
|
-
header2.writeUInt16BE(payload.byteLength, 1);
|
|
1742
|
-
return Buffer.concat([primo, header2, payload]);
|
|
1743
|
-
}
|
|
1744
|
-
const header = Buffer.allocUnsafe(9);
|
|
1745
|
-
header[0] = 127;
|
|
1746
|
-
header.writeBigUInt64BE(BigInt(payload.byteLength), 1);
|
|
1747
|
-
return Buffer.concat([primo, header, payload]);
|
|
1748
|
-
}
|
|
1749
|
-
function testoUtf8(payload) {
|
|
1750
|
-
try {
|
|
1751
|
-
return new TextDecoder("utf-8", { fatal: true }).decode(payload);
|
|
1752
|
-
} catch {
|
|
1753
|
-
return null;
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1698
|
+
import WebSocket, { WebSocketServer } from "ws";
|
|
1756
1699
|
var NodeWebSocket = class extends EventEmitter {
|
|
1757
|
-
constructor(socket
|
|
1700
|
+
constructor(socket) {
|
|
1758
1701
|
super();
|
|
1759
1702
|
this.socket = socket;
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
this.frammentando = false;
|
|
1765
|
-
this.chiusuraInviata = false;
|
|
1766
|
-
this.terminato = false;
|
|
1767
|
-
socket.on("data", (chunk) => this.aggiungi(chunk));
|
|
1768
|
-
socket.on("end", () => this.termina());
|
|
1769
|
-
socket.on("close", () => this.termina());
|
|
1770
|
-
socket.on("error", () => this.termina());
|
|
1771
|
-
if (head.byteLength > 0) queueMicrotask(() => this.aggiungi(head));
|
|
1772
|
-
}
|
|
1773
|
-
send(message) {
|
|
1774
|
-
if (this.chiusuraInviata || this.terminato) return;
|
|
1775
|
-
this.socket.write(creaFrame(1, Buffer.from(message, "utf8")));
|
|
1776
|
-
}
|
|
1777
|
-
close(code = 1e3, reason = "") {
|
|
1778
|
-
if (this.chiusuraInviata || this.terminato) return;
|
|
1779
|
-
const motivo = Buffer.from(reason, "utf8");
|
|
1780
|
-
if (motivo.byteLength > 123) throw new RangeError("WebSocket close reasons must be at most 123 bytes.");
|
|
1781
|
-
const payload = Buffer.allocUnsafe(2 + motivo.byteLength);
|
|
1782
|
-
payload.writeUInt16BE(code, 0);
|
|
1783
|
-
motivo.copy(payload, 2);
|
|
1784
|
-
this.chiusuraInviata = true;
|
|
1785
|
-
this.socket.write(creaFrame(8, payload), () => this.socket.end());
|
|
1786
|
-
}
|
|
1787
|
-
termina() {
|
|
1788
|
-
if (this.terminato) return;
|
|
1789
|
-
this.terminato = true;
|
|
1790
|
-
this.emit("close");
|
|
1791
|
-
}
|
|
1792
|
-
erroreProtocollo(code, reason) {
|
|
1793
|
-
this.close(code, reason);
|
|
1794
|
-
}
|
|
1795
|
-
aggiungi(chunk) {
|
|
1796
|
-
if (this.terminato) return;
|
|
1797
|
-
this.buffer = this.buffer.byteLength === 0 ? chunk : Buffer.concat([this.buffer, chunk]);
|
|
1798
|
-
this.leggiFrame();
|
|
1799
|
-
}
|
|
1800
|
-
leggiFrame() {
|
|
1801
|
-
while (!this.terminato && this.buffer.byteLength >= 2) {
|
|
1802
|
-
const primo = this.buffer[0] ?? 0;
|
|
1803
|
-
const secondo = this.buffer[1] ?? 0;
|
|
1804
|
-
const fin = (primo & 128) !== 0;
|
|
1805
|
-
const opcode = primo & 15;
|
|
1806
|
-
const controllo = opcode >= 8;
|
|
1807
|
-
if ((primo & 112) !== 0 || ![0, 1, 8, 9, 10].includes(opcode)) {
|
|
1808
|
-
this.erroreProtocollo(1002, "protocol_error");
|
|
1809
|
-
return;
|
|
1810
|
-
}
|
|
1811
|
-
if ((secondo & 128) === 0 || controllo && !fin) {
|
|
1812
|
-
this.erroreProtocollo(1002, "protocol_error");
|
|
1813
|
-
return;
|
|
1814
|
-
}
|
|
1815
|
-
let lunghezza = secondo & 127;
|
|
1816
|
-
let offset = 2;
|
|
1817
|
-
if (lunghezza === 126) {
|
|
1818
|
-
if (this.buffer.byteLength < 4) return;
|
|
1819
|
-
lunghezza = this.buffer.readUInt16BE(2);
|
|
1820
|
-
offset = 4;
|
|
1821
|
-
} else if (lunghezza === 127) {
|
|
1822
|
-
if (this.buffer.byteLength < 10) return;
|
|
1823
|
-
const grande = this.buffer.readBigUInt64BE(2);
|
|
1824
|
-
if (grande > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
1825
|
-
this.erroreProtocollo(1009, "message_too_large");
|
|
1826
|
-
return;
|
|
1827
|
-
}
|
|
1828
|
-
lunghezza = Number(grande);
|
|
1829
|
-
offset = 10;
|
|
1830
|
-
}
|
|
1831
|
-
if (controllo && lunghezza > 125) {
|
|
1832
|
-
this.erroreProtocollo(1002, "protocol_error");
|
|
1703
|
+
socket.on("message", (data, binary) => {
|
|
1704
|
+
if (socket.readyState !== WebSocket.OPEN) return;
|
|
1705
|
+
if (binary) {
|
|
1706
|
+
socket.close(1003, "text_only");
|
|
1833
1707
|
return;
|
|
1834
1708
|
}
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
for (let indice = 0; indice < payload.byteLength; indice += 1) {
|
|
1840
|
-
payload[indice] = (payload[indice] ?? 0) ^ (maschera[indice % 4] ?? 0);
|
|
1841
|
-
}
|
|
1842
|
-
if (controllo) {
|
|
1843
|
-
this.gestisciControllo(opcode, payload);
|
|
1844
|
-
continue;
|
|
1845
|
-
}
|
|
1846
|
-
this.gestisciDati(opcode, fin, payload);
|
|
1847
|
-
}
|
|
1709
|
+
this.emit("message", data.toString());
|
|
1710
|
+
});
|
|
1711
|
+
socket.on("close", () => this.emit("close"));
|
|
1712
|
+
socket.on("error", () => void 0);
|
|
1848
1713
|
}
|
|
1849
|
-
|
|
1850
|
-
if (
|
|
1851
|
-
if (!this.chiusuraInviata) this.socket.write(creaFrame(10, payload));
|
|
1852
|
-
return;
|
|
1853
|
-
}
|
|
1854
|
-
if (opcode === 10) return;
|
|
1855
|
-
if (payload.byteLength === 1) {
|
|
1856
|
-
this.erroreProtocollo(1002, "protocol_error");
|
|
1857
|
-
return;
|
|
1858
|
-
}
|
|
1859
|
-
if (!this.chiusuraInviata) {
|
|
1860
|
-
this.chiusuraInviata = true;
|
|
1861
|
-
this.socket.write(creaFrame(8, payload), () => this.socket.end());
|
|
1862
|
-
} else {
|
|
1863
|
-
this.socket.end();
|
|
1864
|
-
}
|
|
1714
|
+
send(message) {
|
|
1715
|
+
if (this.socket.readyState === WebSocket.OPEN) this.socket.send(message);
|
|
1865
1716
|
}
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
this.erroreProtocollo(1002, "protocol_error");
|
|
1869
|
-
return;
|
|
1870
|
-
}
|
|
1871
|
-
if (opcode === 0 && !this.frammentando) {
|
|
1872
|
-
this.erroreProtocollo(1002, "protocol_error");
|
|
1873
|
-
return;
|
|
1874
|
-
}
|
|
1875
|
-
if (opcode === 1 && !fin) this.frammentando = true;
|
|
1876
|
-
this.frammenti.push(payload);
|
|
1877
|
-
this.byteFrammenti += payload.byteLength;
|
|
1878
|
-
if (this.byteFrammenti > this.massimoMessaggio) {
|
|
1879
|
-
this.erroreProtocollo(1009, "message_too_large");
|
|
1880
|
-
return;
|
|
1881
|
-
}
|
|
1882
|
-
if (!fin) return;
|
|
1883
|
-
const completo = this.frammenti.length === 1 ? this.frammenti[0] ?? Buffer.alloc(0) : Buffer.concat(this.frammenti, this.byteFrammenti);
|
|
1884
|
-
this.frammenti = [];
|
|
1885
|
-
this.byteFrammenti = 0;
|
|
1886
|
-
this.frammentando = false;
|
|
1887
|
-
const testo = testoUtf8(completo);
|
|
1888
|
-
if (testo === null) {
|
|
1889
|
-
this.erroreProtocollo(1007, "invalid_utf8");
|
|
1890
|
-
return;
|
|
1891
|
-
}
|
|
1892
|
-
this.emit("message", testo);
|
|
1717
|
+
close(code = 1e3, reason = "") {
|
|
1718
|
+
this.socket.close(code, reason);
|
|
1893
1719
|
}
|
|
1894
1720
|
};
|
|
1895
1721
|
function acceptNodeWebSocket(request, socket, head = Buffer.alloc(0)) {
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1722
|
+
return new Promise((resolve, reject) => {
|
|
1723
|
+
const server = new WebSocketServer({
|
|
1724
|
+
noServer: true,
|
|
1725
|
+
clientTracking: false,
|
|
1726
|
+
maxPayload: 1e6,
|
|
1727
|
+
perMessageDeflate: false,
|
|
1728
|
+
skipUTF8Validation: false
|
|
1729
|
+
});
|
|
1730
|
+
const aborted = () => reject(new Error("The WebSocket upgrade was interrupted."));
|
|
1731
|
+
socket.once("error", aborted);
|
|
1732
|
+
socket.once("close", aborted);
|
|
1733
|
+
const cleanup = () => {
|
|
1734
|
+
socket.off("error", aborted);
|
|
1735
|
+
socket.off("close", aborted);
|
|
1736
|
+
};
|
|
1737
|
+
server.once("wsClientError", (error) => {
|
|
1738
|
+
cleanup();
|
|
1739
|
+
socket.end("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
|
|
1740
|
+
reject(error);
|
|
1741
|
+
});
|
|
1742
|
+
try {
|
|
1743
|
+
server.handleUpgrade(request, socket, head, (websocket) => {
|
|
1744
|
+
cleanup();
|
|
1745
|
+
resolve(new NodeWebSocket(websocket));
|
|
1746
|
+
});
|
|
1747
|
+
} catch (error) {
|
|
1748
|
+
cleanup();
|
|
1749
|
+
reject(error);
|
|
1750
|
+
}
|
|
1751
|
+
});
|
|
1910
1752
|
}
|
|
1911
1753
|
|
|
1912
1754
|
// src/node/index.ts
|
|
@@ -2039,9 +1881,6 @@ var AdattatoreNode = class {
|
|
|
2039
1881
|
giorno(ora) {
|
|
2040
1882
|
return this.dailyDay ?? giornoUtc(ora);
|
|
2041
1883
|
}
|
|
2042
|
-
misuraCpu() {
|
|
2043
|
-
return performance.now();
|
|
2044
|
-
}
|
|
2045
1884
|
rivediVoce() {
|
|
2046
1885
|
}
|
|
2047
1886
|
programmaTick(intervalloMs) {
|
|
@@ -2411,11 +2250,31 @@ var StanzaNode = class {
|
|
|
2411
2250
|
this.adattatore.invia(connessione, { t: "voice", op, r, error: { code, message } });
|
|
2412
2251
|
}
|
|
2413
2252
|
};
|
|
2253
|
+
function conAvvisoTick(definizione, manifest) {
|
|
2254
|
+
const onTick = definizione?.onTick;
|
|
2255
|
+
if (onTick === void 0 || definizione.tickRate === 0) return definizione;
|
|
2256
|
+
const intervallo = 1e3 / definizione.tickRate;
|
|
2257
|
+
let oltre = false;
|
|
2258
|
+
const tickMisurato = async (room, deltaSeconds) => {
|
|
2259
|
+
const inizio = performance.now();
|
|
2260
|
+
try {
|
|
2261
|
+
await onTick(room, deltaSeconds);
|
|
2262
|
+
} finally {
|
|
2263
|
+
const durata = performance.now() - inizio;
|
|
2264
|
+
const sfora = durata > intervallo;
|
|
2265
|
+
if (sfora && !oltre) {
|
|
2266
|
+
console.warn(`Il tick di ${manifest.id} ha richiesto ${durata.toFixed(1)} ms (intervallo ${intervallo.toFixed(1)} ms).`);
|
|
2267
|
+
}
|
|
2268
|
+
oltre = sfora;
|
|
2269
|
+
}
|
|
2270
|
+
};
|
|
2271
|
+
return Object.create(definizione, { onTick: { value: tickMisurato } });
|
|
2272
|
+
}
|
|
2414
2273
|
async function createNodeRoom(definition, manifest, options = {}) {
|
|
2415
2274
|
if (options.dailyDay !== void 0 && !validDay(options.dailyDay)) throw new TypeError("dailyDay must be a real UTC date in YYYY-MM-DD format.");
|
|
2416
2275
|
const storage = await ArchivioNode.apri(options.storageFile ?? null);
|
|
2417
2276
|
const adattatore = new AdattatoreNode(storage, options.dailyDay);
|
|
2418
|
-
const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
|
|
2277
|
+
const nucleo = await NucleoStanza.apri(conAvvisoTick(definition, manifest), manifest, adattatore);
|
|
2419
2278
|
return new StanzaNode(nucleo, adattatore, manifest);
|
|
2420
2279
|
}
|
|
2421
2280
|
export {
|