@caisual/kit 0.8.0 → 0.12.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 +529 -56
- package/dist/node.js +297 -77
- package/dist/overlay.js +360 -53
- package/dist/runtime.js +248 -47
- package/dist/server.js +2 -0
- package/dist/types/api.d.ts +2 -1
- package/dist/types/contracts/i18n.d.ts +12 -0
- package/dist/types/contracts/immagine.d.ts +27 -0
- package/dist/types/contracts/index.d.ts +5 -0
- package/dist/types/contracts/manifest.d.ts +14 -6
- package/dist/types/contracts/match-result.d.ts +16 -0
- package/dist/types/contracts/overlay.d.ts +16 -2
- package/dist/types/contracts/player.d.ts +1 -0
- package/dist/types/contracts/room-limits.d.ts +13 -0
- package/dist/types/daily.d.ts +14 -0
- package/dist/types/handshake.d.ts +4 -0
- package/dist/types/index.d.ts +24 -2
- package/dist/types/kit.d.ts +2 -0
- package/dist/types/node/index.d.ts +5 -0
- package/dist/types/overlay/host-bridge.d.ts +16 -0
- package/dist/types/overlay/i18n.d.ts +13 -0
- package/dist/types/overlay/index.d.ts +1 -0
- package/dist/types/overlay/styles.d.ts +1 -1
- package/dist/types/overlay/ui-model.d.ts +4 -0
- package/dist/types/overlay/ui.d.ts +3 -1
- package/dist/types/protocollo.d.ts +13 -0
- package/dist/types/runtime/index.d.ts +6 -1
- package/dist/types/server/index.d.ts +17 -2
- package/dist/types/stanza/nucleo.d.ts +8 -2
- package/dist/types/stanza/tipi.d.ts +13 -0
- package/dist/types/text.d.ts +2 -0
- package/package.json +1 -1
package/dist/runtime.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// src/daily.ts
|
|
2
|
+
function prossimaMezzanotteUtc(ora) {
|
|
3
|
+
return (Math.floor(ora / 864e5) + 1) * 864e5;
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
// ../contracts/src/slug.ts
|
|
2
7
|
var NOMI_RISERVATI = [
|
|
3
8
|
"www",
|
|
@@ -43,6 +48,47 @@ function modalitaLocale(manifest, mode) {
|
|
|
43
48
|
return mode !== null && manifest.modes.some((voce) => voce.id === mode && voce.execution === "local");
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
// ../contracts/src/room-limits.ts
|
|
52
|
+
var MESSAGGI_GIOCO_AL_SECONDO = 20;
|
|
53
|
+
var MESSAGGI_SERVIZIO_AL_SECONDO = 20;
|
|
54
|
+
var LimiteMessaggiStanza = class {
|
|
55
|
+
connessioni = /* @__PURE__ */ new Map();
|
|
56
|
+
delete(connessione) {
|
|
57
|
+
this.connessioni.delete(connessione);
|
|
58
|
+
}
|
|
59
|
+
clear() {
|
|
60
|
+
this.connessioni.clear();
|
|
61
|
+
}
|
|
62
|
+
controlla(connessione, gioco, ora) {
|
|
63
|
+
let stato = this.connessioni.get(connessione);
|
|
64
|
+
if (stato === void 0) {
|
|
65
|
+
const budget2 = () => ({ accettati: [], finestra: ora, tentativi: 0, finestreAbusive: 0 });
|
|
66
|
+
stato = { gioco: budget2(), servizio: budget2(), ultimoErrore: -Infinity };
|
|
67
|
+
this.connessioni.set(connessione, stato);
|
|
68
|
+
}
|
|
69
|
+
const budget = gioco ? stato.gioco : stato.servizio;
|
|
70
|
+
const limite = gioco ? MESSAGGI_GIOCO_AL_SECONDO : MESSAGGI_SERVIZIO_AL_SECONDO;
|
|
71
|
+
if (ora - budget.finestra >= 1e3) {
|
|
72
|
+
budget.finestreAbusive = ora - budget.finestra < 2e3 && budget.tentativi > limite * 5 ? budget.finestreAbusive + 1 : 0;
|
|
73
|
+
budget.finestra += Math.floor((ora - budget.finestra) / 1e3) * 1e3;
|
|
74
|
+
budget.tentativi = 0;
|
|
75
|
+
}
|
|
76
|
+
budget.tentativi = Math.min(budget.tentativi + 1, limite * 5 + 1);
|
|
77
|
+
budget.accettati = budget.accettati.filter((at) => ora - at < 1e3);
|
|
78
|
+
if (budget.accettati.length < limite) {
|
|
79
|
+
budget.accettati.push(ora);
|
|
80
|
+
return { accetta: true, avvisa: false, chiudi: false };
|
|
81
|
+
}
|
|
82
|
+
const avvisa = ora - stato.ultimoErrore >= 1e3;
|
|
83
|
+
if (avvisa) stato.ultimoErrore = ora;
|
|
84
|
+
return {
|
|
85
|
+
accetta: false,
|
|
86
|
+
avvisa,
|
|
87
|
+
chiudi: budget.finestreAbusive >= 2 && budget.tentativi > limite * 5
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
46
92
|
// src/stanza/json.ts
|
|
47
93
|
function isPlainObject(value) {
|
|
48
94
|
const prototype = Object.getPrototypeOf(value);
|
|
@@ -278,7 +324,9 @@ var LIMITE_DEPOSITO = 64 * 1024;
|
|
|
278
324
|
var LIMITE_OPERAZIONI_DEPOSITO = 120;
|
|
279
325
|
var GRAZIA_MS = 6e4;
|
|
280
326
|
var STANZA_VUOTA_MS = 5 * 6e4;
|
|
327
|
+
var ATTESA_RIVINCITA_MS = 2 * 6e4;
|
|
281
328
|
var COUNTDOWN_MS = 3e3;
|
|
329
|
+
var GRAZIA_GIORNALIERA_MS = 10 * 6e4;
|
|
282
330
|
var RIPOSO_TICK_MS = 3e4;
|
|
283
331
|
var INATTIVITA_MS = 10 * 6e4;
|
|
284
332
|
var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
|
|
@@ -329,21 +377,25 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
329
377
|
this.manifest = manifest;
|
|
330
378
|
this.adattatore = adattatore;
|
|
331
379
|
this.dati = null;
|
|
332
|
-
this.frequenza =
|
|
380
|
+
this.frequenza = new LimiteMessaggiStanza();
|
|
333
381
|
this.frequenzaDeposito = [];
|
|
334
382
|
this.kickRichiesti = /* @__PURE__ */ new Set();
|
|
335
383
|
this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
|
|
336
384
|
this.fineRichiesta = null;
|
|
337
385
|
this.applicandoAzioni = false;
|
|
338
386
|
this.ultimoStatoOsservato = "";
|
|
387
|
+
this.tickRateSincronizzato = null;
|
|
339
388
|
this.voceDaPersistire = false;
|
|
340
389
|
const nucleo = this;
|
|
341
390
|
const daily = {
|
|
342
391
|
get day() {
|
|
343
|
-
return
|
|
392
|
+
return nucleo.richiediDati().daily.day;
|
|
344
393
|
},
|
|
345
394
|
get seed() {
|
|
346
|
-
return
|
|
395
|
+
return nucleo.richiediDati().daily.seed;
|
|
396
|
+
},
|
|
397
|
+
get expiresAt() {
|
|
398
|
+
return nucleo.richiediDati().daily.expiresAt;
|
|
347
399
|
}
|
|
348
400
|
};
|
|
349
401
|
this.room = {
|
|
@@ -397,8 +449,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
397
449
|
setTeam(player, team) {
|
|
398
450
|
nucleo.impostaSquadraDalServer(idGiocatore(player), team);
|
|
399
451
|
},
|
|
400
|
-
end(result) {
|
|
401
|
-
nucleo.richiediFine(result);
|
|
452
|
+
end(result, options) {
|
|
453
|
+
nucleo.richiediFine(result, options?.rematch ?? false);
|
|
402
454
|
},
|
|
403
455
|
save(key, value) {
|
|
404
456
|
return nucleo.salva(key, value);
|
|
@@ -428,7 +480,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
428
480
|
},
|
|
429
481
|
board: {
|
|
430
482
|
submit(player, board, score, options = {}) {
|
|
431
|
-
|
|
483
|
+
if (typeof board === "number") {
|
|
484
|
+
const active = nucleo.richiediDati().giocatori.filter((p) => p.role !== "spectator");
|
|
485
|
+
if (nucleo.configurazione.players.max !== 1 || active.length !== 1) throw erroreConCodice("board_player_required", "Specify the player when submitting a multiplayer score.");
|
|
486
|
+
nucleo.accodaPunteggio(active[0].id, player, board, typeof score === "object" ? score : {});
|
|
487
|
+
} else nucleo.accodaPunteggio(idGiocatore(player), board, score, options);
|
|
432
488
|
}
|
|
433
489
|
},
|
|
434
490
|
daily,
|
|
@@ -458,7 +514,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
458
514
|
const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
|
|
459
515
|
if (salvato !== void 0) {
|
|
460
516
|
nucleo.dati = salvato;
|
|
461
|
-
|
|
517
|
+
nucleo.tickRateSincronizzato = salvato.tickRate;
|
|
518
|
+
const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0;
|
|
519
|
+
salvato.daily ??= nucleo.contestoGiornaliero();
|
|
462
520
|
salvato.ultimoInputAt ??= adattatore.ora();
|
|
463
521
|
salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
|
|
464
522
|
nucleo.ultimoStatoOsservato = JSON.stringify(salvato.state);
|
|
@@ -490,6 +548,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
490
548
|
const ora = this.adattatore.ora();
|
|
491
549
|
this.dati = {
|
|
492
550
|
versione: 1,
|
|
551
|
+
daily: this.contestoGiornaliero(ora),
|
|
493
552
|
id,
|
|
494
553
|
mode,
|
|
495
554
|
status: "lobby",
|
|
@@ -532,7 +591,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
532
591
|
return true;
|
|
533
592
|
}
|
|
534
593
|
get configurazione() {
|
|
535
|
-
|
|
594
|
+
const mode = risolviModalita(this.manifest, this.dati?.mode ?? null);
|
|
595
|
+
return mode.players.max === 1 ? { ...mode, lobby: false } : mode;
|
|
536
596
|
}
|
|
537
597
|
info(playerId) {
|
|
538
598
|
if (this.dati === null) return null;
|
|
@@ -585,7 +645,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
585
645
|
if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
|
|
586
646
|
const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
|
|
587
647
|
if (esistente !== void 0) return { ok: true };
|
|
588
|
-
if (this.configurazione.lobby && this.dati.status
|
|
648
|
+
if (this.configurazione.lobby && !["lobby", "finished"].includes(this.dati.status)) {
|
|
589
649
|
return { ok: false, code: "room_playing" };
|
|
590
650
|
}
|
|
591
651
|
if (this.dati.giocatori.length >= this.configurazione.players.max) {
|
|
@@ -604,6 +664,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
604
664
|
const ora = this.adattatore.ora();
|
|
605
665
|
let player = dati.giocatori.find((item) => item.id === identity.id);
|
|
606
666
|
const nuovo = player === void 0;
|
|
667
|
+
const riconnesso = player !== void 0 && !player.connected;
|
|
607
668
|
if (player === void 0) {
|
|
608
669
|
player = {
|
|
609
670
|
...identity,
|
|
@@ -619,6 +680,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
619
680
|
dati.giocatori.push(player);
|
|
620
681
|
} else {
|
|
621
682
|
if (player.connected && player.connessione !== null && player.connessione !== connessione) {
|
|
683
|
+
this.frequenza.delete(player.connessione);
|
|
622
684
|
this.adattatore.chiudi(player.connessione, 4006, "replaced");
|
|
623
685
|
}
|
|
624
686
|
player.name = identity.name;
|
|
@@ -640,6 +702,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
640
702
|
copiaGiocatore(player)
|
|
641
703
|
);
|
|
642
704
|
}
|
|
705
|
+
if (riconnesso) {
|
|
706
|
+
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), true);
|
|
707
|
+
}
|
|
643
708
|
if (primaConnessione) {
|
|
644
709
|
await this.chiama(this.definizione.onStart, this.room);
|
|
645
710
|
this.inviaStatus(ora);
|
|
@@ -662,14 +727,16 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
662
727
|
player.connected = false;
|
|
663
728
|
player.connessione = null;
|
|
664
729
|
player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
|
|
665
|
-
if (this.manifest.persistent === true && this.dati.status === "lobby") player.ready = false;
|
|
730
|
+
if (this.dati.status === "finished" || this.manifest.persistent === true && this.dati.status === "lobby") player.ready = false;
|
|
666
731
|
this.frequenza.delete(connessione);
|
|
667
732
|
if (this.dati.hostId === player.id) this.assegnaHost();
|
|
668
733
|
this.verificaCountdown();
|
|
734
|
+
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
735
|
+
await this.concludiEvento();
|
|
669
736
|
this.inviaGiocatori();
|
|
670
737
|
await this.persistiEProgramma();
|
|
671
738
|
}
|
|
672
|
-
async ricevi(connessione, frame) {
|
|
739
|
+
async ricevi(connessione, frame, limiteVerificato = false) {
|
|
673
740
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
674
741
|
if (await this.terminaSeInattiva()) return;
|
|
675
742
|
const player = this.dati.giocatori.find(
|
|
@@ -677,17 +744,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
677
744
|
);
|
|
678
745
|
if (player === void 0) return;
|
|
679
746
|
if (new TextEncoder().encode(frame).byteLength > LIMITE_FRAME) {
|
|
680
|
-
await this.chiudiConnessione(player, 4009, "
|
|
747
|
+
await this.chiudiConnessione(player, 4009, "message_too_large");
|
|
681
748
|
return;
|
|
682
749
|
}
|
|
683
750
|
const ora = this.adattatore.ora();
|
|
684
|
-
const recenti = (this.frequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
|
|
685
|
-
if (recenti.length >= 20) {
|
|
686
|
-
await this.chiudiConnessione(player, 4008, "rate_limited");
|
|
687
|
-
return;
|
|
688
|
-
}
|
|
689
|
-
recenti.push(ora);
|
|
690
|
-
this.frequenza.set(connessione, recenti);
|
|
691
751
|
let message = null;
|
|
692
752
|
try {
|
|
693
753
|
message = record(JSON.parse(frame));
|
|
@@ -697,6 +757,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
697
757
|
await this.chiudiConnessione(player, 4009, "bad_message");
|
|
698
758
|
return;
|
|
699
759
|
}
|
|
760
|
+
if (!limiteVerificato) {
|
|
761
|
+
const limite = this.frequenza.controlla(connessione, message.t === "msg", ora);
|
|
762
|
+
if (!limite.accetta) {
|
|
763
|
+
if (limite.avvisa) this.inviaErrore(player, "rate_limited", "Too many room messages. Excess messages are dropped.");
|
|
764
|
+
if (limite.chiudi) await this.chiudiConnessione(player, 4008, "rate_limited");
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
700
768
|
if (message.t === "ping") {
|
|
701
769
|
if (typeof message.c !== "number" || !Number.isFinite(message.c)) {
|
|
702
770
|
await this.chiudiConnessione(player, 4009, "bad_message");
|
|
@@ -719,6 +787,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
719
787
|
await this.persistiEProgramma();
|
|
720
788
|
return;
|
|
721
789
|
}
|
|
790
|
+
if (message.t === "restart") {
|
|
791
|
+
await this.rivincita(player);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
722
794
|
if (message.t === "ready") {
|
|
723
795
|
if (typeof message.ready !== "boolean") return this.messaggioErrato(player);
|
|
724
796
|
if (!this.inLobby(player)) return;
|
|
@@ -790,6 +862,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
790
862
|
await this.persistiEProgramma();
|
|
791
863
|
return;
|
|
792
864
|
}
|
|
865
|
+
if (this.dati.status === "finished") return;
|
|
793
866
|
this.dati.ultimoInputAt = ora;
|
|
794
867
|
await this.chiama(
|
|
795
868
|
this.definizione.onMessage,
|
|
@@ -842,7 +915,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
842
915
|
else await this.rimuoviGiocatore(player, "timeout");
|
|
843
916
|
}
|
|
844
917
|
if (this.dati.status === "countdown" && this.dati.countdownAt !== null && this.dati.countdownAt <= ora) {
|
|
845
|
-
const errore = this.erroreMinimi();
|
|
918
|
+
const errore = this.erroreMinimi(this.dati.countdownRivincita);
|
|
846
919
|
if (errore !== null) {
|
|
847
920
|
this.dati.status = "lobby";
|
|
848
921
|
this.dati.countdownAt = null;
|
|
@@ -860,7 +933,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
860
933
|
if (dovuti.length > 0) {
|
|
861
934
|
const ids = new Set(dovuti.map((timer) => timer.id));
|
|
862
935
|
this.dati.timer = this.dati.timer.filter((timer) => !ids.has(timer.id));
|
|
863
|
-
for (const timer of dovuti)
|
|
936
|
+
for (const timer of dovuti) {
|
|
937
|
+
await this.eseguiTimer(timer);
|
|
938
|
+
await this.applicaAzioni();
|
|
939
|
+
if (this.dati.status === "finished" || this.stanzaTerminata()) break;
|
|
940
|
+
}
|
|
864
941
|
}
|
|
865
942
|
await this.concludiEvento();
|
|
866
943
|
if (!this.stanzaTerminata() && this.dati.giocatori.length === 0 && this.dati.vuotaDa !== null && this.dati.vuotaDa + STANZA_VUOTA_MS <= ora) {
|
|
@@ -891,22 +968,30 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
891
968
|
return esito;
|
|
892
969
|
}
|
|
893
970
|
async riconciliaConnessioni() {
|
|
971
|
+
if (this.dati === null || this.dati.status === "ended") return;
|
|
894
972
|
const dati = this.richiediDati();
|
|
895
973
|
const attive = new Set(this.adattatore.connessioniAttive());
|
|
896
974
|
const ora = this.adattatore.ora();
|
|
897
|
-
|
|
975
|
+
const disconnessi = [];
|
|
898
976
|
for (const player of dati.giocatori) {
|
|
899
977
|
if (player.connected && (player.connessione === null || !attive.has(player.connessione))) {
|
|
900
978
|
player.connected = false;
|
|
979
|
+
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
901
980
|
player.connessione = null;
|
|
902
981
|
player.graziaFinoA = ora + GRAZIA_MS;
|
|
903
|
-
if (this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
|
|
904
|
-
|
|
982
|
+
if (dati.status === "finished" || this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
|
|
983
|
+
disconnessi.push(player);
|
|
905
984
|
}
|
|
906
985
|
}
|
|
907
|
-
if (
|
|
986
|
+
if (disconnessi.length > 0) {
|
|
908
987
|
this.assegnaHost();
|
|
909
|
-
|
|
988
|
+
this.verificaCountdown();
|
|
989
|
+
for (const player of disconnessi) {
|
|
990
|
+
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
991
|
+
}
|
|
992
|
+
await this.concludiEvento();
|
|
993
|
+
this.inviaGiocatori();
|
|
994
|
+
await this.persistiEProgramma();
|
|
910
995
|
}
|
|
911
996
|
}
|
|
912
997
|
ruoloAutomatico() {
|
|
@@ -998,13 +1083,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
998
1083
|
if (player === void 0) throw erroreConCodice("player_not_found", "Player not found.");
|
|
999
1084
|
return player;
|
|
1000
1085
|
}
|
|
1001
|
-
erroreMinimi() {
|
|
1086
|
+
erroreMinimi(saltaPronti = false) {
|
|
1002
1087
|
const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
|
|
1003
1088
|
const attivi = connessi.filter((player) => player.role !== "spectator");
|
|
1004
1089
|
if (attivi.length < this.configurazione.players.min) {
|
|
1005
1090
|
return { code: "not_enough_players", message: "The room does not have enough players." };
|
|
1006
1091
|
}
|
|
1007
|
-
if (connessi.some((player) => !player.ready)) {
|
|
1092
|
+
if (!saltaPronti && connessi.some((player) => !player.ready)) {
|
|
1008
1093
|
return { code: "players_not_ready", message: "Every connected player must be ready." };
|
|
1009
1094
|
}
|
|
1010
1095
|
for (const role of this.manifest.roles) {
|
|
@@ -1020,6 +1105,60 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1020
1105
|
}
|
|
1021
1106
|
return null;
|
|
1022
1107
|
}
|
|
1108
|
+
async rivincita(player) {
|
|
1109
|
+
const dati = this.richiediDati();
|
|
1110
|
+
if (dati.status !== "finished") {
|
|
1111
|
+
this.inviaErrore(player, "rematch_unavailable", "This room is not waiting for a rematch.");
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
if (this.configurazione.players.max === 1) {
|
|
1115
|
+
await this.avviaRivincita();
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
if (player.role !== "spectator" && !player.ready) {
|
|
1119
|
+
player.ready = true;
|
|
1120
|
+
this.inviaGiocatori();
|
|
1121
|
+
if (dati.rivincita?.autoStart && this.prontiRivincita()) await this.avviaRivincita();
|
|
1122
|
+
else await this.persistiEProgramma();
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
if (dati.hostId !== player.id) return;
|
|
1126
|
+
if (!this.prontiRivincita(dati.rivincita?.autoStart === true)) {
|
|
1127
|
+
this.inviaErrore(player, "players_not_ready", "Enough connected players must be ready for the rematch.");
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
await this.avviaRivincita();
|
|
1131
|
+
}
|
|
1132
|
+
prontiRivincita(anticipata = false) {
|
|
1133
|
+
const attivi = this.richiediDati().giocatori.filter((item) => item.connected && item.role !== "spectator");
|
|
1134
|
+
return attivi.filter((item) => item.ready).length >= this.configurazione.players.min && (anticipata || attivi.every((item) => item.ready));
|
|
1135
|
+
}
|
|
1136
|
+
async avviaRivincita() {
|
|
1137
|
+
const dati = this.richiediDati(), solo = this.configurazione.players.max === 1;
|
|
1138
|
+
const keepSetup = dati.rivincita?.keepSetup === true && !solo;
|
|
1139
|
+
dati.status = solo ? "playing" : keepSetup ? "countdown" : this.configurazione.lobby ? "lobby" : "playing";
|
|
1140
|
+
dati.countdownRivincita = keepSetup;
|
|
1141
|
+
dati.countdownAt = keepSetup ? this.adattatore.ora() + COUNTDOWN_MS : null;
|
|
1142
|
+
dati.rivincitaFinoA = null;
|
|
1143
|
+
dati.rivincita = null;
|
|
1144
|
+
dati.result = null;
|
|
1145
|
+
dati.resultAt = null;
|
|
1146
|
+
dati.ultimoInputAt = this.adattatore.ora();
|
|
1147
|
+
for (const item of dati.giocatori) item.ready = keepSetup && (dati.prontiPrecedenti?.includes(item.id) ?? false);
|
|
1148
|
+
dati.prontiPrecedenti = [];
|
|
1149
|
+
await this.chiama(this.definizione.onRestart, this.room);
|
|
1150
|
+
await this.applicaAzioni();
|
|
1151
|
+
if (dati.status === "playing") {
|
|
1152
|
+
await this.chiama(this.definizione.onStart, this.room);
|
|
1153
|
+
}
|
|
1154
|
+
await this.concludiEvento();
|
|
1155
|
+
if (dati.status === "lobby" || dati.status === "playing" || dati.status === "countdown") {
|
|
1156
|
+
this.inviaSnapshotTutti();
|
|
1157
|
+
this.inviaGiocatori();
|
|
1158
|
+
this.inviaStatus(this.adattatore.ora());
|
|
1159
|
+
}
|
|
1160
|
+
await this.persistiEProgramma();
|
|
1161
|
+
}
|
|
1023
1162
|
async avviaCountdown(player) {
|
|
1024
1163
|
const dati = this.richiediDati();
|
|
1025
1164
|
if (dati.hostId !== player.id) {
|
|
@@ -1032,13 +1171,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1032
1171
|
return;
|
|
1033
1172
|
}
|
|
1034
1173
|
dati.status = "countdown";
|
|
1174
|
+
dati.countdownRivincita = false;
|
|
1035
1175
|
dati.countdownAt = this.adattatore.ora() + COUNTDOWN_MS;
|
|
1036
1176
|
this.inviaStatus(dati.countdownAt);
|
|
1037
1177
|
await this.persistiEProgramma();
|
|
1038
1178
|
}
|
|
1039
1179
|
verificaCountdown() {
|
|
1040
1180
|
const dati = this.richiediDati();
|
|
1041
|
-
if (dati.status !== "countdown" || this.erroreMinimi() === null) return;
|
|
1181
|
+
if (dati.status !== "countdown" || this.erroreMinimi(dati.countdownRivincita) === null) return;
|
|
1042
1182
|
dati.status = "lobby";
|
|
1043
1183
|
dati.countdownAt = null;
|
|
1044
1184
|
this.inviaStatus(this.adattatore.ora());
|
|
@@ -1056,6 +1196,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1056
1196
|
const indice = dati.giocatori.findIndex((item) => item.id === player.id);
|
|
1057
1197
|
if (indice < 0) return;
|
|
1058
1198
|
dati.giocatori.splice(indice, 1);
|
|
1199
|
+
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1059
1200
|
this.rivediVoce();
|
|
1060
1201
|
this.pulisciGuadagni(player.id);
|
|
1061
1202
|
if (dati.hostId === player.id) this.assegnaHost();
|
|
@@ -1095,6 +1236,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1095
1236
|
const json = analizzaJson(payload);
|
|
1096
1237
|
if (!json.ok) throw new TypeError("Schedule payload must be valid JSON.");
|
|
1097
1238
|
const dati = this.richiediDati();
|
|
1239
|
+
if (dati.status === "finished" || dati.status === "ended") return;
|
|
1098
1240
|
dati.timer.push({
|
|
1099
1241
|
id: dati.prossimoTimerId++,
|
|
1100
1242
|
at: this.adattatore.ora() + milliseconds,
|
|
@@ -1102,7 +1244,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1102
1244
|
payload: json.valore
|
|
1103
1245
|
});
|
|
1104
1246
|
}
|
|
1105
|
-
|
|
1247
|
+
giornata(ora = this.adattatore.ora()) {
|
|
1248
|
+
return this.adattatore.giorno?.(ora) ?? giornoUtc(ora);
|
|
1249
|
+
}
|
|
1250
|
+
contestoGiornaliero(ora = this.adattatore.ora()) {
|
|
1251
|
+
const day = this.giornata(ora);
|
|
1252
|
+
return { day, seed: seedGiornata(this.manifest.id, day), expiresAt: prossimaMezzanotteUtc(ora) };
|
|
1253
|
+
}
|
|
1254
|
+
accodaPunteggio(playerId, board, score, options) {
|
|
1106
1255
|
if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
|
|
1107
1256
|
throw new Error("Player not found.");
|
|
1108
1257
|
}
|
|
@@ -1113,25 +1262,39 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1113
1262
|
throw new TypeError("Score must be a non-negative safe integer.");
|
|
1114
1263
|
}
|
|
1115
1264
|
const submittedAt = this.adattatore.ora();
|
|
1265
|
+
const daily = options.daily === true || options.day !== void 0;
|
|
1266
|
+
let day = daily ? this.giornata(submittedAt) : null;
|
|
1267
|
+
const start = this.manifest.boards?.[board]?.day === "start";
|
|
1268
|
+
if (options.day !== void 0 && (!start || options.daily === false)) {
|
|
1269
|
+
throw erroreConCodice("board_day_policy", "An explicit day requires a start-day board and a daily score.");
|
|
1270
|
+
}
|
|
1271
|
+
if (daily && start) {
|
|
1272
|
+
const run = this.richiediDati().daily;
|
|
1273
|
+
if (options.day === void 0) throw erroreConCodice("board_day_required", "Submit the daily score with room.daily.day.");
|
|
1274
|
+
if (options.day !== run.day) throw erroreConCodice("board_day_mismatch", "The score day must match room.daily.day.");
|
|
1275
|
+
if (submittedAt > run.expiresAt + GRAZIA_GIORNALIERA_MS) throw erroreConCodice("board_day_expired", "The starting day grace period has expired.");
|
|
1276
|
+
day = run.day;
|
|
1277
|
+
}
|
|
1116
1278
|
this.richiediDati().punteggi.push({
|
|
1117
1279
|
playerId,
|
|
1118
1280
|
board,
|
|
1119
1281
|
score,
|
|
1120
1282
|
daily,
|
|
1121
1283
|
submittedAt,
|
|
1122
|
-
day
|
|
1284
|
+
day
|
|
1123
1285
|
});
|
|
1124
1286
|
const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
|
|
1125
1287
|
if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
|
|
1126
1288
|
t: "score-queued",
|
|
1127
|
-
score: { player: playerId, board, score, day
|
|
1289
|
+
score: { player: playerId, board, score, day, submittedAt }
|
|
1128
1290
|
});
|
|
1129
1291
|
this.broadcast({ t: "flush" });
|
|
1130
1292
|
}
|
|
1131
|
-
richiediFine(result) {
|
|
1293
|
+
richiediFine(result, rematch = false) {
|
|
1132
1294
|
const json = analizzaJson(result);
|
|
1133
1295
|
if (!json.ok) throw new TypeError("Game result must be valid JSON.");
|
|
1134
|
-
this.
|
|
1296
|
+
if (this.dati?.status === "ended" || this.dati?.status === "finished" && rematch) return;
|
|
1297
|
+
this.fineRichiesta = { result: json.valore, rematch };
|
|
1135
1298
|
}
|
|
1136
1299
|
async salva(key, value) {
|
|
1137
1300
|
if (!CHIAVE.test(key)) {
|
|
@@ -1298,6 +1461,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1298
1461
|
id: dati.id,
|
|
1299
1462
|
seed: seedStanza(dati.id),
|
|
1300
1463
|
status: dati.status,
|
|
1464
|
+
result: dati.result,
|
|
1465
|
+
rematch: dati.status === "finished" ? dati.rivincita ?? null : null,
|
|
1301
1466
|
mode: dati.mode,
|
|
1302
1467
|
tick: dati.tick,
|
|
1303
1468
|
tickRate: dati.tickRate,
|
|
@@ -1319,20 +1484,23 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1319
1484
|
const dati = this.richiediDati();
|
|
1320
1485
|
this.broadcast({
|
|
1321
1486
|
t: "status",
|
|
1487
|
+
rematch: dati.status === "finished" ? dati.rivincita ?? null : null,
|
|
1322
1488
|
status: dati.status,
|
|
1323
1489
|
host: dati.hostId,
|
|
1324
1490
|
countdownAt: dati.countdownAt,
|
|
1325
1491
|
at,
|
|
1326
|
-
result: dati.status === "ended" ? dati.result : null
|
|
1492
|
+
result: dati.status === "ended" || dati.status === "finished" ? dati.result : null
|
|
1327
1493
|
});
|
|
1328
1494
|
}
|
|
1329
1495
|
inviaDiff() {
|
|
1330
1496
|
const dati = this.richiediDati();
|
|
1331
1497
|
const patch = creaDiff(dati.statoSincronizzato, dati.state);
|
|
1332
|
-
if (patch.length === 0) return;
|
|
1498
|
+
if (patch.length === 0 && this.tickRateSincronizzato === dati.tickRate) return;
|
|
1499
|
+
this.tickRateSincronizzato = dati.tickRate;
|
|
1333
1500
|
this.broadcast({
|
|
1334
1501
|
t: "state",
|
|
1335
1502
|
tick: dati.tick,
|
|
1503
|
+
tickRate: dati.tickRate,
|
|
1336
1504
|
base: dati.tickSincronizzato,
|
|
1337
1505
|
serverTime: this.adattatore.ora(),
|
|
1338
1506
|
patch
|
|
@@ -1347,8 +1515,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1347
1515
|
dati.state = stato.valore;
|
|
1348
1516
|
dati.statoSincronizzato = copiaJson(stato.valore);
|
|
1349
1517
|
dati.tickSincronizzato = dati.tick;
|
|
1518
|
+
this.tickRateSincronizzato = dati.tickRate;
|
|
1350
1519
|
this.broadcast({
|
|
1351
1520
|
t: "snapshot",
|
|
1521
|
+
tickRate: dati.tickRate,
|
|
1352
1522
|
tick: dati.tick,
|
|
1353
1523
|
serverTime: this.adattatore.ora(),
|
|
1354
1524
|
state: stato.valore
|
|
@@ -1359,7 +1529,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1359
1529
|
try {
|
|
1360
1530
|
await callback(...args);
|
|
1361
1531
|
} catch {
|
|
1362
|
-
this.fineRichiesta = { error: "callback_error" };
|
|
1532
|
+
this.fineRichiesta = { result: { error: "callback_error" }, rematch: false };
|
|
1363
1533
|
}
|
|
1364
1534
|
}
|
|
1365
1535
|
async applicaAzioni() {
|
|
@@ -1378,7 +1548,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1378
1548
|
if (this.fineRichiesta !== null && this.dati.status !== "ended") {
|
|
1379
1549
|
const result = this.fineRichiesta;
|
|
1380
1550
|
this.fineRichiesta = null;
|
|
1381
|
-
await this.terminaInterna(result);
|
|
1551
|
+
await this.terminaInterna(result.result, result.rematch);
|
|
1382
1552
|
}
|
|
1383
1553
|
} finally {
|
|
1384
1554
|
this.applicandoAzioni = false;
|
|
@@ -1387,6 +1557,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1387
1557
|
async concludiEvento() {
|
|
1388
1558
|
await this.applicaAzioni();
|
|
1389
1559
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
1560
|
+
if (this.dati.status === "finished" && this.dati.rivincita?.autoStart && this.prontiRivincita()) {
|
|
1561
|
+
await this.avviaRivincita();
|
|
1562
|
+
return;
|
|
1563
|
+
}
|
|
1390
1564
|
this.inviaGuadagniCambiati();
|
|
1391
1565
|
const stato = await this.verificaStato();
|
|
1392
1566
|
if (stato === null || this.stanzaTerminata()) return;
|
|
@@ -1477,33 +1651,47 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1477
1651
|
dati.state = dati.statoSincronizzato;
|
|
1478
1652
|
await this.terminaInterna({ error: code });
|
|
1479
1653
|
}
|
|
1480
|
-
async terminaInterna(result) {
|
|
1654
|
+
async terminaInterna(result, rematch = false) {
|
|
1481
1655
|
const dati = this.richiediDati();
|
|
1482
1656
|
if (dati.status === "ended") return;
|
|
1483
|
-
dati.status
|
|
1657
|
+
const giaFinita = dati.status === "finished";
|
|
1658
|
+
dati.status = rematch ? "finished" : "ended";
|
|
1659
|
+
dati.rivincita = rematch ? {
|
|
1660
|
+
keepSetup: typeof rematch === "object" && rematch.keepSetup === true,
|
|
1661
|
+
autoStart: typeof rematch === "object" && rematch.autoStart === true
|
|
1662
|
+
} : null;
|
|
1663
|
+
dati.prontiPrecedenti = rematch ? dati.giocatori.filter((p) => p.ready).map((p) => p.id) : [];
|
|
1664
|
+
dati.rivincitaFinoA = rematch ? this.adattatore.ora() + ATTESA_RIVINCITA_MS : null;
|
|
1665
|
+
dati.timer = [];
|
|
1666
|
+
if (rematch) for (const player of dati.giocatori) player.ready = false;
|
|
1484
1667
|
dati.countdownAt = null;
|
|
1485
1668
|
dati.result = result;
|
|
1486
1669
|
dati.resultAt = this.adattatore.ora();
|
|
1487
1670
|
try {
|
|
1488
|
-
await this.definizione.onEnd?.(this.room);
|
|
1671
|
+
if (!giaFinita) await this.definizione.onEnd?.(this.room);
|
|
1489
1672
|
} catch {
|
|
1490
1673
|
if (record(result)?.error === void 0) dati.result = { error: "callback_error" };
|
|
1674
|
+
dati.status = "ended";
|
|
1675
|
+
dati.rivincitaFinoA = null;
|
|
1491
1676
|
}
|
|
1492
1677
|
const stato = analizzaJson(dati.state);
|
|
1493
1678
|
if (!stato.ok || stato.bytes > LIMITE_STATO) {
|
|
1494
1679
|
dati.state = dati.statoSincronizzato;
|
|
1495
1680
|
dati.result = { error: stato.ok ? "state_too_large" : "state_invalid" };
|
|
1681
|
+
dati.status = "ended";
|
|
1682
|
+
dati.rivincitaFinoA = null;
|
|
1496
1683
|
} else if (stato.testo !== JSON.stringify(dati.statoSincronizzato)) {
|
|
1497
1684
|
dati.tick++;
|
|
1498
1685
|
dati.state = stato.valore;
|
|
1499
1686
|
this.inviaDiff();
|
|
1500
1687
|
}
|
|
1501
1688
|
const fine = { result: dati.result, at: dati.resultAt };
|
|
1502
|
-
dati.fineInCoda = fine;
|
|
1689
|
+
if (dati.status === "ended") dati.fineInCoda = fine;
|
|
1690
|
+
if (dati.status === "finished") this.inviaGiocatori();
|
|
1503
1691
|
this.inviaStatus(dati.resultAt);
|
|
1504
1692
|
this.broadcast({ t: "flush" });
|
|
1505
1693
|
for (const player of dati.giocatori) {
|
|
1506
|
-
if (player.connected && player.connessione !== null) {
|
|
1694
|
+
if (dati.status === "ended" && player.connected && player.connessione !== null) {
|
|
1507
1695
|
this.adattatore.chiudi(player.connessione, 4004, "room_ended");
|
|
1508
1696
|
player.connected = false;
|
|
1509
1697
|
player.connessione = null;
|
|
@@ -1518,7 +1706,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1518
1706
|
if (dati.durateCpu.length > 50) dati.durateCpu.shift();
|
|
1519
1707
|
dati.cpuOltreCento = durata > 100 ? dati.cpuOltreCento + 1 : 0;
|
|
1520
1708
|
if (dati.cpuOltreCento >= 20) {
|
|
1521
|
-
this.fineRichiesta = { error: "cpu_budget" };
|
|
1709
|
+
this.fineRichiesta = { result: { error: "cpu_budget" }, rematch: false };
|
|
1522
1710
|
return;
|
|
1523
1711
|
}
|
|
1524
1712
|
if (dati.durateCpu.length === 50) {
|
|
@@ -1549,6 +1737,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1549
1737
|
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;
|
|
1550
1738
|
}
|
|
1551
1739
|
async terminaSeInattiva() {
|
|
1740
|
+
if (this.dati?.status === "finished") {
|
|
1741
|
+
if (this.adattatore.ora() < (this.dati.rivincitaFinoA ?? 0)) return false;
|
|
1742
|
+
await this.terminaInterna(this.dati.result);
|
|
1743
|
+
return true;
|
|
1744
|
+
}
|
|
1552
1745
|
if (this.manifest.persistent === true) {
|
|
1553
1746
|
if (this.dati?.status === "ended" || this.dati === null || this.adattatore.ora() < Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS) return false;
|
|
1554
1747
|
await this.terminaInterna({ error: "expired" });
|
|
@@ -1568,6 +1761,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1568
1761
|
this.serveTick() ? 1e3 / this.dati.tickRate : null
|
|
1569
1762
|
);
|
|
1570
1763
|
const prossime = [];
|
|
1764
|
+
if (this.dati.status === "finished") prossime.push(this.dati.rivincitaFinoA ?? this.adattatore.ora());
|
|
1571
1765
|
if (this.manifest.persistent === true) {
|
|
1572
1766
|
prossime.push(
|
|
1573
1767
|
Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
|
|
@@ -1758,12 +1952,12 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1758
1952
|
await nucleo.entra(identita, connessione);
|
|
1759
1953
|
});
|
|
1760
1954
|
}
|
|
1761
|
-
ricevi(connessione, testo) {
|
|
1955
|
+
ricevi(connessione, testo, limiteVerificato = false) {
|
|
1762
1956
|
return this.#esegui(async (nucleo) => {
|
|
1763
1957
|
if (new TextEncoder().encode(testo).byteLength > 16 * 1024) {
|
|
1764
|
-
this.#adattatore.chiudi(connessione,
|
|
1958
|
+
this.#adattatore.chiudi(connessione, 4009, "message_too_large");
|
|
1765
1959
|
await nucleo.disconnetti(connessione);
|
|
1766
|
-
} else await nucleo.ricevi(connessione, testo);
|
|
1960
|
+
} else await nucleo.ricevi(connessione, testo, limiteVerificato);
|
|
1767
1961
|
});
|
|
1768
1962
|
}
|
|
1769
1963
|
disconnetti(connessione) {
|
|
@@ -1772,6 +1966,13 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1772
1966
|
await nucleo.disconnetti(connessione);
|
|
1773
1967
|
});
|
|
1774
1968
|
}
|
|
1969
|
+
riconciliaConnessioni(connessioni) {
|
|
1970
|
+
return this.#esegui(async (nucleo) => {
|
|
1971
|
+
this.#adattatore.connessioni.clear();
|
|
1972
|
+
for (const connessione of connessioni) this.#adattatore.connessioni.add(connessione);
|
|
1973
|
+
await nucleo.riconciliaConnessioni();
|
|
1974
|
+
});
|
|
1975
|
+
}
|
|
1775
1976
|
tick() {
|
|
1776
1977
|
return this.#esegui((nucleo) => nucleo.eseguiTick());
|
|
1777
1978
|
}
|
package/dist/server.js
CHANGED
package/dist/types/api.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { BoardResult, BoardTop, JsonValue, Player, SaveInfo } from './index.js';
|
|
2
2
|
import type { GestoreBiglietto } from './handshake.js';
|
|
3
3
|
export interface RispostaMe {
|
|
4
|
-
player: Player
|
|
4
|
+
player: Omit<Player, 'language' | 'uiLanguage'>;
|
|
5
5
|
game: {
|
|
6
6
|
slug: string;
|
|
7
7
|
};
|
|
8
8
|
day: string;
|
|
9
9
|
seed: number;
|
|
10
10
|
serverTime: number;
|
|
11
|
+
expiresAt?: number;
|
|
11
12
|
}
|
|
12
13
|
export declare function creaClienteApi(appOrigin: string, fetcher: typeof fetch, biglietto: GestoreBiglietto): {
|
|
13
14
|
me: () => Promise<RispostaMe>;
|