@caisual/kit 0.4.0 → 0.8.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.d.ts +2 -157
- package/dist/kit.js +1539 -59
- package/dist/node.d.ts +1 -77
- package/dist/node.js +341 -42
- package/dist/overlay.d.ts +1 -0
- package/dist/overlay.js +1846 -0
- package/dist/runtime.d.ts +1 -108
- package/dist/runtime.js +191 -41
- package/dist/server.d.ts +1 -71
- package/dist/server.js +1 -0
- package/dist/types/api.d.ts +25 -0
- package/dist/types/contracts/biglietto.d.ts +24 -0
- package/dist/types/contracts/device.d.ts +66 -0
- package/dist/types/contracts/firma.d.ts +21 -0
- package/dist/types/contracts/index.d.ts +10 -0
- package/dist/types/contracts/keys.d.ts +59 -0
- package/dist/types/contracts/manifest.d.ts +118 -0
- package/dist/types/contracts/overlay-boards.d.ts +3 -0
- package/dist/types/contracts/overlay.d.ts +215 -0
- package/dist/types/contracts/runtime.d.ts +70 -0
- package/dist/types/contracts/server-js.d.ts +11 -0
- package/dist/types/contracts/slug.d.ts +5 -0
- package/dist/types/contracts/token-crew.d.ts +21 -0
- package/dist/types/daily.d.ts +3 -0
- package/dist/types/errors.d.ts +6 -0
- package/dist/types/handshake.d.ts +26 -0
- package/dist/types/http.d.ts +3 -0
- package/dist/types/index.d.ts +217 -0
- package/dist/types/kit.d.ts +26 -0
- package/dist/types/node/index.d.ts +96 -0
- package/dist/types/node/websocket.d.ts +29 -0
- package/dist/types/overlay/boards.d.ts +48 -0
- package/dist/types/overlay/bridge.d.ts +3 -0
- package/dist/types/overlay/host-bridge.d.ts +58 -0
- package/dist/types/overlay/i18n.d.ts +122 -0
- package/dist/types/overlay/index.d.ts +9 -0
- package/dist/types/overlay/shortcut.d.ts +4 -0
- package/dist/types/overlay/styles.d.ts +1 -0
- package/dist/types/overlay/ui-model.d.ts +45 -0
- package/dist/types/overlay/ui.d.ts +80 -0
- package/dist/types/overlay/voice-panel.d.ts +13 -0
- package/dist/types/protocollo.d.ts +257 -0
- package/dist/types/runtime/index.d.ts +127 -0
- package/dist/types/server/index.d.ts +72 -0
- package/dist/types/session/index.d.ts +49 -0
- package/dist/types/session/resume.d.ts +13 -0
- package/dist/types/standalone.d.ts +14 -0
- package/dist/types/stanza/index.d.ts +3 -0
- package/dist/types/stanza/json.d.ts +18 -0
- package/dist/types/stanza/nucleo.d.ts +97 -0
- package/dist/types/stanza/tipi.d.ts +134 -0
- package/dist/types/stanza-client/index.d.ts +24 -0
- package/dist/types/voce/index.d.ts +124 -0
- package/package.json +10 -2
package/dist/node.d.ts
CHANGED
|
@@ -1,77 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Duplex } from 'node:stream';
|
|
3
|
-
import type { GameDefinition, JsonValue, RoomStatus, SharedGameStore } from './server.js';
|
|
4
|
-
export interface NodeWebSocket {
|
|
5
|
-
on(event: 'message', listener: (message: string) => void): this;
|
|
6
|
-
on(event: 'close', listener: () => void): this;
|
|
7
|
-
send(message: string): void;
|
|
8
|
-
close(code?: number, reason?: string): void;
|
|
9
|
-
}
|
|
10
|
-
export declare function acceptNodeWebSocket(request: IncomingMessage, socket: Duplex, head?: Buffer): NodeWebSocket;
|
|
11
|
-
export interface NodePlayerIdentity {
|
|
12
|
-
id: string;
|
|
13
|
-
name: string;
|
|
14
|
-
guest: boolean;
|
|
15
|
-
}
|
|
16
|
-
export interface NodeRoomManifest {
|
|
17
|
-
id: string;
|
|
18
|
-
players: {
|
|
19
|
-
min: number;
|
|
20
|
-
max: number;
|
|
21
|
-
};
|
|
22
|
-
lobby: boolean;
|
|
23
|
-
persistent?: boolean;
|
|
24
|
-
roles: Array<{
|
|
25
|
-
id: string;
|
|
26
|
-
min: number;
|
|
27
|
-
max?: number;
|
|
28
|
-
}>;
|
|
29
|
-
teams: {
|
|
30
|
-
min: number;
|
|
31
|
-
max: number;
|
|
32
|
-
} | null;
|
|
33
|
-
modes: Array<{
|
|
34
|
-
id: string;
|
|
35
|
-
}>;
|
|
36
|
-
voice?: 'none' | 'room' | 'team' | 'proximity';
|
|
37
|
-
}
|
|
38
|
-
export type NodeRoomJoinResult = {
|
|
39
|
-
ok: true;
|
|
40
|
-
} | {
|
|
41
|
-
ok: false;
|
|
42
|
-
code: 'room_not_found' | 'room_full' | 'room_ended' | 'room_playing';
|
|
43
|
-
};
|
|
44
|
-
export interface NodeRoomInfo {
|
|
45
|
-
roomId: string;
|
|
46
|
-
status: RoomStatus;
|
|
47
|
-
players: number;
|
|
48
|
-
max: number;
|
|
49
|
-
mode: string | null;
|
|
50
|
-
}
|
|
51
|
-
export interface NodeRoomFlush {
|
|
52
|
-
scores: Array<{
|
|
53
|
-
playerId: string;
|
|
54
|
-
board: string;
|
|
55
|
-
score: number;
|
|
56
|
-
daily: boolean;
|
|
57
|
-
}>;
|
|
58
|
-
ended: {
|
|
59
|
-
result: JsonValue;
|
|
60
|
-
at: number;
|
|
61
|
-
} | null;
|
|
62
|
-
}
|
|
63
|
-
export interface NodeRoomOptions {
|
|
64
|
-
storageFile?: string;
|
|
65
|
-
deposito?: SharedGameStore | null;
|
|
66
|
-
}
|
|
67
|
-
export interface NodeRoom {
|
|
68
|
-
create(roomId: string, mode: string | null, creator: NodePlayerIdentity): Promise<boolean>;
|
|
69
|
-
info(): Promise<NodeRoomInfo | null>;
|
|
70
|
-
canJoin(identity: NodePlayerIdentity): Promise<NodeRoomJoinResult>;
|
|
71
|
-
connect(socket: NodeWebSocket, identity: NodePlayerIdentity): Promise<NodeRoomJoinResult>;
|
|
72
|
-
flush(): Promise<NodeRoomFlush>;
|
|
73
|
-
wake(): Promise<void>;
|
|
74
|
-
close(): Promise<void>;
|
|
75
|
-
}
|
|
76
|
-
/** Crea una stanza Node usando memoria o un singolo file JSON persistente. */
|
|
77
|
-
export declare function createNodeRoom(definition: GameDefinition, manifest: NodeRoomManifest, options?: NodeRoomOptions): Promise<NodeRoom>;
|
|
1
|
+
export * from './types/node/index.js';
|
package/dist/node.js
CHANGED
|
@@ -4,6 +4,53 @@ import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { dirname } from "node:path";
|
|
5
5
|
import { performance } from "node:perf_hooks";
|
|
6
6
|
|
|
7
|
+
// ../contracts/src/slug.ts
|
|
8
|
+
var NOMI_RISERVATI = [
|
|
9
|
+
"www",
|
|
10
|
+
"api",
|
|
11
|
+
"app",
|
|
12
|
+
"play",
|
|
13
|
+
"live",
|
|
14
|
+
"multi",
|
|
15
|
+
"cdn",
|
|
16
|
+
"assets",
|
|
17
|
+
"static",
|
|
18
|
+
"mail",
|
|
19
|
+
"mx",
|
|
20
|
+
"ns1",
|
|
21
|
+
"ns2",
|
|
22
|
+
"autodiscover",
|
|
23
|
+
"_dmarc",
|
|
24
|
+
"admin",
|
|
25
|
+
"login",
|
|
26
|
+
"account",
|
|
27
|
+
"auth",
|
|
28
|
+
"pay",
|
|
29
|
+
"secure",
|
|
30
|
+
"support",
|
|
31
|
+
"help",
|
|
32
|
+
"blog",
|
|
33
|
+
"status",
|
|
34
|
+
"dev",
|
|
35
|
+
"staging",
|
|
36
|
+
"test",
|
|
37
|
+
"caisual",
|
|
38
|
+
"shipz"
|
|
39
|
+
];
|
|
40
|
+
var RISERVATI = new Set(NOMI_RISERVATI);
|
|
41
|
+
|
|
42
|
+
// ../contracts/src/manifest.ts
|
|
43
|
+
function risolviModalita(manifest, mode) {
|
|
44
|
+
const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);
|
|
45
|
+
if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");
|
|
46
|
+
return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };
|
|
47
|
+
}
|
|
48
|
+
function modalitaLocale(manifest, mode) {
|
|
49
|
+
return mode !== null && manifest.modes.some((voce) => voce.id === mode && voce.execution === "local");
|
|
50
|
+
}
|
|
51
|
+
var MASSIMO_SPETTATORI = 100;
|
|
52
|
+
var RITARDO_SPETTATORI_MS = 3e3;
|
|
53
|
+
|
|
7
54
|
// src/stanza/json.ts
|
|
8
55
|
function isPlainObject(value) {
|
|
9
56
|
const prototype = Object.getPrototypeOf(value);
|
|
@@ -446,9 +493,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
446
493
|
}
|
|
447
494
|
async crea(id, mode, _creator) {
|
|
448
495
|
if (this.dati !== null) return false;
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
}
|
|
496
|
+
risolviModalita(this.manifest, mode);
|
|
497
|
+
if (modalitaLocale(this.manifest, mode)) throw new Error("Local modes cannot create rooms.");
|
|
452
498
|
const ora = this.adattatore.ora();
|
|
453
499
|
this.dati = {
|
|
454
500
|
versione: 1,
|
|
@@ -493,6 +539,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
493
539
|
await this.persistiEProgramma();
|
|
494
540
|
return true;
|
|
495
541
|
}
|
|
542
|
+
get configurazione() {
|
|
543
|
+
return risolviModalita(this.manifest, this.dati?.mode ?? null);
|
|
544
|
+
}
|
|
496
545
|
info(playerId) {
|
|
497
546
|
if (this.dati === null) return null;
|
|
498
547
|
return {
|
|
@@ -501,26 +550,53 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
501
550
|
players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
|
|
502
551
|
(player) => player.connected || player.graziaFinoA !== null
|
|
503
552
|
).length,
|
|
504
|
-
max: this.
|
|
553
|
+
max: this.configurazione.players.max,
|
|
505
554
|
mode: this.dati.mode,
|
|
506
555
|
member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
|
|
507
556
|
};
|
|
508
557
|
}
|
|
558
|
+
fotografia() {
|
|
559
|
+
if (this.dati === null) return null;
|
|
560
|
+
return {
|
|
561
|
+
room: this.stanzaProtocollo(),
|
|
562
|
+
players: this.giocatoriProtocollo(),
|
|
563
|
+
state: this.dati.state
|
|
564
|
+
};
|
|
565
|
+
}
|
|
509
566
|
giocatoreConnesso(connessione) {
|
|
510
567
|
const player = this.dati?.giocatori.find(
|
|
511
568
|
(item) => item.connected && item.connessione === connessione
|
|
512
569
|
);
|
|
513
570
|
return player === void 0 ? null : copiaGiocatore(player);
|
|
514
571
|
}
|
|
572
|
+
puoAscoltare(listener, speakers) {
|
|
573
|
+
const mode = this.manifest.voice ?? "none";
|
|
574
|
+
if (mode === "none") return speakers.map(() => ({ ok: false, reason: "mode" }));
|
|
575
|
+
const dati = this.richiediDati();
|
|
576
|
+
const ascoltatore = dati.giocatori.find((player) => player.id === listener);
|
|
577
|
+
return speakers.map((speakerId) => {
|
|
578
|
+
const speaker = dati.giocatori.find((player) => player.id === speakerId);
|
|
579
|
+
if (ascoltatore === void 0 || speaker === void 0 || listener === speakerId) {
|
|
580
|
+
return { ok: false, reason: "unknown" };
|
|
581
|
+
}
|
|
582
|
+
if (mode === "team" && ascoltatore.role !== "spectator" && ascoltatore.team !== speaker.team) {
|
|
583
|
+
return { ok: false, reason: "team" };
|
|
584
|
+
}
|
|
585
|
+
if ((dati.voceGuadagni?.[listener]?.[speakerId] ?? 1) <= 0) {
|
|
586
|
+
return { ok: false, reason: "gain" };
|
|
587
|
+
}
|
|
588
|
+
return { ok: true };
|
|
589
|
+
});
|
|
590
|
+
}
|
|
515
591
|
puoEntrare(identity) {
|
|
516
592
|
if (this.dati === null) return { ok: false, code: "room_not_found" };
|
|
517
593
|
if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
|
|
518
594
|
const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
|
|
519
595
|
if (esistente !== void 0) return { ok: true };
|
|
520
|
-
if (this.
|
|
596
|
+
if (this.configurazione.lobby && this.dati.status !== "lobby") {
|
|
521
597
|
return { ok: false, code: "room_playing" };
|
|
522
598
|
}
|
|
523
|
-
if (this.dati.giocatori.length >= this.
|
|
599
|
+
if (this.dati.giocatori.length >= this.configurazione.players.max) {
|
|
524
600
|
return { ok: false, code: "room_full" };
|
|
525
601
|
}
|
|
526
602
|
return { ok: true };
|
|
@@ -563,7 +639,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
563
639
|
dati.hostId ??= player.id;
|
|
564
640
|
dati.vuotaDa = null;
|
|
565
641
|
dati.ultimoInputAt = ora;
|
|
566
|
-
const primaConnessione = !this.
|
|
642
|
+
const primaConnessione = !this.configurazione.lobby && dati.status === "lobby";
|
|
567
643
|
if (primaConnessione) dati.status = "playing";
|
|
568
644
|
if (nuovo) {
|
|
569
645
|
await this.chiama(
|
|
@@ -669,6 +745,26 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
669
745
|
await this.persistiEProgramma();
|
|
670
746
|
return;
|
|
671
747
|
}
|
|
748
|
+
if (message.t === "request-role") {
|
|
749
|
+
if (!Number.isSafeInteger(message.r) || message.r < 1 || typeof message.role !== "string") return this.messaggioErrato(player);
|
|
750
|
+
let code = null;
|
|
751
|
+
if (this.dati.status !== "playing" || this.definizione.onRoleRequest === void 0) code = "role_change_unavailable";
|
|
752
|
+
else if (!this.manifest.roles.some((role) => role.id === message.role)) code = "invalid_role";
|
|
753
|
+
else {
|
|
754
|
+
this.dati.ultimoInputAt = ora;
|
|
755
|
+
await this.chiama(this.definizione.onRoleRequest, this.room, copiaGiocatore(player), message.role);
|
|
756
|
+
await this.concludiEvento();
|
|
757
|
+
if (player.role !== message.role) code = "role_change_refused";
|
|
758
|
+
}
|
|
759
|
+
if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
|
|
760
|
+
t: "role-result",
|
|
761
|
+
r: message.r,
|
|
762
|
+
ok: code === null,
|
|
763
|
+
code
|
|
764
|
+
});
|
|
765
|
+
await this.persistiEProgramma();
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
672
768
|
if (message.t === "team") {
|
|
673
769
|
if (!Number.isInteger(message.team)) return this.messaggioErrato(player);
|
|
674
770
|
if (!this.inLobby(player)) return;
|
|
@@ -737,23 +833,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
737
833
|
this.inviaGuadagniCambiati();
|
|
738
834
|
const stato = await this.verificaStato();
|
|
739
835
|
if (stato === null || this.stanzaTerminata()) return;
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
dati.state = stato.valore;
|
|
743
|
-
this.inviaSnapshotTutti();
|
|
744
|
-
} else if (cambiato) {
|
|
745
|
-
const base = dati.tickSincronizzato;
|
|
746
|
-
this.broadcast({
|
|
747
|
-
t: "state",
|
|
748
|
-
tick: dati.tick,
|
|
749
|
-
serverTime: this.adattatore.ora(),
|
|
750
|
-
base,
|
|
751
|
-
patch: creaDiff(dati.statoSincronizzato, stato.valore)
|
|
752
|
-
});
|
|
753
|
-
dati.state = stato.valore;
|
|
754
|
-
dati.statoSincronizzato = copiaJson(stato.valore);
|
|
755
|
-
dati.tickSincronizzato = dati.tick;
|
|
756
|
-
}
|
|
836
|
+
dati.state = stato.valore;
|
|
837
|
+
this.inviaDiff();
|
|
757
838
|
if (dati.tick % 100 === 0 || this.voceDaPersistire) await this.persisti();
|
|
758
839
|
await this.aggiornaProgrammazione();
|
|
759
840
|
}
|
|
@@ -882,10 +963,15 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
882
963
|
if (ruolo?.max !== void 0 && occupati >= ruolo.max) {
|
|
883
964
|
return { code: "role_full", message: "This role is full." };
|
|
884
965
|
}
|
|
966
|
+
const ruoloPrecedente = player.role;
|
|
967
|
+
const squadraPrecedente = player.team;
|
|
885
968
|
player.role = roleId;
|
|
886
969
|
if (roleId === "spectator") player.team = null;
|
|
887
970
|
else if (player.team === null) player.team = this.squadraAutomatica();
|
|
888
971
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
972
|
+
if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
|
|
973
|
+
this.rivediVoce();
|
|
974
|
+
}
|
|
889
975
|
this.inviaGiocatori();
|
|
890
976
|
return null;
|
|
891
977
|
}
|
|
@@ -896,8 +982,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
896
982
|
if (player.role === "spectator") {
|
|
897
983
|
return { code: "spectator", message: "Spectators cannot join a team." };
|
|
898
984
|
}
|
|
985
|
+
const squadraPrecedente = player.team;
|
|
899
986
|
player.team = team;
|
|
900
987
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
988
|
+
if (player.team !== squadraPrecedente) this.rivediVoce();
|
|
901
989
|
this.inviaGiocatori();
|
|
902
990
|
return null;
|
|
903
991
|
}
|
|
@@ -921,7 +1009,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
921
1009
|
erroreMinimi() {
|
|
922
1010
|
const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
|
|
923
1011
|
const attivi = connessi.filter((player) => player.role !== "spectator");
|
|
924
|
-
if (attivi.length < this.
|
|
1012
|
+
if (attivi.length < this.configurazione.players.min) {
|
|
925
1013
|
return { code: "not_enough_players", message: "The room does not have enough players." };
|
|
926
1014
|
}
|
|
927
1015
|
if (connessi.some((player) => !player.ready)) {
|
|
@@ -976,6 +1064,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
976
1064
|
const indice = dati.giocatori.findIndex((item) => item.id === player.id);
|
|
977
1065
|
if (indice < 0) return;
|
|
978
1066
|
dati.giocatori.splice(indice, 1);
|
|
1067
|
+
this.rivediVoce();
|
|
979
1068
|
this.pulisciGuadagni(player.id);
|
|
980
1069
|
if (dati.hostId === player.id) this.assegnaHost();
|
|
981
1070
|
if (dati.giocatori.length === 0) dati.vuotaDa = this.adattatore.ora();
|
|
@@ -1022,7 +1111,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1022
1111
|
});
|
|
1023
1112
|
}
|
|
1024
1113
|
accodaPunteggio(playerId, board, score, daily) {
|
|
1025
|
-
if (!this.richiediDati().giocatori.some((
|
|
1114
|
+
if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
|
|
1026
1115
|
throw new Error("Player not found.");
|
|
1027
1116
|
}
|
|
1028
1117
|
if (!CHIAVE.test(board)) {
|
|
@@ -1031,7 +1120,20 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1031
1120
|
if (!Number.isSafeInteger(score) || score < 0) {
|
|
1032
1121
|
throw new TypeError("Score must be a non-negative safe integer.");
|
|
1033
1122
|
}
|
|
1034
|
-
this.
|
|
1123
|
+
const submittedAt = this.adattatore.ora();
|
|
1124
|
+
this.richiediDati().punteggi.push({
|
|
1125
|
+
playerId,
|
|
1126
|
+
board,
|
|
1127
|
+
score,
|
|
1128
|
+
daily,
|
|
1129
|
+
submittedAt,
|
|
1130
|
+
day: daily ? giornoUtc(submittedAt) : null
|
|
1131
|
+
});
|
|
1132
|
+
const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
|
|
1133
|
+
if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
|
|
1134
|
+
t: "score-queued",
|
|
1135
|
+
score: { player: playerId, board, score, day: daily ? giornoUtc(submittedAt) : null, submittedAt }
|
|
1136
|
+
});
|
|
1035
1137
|
this.broadcast({ t: "flush" });
|
|
1036
1138
|
}
|
|
1037
1139
|
richiediFine(result) {
|
|
@@ -1165,6 +1267,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1165
1267
|
this.adattatore.invia(player.connessione, message);
|
|
1166
1268
|
}
|
|
1167
1269
|
}
|
|
1270
|
+
if (message.t !== "flush") this.adattatore.pubblica(message);
|
|
1168
1271
|
}
|
|
1169
1272
|
inviaErrore(player, code, message) {
|
|
1170
1273
|
if (player.connected && player.connessione !== null) {
|
|
@@ -1188,16 +1291,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1188
1291
|
this.adattatore.invia(player.connessione, {
|
|
1189
1292
|
t: "welcome",
|
|
1190
1293
|
you: player.id,
|
|
1191
|
-
room:
|
|
1192
|
-
id: dati.id,
|
|
1193
|
-
seed: seedStanza(dati.id),
|
|
1194
|
-
status: dati.status,
|
|
1195
|
-
mode: dati.mode,
|
|
1196
|
-
tick: dati.tick,
|
|
1197
|
-
tickRate: dati.tickRate,
|
|
1198
|
-
serverTime: this.adattatore.ora(),
|
|
1199
|
-
host: dati.hostId
|
|
1200
|
-
},
|
|
1294
|
+
room: this.stanzaProtocollo(),
|
|
1201
1295
|
players: this.giocatoriProtocollo(),
|
|
1202
1296
|
state: dati.state
|
|
1203
1297
|
});
|
|
@@ -1206,18 +1300,54 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1206
1300
|
this.adattatore.invia(player.connessione, { t: "voice", op: "gain", gains: { ...gains } });
|
|
1207
1301
|
}
|
|
1208
1302
|
}
|
|
1303
|
+
stanzaProtocollo() {
|
|
1304
|
+
const dati = this.richiediDati();
|
|
1305
|
+
return {
|
|
1306
|
+
id: dati.id,
|
|
1307
|
+
seed: seedStanza(dati.id),
|
|
1308
|
+
status: dati.status,
|
|
1309
|
+
mode: dati.mode,
|
|
1310
|
+
tick: dati.tick,
|
|
1311
|
+
tickRate: dati.tickRate,
|
|
1312
|
+
serverTime: this.adattatore.ora(),
|
|
1313
|
+
host: dati.hostId,
|
|
1314
|
+
countdownAt: dati.countdownAt,
|
|
1315
|
+
configuration: {
|
|
1316
|
+
players: { ...this.configurazione.players },
|
|
1317
|
+
lobby: this.configurazione.lobby,
|
|
1318
|
+
persistent: this.manifest.persistent === true,
|
|
1319
|
+
requestRole: this.definizione.onRoleRequest !== void 0
|
|
1320
|
+
}
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1209
1323
|
inviaGiocatori() {
|
|
1210
|
-
this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
|
|
1324
|
+
this.broadcast({ t: "players", players: this.giocatoriProtocollo(), host: this.richiediDati().hostId });
|
|
1211
1325
|
}
|
|
1212
1326
|
inviaStatus(at) {
|
|
1213
1327
|
const dati = this.richiediDati();
|
|
1214
1328
|
this.broadcast({
|
|
1215
1329
|
t: "status",
|
|
1216
1330
|
status: dati.status,
|
|
1331
|
+
host: dati.hostId,
|
|
1332
|
+
countdownAt: dati.countdownAt,
|
|
1217
1333
|
at,
|
|
1218
1334
|
result: dati.status === "ended" ? dati.result : null
|
|
1219
1335
|
});
|
|
1220
1336
|
}
|
|
1337
|
+
inviaDiff() {
|
|
1338
|
+
const dati = this.richiediDati();
|
|
1339
|
+
const patch = creaDiff(dati.statoSincronizzato, dati.state);
|
|
1340
|
+
if (patch.length === 0) return;
|
|
1341
|
+
this.broadcast({
|
|
1342
|
+
t: "state",
|
|
1343
|
+
tick: dati.tick,
|
|
1344
|
+
base: dati.tickSincronizzato,
|
|
1345
|
+
serverTime: this.adattatore.ora(),
|
|
1346
|
+
patch
|
|
1347
|
+
});
|
|
1348
|
+
dati.statoSincronizzato = copiaJson(dati.state);
|
|
1349
|
+
dati.tickSincronizzato = dati.tick;
|
|
1350
|
+
}
|
|
1221
1351
|
inviaSnapshotTutti() {
|
|
1222
1352
|
const dati = this.richiediDati();
|
|
1223
1353
|
const stato = analizzaJson(dati.state);
|
|
@@ -1272,7 +1402,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1272
1402
|
this.dati.state = stato.valore;
|
|
1273
1403
|
if ((this.dati.tickRate === 0 || this.dati.status !== "playing") && stato.testo !== precedente) {
|
|
1274
1404
|
this.dati.tick++;
|
|
1275
|
-
this.
|
|
1405
|
+
this.inviaDiff();
|
|
1276
1406
|
}
|
|
1277
1407
|
}
|
|
1278
1408
|
async verificaStato() {
|
|
@@ -1306,6 +1436,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1306
1436
|
dati.voceGuadagni ??= {};
|
|
1307
1437
|
const precedente = dati.voceGuadagni[playerId]?.[altroId] ?? 1;
|
|
1308
1438
|
if (precedente === valore) return;
|
|
1439
|
+
if (precedente > 0 && valore === 0) this.rivediVoce();
|
|
1309
1440
|
this.voceDaPersistire = true;
|
|
1310
1441
|
if (valore === 1) {
|
|
1311
1442
|
const riga = dati.voceGuadagni[playerId];
|
|
@@ -1323,6 +1454,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1323
1454
|
this.voceGuadagniCambiati.set(playerId, cambi);
|
|
1324
1455
|
return cambi;
|
|
1325
1456
|
}
|
|
1457
|
+
rivediVoce() {
|
|
1458
|
+
if ((this.manifest.voice ?? "none") !== "none") this.adattatore.rivediVoce();
|
|
1459
|
+
}
|
|
1326
1460
|
inviaGuadagniCambiati() {
|
|
1327
1461
|
if (this.dati === null || this.voceGuadagniCambiati.size === 0) return;
|
|
1328
1462
|
for (const [playerId, gains] of this.voceGuadagniCambiati) {
|
|
@@ -1370,7 +1504,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1370
1504
|
} else if (stato.testo !== JSON.stringify(dati.statoSincronizzato)) {
|
|
1371
1505
|
dati.tick++;
|
|
1372
1506
|
dati.state = stato.valore;
|
|
1373
|
-
this.
|
|
1507
|
+
this.inviaDiff();
|
|
1374
1508
|
}
|
|
1375
1509
|
const fine = { result: dati.result, at: dati.resultAt };
|
|
1376
1510
|
dati.fineInCoda = fine;
|
|
@@ -1729,10 +1863,13 @@ var ArchivioNode = class _ArchivioNode {
|
|
|
1729
1863
|
}
|
|
1730
1864
|
};
|
|
1731
1865
|
var AdattatoreNode = class {
|
|
1732
|
-
constructor(storage, deposito) {
|
|
1866
|
+
constructor(storage, deposito, ritardoSpettatori) {
|
|
1733
1867
|
this.storage = storage;
|
|
1734
1868
|
this.deposito = deposito;
|
|
1869
|
+
this.ritardoSpettatori = ritardoSpettatori;
|
|
1735
1870
|
this.connessioni = /* @__PURE__ */ new Map();
|
|
1871
|
+
this.spettatori = /* @__PURE__ */ new Map();
|
|
1872
|
+
this.timerSpettatori = /* @__PURE__ */ new Set();
|
|
1736
1873
|
this.tickTimer = null;
|
|
1737
1874
|
this.tickIntervallo = null;
|
|
1738
1875
|
this.tickGenerazione = 0;
|
|
@@ -1751,12 +1888,66 @@ var AdattatoreNode = class {
|
|
|
1751
1888
|
rimuovi(id) {
|
|
1752
1889
|
this.connessioni.delete(id);
|
|
1753
1890
|
}
|
|
1891
|
+
aggiungiSpettatore(id, socket) {
|
|
1892
|
+
this.spettatori.set(id, socket);
|
|
1893
|
+
}
|
|
1894
|
+
rimuoviSpettatore(id) {
|
|
1895
|
+
this.spettatori.delete(id);
|
|
1896
|
+
}
|
|
1897
|
+
numeroSpettatori() {
|
|
1898
|
+
return this.spettatori.size;
|
|
1899
|
+
}
|
|
1900
|
+
elencoSpettatori() {
|
|
1901
|
+
return [...this.spettatori];
|
|
1902
|
+
}
|
|
1754
1903
|
elencoConnessioni() {
|
|
1755
1904
|
return [...this.connessioni];
|
|
1756
1905
|
}
|
|
1757
1906
|
invia(connessione, messaggio) {
|
|
1758
1907
|
this.connessioni.get(connessione)?.send(JSON.stringify(messaggio));
|
|
1759
1908
|
}
|
|
1909
|
+
pubblica(messaggio) {
|
|
1910
|
+
const testo = JSON.stringify(messaggio);
|
|
1911
|
+
for (const [connessione] of this.spettatori) {
|
|
1912
|
+
this.inviaSpettatore(connessione, testo);
|
|
1913
|
+
if (messaggio.t === "status" && messaggio.status === "ended") {
|
|
1914
|
+
this.chiudiSpettatoreRitardato(connessione);
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
inviaSpettatore(connessione, testo) {
|
|
1919
|
+
this.accodaSpettatore(connessione, (socket) => {
|
|
1920
|
+
socket.send(testo);
|
|
1921
|
+
});
|
|
1922
|
+
}
|
|
1923
|
+
inviaSpettatoreSubito(connessione, testo) {
|
|
1924
|
+
this.spettatori.get(connessione)?.send(testo);
|
|
1925
|
+
}
|
|
1926
|
+
chiudiSpettatore(connessione, codice, motivo) {
|
|
1927
|
+
const socket = this.spettatori.get(connessione);
|
|
1928
|
+
this.spettatori.delete(connessione);
|
|
1929
|
+
socket?.close(codice, motivo);
|
|
1930
|
+
}
|
|
1931
|
+
chiudiSpettatoreRitardato(connessione) {
|
|
1932
|
+
this.accodaSpettatore(connessione, (socket) => {
|
|
1933
|
+
this.spettatori.delete(connessione);
|
|
1934
|
+
socket.close(4004, "room_ended");
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
accodaSpettatore(connessione, azione) {
|
|
1938
|
+
if (this.ritardoSpettatori === 0) {
|
|
1939
|
+
const socket = this.spettatori.get(connessione);
|
|
1940
|
+
if (socket !== void 0) azione(socket);
|
|
1941
|
+
return;
|
|
1942
|
+
}
|
|
1943
|
+
const timer = setTimeout(() => {
|
|
1944
|
+
this.timerSpettatori.delete(timer);
|
|
1945
|
+
const socket = this.spettatori.get(connessione);
|
|
1946
|
+
if (socket !== void 0) azione(socket);
|
|
1947
|
+
}, this.ritardoSpettatori ?? RITARDO_SPETTATORI_MS);
|
|
1948
|
+
this.timerSpettatori.add(timer);
|
|
1949
|
+
timer.unref();
|
|
1950
|
+
}
|
|
1760
1951
|
chiudi(connessione, codice, motivo) {
|
|
1761
1952
|
this.connessioni.get(connessione)?.close(codice, motivo);
|
|
1762
1953
|
}
|
|
@@ -1769,6 +1960,8 @@ var AdattatoreNode = class {
|
|
|
1769
1960
|
misuraCpu() {
|
|
1770
1961
|
return performance.now();
|
|
1771
1962
|
}
|
|
1963
|
+
rivediVoce() {
|
|
1964
|
+
}
|
|
1772
1965
|
programmaTick(intervalloMs) {
|
|
1773
1966
|
if (this.tickIntervallo === intervalloMs) return;
|
|
1774
1967
|
this.tickIntervallo = intervalloMs;
|
|
@@ -1789,6 +1982,8 @@ var AdattatoreNode = class {
|
|
|
1789
1982
|
this.tickGenerazione += 1;
|
|
1790
1983
|
if (this.tickTimer !== null) clearTimeout(this.tickTimer);
|
|
1791
1984
|
if (this.svegliaTimer !== null) clearTimeout(this.svegliaTimer);
|
|
1985
|
+
for (const timer of this.timerSpettatori) clearTimeout(timer);
|
|
1986
|
+
this.timerSpettatori.clear();
|
|
1792
1987
|
this.tickTimer = null;
|
|
1793
1988
|
this.svegliaTimer = null;
|
|
1794
1989
|
}
|
|
@@ -1847,6 +2042,9 @@ var StanzaNode = class {
|
|
|
1847
2042
|
canJoin(identity) {
|
|
1848
2043
|
return this.serializza(() => Promise.resolve(this.nucleo.puoEntrare(identity)));
|
|
1849
2044
|
}
|
|
2045
|
+
canWatch() {
|
|
2046
|
+
return this.serializza(() => Promise.resolve(this.permessoSpettatore()));
|
|
2047
|
+
}
|
|
1850
2048
|
async connect(socket, identity) {
|
|
1851
2049
|
const connessione = randomUUID();
|
|
1852
2050
|
this.adattatore.aggiungi(connessione, socket);
|
|
@@ -1877,6 +2075,28 @@ var StanzaNode = class {
|
|
|
1877
2075
|
throw cause;
|
|
1878
2076
|
}
|
|
1879
2077
|
}
|
|
2078
|
+
watch(socket, _identity) {
|
|
2079
|
+
return this.serializza(async () => {
|
|
2080
|
+
const permesso = this.permessoSpettatore();
|
|
2081
|
+
if (!permesso.ok) return permesso;
|
|
2082
|
+
const connessione = randomUUID();
|
|
2083
|
+
this.adattatore.aggiungiSpettatore(connessione, socket);
|
|
2084
|
+
socket.on("message", (message) => {
|
|
2085
|
+
void this.serializza(() => this.riceviSpettatore(connessione, message));
|
|
2086
|
+
});
|
|
2087
|
+
socket.on("close", () => {
|
|
2088
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2089
|
+
this.frameFrequenza.delete(connessione);
|
|
2090
|
+
});
|
|
2091
|
+
const fotografia = this.nucleo.fotografia();
|
|
2092
|
+
if (fotografia === null) {
|
|
2093
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2094
|
+
return { ok: false, code: "room_not_found" };
|
|
2095
|
+
}
|
|
2096
|
+
this.inviaWatching(connessione, fotografia);
|
|
2097
|
+
return { ok: true };
|
|
2098
|
+
});
|
|
2099
|
+
}
|
|
1880
2100
|
flush() {
|
|
1881
2101
|
return this.serializza(() => this.nucleo.flush());
|
|
1882
2102
|
}
|
|
@@ -1891,6 +2111,11 @@ var StanzaNode = class {
|
|
|
1891
2111
|
socket.close(1001, "server_shutdown");
|
|
1892
2112
|
await this.serializza(() => this.nucleo.disconnetti(connessione));
|
|
1893
2113
|
}
|
|
2114
|
+
for (const [connessione, socket] of this.adattatore.elencoSpettatori()) {
|
|
2115
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2116
|
+
this.frameFrequenza.delete(connessione);
|
|
2117
|
+
socket.close(1001, "server_shutdown");
|
|
2118
|
+
}
|
|
1894
2119
|
}
|
|
1895
2120
|
serializza(operazione) {
|
|
1896
2121
|
const risultato = this.coda.then(operazione);
|
|
@@ -1931,6 +2156,66 @@ var StanzaNode = class {
|
|
|
1931
2156
|
}
|
|
1932
2157
|
await this.riceviVoce(connessione, player.id, player.role, message);
|
|
1933
2158
|
}
|
|
2159
|
+
permessoSpettatore() {
|
|
2160
|
+
const info = this.nucleo.info();
|
|
2161
|
+
if (info === null) return { ok: false, code: "room_not_found" };
|
|
2162
|
+
if (info.status === "ended") return { ok: false, code: "room_ended" };
|
|
2163
|
+
if (this.manifest.spectators === null) {
|
|
2164
|
+
return { ok: false, code: "spectators_disabled" };
|
|
2165
|
+
}
|
|
2166
|
+
if (this.adattatore.numeroSpettatori() >= MASSIMO_SPETTATORI) {
|
|
2167
|
+
return { ok: false, code: "spectators_full" };
|
|
2168
|
+
}
|
|
2169
|
+
return { ok: true };
|
|
2170
|
+
}
|
|
2171
|
+
inviaWatching(connessione, fotografia) {
|
|
2172
|
+
this.adattatore.inviaSpettatore(connessione, JSON.stringify({
|
|
2173
|
+
t: "watching",
|
|
2174
|
+
...fotografia,
|
|
2175
|
+
delayMs: this.manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS
|
|
2176
|
+
}));
|
|
2177
|
+
}
|
|
2178
|
+
async riceviSpettatore(connessione, frame) {
|
|
2179
|
+
if (Buffer.byteLength(frame, "utf8") > 16 * 1024) {
|
|
2180
|
+
this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
|
|
2181
|
+
this.frameFrequenza.delete(connessione);
|
|
2182
|
+
return;
|
|
2183
|
+
}
|
|
2184
|
+
const ora = Date.now();
|
|
2185
|
+
const frames = (this.frameFrequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
|
|
2186
|
+
if (frames.length >= 20) {
|
|
2187
|
+
this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
|
|
2188
|
+
this.frameFrequenza.delete(connessione);
|
|
2189
|
+
return;
|
|
2190
|
+
}
|
|
2191
|
+
frames.push(ora);
|
|
2192
|
+
this.frameFrequenza.set(connessione, frames);
|
|
2193
|
+
let message = null;
|
|
2194
|
+
try {
|
|
2195
|
+
const value = JSON.parse(frame);
|
|
2196
|
+
message = typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
2197
|
+
} catch {
|
|
2198
|
+
}
|
|
2199
|
+
if (message?.t === "ping" && typeof message.c === "number" && Number.isFinite(message.c)) {
|
|
2200
|
+
this.adattatore.inviaSpettatoreSubito(
|
|
2201
|
+
connessione,
|
|
2202
|
+
JSON.stringify({ t: "pong", c: message.c, s: ora })
|
|
2203
|
+
);
|
|
2204
|
+
return;
|
|
2205
|
+
}
|
|
2206
|
+
if (message?.t === "resync") {
|
|
2207
|
+
const fotografia = this.nucleo.fotografia();
|
|
2208
|
+
if (fotografia !== null) this.inviaWatching(connessione, fotografia);
|
|
2209
|
+
return;
|
|
2210
|
+
}
|
|
2211
|
+
if (message?.t === "leave") {
|
|
2212
|
+
this.adattatore.chiudiSpettatore(connessione, 1e3, "left");
|
|
2213
|
+
this.frameFrequenza.delete(connessione);
|
|
2214
|
+
return;
|
|
2215
|
+
}
|
|
2216
|
+
this.adattatore.chiudiSpettatore(connessione, 4009, "bad_message");
|
|
2217
|
+
this.frameFrequenza.delete(connessione);
|
|
2218
|
+
}
|
|
1934
2219
|
async riceviVoce(connessione, playerId, role, value) {
|
|
1935
2220
|
const richiesta = this.richiestaVoce(value);
|
|
1936
2221
|
if (richiesta === null) {
|
|
@@ -2002,6 +2287,19 @@ var StanzaNode = class {
|
|
|
2002
2287
|
}
|
|
2003
2288
|
if (richiesta.op === "signal") {
|
|
2004
2289
|
const destinazione = this.giocatoriConnessioni.get(richiesta.to);
|
|
2290
|
+
const autorizzazioni = [
|
|
2291
|
+
...this.nucleo.puoAscoltare(richiesta.to, [playerId]),
|
|
2292
|
+
...this.nucleo.puoAscoltare(playerId, [richiesta.to])
|
|
2293
|
+
];
|
|
2294
|
+
if (autorizzazioni.some((esito) => !esito.ok)) {
|
|
2295
|
+
this.inviaErroreVoce(
|
|
2296
|
+
connessione,
|
|
2297
|
+
richiesta,
|
|
2298
|
+
"not_allowed",
|
|
2299
|
+
"The player is not allowed to hear this participant."
|
|
2300
|
+
);
|
|
2301
|
+
return;
|
|
2302
|
+
}
|
|
2005
2303
|
if (destinazione !== void 0) {
|
|
2006
2304
|
this.adattatore.invia(destinazione, {
|
|
2007
2305
|
t: "voice",
|
|
@@ -2121,7 +2419,8 @@ var StanzaNode = class {
|
|
|
2121
2419
|
};
|
|
2122
2420
|
async function createNodeRoom(definition, manifest, options = {}) {
|
|
2123
2421
|
const storage = await ArchivioNode.apri(options.storageFile ?? null);
|
|
2124
|
-
const
|
|
2422
|
+
const ritardoSpettatori = manifest.spectators === null ? null : manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS;
|
|
2423
|
+
const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori);
|
|
2125
2424
|
const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
|
|
2126
2425
|
return new StanzaNode(nucleo, adattatore, manifest);
|
|
2127
2426
|
}
|