@caisual/kit 0.8.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.
- package/dist/kit.js +407 -45
- package/dist/node.js +230 -69
- package/dist/overlay.js +260 -32
- package/dist/runtime.js +179 -39
- package/dist/server.js +2 -0
- package/dist/types/api.d.ts +1 -1
- package/dist/types/contracts/i18n.d.ts +12 -0
- package/dist/types/contracts/index.d.ts +3 -0
- package/dist/types/contracts/manifest.d.ts +10 -5
- package/dist/types/contracts/overlay.d.ts +9 -1
- package/dist/types/contracts/player.d.ts +1 -0
- package/dist/types/contracts/room-limits.d.ts +13 -0
- package/dist/types/handshake.d.ts +4 -0
- package/dist/types/index.d.ts +16 -2
- package/dist/types/kit.d.ts +2 -0
- package/dist/types/node/index.d.ts +1 -0
- package/dist/types/overlay/host-bridge.d.ts +16 -0
- package/dist/types/overlay/i18n.d.ts +6 -0
- package/dist/types/overlay/index.d.ts +1 -0
- package/dist/types/overlay/styles.d.ts +1 -1
- package/dist/types/overlay/ui.d.ts +1 -1
- package/dist/types/protocollo.d.ts +5 -0
- package/dist/types/runtime/index.d.ts +2 -1
- package/dist/types/server/index.d.ts +6 -2
- package/dist/types/stanza/nucleo.d.ts +5 -2
- package/dist/types/stanza/tipi.d.ts +2 -0
- package/dist/types/text.d.ts +2 -0
- package/package.json +1 -1
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",
|
|
@@ -51,6 +45,65 @@ function modalitaLocale(manifest, mode) {
|
|
|
51
45
|
var MASSIMO_SPETTATORI = 100;
|
|
52
46
|
var RITARDO_SPETTATORI_MS = 3e3;
|
|
53
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
|
+
|
|
54
107
|
// src/stanza/json.ts
|
|
55
108
|
function isPlainObject(value) {
|
|
56
109
|
const prototype = Object.getPrototypeOf(value);
|
|
@@ -133,7 +186,7 @@ function creaDiff(prima, dopo) {
|
|
|
133
186
|
visitaDiff(prima, dopo, [], patch);
|
|
134
187
|
return patch;
|
|
135
188
|
}
|
|
136
|
-
function
|
|
189
|
+
function giornoUtc2(ora) {
|
|
137
190
|
return new Date(ora).toISOString().slice(0, 10);
|
|
138
191
|
}
|
|
139
192
|
var COSTANTI_SHA256 = [
|
|
@@ -286,6 +339,7 @@ var LIMITE_DEPOSITO = 64 * 1024;
|
|
|
286
339
|
var LIMITE_OPERAZIONI_DEPOSITO = 120;
|
|
287
340
|
var GRAZIA_MS = 6e4;
|
|
288
341
|
var STANZA_VUOTA_MS = 5 * 6e4;
|
|
342
|
+
var ATTESA_RIVINCITA_MS = 2 * 6e4;
|
|
289
343
|
var COUNTDOWN_MS = 3e3;
|
|
290
344
|
var RIPOSO_TICK_MS = 3e4;
|
|
291
345
|
var INATTIVITA_MS = 10 * 6e4;
|
|
@@ -337,21 +391,22 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
337
391
|
this.manifest = manifest;
|
|
338
392
|
this.adattatore = adattatore;
|
|
339
393
|
this.dati = null;
|
|
340
|
-
this.frequenza =
|
|
394
|
+
this.frequenza = new LimiteMessaggiStanza();
|
|
341
395
|
this.frequenzaDeposito = [];
|
|
342
396
|
this.kickRichiesti = /* @__PURE__ */ new Set();
|
|
343
397
|
this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
|
|
344
398
|
this.fineRichiesta = null;
|
|
345
399
|
this.applicandoAzioni = false;
|
|
346
400
|
this.ultimoStatoOsservato = "";
|
|
401
|
+
this.tickRateSincronizzato = null;
|
|
347
402
|
this.voceDaPersistire = false;
|
|
348
403
|
const nucleo = this;
|
|
349
404
|
const daily = {
|
|
350
405
|
get day() {
|
|
351
|
-
return
|
|
406
|
+
return nucleo.giornata();
|
|
352
407
|
},
|
|
353
408
|
get seed() {
|
|
354
|
-
return seedGiornata(nucleo.manifest.id,
|
|
409
|
+
return seedGiornata(nucleo.manifest.id, nucleo.giornata());
|
|
355
410
|
}
|
|
356
411
|
};
|
|
357
412
|
this.room = {
|
|
@@ -405,8 +460,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
405
460
|
setTeam(player, team) {
|
|
406
461
|
nucleo.impostaSquadraDalServer(idGiocatore(player), team);
|
|
407
462
|
},
|
|
408
|
-
end(result) {
|
|
409
|
-
nucleo.richiediFine(result);
|
|
463
|
+
end(result, options) {
|
|
464
|
+
nucleo.richiediFine(result, options?.rematch === true);
|
|
410
465
|
},
|
|
411
466
|
save(key, value) {
|
|
412
467
|
return nucleo.salva(key, value);
|
|
@@ -466,6 +521,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
466
521
|
const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
|
|
467
522
|
if (salvato !== void 0) {
|
|
468
523
|
nucleo.dati = salvato;
|
|
524
|
+
nucleo.tickRateSincronizzato = salvato.tickRate;
|
|
469
525
|
const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0;
|
|
470
526
|
salvato.ultimoInputAt ??= adattatore.ora();
|
|
471
527
|
salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
|
|
@@ -593,7 +649,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
593
649
|
if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
|
|
594
650
|
const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
|
|
595
651
|
if (esistente !== void 0) return { ok: true };
|
|
596
|
-
if (this.configurazione.lobby && this.dati.status
|
|
652
|
+
if (this.configurazione.lobby && !["lobby", "finished"].includes(this.dati.status)) {
|
|
597
653
|
return { ok: false, code: "room_playing" };
|
|
598
654
|
}
|
|
599
655
|
if (this.dati.giocatori.length >= this.configurazione.players.max) {
|
|
@@ -612,6 +668,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
612
668
|
const ora = this.adattatore.ora();
|
|
613
669
|
let player = dati.giocatori.find((item) => item.id === identity.id);
|
|
614
670
|
const nuovo = player === void 0;
|
|
671
|
+
const riconnesso = player !== void 0 && !player.connected;
|
|
615
672
|
if (player === void 0) {
|
|
616
673
|
player = {
|
|
617
674
|
...identity,
|
|
@@ -627,6 +684,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
627
684
|
dati.giocatori.push(player);
|
|
628
685
|
} else {
|
|
629
686
|
if (player.connected && player.connessione !== null && player.connessione !== connessione) {
|
|
687
|
+
this.frequenza.delete(player.connessione);
|
|
630
688
|
this.adattatore.chiudi(player.connessione, 4006, "replaced");
|
|
631
689
|
}
|
|
632
690
|
player.name = identity.name;
|
|
@@ -648,6 +706,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
648
706
|
copiaGiocatore(player)
|
|
649
707
|
);
|
|
650
708
|
}
|
|
709
|
+
if (riconnesso) {
|
|
710
|
+
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), true);
|
|
711
|
+
}
|
|
651
712
|
if (primaConnessione) {
|
|
652
713
|
await this.chiama(this.definizione.onStart, this.room);
|
|
653
714
|
this.inviaStatus(ora);
|
|
@@ -670,14 +731,16 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
670
731
|
player.connected = false;
|
|
671
732
|
player.connessione = null;
|
|
672
733
|
player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
|
|
673
|
-
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;
|
|
674
735
|
this.frequenza.delete(connessione);
|
|
675
736
|
if (this.dati.hostId === player.id) this.assegnaHost();
|
|
676
737
|
this.verificaCountdown();
|
|
738
|
+
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
739
|
+
await this.concludiEvento();
|
|
677
740
|
this.inviaGiocatori();
|
|
678
741
|
await this.persistiEProgramma();
|
|
679
742
|
}
|
|
680
|
-
async ricevi(connessione, frame) {
|
|
743
|
+
async ricevi(connessione, frame, limiteVerificato = false) {
|
|
681
744
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
682
745
|
if (await this.terminaSeInattiva()) return;
|
|
683
746
|
const player = this.dati.giocatori.find(
|
|
@@ -685,17 +748,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
685
748
|
);
|
|
686
749
|
if (player === void 0) return;
|
|
687
750
|
if (new TextEncoder().encode(frame).byteLength > LIMITE_FRAME) {
|
|
688
|
-
await this.chiudiConnessione(player, 4009, "
|
|
751
|
+
await this.chiudiConnessione(player, 4009, "message_too_large");
|
|
689
752
|
return;
|
|
690
753
|
}
|
|
691
754
|
const ora = this.adattatore.ora();
|
|
692
|
-
const recenti = (this.frequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
|
|
693
|
-
if (recenti.length >= 20) {
|
|
694
|
-
await this.chiudiConnessione(player, 4008, "rate_limited");
|
|
695
|
-
return;
|
|
696
|
-
}
|
|
697
|
-
recenti.push(ora);
|
|
698
|
-
this.frequenza.set(connessione, recenti);
|
|
699
755
|
let message = null;
|
|
700
756
|
try {
|
|
701
757
|
message = record(JSON.parse(frame));
|
|
@@ -705,6 +761,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
705
761
|
await this.chiudiConnessione(player, 4009, "bad_message");
|
|
706
762
|
return;
|
|
707
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
|
+
}
|
|
708
772
|
if (message.t === "ping") {
|
|
709
773
|
if (typeof message.c !== "number" || !Number.isFinite(message.c)) {
|
|
710
774
|
await this.chiudiConnessione(player, 4009, "bad_message");
|
|
@@ -727,6 +791,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
727
791
|
await this.persistiEProgramma();
|
|
728
792
|
return;
|
|
729
793
|
}
|
|
794
|
+
if (message.t === "restart") {
|
|
795
|
+
await this.rivincita(player);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
730
798
|
if (message.t === "ready") {
|
|
731
799
|
if (typeof message.ready !== "boolean") return this.messaggioErrato(player);
|
|
732
800
|
if (!this.inLobby(player)) return;
|
|
@@ -798,6 +866,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
798
866
|
await this.persistiEProgramma();
|
|
799
867
|
return;
|
|
800
868
|
}
|
|
869
|
+
if (this.dati.status === "finished") return;
|
|
801
870
|
this.dati.ultimoInputAt = ora;
|
|
802
871
|
await this.chiama(
|
|
803
872
|
this.definizione.onMessage,
|
|
@@ -868,7 +937,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
868
937
|
if (dovuti.length > 0) {
|
|
869
938
|
const ids = new Set(dovuti.map((timer) => timer.id));
|
|
870
939
|
this.dati.timer = this.dati.timer.filter((timer) => !ids.has(timer.id));
|
|
871
|
-
for (const timer of dovuti)
|
|
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
|
+
}
|
|
872
945
|
}
|
|
873
946
|
await this.concludiEvento();
|
|
874
947
|
if (!this.stanzaTerminata() && this.dati.giocatori.length === 0 && this.dati.vuotaDa !== null && this.dati.vuotaDa + STANZA_VUOTA_MS <= ora) {
|
|
@@ -899,22 +972,30 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
899
972
|
return esito;
|
|
900
973
|
}
|
|
901
974
|
async riconciliaConnessioni() {
|
|
975
|
+
if (this.dati === null || this.dati.status === "ended") return;
|
|
902
976
|
const dati = this.richiediDati();
|
|
903
977
|
const attive = new Set(this.adattatore.connessioniAttive());
|
|
904
978
|
const ora = this.adattatore.ora();
|
|
905
|
-
|
|
979
|
+
const disconnessi = [];
|
|
906
980
|
for (const player of dati.giocatori) {
|
|
907
981
|
if (player.connected && (player.connessione === null || !attive.has(player.connessione))) {
|
|
908
982
|
player.connected = false;
|
|
983
|
+
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
909
984
|
player.connessione = null;
|
|
910
985
|
player.graziaFinoA = ora + GRAZIA_MS;
|
|
911
|
-
if (this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
|
|
912
|
-
|
|
986
|
+
if (dati.status === "finished" || this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
|
|
987
|
+
disconnessi.push(player);
|
|
913
988
|
}
|
|
914
989
|
}
|
|
915
|
-
if (
|
|
990
|
+
if (disconnessi.length > 0) {
|
|
916
991
|
this.assegnaHost();
|
|
917
|
-
|
|
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();
|
|
918
999
|
}
|
|
919
1000
|
}
|
|
920
1001
|
ruoloAutomatico() {
|
|
@@ -1028,6 +1109,43 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1028
1109
|
}
|
|
1029
1110
|
return null;
|
|
1030
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
|
+
}
|
|
1031
1149
|
async avviaCountdown(player) {
|
|
1032
1150
|
const dati = this.richiediDati();
|
|
1033
1151
|
if (dati.hostId !== player.id) {
|
|
@@ -1064,6 +1182,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1064
1182
|
const indice = dati.giocatori.findIndex((item) => item.id === player.id);
|
|
1065
1183
|
if (indice < 0) return;
|
|
1066
1184
|
dati.giocatori.splice(indice, 1);
|
|
1185
|
+
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1067
1186
|
this.rivediVoce();
|
|
1068
1187
|
this.pulisciGuadagni(player.id);
|
|
1069
1188
|
if (dati.hostId === player.id) this.assegnaHost();
|
|
@@ -1103,6 +1222,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1103
1222
|
const json = analizzaJson(payload);
|
|
1104
1223
|
if (!json.ok) throw new TypeError("Schedule payload must be valid JSON.");
|
|
1105
1224
|
const dati = this.richiediDati();
|
|
1225
|
+
if (dati.status === "finished" || dati.status === "ended") return;
|
|
1106
1226
|
dati.timer.push({
|
|
1107
1227
|
id: dati.prossimoTimerId++,
|
|
1108
1228
|
at: this.adattatore.ora() + milliseconds,
|
|
@@ -1110,6 +1230,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1110
1230
|
payload: json.valore
|
|
1111
1231
|
});
|
|
1112
1232
|
}
|
|
1233
|
+
giornata(ora = this.adattatore.ora()) {
|
|
1234
|
+
return this.adattatore.giorno?.(ora) ?? giornoUtc2(ora);
|
|
1235
|
+
}
|
|
1113
1236
|
accodaPunteggio(playerId, board, score, daily) {
|
|
1114
1237
|
if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
|
|
1115
1238
|
throw new Error("Player not found.");
|
|
@@ -1121,25 +1244,27 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1121
1244
|
throw new TypeError("Score must be a non-negative safe integer.");
|
|
1122
1245
|
}
|
|
1123
1246
|
const submittedAt = this.adattatore.ora();
|
|
1247
|
+
const day = daily ? this.giornata(submittedAt) : null;
|
|
1124
1248
|
this.richiediDati().punteggi.push({
|
|
1125
1249
|
playerId,
|
|
1126
1250
|
board,
|
|
1127
1251
|
score,
|
|
1128
1252
|
daily,
|
|
1129
1253
|
submittedAt,
|
|
1130
|
-
day
|
|
1254
|
+
day
|
|
1131
1255
|
});
|
|
1132
1256
|
const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
|
|
1133
1257
|
if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
|
|
1134
1258
|
t: "score-queued",
|
|
1135
|
-
score: { player: playerId, board, score, day
|
|
1259
|
+
score: { player: playerId, board, score, day, submittedAt }
|
|
1136
1260
|
});
|
|
1137
1261
|
this.broadcast({ t: "flush" });
|
|
1138
1262
|
}
|
|
1139
|
-
richiediFine(result) {
|
|
1263
|
+
richiediFine(result, rematch = false) {
|
|
1140
1264
|
const json = analizzaJson(result);
|
|
1141
1265
|
if (!json.ok) throw new TypeError("Game result must be valid JSON.");
|
|
1142
|
-
this.
|
|
1266
|
+
if (this.dati?.status === "ended" || this.dati?.status === "finished" && rematch) return;
|
|
1267
|
+
this.fineRichiesta = { result: json.valore, rematch };
|
|
1143
1268
|
}
|
|
1144
1269
|
async salva(key, value) {
|
|
1145
1270
|
if (!CHIAVE.test(key)) {
|
|
@@ -1306,6 +1431,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1306
1431
|
id: dati.id,
|
|
1307
1432
|
seed: seedStanza(dati.id),
|
|
1308
1433
|
status: dati.status,
|
|
1434
|
+
result: dati.result,
|
|
1309
1435
|
mode: dati.mode,
|
|
1310
1436
|
tick: dati.tick,
|
|
1311
1437
|
tickRate: dati.tickRate,
|
|
@@ -1331,16 +1457,18 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1331
1457
|
host: dati.hostId,
|
|
1332
1458
|
countdownAt: dati.countdownAt,
|
|
1333
1459
|
at,
|
|
1334
|
-
result: dati.status === "ended" ? dati.result : null
|
|
1460
|
+
result: dati.status === "ended" || dati.status === "finished" ? dati.result : null
|
|
1335
1461
|
});
|
|
1336
1462
|
}
|
|
1337
1463
|
inviaDiff() {
|
|
1338
1464
|
const dati = this.richiediDati();
|
|
1339
1465
|
const patch = creaDiff(dati.statoSincronizzato, dati.state);
|
|
1340
|
-
if (patch.length === 0) return;
|
|
1466
|
+
if (patch.length === 0 && this.tickRateSincronizzato === dati.tickRate) return;
|
|
1467
|
+
this.tickRateSincronizzato = dati.tickRate;
|
|
1341
1468
|
this.broadcast({
|
|
1342
1469
|
t: "state",
|
|
1343
1470
|
tick: dati.tick,
|
|
1471
|
+
tickRate: dati.tickRate,
|
|
1344
1472
|
base: dati.tickSincronizzato,
|
|
1345
1473
|
serverTime: this.adattatore.ora(),
|
|
1346
1474
|
patch
|
|
@@ -1355,8 +1483,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1355
1483
|
dati.state = stato.valore;
|
|
1356
1484
|
dati.statoSincronizzato = copiaJson(stato.valore);
|
|
1357
1485
|
dati.tickSincronizzato = dati.tick;
|
|
1486
|
+
this.tickRateSincronizzato = dati.tickRate;
|
|
1358
1487
|
this.broadcast({
|
|
1359
1488
|
t: "snapshot",
|
|
1489
|
+
tickRate: dati.tickRate,
|
|
1360
1490
|
tick: dati.tick,
|
|
1361
1491
|
serverTime: this.adattatore.ora(),
|
|
1362
1492
|
state: stato.valore
|
|
@@ -1367,7 +1497,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1367
1497
|
try {
|
|
1368
1498
|
await callback(...args);
|
|
1369
1499
|
} catch {
|
|
1370
|
-
this.fineRichiesta = { error: "callback_error" };
|
|
1500
|
+
this.fineRichiesta = { result: { error: "callback_error" }, rematch: false };
|
|
1371
1501
|
}
|
|
1372
1502
|
}
|
|
1373
1503
|
async applicaAzioni() {
|
|
@@ -1386,7 +1516,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1386
1516
|
if (this.fineRichiesta !== null && this.dati.status !== "ended") {
|
|
1387
1517
|
const result = this.fineRichiesta;
|
|
1388
1518
|
this.fineRichiesta = null;
|
|
1389
|
-
await this.terminaInterna(result);
|
|
1519
|
+
await this.terminaInterna(result.result, result.rematch);
|
|
1390
1520
|
}
|
|
1391
1521
|
} finally {
|
|
1392
1522
|
this.applicandoAzioni = false;
|
|
@@ -1485,33 +1615,42 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1485
1615
|
dati.state = dati.statoSincronizzato;
|
|
1486
1616
|
await this.terminaInterna({ error: code });
|
|
1487
1617
|
}
|
|
1488
|
-
async terminaInterna(result) {
|
|
1618
|
+
async terminaInterna(result, rematch = false) {
|
|
1489
1619
|
const dati = this.richiediDati();
|
|
1490
1620
|
if (dati.status === "ended") return;
|
|
1491
|
-
dati.status
|
|
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;
|
|
1492
1626
|
dati.countdownAt = null;
|
|
1493
1627
|
dati.result = result;
|
|
1494
1628
|
dati.resultAt = this.adattatore.ora();
|
|
1495
1629
|
try {
|
|
1496
|
-
await this.definizione.onEnd?.(this.room);
|
|
1630
|
+
if (!giaFinita) await this.definizione.onEnd?.(this.room);
|
|
1497
1631
|
} catch {
|
|
1498
1632
|
if (record(result)?.error === void 0) dati.result = { error: "callback_error" };
|
|
1633
|
+
dati.status = "ended";
|
|
1634
|
+
dati.rivincitaFinoA = null;
|
|
1499
1635
|
}
|
|
1500
1636
|
const stato = analizzaJson(dati.state);
|
|
1501
1637
|
if (!stato.ok || stato.bytes > LIMITE_STATO) {
|
|
1502
1638
|
dati.state = dati.statoSincronizzato;
|
|
1503
1639
|
dati.result = { error: stato.ok ? "state_too_large" : "state_invalid" };
|
|
1640
|
+
dati.status = "ended";
|
|
1641
|
+
dati.rivincitaFinoA = null;
|
|
1504
1642
|
} else if (stato.testo !== JSON.stringify(dati.statoSincronizzato)) {
|
|
1505
1643
|
dati.tick++;
|
|
1506
1644
|
dati.state = stato.valore;
|
|
1507
1645
|
this.inviaDiff();
|
|
1508
1646
|
}
|
|
1509
1647
|
const fine = { result: dati.result, at: dati.resultAt };
|
|
1510
|
-
dati.fineInCoda = fine;
|
|
1648
|
+
if (dati.status === "ended") dati.fineInCoda = fine;
|
|
1649
|
+
if (dati.status === "finished") this.inviaGiocatori();
|
|
1511
1650
|
this.inviaStatus(dati.resultAt);
|
|
1512
1651
|
this.broadcast({ t: "flush" });
|
|
1513
1652
|
for (const player of dati.giocatori) {
|
|
1514
|
-
if (player.connected && player.connessione !== null) {
|
|
1653
|
+
if (dati.status === "ended" && player.connected && player.connessione !== null) {
|
|
1515
1654
|
this.adattatore.chiudi(player.connessione, 4004, "room_ended");
|
|
1516
1655
|
player.connected = false;
|
|
1517
1656
|
player.connessione = null;
|
|
@@ -1526,7 +1665,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1526
1665
|
if (dati.durateCpu.length > 50) dati.durateCpu.shift();
|
|
1527
1666
|
dati.cpuOltreCento = durata > 100 ? dati.cpuOltreCento + 1 : 0;
|
|
1528
1667
|
if (dati.cpuOltreCento >= 20) {
|
|
1529
|
-
this.fineRichiesta = { error: "cpu_budget" };
|
|
1668
|
+
this.fineRichiesta = { result: { error: "cpu_budget" }, rematch: false };
|
|
1530
1669
|
return;
|
|
1531
1670
|
}
|
|
1532
1671
|
if (dati.durateCpu.length === 50) {
|
|
@@ -1557,6 +1696,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1557
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;
|
|
1558
1697
|
}
|
|
1559
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
|
+
}
|
|
1560
1704
|
if (this.manifest.persistent === true) {
|
|
1561
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;
|
|
1562
1706
|
await this.terminaInterna({ error: "expired" });
|
|
@@ -1576,6 +1720,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1576
1720
|
this.serveTick() ? 1e3 / this.dati.tickRate : null
|
|
1577
1721
|
);
|
|
1578
1722
|
const prossime = [];
|
|
1723
|
+
if (this.dati.status === "finished") prossime.push(this.dati.rivincitaFinoA ?? this.adattatore.ora());
|
|
1579
1724
|
if (this.manifest.persistent === true) {
|
|
1580
1725
|
prossime.push(
|
|
1581
1726
|
Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
|
|
@@ -1863,10 +2008,11 @@ var ArchivioNode = class _ArchivioNode {
|
|
|
1863
2008
|
}
|
|
1864
2009
|
};
|
|
1865
2010
|
var AdattatoreNode = class {
|
|
1866
|
-
constructor(storage, deposito, ritardoSpettatori) {
|
|
2011
|
+
constructor(storage, deposito, ritardoSpettatori, dailyDay) {
|
|
1867
2012
|
this.storage = storage;
|
|
1868
2013
|
this.deposito = deposito;
|
|
1869
2014
|
this.ritardoSpettatori = ritardoSpettatori;
|
|
2015
|
+
this.dailyDay = dailyDay;
|
|
1870
2016
|
this.connessioni = /* @__PURE__ */ new Map();
|
|
1871
2017
|
this.spettatori = /* @__PURE__ */ new Map();
|
|
1872
2018
|
this.timerSpettatori = /* @__PURE__ */ new Set();
|
|
@@ -1957,6 +2103,10 @@ var AdattatoreNode = class {
|
|
|
1957
2103
|
ora() {
|
|
1958
2104
|
return Date.now();
|
|
1959
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
|
+
}
|
|
1960
2110
|
misuraCpu() {
|
|
1961
2111
|
return performance.now();
|
|
1962
2112
|
}
|
|
@@ -2025,7 +2175,7 @@ var StanzaNode = class {
|
|
|
2025
2175
|
this.voceListeners = /* @__PURE__ */ new Map();
|
|
2026
2176
|
this.voceFrequenza = /* @__PURE__ */ new Map();
|
|
2027
2177
|
this.voceUltimaRichiesta = /* @__PURE__ */ new Map();
|
|
2028
|
-
this.frameFrequenza =
|
|
2178
|
+
this.frameFrequenza = new LimiteMessaggiStanza();
|
|
2029
2179
|
this.connessioniGiocatori = /* @__PURE__ */ new Map();
|
|
2030
2180
|
this.giocatoriConnessioni = /* @__PURE__ */ new Map();
|
|
2031
2181
|
adattatore.collega({
|
|
@@ -2126,15 +2276,6 @@ var StanzaNode = class {
|
|
|
2126
2276
|
const player = this.nucleo.giocatoreConnesso(connessione);
|
|
2127
2277
|
if (player === null) return;
|
|
2128
2278
|
const ora = Date.now();
|
|
2129
|
-
const frames = (this.frameFrequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
|
|
2130
|
-
if (frames.length >= 20) {
|
|
2131
|
-
this.adattatore.chiudi(connessione, 4008, "rate_limited");
|
|
2132
|
-
this.rimuoviConnessione(connessione);
|
|
2133
|
-
await this.nucleo.disconnetti(connessione);
|
|
2134
|
-
return;
|
|
2135
|
-
}
|
|
2136
|
-
frames.push(ora);
|
|
2137
|
-
this.frameFrequenza.set(connessione, frames);
|
|
2138
2279
|
let value;
|
|
2139
2280
|
try {
|
|
2140
2281
|
value = JSON.parse(frame);
|
|
@@ -2143,17 +2284,31 @@ var StanzaNode = class {
|
|
|
2143
2284
|
return;
|
|
2144
2285
|
}
|
|
2145
2286
|
const message = typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
2146
|
-
if (message?.t
|
|
2147
|
-
|
|
2148
|
-
this.riconciliaRoster();
|
|
2149
|
-
return;
|
|
2150
|
-
}
|
|
2151
|
-
if (Buffer.byteLength(frame, "utf8") > 64 * 1024) {
|
|
2152
|
-
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");
|
|
2153
2289
|
this.rimuoviConnessione(connessione);
|
|
2154
2290
|
await this.nucleo.disconnetti(connessione);
|
|
2155
2291
|
return;
|
|
2156
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
|
+
}
|
|
2157
2312
|
await this.riceviVoce(connessione, player.id, player.role, message);
|
|
2158
2313
|
}
|
|
2159
2314
|
permessoSpettatore() {
|
|
@@ -2177,19 +2332,24 @@ var StanzaNode = class {
|
|
|
2177
2332
|
}
|
|
2178
2333
|
async riceviSpettatore(connessione, frame) {
|
|
2179
2334
|
if (Buffer.byteLength(frame, "utf8") > 16 * 1024) {
|
|
2180
|
-
this.adattatore.chiudiSpettatore(connessione,
|
|
2335
|
+
this.adattatore.chiudiSpettatore(connessione, 4009, "message_too_large");
|
|
2181
2336
|
this.frameFrequenza.delete(connessione);
|
|
2182
2337
|
return;
|
|
2183
2338
|
}
|
|
2184
2339
|
const ora = Date.now();
|
|
2185
|
-
const
|
|
2186
|
-
if (
|
|
2187
|
-
this.adattatore.
|
|
2188
|
-
|
|
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
|
+
}
|
|
2189
2351
|
return;
|
|
2190
2352
|
}
|
|
2191
|
-
frames.push(ora);
|
|
2192
|
-
this.frameFrequenza.set(connessione, frames);
|
|
2193
2353
|
let message = null;
|
|
2194
2354
|
try {
|
|
2195
2355
|
const value = JSON.parse(frame);
|
|
@@ -2418,9 +2578,10 @@ var StanzaNode = class {
|
|
|
2418
2578
|
}
|
|
2419
2579
|
};
|
|
2420
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.");
|
|
2421
2582
|
const storage = await ArchivioNode.apri(options.storageFile ?? null);
|
|
2422
2583
|
const ritardoSpettatori = manifest.spectators === null ? null : manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS;
|
|
2423
|
-
const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori);
|
|
2584
|
+
const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori, options.dailyDay);
|
|
2424
2585
|
const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
|
|
2425
2586
|
return new StanzaNode(nucleo, adattatore, manifest);
|
|
2426
2587
|
}
|