@caisual/kit 0.3.0 → 0.5.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 +38 -1
- package/dist/kit.js +602 -79
- package/dist/node.d.ts +14 -1
- package/dist/node.js +458 -55
- package/dist/runtime.d.ts +7 -2
- package/dist/runtime.js +253 -51
- package/dist/server.d.ts +14 -0
- package/package.json +2 -1
package/dist/kit.js
CHANGED
|
@@ -1,4 +1,169 @@
|
|
|
1
|
-
// Caisual game kit v0.
|
|
1
|
+
// Caisual game kit v0.5.0
|
|
2
|
+
|
|
3
|
+
// ../contracts/src/slug.ts
|
|
4
|
+
var NOMI_RISERVATI = [
|
|
5
|
+
"www",
|
|
6
|
+
"api",
|
|
7
|
+
"app",
|
|
8
|
+
"play",
|
|
9
|
+
"live",
|
|
10
|
+
"multi",
|
|
11
|
+
"cdn",
|
|
12
|
+
"assets",
|
|
13
|
+
"static",
|
|
14
|
+
"mail",
|
|
15
|
+
"mx",
|
|
16
|
+
"ns1",
|
|
17
|
+
"ns2",
|
|
18
|
+
"autodiscover",
|
|
19
|
+
"_dmarc",
|
|
20
|
+
"admin",
|
|
21
|
+
"login",
|
|
22
|
+
"account",
|
|
23
|
+
"auth",
|
|
24
|
+
"pay",
|
|
25
|
+
"secure",
|
|
26
|
+
"support",
|
|
27
|
+
"help",
|
|
28
|
+
"blog",
|
|
29
|
+
"status",
|
|
30
|
+
"dev",
|
|
31
|
+
"staging",
|
|
32
|
+
"test",
|
|
33
|
+
"caisual",
|
|
34
|
+
"shipz"
|
|
35
|
+
];
|
|
36
|
+
var RISERVATI = new Set(NOMI_RISERVATI);
|
|
37
|
+
|
|
38
|
+
// ../contracts/src/device.ts
|
|
39
|
+
function deviceTier(report) {
|
|
40
|
+
if (report.gpu !== "hardware" || report.memoryMb !== null && report.memoryMb <= 2048) return "low";
|
|
41
|
+
if (report.mobile || report.memoryMb !== null && report.memoryMb <= 4096 || report.cores !== null && report.cores <= 4) return "mid";
|
|
42
|
+
return "high";
|
|
43
|
+
}
|
|
44
|
+
function perdiContesto(context) {
|
|
45
|
+
try {
|
|
46
|
+
context?.getExtension("WEBGL_lose_context")?.loseContext();
|
|
47
|
+
} catch {
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function valoriSincroni(ambiente) {
|
|
51
|
+
let navigator2;
|
|
52
|
+
try {
|
|
53
|
+
navigator2 = ambiente.navigator;
|
|
54
|
+
} catch {
|
|
55
|
+
navigator2 = void 0;
|
|
56
|
+
}
|
|
57
|
+
let memoryMb = null;
|
|
58
|
+
try {
|
|
59
|
+
const memory = navigator2?.deviceMemory;
|
|
60
|
+
const converted = typeof memory === "number" ? memory * 1024 : NaN;
|
|
61
|
+
if (Number.isFinite(converted)) memoryMb = converted;
|
|
62
|
+
} catch {
|
|
63
|
+
memoryMb = null;
|
|
64
|
+
}
|
|
65
|
+
let cores = null;
|
|
66
|
+
try {
|
|
67
|
+
const value = navigator2?.hardwareConcurrency;
|
|
68
|
+
if (typeof value === "number" && Number.isFinite(value)) cores = value;
|
|
69
|
+
} catch {
|
|
70
|
+
cores = null;
|
|
71
|
+
}
|
|
72
|
+
let mobile = false;
|
|
73
|
+
try {
|
|
74
|
+
mobile = typeof navigator2?.userAgentData?.mobile === "boolean" ? navigator2.userAgentData.mobile : /Android|iPhone|iPad|iPod|Mobile/i.test(navigator2?.userAgent ?? "");
|
|
75
|
+
} catch {
|
|
76
|
+
mobile = false;
|
|
77
|
+
}
|
|
78
|
+
let isolated = false;
|
|
79
|
+
try {
|
|
80
|
+
isolated = ambiente.crossOriginIsolated === true;
|
|
81
|
+
} catch {
|
|
82
|
+
isolated = false;
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
webgl2: false,
|
|
86
|
+
webgpu: false,
|
|
87
|
+
wasm: false,
|
|
88
|
+
threads: false,
|
|
89
|
+
isolated,
|
|
90
|
+
gpu: "none",
|
|
91
|
+
memoryMb,
|
|
92
|
+
cores,
|
|
93
|
+
mobile
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async function probeDevice(globals, timeoutMs = 1500) {
|
|
97
|
+
const ambiente = globals ?? globalThis;
|
|
98
|
+
const report = valoriSincroni(ambiente);
|
|
99
|
+
const webgl = Promise.resolve().then(() => {
|
|
100
|
+
try {
|
|
101
|
+
const canvas = ambiente.document?.createElement("canvas");
|
|
102
|
+
if (canvas === void 0) return;
|
|
103
|
+
const hardware = canvas.getContext("webgl2", { failIfMajorPerformanceCaveat: true });
|
|
104
|
+
if (hardware !== null) {
|
|
105
|
+
report.webgl2 = true;
|
|
106
|
+
report.gpu = "hardware";
|
|
107
|
+
perdiContesto(hardware);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const software = canvas.getContext("webgl2");
|
|
111
|
+
if (software !== null) {
|
|
112
|
+
report.webgl2 = true;
|
|
113
|
+
report.gpu = "software";
|
|
114
|
+
perdiContesto(software);
|
|
115
|
+
}
|
|
116
|
+
} catch {
|
|
117
|
+
report.webgl2 = false;
|
|
118
|
+
report.gpu = "none";
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
const webgpu = Promise.resolve().then(async () => {
|
|
122
|
+
let device;
|
|
123
|
+
try {
|
|
124
|
+
const gpu = ambiente.navigator?.gpu;
|
|
125
|
+
if (gpu === void 0) return;
|
|
126
|
+
const adapter = await gpu.requestAdapter();
|
|
127
|
+
if (adapter === null) return;
|
|
128
|
+
device = await adapter.requestDevice();
|
|
129
|
+
report.webgpu = true;
|
|
130
|
+
} catch {
|
|
131
|
+
report.webgpu = false;
|
|
132
|
+
} finally {
|
|
133
|
+
try {
|
|
134
|
+
device?.destroy?.();
|
|
135
|
+
} catch {
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
const wasm = Promise.resolve().then(() => {
|
|
140
|
+
try {
|
|
141
|
+
report.wasm = ambiente.WebAssembly?.validate(
|
|
142
|
+
new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0])
|
|
143
|
+
) === true;
|
|
144
|
+
} catch {
|
|
145
|
+
report.wasm = false;
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
const threads = Promise.resolve().then(() => {
|
|
149
|
+
try {
|
|
150
|
+
if (ambiente.WebAssembly === void 0) return;
|
|
151
|
+
new ambiente.WebAssembly.Memory({ initial: 1, maximum: 1, shared: true });
|
|
152
|
+
report.threads = true;
|
|
153
|
+
} catch {
|
|
154
|
+
report.threads = false;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
let timer;
|
|
158
|
+
await Promise.race([
|
|
159
|
+
Promise.all([webgl, webgpu, wasm, threads]),
|
|
160
|
+
new Promise((resolve) => {
|
|
161
|
+
timer = setTimeout(resolve, Math.max(0, timeoutMs));
|
|
162
|
+
})
|
|
163
|
+
]);
|
|
164
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
165
|
+
return { ...report, tier: deviceTier(report) };
|
|
166
|
+
}
|
|
2
167
|
|
|
3
168
|
// src/errors.ts
|
|
4
169
|
function creaErrore(code, message) {
|
|
@@ -294,11 +459,12 @@ var VoceClient = class {
|
|
|
294
459
|
this.midGiocatori = /* @__PURE__ */ new Map();
|
|
295
460
|
this.mesh = /* @__PURE__ */ new Map();
|
|
296
461
|
this.stream = null;
|
|
297
|
-
this.
|
|
462
|
+
this.tracciaMic = null;
|
|
298
463
|
this.audioContext = null;
|
|
299
464
|
this.analyser = null;
|
|
300
465
|
this.peerSfu = null;
|
|
301
466
|
this.sessioneSfu = null;
|
|
467
|
+
this.connessioneSfuAttesa = false;
|
|
302
468
|
this.trasporto = null;
|
|
303
469
|
this.intervalloAudio = null;
|
|
304
470
|
this.timerConnessione = null;
|
|
@@ -309,6 +475,7 @@ var VoceClient = class {
|
|
|
309
475
|
this.generazione = 0;
|
|
310
476
|
this.tentativoRiconnessione = 0;
|
|
311
477
|
this.desiderata = false;
|
|
478
|
+
this.micDesiderato = true;
|
|
312
479
|
this.promessaIngresso = null;
|
|
313
480
|
this.negoziazione = Promise.resolve();
|
|
314
481
|
this.dipendenze = dipendenze ?? dipendenzeReali(timer);
|
|
@@ -319,6 +486,9 @@ var VoceClient = class {
|
|
|
319
486
|
get state() {
|
|
320
487
|
return this.stateCorrente;
|
|
321
488
|
}
|
|
489
|
+
get mic() {
|
|
490
|
+
return this.stateCorrente === "on" && this.tracciaMic !== null;
|
|
491
|
+
}
|
|
322
492
|
get muted() {
|
|
323
493
|
return this.mutedCorrente;
|
|
324
494
|
}
|
|
@@ -328,14 +498,16 @@ var VoceClient = class {
|
|
|
328
498
|
get peers() {
|
|
329
499
|
return this.copiaPeers();
|
|
330
500
|
}
|
|
331
|
-
async join() {
|
|
501
|
+
async join(options = {}) {
|
|
332
502
|
if (this.stateCorrente === "on") return;
|
|
333
503
|
if (this.stateCorrente === "joining") {
|
|
334
504
|
if (this.promessaIngresso !== null) await this.promessaIngresso;
|
|
335
505
|
return;
|
|
336
506
|
}
|
|
337
507
|
if (this.stateCorrente === "reconnecting" && this.desiderata) return;
|
|
338
|
-
this.
|
|
508
|
+
const mic = this.scegliMic(options);
|
|
509
|
+
this.verificaIngresso(mic);
|
|
510
|
+
this.micDesiderato = mic;
|
|
339
511
|
this.desiderata = true;
|
|
340
512
|
this.tentativoRiconnessione = 0;
|
|
341
513
|
this.aggiornaState("joining");
|
|
@@ -372,11 +544,11 @@ var VoceClient = class {
|
|
|
372
544
|
this.aggiornaState("off");
|
|
373
545
|
}
|
|
374
546
|
mute(muted = true) {
|
|
375
|
-
if (this.stateCorrente !== "on" || this.
|
|
547
|
+
if (this.stateCorrente !== "on" || this.tracciaMic === null) {
|
|
376
548
|
throw creaErrore("not_publishing", "Join voice before changing mute.");
|
|
377
549
|
}
|
|
378
550
|
this.mutedCorrente = muted;
|
|
379
|
-
this.
|
|
551
|
+
this.tracciaMic.enabled = !muted;
|
|
380
552
|
void this.richiedi({ t: "voice", op: "mute", muted }).catch(() => void 0);
|
|
381
553
|
}
|
|
382
554
|
setVolume(playerId, volume) {
|
|
@@ -410,7 +582,11 @@ var VoceClient = class {
|
|
|
410
582
|
}
|
|
411
583
|
if (message.op === "roster") {
|
|
412
584
|
this.modeCorrente = message.mode;
|
|
413
|
-
|
|
585
|
+
const publisher = new Set(message.peers.map((peer) => peer.id));
|
|
586
|
+
this.roster = [
|
|
587
|
+
...message.peers.map((peer) => ({ ...peer, mic: true })),
|
|
588
|
+
...message.listeners.flatMap((id) => publisher.has(id) ? [] : [{ id, mic: false, muted: true }])
|
|
589
|
+
];
|
|
414
590
|
for (const peer of this.roster) {
|
|
415
591
|
if (peer.muted) this.speakingPeers.set(peer.id, false);
|
|
416
592
|
}
|
|
@@ -467,14 +643,19 @@ var VoceClient = class {
|
|
|
467
643
|
this.chiudiRisorse();
|
|
468
644
|
this.aggiornaState("off");
|
|
469
645
|
}
|
|
470
|
-
|
|
646
|
+
scegliMic(options) {
|
|
647
|
+
if (options.mic !== void 0) return options.mic;
|
|
648
|
+
const you = this.contesto.giocatori().find((player) => player.id === this.contesto.you());
|
|
649
|
+
return you?.role !== "spectator";
|
|
650
|
+
}
|
|
651
|
+
verificaIngresso(mic = this.micDesiderato) {
|
|
471
652
|
if (!this.contesto.connessa()) throw creaErrore("offline", "The room is not connected.");
|
|
472
653
|
if (this.modeCorrente === "none") {
|
|
473
654
|
throw creaErrore("voice_disabled", "Voice is disabled for this room.");
|
|
474
655
|
}
|
|
475
656
|
const you = this.contesto.giocatori().find((player) => player.id === this.contesto.you());
|
|
476
|
-
if (you?.role === "spectator") {
|
|
477
|
-
throw creaErrore("spectator", "Spectators cannot
|
|
657
|
+
if (you?.role === "spectator" && mic) {
|
|
658
|
+
throw creaErrore("spectator", "Spectators cannot publish voice.");
|
|
478
659
|
}
|
|
479
660
|
if (this.dipendenze === null) {
|
|
480
661
|
throw creaErrore("unsupported", "Voice is not supported in this browser.");
|
|
@@ -489,27 +670,29 @@ var VoceClient = class {
|
|
|
489
670
|
await audioContext.resume();
|
|
490
671
|
} catch {
|
|
491
672
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
673
|
+
if (this.micDesiderato) {
|
|
674
|
+
let stream;
|
|
675
|
+
try {
|
|
676
|
+
stream = await dipendenze.getUserMedia({ audio: true });
|
|
677
|
+
} catch (cause) {
|
|
678
|
+
if (this.permessoNegato(cause)) {
|
|
679
|
+
throw creaErrore("permission_denied", "Microphone permission was denied.");
|
|
680
|
+
}
|
|
681
|
+
throw creaErrore("voice_error", "The microphone could not be opened.");
|
|
682
|
+
}
|
|
683
|
+
try {
|
|
684
|
+
this.controllaGenerazione(generazione);
|
|
685
|
+
} catch (cause) {
|
|
686
|
+
for (const track of stream.getTracks()) track.stop();
|
|
687
|
+
throw cause;
|
|
498
688
|
}
|
|
499
|
-
|
|
689
|
+
const mic = stream.getAudioTracks()[0];
|
|
690
|
+
if (mic === void 0) throw creaErrore("voice_error", "The microphone has no audio track.");
|
|
691
|
+
this.stream = stream;
|
|
692
|
+
this.tracciaMic = mic;
|
|
693
|
+
mic.enabled = !this.mutedCorrente;
|
|
694
|
+
this.preparaAnalizzatore(stream);
|
|
500
695
|
}
|
|
501
|
-
try {
|
|
502
|
-
this.controllaGenerazione(generazione);
|
|
503
|
-
} catch (cause) {
|
|
504
|
-
for (const track of stream.getTracks()) track.stop();
|
|
505
|
-
throw cause;
|
|
506
|
-
}
|
|
507
|
-
const mic = stream.getAudioTracks()[0];
|
|
508
|
-
if (mic === void 0) throw creaErrore("voice_error", "The microphone has no audio track.");
|
|
509
|
-
this.stream = stream;
|
|
510
|
-
this.mic = mic;
|
|
511
|
-
mic.enabled = !this.mutedCorrente;
|
|
512
|
-
this.preparaAnalizzatore(stream);
|
|
513
696
|
const risposta = await this.richiedi({ t: "voice", op: "ice" });
|
|
514
697
|
this.controllaGenerazione(generazione);
|
|
515
698
|
if (risposta.op !== "ice") throw creaErrore("voice_error", "The voice service returned an invalid response.");
|
|
@@ -519,9 +702,11 @@ var VoceClient = class {
|
|
|
519
702
|
if (risposta.transport === "sfu") {
|
|
520
703
|
await this.entraSfu(risposta.iceServers, generazione);
|
|
521
704
|
} else {
|
|
522
|
-
await this.richiedi({ t: "voice", op: "publish" });
|
|
705
|
+
await this.richiedi({ t: "voice", op: "publish", mic: this.micDesiderato });
|
|
706
|
+
}
|
|
707
|
+
if (this.micDesiderato && this.mutedCorrente) {
|
|
708
|
+
await this.richiedi({ t: "voice", op: "mute", muted: true });
|
|
523
709
|
}
|
|
524
|
-
if (this.mutedCorrente) await this.richiedi({ t: "voice", op: "mute", muted: true });
|
|
525
710
|
this.controllaGenerazione(generazione);
|
|
526
711
|
this.tentativoRiconnessione = 0;
|
|
527
712
|
this.aggiornaState("on");
|
|
@@ -536,27 +721,45 @@ var VoceClient = class {
|
|
|
536
721
|
});
|
|
537
722
|
this.peerSfu = pc;
|
|
538
723
|
pc.ontrack = (event) => {
|
|
539
|
-
const
|
|
540
|
-
const playerId =
|
|
724
|
+
const mid = event.transceiver.mid;
|
|
725
|
+
const playerId = mid === null ? void 0 : this.midGiocatori.get(mid);
|
|
541
726
|
if (playerId !== void 0) this.collegaTraccia(playerId, event.track, event.receiver);
|
|
542
727
|
};
|
|
543
728
|
this.osservaCaduta(pc);
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
729
|
+
let risposta;
|
|
730
|
+
if (this.micDesiderato) {
|
|
731
|
+
const transceiver = pc.addTransceiver(this.richiediMic(), { direction: "sendonly" });
|
|
732
|
+
const offer = await pc.createOffer();
|
|
733
|
+
await pc.setLocalDescription(offer);
|
|
734
|
+
this.controllaGenerazione(generazione);
|
|
735
|
+
const mid = transceiver.mid;
|
|
736
|
+
const sdp = pc.localDescription?.sdp;
|
|
737
|
+
if (mid === null || sdp === void 0) {
|
|
738
|
+
throw creaErrore("voice_error", "The voice connection could not create an offer.");
|
|
739
|
+
}
|
|
740
|
+
risposta = await this.richiedi({ t: "voice", op: "session", sdp, mid });
|
|
741
|
+
} else {
|
|
742
|
+
risposta = await this.richiedi({ t: "voice", op: "session" });
|
|
552
743
|
}
|
|
553
|
-
const risposta = await this.richiedi({ t: "voice", op: "session", sdp, mid });
|
|
554
744
|
if (risposta.op !== "session") {
|
|
555
745
|
throw creaErrore("voice_error", "The voice service returned an invalid response.");
|
|
556
746
|
}
|
|
557
747
|
this.sessioneSfu = risposta.session;
|
|
558
|
-
|
|
559
|
-
|
|
748
|
+
if (this.micDesiderato) {
|
|
749
|
+
if (risposta.sdp === null) {
|
|
750
|
+
throw creaErrore("voice_error", "The voice service returned an invalid response.");
|
|
751
|
+
}
|
|
752
|
+
await pc.setRemoteDescription({ type: "answer", sdp: risposta.sdp });
|
|
753
|
+
await this.attendiConnessione(pc, generazione);
|
|
754
|
+
this.connessioneSfuAttesa = true;
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
if (risposta.sdp !== null) {
|
|
758
|
+
throw creaErrore("voice_error", "The voice service returned an invalid response.");
|
|
759
|
+
}
|
|
760
|
+
if (this.publisherDesiderati().length > 0) {
|
|
761
|
+
await this.riconciliaSfu();
|
|
762
|
+
}
|
|
560
763
|
}
|
|
561
764
|
attendiConnessione(pc, generazione) {
|
|
562
765
|
if (pc.connectionState === "connected") return Promise.resolve();
|
|
@@ -603,7 +806,7 @@ var VoceClient = class {
|
|
|
603
806
|
const sessione = this.sessioneSfu;
|
|
604
807
|
const pc = this.peerSfu;
|
|
605
808
|
if (sessione === null || pc === null) return;
|
|
606
|
-
const desiderati = new Map(this.
|
|
809
|
+
const desiderati = new Map(this.publisherDesiderati().map((peer) => [peer.id, peer]));
|
|
607
810
|
const daChiudere = [];
|
|
608
811
|
for (const [playerId, attiva] of this.sfuAttive) {
|
|
609
812
|
const peer = desiderati.get(playerId);
|
|
@@ -652,6 +855,10 @@ var VoceClient = class {
|
|
|
652
855
|
const sdp = pc.localDescription?.sdp;
|
|
653
856
|
if (sdp === void 0) throw creaErrore("voice_error", "The voice answer is missing.");
|
|
654
857
|
await this.richiedi({ t: "voice", op: "answer", session: sessione, sdp });
|
|
858
|
+
if (!this.connessioneSfuAttesa) {
|
|
859
|
+
await this.attendiConnessione(pc, this.generazione);
|
|
860
|
+
this.connessioneSfuAttesa = true;
|
|
861
|
+
}
|
|
655
862
|
}
|
|
656
863
|
riconciliaMesh() {
|
|
657
864
|
const desiderati = new Map(this.peerDesiderati().map((peer) => [peer.id, peer]));
|
|
@@ -662,10 +869,11 @@ var VoceClient = class {
|
|
|
662
869
|
this.scollegaTraccia(playerId);
|
|
663
870
|
}
|
|
664
871
|
for (const peer of desiderati.values()) {
|
|
665
|
-
if (!this.mesh.has(peer.id)) this.creaMesh(peer
|
|
872
|
+
if (!this.mesh.has(peer.id)) this.creaMesh(peer);
|
|
666
873
|
}
|
|
667
874
|
}
|
|
668
|
-
creaMesh(
|
|
875
|
+
creaMesh(peer) {
|
|
876
|
+
const playerId = peer.id;
|
|
669
877
|
const pc = this.richiediDipendenze().creaPeerConnection();
|
|
670
878
|
const item = {
|
|
671
879
|
pc,
|
|
@@ -688,7 +896,13 @@ var VoceClient = class {
|
|
|
688
896
|
this.collegaTraccia(playerId, event.track, event.receiver);
|
|
689
897
|
};
|
|
690
898
|
this.osservaCaduta(pc);
|
|
691
|
-
|
|
899
|
+
if (this.micDesiderato) {
|
|
900
|
+
pc.addTransceiver(this.richiediMic(), {
|
|
901
|
+
direction: peer.mic ? "sendrecv" : "sendonly"
|
|
902
|
+
});
|
|
903
|
+
} else {
|
|
904
|
+
pc.addTransceiver("audio", { direction: "recvonly" });
|
|
905
|
+
}
|
|
692
906
|
}
|
|
693
907
|
async offriMesh(playerId, item) {
|
|
694
908
|
try {
|
|
@@ -703,8 +917,9 @@ var VoceClient = class {
|
|
|
703
917
|
}
|
|
704
918
|
async riceviSegnale(from, data) {
|
|
705
919
|
if (this.trasporto !== "mesh" || this.stateCorrente !== "on") return;
|
|
706
|
-
|
|
707
|
-
if (
|
|
920
|
+
const peer = this.peerDesiderati().find((item2) => item2.id === from);
|
|
921
|
+
if (peer === void 0) return;
|
|
922
|
+
if (!this.mesh.has(from)) this.creaMesh(peer);
|
|
708
923
|
const item = this.mesh.get(from);
|
|
709
924
|
if (item === void 0 || typeof data !== "object" || data === null || Array.isArray(data)) return;
|
|
710
925
|
const segnale = data;
|
|
@@ -740,14 +955,23 @@ var VoceClient = class {
|
|
|
740
955
|
const player = giocatori.find((item) => item.id === you);
|
|
741
956
|
return this.roster.filter((peer) => {
|
|
742
957
|
if (peer.id === you) return false;
|
|
958
|
+
if (!this.micDesiderato && !peer.mic) return false;
|
|
743
959
|
if (this.modeCorrente === "team") {
|
|
744
960
|
const altro = giocatori.find((item) => item.id === peer.id);
|
|
745
961
|
if (player?.role !== "spectator" && altro?.team !== player?.team) return false;
|
|
746
962
|
}
|
|
747
|
-
|
|
748
|
-
return zeroAt === void 0 || this.richiediDipendenze().ora() - zeroAt < DURATA_ZERO;
|
|
963
|
+
return true;
|
|
749
964
|
});
|
|
750
965
|
}
|
|
966
|
+
publisherDesiderati() {
|
|
967
|
+
return this.peerDesiderati().filter(
|
|
968
|
+
(peer) => {
|
|
969
|
+
if (!peer.mic) return false;
|
|
970
|
+
const zeroAt = this.zeroDa.get(peer.id);
|
|
971
|
+
return zeroAt === void 0 || this.richiediDipendenze().ora() - zeroAt < DURATA_ZERO;
|
|
972
|
+
}
|
|
973
|
+
);
|
|
974
|
+
}
|
|
751
975
|
aggiornaZero(playerId) {
|
|
752
976
|
const dipendenze = this.dipendenze;
|
|
753
977
|
if (dipendenze === null) return;
|
|
@@ -872,8 +1096,9 @@ var VoceClient = class {
|
|
|
872
1096
|
}
|
|
873
1097
|
return [{
|
|
874
1098
|
id: peer.id,
|
|
1099
|
+
mic: peer.mic,
|
|
875
1100
|
muted: peer.muted,
|
|
876
|
-
speaking: !peer.muted && (this.speakingPeers.get(peer.id) ?? false),
|
|
1101
|
+
speaking: peer.mic && !peer.muted && (this.speakingPeers.get(peer.id) ?? false),
|
|
877
1102
|
volume: this.volumi.get(peer.id) ?? 1,
|
|
878
1103
|
gain: this.gains.get(peer.id) ?? 1
|
|
879
1104
|
}];
|
|
@@ -952,11 +1177,12 @@ var VoceClient = class {
|
|
|
952
1177
|
this.midGiocatori.clear();
|
|
953
1178
|
for (const track of this.stream?.getTracks() ?? []) track.stop();
|
|
954
1179
|
this.stream = null;
|
|
955
|
-
this.
|
|
1180
|
+
this.tracciaMic = null;
|
|
956
1181
|
this.analyser = null;
|
|
957
1182
|
void this.audioContext?.close().catch(() => void 0);
|
|
958
1183
|
this.audioContext = null;
|
|
959
1184
|
this.sessioneSfu = null;
|
|
1185
|
+
this.connessioneSfuAttesa = false;
|
|
960
1186
|
this.trasporto = null;
|
|
961
1187
|
this.speakingCorrente = false;
|
|
962
1188
|
this.ultimoAudioMic = Number.NEGATIVE_INFINITY;
|
|
@@ -1010,12 +1236,8 @@ var VoceClient = class {
|
|
|
1010
1236
|
return this.dipendenze;
|
|
1011
1237
|
}
|
|
1012
1238
|
richiediMic() {
|
|
1013
|
-
if (this.
|
|
1014
|
-
return this.
|
|
1015
|
-
}
|
|
1016
|
-
richiediStream() {
|
|
1017
|
-
if (this.stream === null) throw creaErrore("voice_error", "The microphone is not ready.");
|
|
1018
|
-
return this.stream;
|
|
1239
|
+
if (this.tracciaMic === null) throw creaErrore("voice_error", "The microphone is not ready.");
|
|
1240
|
+
return this.tracciaMic;
|
|
1019
1241
|
}
|
|
1020
1242
|
richiediAudioContext() {
|
|
1021
1243
|
if (this.audioContext === null) throw creaErrore("voice_error", "Audio is not ready.");
|
|
@@ -1042,9 +1264,23 @@ var INTERVALLO_PING = 5e3;
|
|
|
1042
1264
|
var RITARDO_FLUSH = 500;
|
|
1043
1265
|
var ATTESA_ROSTER = 2e3;
|
|
1044
1266
|
var CHIUSURE_DEFINITIVE = /* @__PURE__ */ new Set([4003, 4004, 4005, 4006]);
|
|
1267
|
+
var CHIUSURE_DEFINITIVE_SPETTATORE = /* @__PURE__ */ new Set([4008, 4009]);
|
|
1045
1268
|
function record2(value) {
|
|
1046
1269
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
1047
1270
|
}
|
|
1271
|
+
function ingressoValido(value) {
|
|
1272
|
+
const dati = record2(value);
|
|
1273
|
+
return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.join === "string" && typeof dati.url === "string";
|
|
1274
|
+
}
|
|
1275
|
+
function visioneValida(value) {
|
|
1276
|
+
const dati = record2(value);
|
|
1277
|
+
return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.watch === "string" && typeof dati.url === "string";
|
|
1278
|
+
}
|
|
1279
|
+
function rispostaMatchValida(value) {
|
|
1280
|
+
const dati = record2(value);
|
|
1281
|
+
const players = record2(dati?.players);
|
|
1282
|
+
return dati !== null && typeof dati.url === "string" && Number.isInteger(dati.timeoutMs) && dati.timeoutMs >= 1e3 && dati.timeoutMs <= 3e5 && players !== null && Number.isInteger(players.min) && Number.isInteger(players.max) && players.min >= 1 && players.max >= players.min;
|
|
1283
|
+
}
|
|
1048
1284
|
function copiaJson(value) {
|
|
1049
1285
|
return JSON.parse(JSON.stringify(value));
|
|
1050
1286
|
}
|
|
@@ -1095,10 +1331,6 @@ function applicaPatch(state, value) {
|
|
|
1095
1331
|
}
|
|
1096
1332
|
function creaApiLive(input) {
|
|
1097
1333
|
const richiesta = creaRichiedente(input.liveOrigin, "", input.fetcher, input.biglietto);
|
|
1098
|
-
function ingressoValido(value) {
|
|
1099
|
-
const dati = record2(value);
|
|
1100
|
-
return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.join === "string" && typeof dati.url === "string";
|
|
1101
|
-
}
|
|
1102
1334
|
async function ingresso(path, body, rinnova = false) {
|
|
1103
1335
|
const value = await richiesta(path, "POST", body, rinnova);
|
|
1104
1336
|
if (!ingressoValido(value)) {
|
|
@@ -1106,10 +1338,30 @@ function creaApiLive(input) {
|
|
|
1106
1338
|
}
|
|
1107
1339
|
return value;
|
|
1108
1340
|
}
|
|
1341
|
+
async function match(options) {
|
|
1342
|
+
const value = await richiesta("/match", "POST", {
|
|
1343
|
+
mode: options.mode,
|
|
1344
|
+
key: options.key
|
|
1345
|
+
});
|
|
1346
|
+
if (!rispostaMatchValida(value)) {
|
|
1347
|
+
throw creaErrore("internal_error", "The matchmaking service returned an invalid response.");
|
|
1348
|
+
}
|
|
1349
|
+
return value;
|
|
1350
|
+
}
|
|
1351
|
+
async function visione(body, rinnova = false) {
|
|
1352
|
+
const value = await richiesta("/rooms/watch", "POST", body, rinnova);
|
|
1353
|
+
if (!visioneValida(value)) {
|
|
1354
|
+
throw creaErrore("internal_error", "The room service returned an invalid response.");
|
|
1355
|
+
}
|
|
1356
|
+
return value;
|
|
1357
|
+
}
|
|
1109
1358
|
return {
|
|
1110
1359
|
create: (mode) => ingresso("/rooms", { mode }),
|
|
1111
1360
|
joinCode: (code) => ingresso("/rooms/join", { code }),
|
|
1112
1361
|
joinRoom: (roomId) => ingresso("/rooms/join", { roomId }, true),
|
|
1362
|
+
watchCode: (code) => visione({ code }),
|
|
1363
|
+
watchRoom: (roomId) => visione({ roomId }, true),
|
|
1364
|
+
match,
|
|
1113
1365
|
flush: (roomId) => richiesta(
|
|
1114
1366
|
`/rooms/${encodeURIComponent(roomId)}/flush`,
|
|
1115
1367
|
"POST"
|
|
@@ -1117,19 +1369,23 @@ function creaApiLive(input) {
|
|
|
1117
1369
|
};
|
|
1118
1370
|
}
|
|
1119
1371
|
var StanzaClient = class {
|
|
1120
|
-
constructor(roomId, codice, url, input, api) {
|
|
1372
|
+
constructor(roomId, codice, url, input, api, segnalaStanza, spettatore = false) {
|
|
1121
1373
|
this.roomId = roomId;
|
|
1122
1374
|
this.codice = codice;
|
|
1123
1375
|
this.input = input;
|
|
1124
1376
|
this.api = api;
|
|
1377
|
+
this.segnalaStanza = segnalaStanza;
|
|
1378
|
+
this.spettatore = spettatore;
|
|
1125
1379
|
this.statoPubblico = null;
|
|
1126
1380
|
this.statoSincronizzato = null;
|
|
1127
1381
|
this.tickCorrente = 0;
|
|
1382
|
+
this.seedCorrente = 0;
|
|
1128
1383
|
this.statusCorrente = "lobby";
|
|
1129
1384
|
this.giocatoriCorrenti = [];
|
|
1130
1385
|
this.youCorrente = "";
|
|
1131
1386
|
this.hostCorrente = null;
|
|
1132
1387
|
this.resultCorrente = null;
|
|
1388
|
+
this.delaySpettatore = 0;
|
|
1133
1389
|
this.socket = null;
|
|
1134
1390
|
this.seq = 0;
|
|
1135
1391
|
this.scartoOrario = 0;
|
|
@@ -1167,6 +1423,7 @@ var StanzaClient = class {
|
|
|
1167
1423
|
this.risolviProntaSePossibile();
|
|
1168
1424
|
}
|
|
1169
1425
|
}, input, input.voce);
|
|
1426
|
+
if (spettatore) this.rosterRicevuto = true;
|
|
1170
1427
|
this.apri(url);
|
|
1171
1428
|
}
|
|
1172
1429
|
get state() {
|
|
@@ -1175,6 +1432,9 @@ var StanzaClient = class {
|
|
|
1175
1432
|
get tick() {
|
|
1176
1433
|
return this.tickCorrente;
|
|
1177
1434
|
}
|
|
1435
|
+
get seed() {
|
|
1436
|
+
return this.seedCorrente;
|
|
1437
|
+
}
|
|
1178
1438
|
get status() {
|
|
1179
1439
|
return this.statusCorrente;
|
|
1180
1440
|
}
|
|
@@ -1193,6 +1453,9 @@ var StanzaClient = class {
|
|
|
1193
1453
|
get result() {
|
|
1194
1454
|
return this.resultCorrente;
|
|
1195
1455
|
}
|
|
1456
|
+
get delayMs() {
|
|
1457
|
+
return this.delaySpettatore;
|
|
1458
|
+
}
|
|
1196
1459
|
pronta() {
|
|
1197
1460
|
return this.promessaPronta;
|
|
1198
1461
|
}
|
|
@@ -1242,9 +1505,14 @@ var StanzaClient = class {
|
|
|
1242
1505
|
}
|
|
1243
1506
|
leave() {
|
|
1244
1507
|
if (this.lasciata) return;
|
|
1245
|
-
this.voice.leave();
|
|
1508
|
+
if (!this.spettatore) this.voice.leave();
|
|
1246
1509
|
this.lasciata = true;
|
|
1247
|
-
|
|
1510
|
+
this.segnalaStanza(null);
|
|
1511
|
+
if (this.socket?.readyState === APERTO) {
|
|
1512
|
+
const socket = this.socket;
|
|
1513
|
+
this.invia({ t: "leave" });
|
|
1514
|
+
if (this.spettatore) socket.close(1e3);
|
|
1515
|
+
}
|
|
1248
1516
|
this.termina(1e3);
|
|
1249
1517
|
}
|
|
1250
1518
|
serverTime() {
|
|
@@ -1318,7 +1586,8 @@ var StanzaClient = class {
|
|
|
1318
1586
|
return;
|
|
1319
1587
|
}
|
|
1320
1588
|
try {
|
|
1321
|
-
if (dati.t === "
|
|
1589
|
+
if (dati.t === "watching") this.riceviWatching(dati);
|
|
1590
|
+
else if (dati.t === "welcome") this.riceviWelcome(dati);
|
|
1322
1591
|
else if (dati.t === "players") this.riceviGiocatori(dati.players);
|
|
1323
1592
|
else if (dati.t === "status") this.riceviStatus(dati);
|
|
1324
1593
|
else if (dati.t === "state") this.riceviDiff(dati);
|
|
@@ -1331,10 +1600,34 @@ var StanzaClient = class {
|
|
|
1331
1600
|
if (dati.t === "state" || dati.t === "snapshot") this.chiediResync();
|
|
1332
1601
|
}
|
|
1333
1602
|
}
|
|
1603
|
+
riceviWatching(dati) {
|
|
1604
|
+
const room = dati.room;
|
|
1605
|
+
if (!this.spettatore || room.id !== this.roomId) return;
|
|
1606
|
+
this.seedCorrente = room.seed;
|
|
1607
|
+
this.hostCorrente = room.host;
|
|
1608
|
+
this.statusCorrente = room.status;
|
|
1609
|
+
this.giocatoriCorrenti = dati.players.map((player) => ({ ...player }));
|
|
1610
|
+
this.delaySpettatore = dati.delayMs;
|
|
1611
|
+
this.aggiornaStato(dati.state, room.tick, room.serverTime);
|
|
1612
|
+
this.scartoOrario = room.serverTime - this.input.ora();
|
|
1613
|
+
this.resyncRichiesto = false;
|
|
1614
|
+
this.welcomeRicevuto = true;
|
|
1615
|
+
this.ritardoIndice = 0;
|
|
1616
|
+
this.tempoRiconnessione = 0;
|
|
1617
|
+
this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());
|
|
1618
|
+
this.notifica(
|
|
1619
|
+
this.ascoltatoriStatus,
|
|
1620
|
+
this.statusCorrente,
|
|
1621
|
+
this.resultCorrente,
|
|
1622
|
+
room.serverTime
|
|
1623
|
+
);
|
|
1624
|
+
this.risolviProntaSePossibile();
|
|
1625
|
+
}
|
|
1334
1626
|
riceviWelcome(dati) {
|
|
1335
1627
|
const room = dati.room;
|
|
1336
1628
|
if (room.id !== this.roomId) return;
|
|
1337
1629
|
this.youCorrente = dati.you;
|
|
1630
|
+
this.seedCorrente = room.seed;
|
|
1338
1631
|
this.hostCorrente = room.host;
|
|
1339
1632
|
this.statusCorrente = room.status;
|
|
1340
1633
|
this.giocatoriCorrenti = dati.players.map((player) => ({ ...player }));
|
|
@@ -1377,7 +1670,8 @@ var StanzaClient = class {
|
|
|
1377
1670
|
this.resultCorrente = copiaJson(dati.result);
|
|
1378
1671
|
if (dati.status === "ended") {
|
|
1379
1672
|
this.terminata = true;
|
|
1380
|
-
this.
|
|
1673
|
+
this.segnalaStanza(null);
|
|
1674
|
+
if (!this.spettatore) this.voice.termina();
|
|
1381
1675
|
this.fermaPing();
|
|
1382
1676
|
this.fermaRiconnessione();
|
|
1383
1677
|
}
|
|
@@ -1423,12 +1717,12 @@ var StanzaClient = class {
|
|
|
1423
1717
|
this.socket = null;
|
|
1424
1718
|
this.welcomeRicevuto = false;
|
|
1425
1719
|
this.fermaPing();
|
|
1426
|
-
if (
|
|
1720
|
+
if (this.lasciata || this.terminata) return;
|
|
1721
|
+
if (CHIUSURE_DEFINITIVE.has(code) || this.spettatore && CHIUSURE_DEFINITIVE_SPETTATORE.has(code)) {
|
|
1427
1722
|
this.termina(code);
|
|
1428
1723
|
return;
|
|
1429
1724
|
}
|
|
1430
|
-
if (this.
|
|
1431
|
-
this.voice.socketDisconnesso();
|
|
1725
|
+
if (!this.spettatore) this.voice.socketDisconnesso();
|
|
1432
1726
|
this.programmaRiconnessione();
|
|
1433
1727
|
}
|
|
1434
1728
|
programmaRiconnessione() {
|
|
@@ -1449,8 +1743,12 @@ var StanzaClient = class {
|
|
|
1449
1743
|
async riconnetti() {
|
|
1450
1744
|
if (this.terminata || this.lasciata) return;
|
|
1451
1745
|
try {
|
|
1452
|
-
const ingresso = await this.api.joinRoom(this.roomId);
|
|
1746
|
+
const ingresso = this.spettatore ? await this.api.watchRoom(this.roomId) : await this.api.joinRoom(this.roomId);
|
|
1747
|
+
const codiceCambiato = this.codice !== ingresso.code;
|
|
1453
1748
|
this.codice = ingresso.code;
|
|
1749
|
+
if (codiceCambiato && this.prontaRisolta && !this.terminata && !this.lasciata) {
|
|
1750
|
+
this.segnalaStanza({ code: this.codice });
|
|
1751
|
+
}
|
|
1454
1752
|
this.apri(ingresso.url);
|
|
1455
1753
|
} catch {
|
|
1456
1754
|
this.programmaRiconnessione();
|
|
@@ -1465,9 +1763,10 @@ var StanzaClient = class {
|
|
|
1465
1763
|
const risultato = { closed: code };
|
|
1466
1764
|
const cambiato = this.statusCorrente !== "ended" || JSON.stringify(this.resultCorrente) !== JSON.stringify(risultato);
|
|
1467
1765
|
this.terminata = true;
|
|
1766
|
+
this.segnalaStanza(null);
|
|
1468
1767
|
this.statusCorrente = "ended";
|
|
1469
1768
|
this.resultCorrente = risultato;
|
|
1470
|
-
this.voice.termina();
|
|
1769
|
+
if (!this.spettatore) this.voice.termina();
|
|
1471
1770
|
this.fermaPing();
|
|
1472
1771
|
this.fermaRiconnessione();
|
|
1473
1772
|
if (cambiato) this.notifica(this.ascoltatoriStatus, "ended", risultato, this.serverTime());
|
|
@@ -1477,7 +1776,9 @@ var StanzaClient = class {
|
|
|
1477
1776
|
4003: "kicked",
|
|
1478
1777
|
4004: "room_ended",
|
|
1479
1778
|
4005: "version_closed",
|
|
1480
|
-
4006: "replaced"
|
|
1779
|
+
4006: "replaced",
|
|
1780
|
+
4008: "rate_limited",
|
|
1781
|
+
4009: "invalid_request"
|
|
1481
1782
|
};
|
|
1482
1783
|
const erroreCode = typeof code === "number" ? codici[code] ?? "offline" : "offline";
|
|
1483
1784
|
this.rifiutaPronta(creaErrore(erroreCode, "The room connection ended."));
|
|
@@ -1490,6 +1791,9 @@ var StanzaClient = class {
|
|
|
1490
1791
|
this.timerRoster = null;
|
|
1491
1792
|
}
|
|
1492
1793
|
this.prontaRisolta = true;
|
|
1794
|
+
if (!this.spettatore && !this.terminata && !this.lasciata) {
|
|
1795
|
+
this.segnalaStanza({ code: this.codice });
|
|
1796
|
+
}
|
|
1493
1797
|
this.risolviPronta();
|
|
1494
1798
|
}
|
|
1495
1799
|
richiediFlush() {
|
|
@@ -1521,16 +1825,184 @@ function creaStanzeOffline(invited = null) {
|
|
|
1521
1825
|
},
|
|
1522
1826
|
async join() {
|
|
1523
1827
|
throw erroreOffline();
|
|
1828
|
+
},
|
|
1829
|
+
async watch() {
|
|
1830
|
+
throw erroreOffline();
|
|
1831
|
+
},
|
|
1832
|
+
async match() {
|
|
1833
|
+
throw erroreOffline();
|
|
1524
1834
|
}
|
|
1525
1835
|
};
|
|
1526
1836
|
}
|
|
1527
1837
|
function creaGestoreStanze(input, invited) {
|
|
1528
1838
|
const api = creaApiLive(input);
|
|
1839
|
+
let haSegnalato = false;
|
|
1840
|
+
let ultimoCodice = null;
|
|
1841
|
+
const segnalaStanza = (room) => {
|
|
1842
|
+
const codice = room?.code ?? null;
|
|
1843
|
+
if (haSegnalato && codice === ultimoCodice) return;
|
|
1844
|
+
haSegnalato = true;
|
|
1845
|
+
ultimoCodice = codice;
|
|
1846
|
+
input.segnalaStanza?.(room);
|
|
1847
|
+
};
|
|
1529
1848
|
const collega = async (ingresso) => {
|
|
1530
|
-
const stanza = new StanzaClient(
|
|
1849
|
+
const stanza = new StanzaClient(
|
|
1850
|
+
ingresso.roomId,
|
|
1851
|
+
ingresso.code,
|
|
1852
|
+
ingresso.url,
|
|
1853
|
+
input,
|
|
1854
|
+
api,
|
|
1855
|
+
segnalaStanza
|
|
1856
|
+
);
|
|
1531
1857
|
await stanza.pronta();
|
|
1532
1858
|
return stanza;
|
|
1533
1859
|
};
|
|
1860
|
+
const guarda = async (ingresso) => {
|
|
1861
|
+
const stanza = new StanzaClient(
|
|
1862
|
+
ingresso.roomId,
|
|
1863
|
+
ingresso.code,
|
|
1864
|
+
ingresso.url,
|
|
1865
|
+
input,
|
|
1866
|
+
api,
|
|
1867
|
+
() => void 0,
|
|
1868
|
+
true
|
|
1869
|
+
);
|
|
1870
|
+
await stanza.pronta();
|
|
1871
|
+
return {
|
|
1872
|
+
get state() {
|
|
1873
|
+
return stanza.state;
|
|
1874
|
+
},
|
|
1875
|
+
get tick() {
|
|
1876
|
+
return stanza.tick;
|
|
1877
|
+
},
|
|
1878
|
+
get seed() {
|
|
1879
|
+
return stanza.seed;
|
|
1880
|
+
},
|
|
1881
|
+
get status() {
|
|
1882
|
+
return stanza.status;
|
|
1883
|
+
},
|
|
1884
|
+
get players() {
|
|
1885
|
+
return stanza.players;
|
|
1886
|
+
},
|
|
1887
|
+
get host() {
|
|
1888
|
+
return stanza.host;
|
|
1889
|
+
},
|
|
1890
|
+
get code() {
|
|
1891
|
+
return stanza.code;
|
|
1892
|
+
},
|
|
1893
|
+
get result() {
|
|
1894
|
+
return stanza.result;
|
|
1895
|
+
},
|
|
1896
|
+
get delayMs() {
|
|
1897
|
+
return stanza.delayMs;
|
|
1898
|
+
},
|
|
1899
|
+
onState: (listener) => stanza.onState(listener),
|
|
1900
|
+
onPlayers: (listener) => stanza.onPlayers(listener),
|
|
1901
|
+
onStatus: (listener) => stanza.onStatus(listener),
|
|
1902
|
+
onMessage: (listener) => stanza.onMessage(listener),
|
|
1903
|
+
leave: () => {
|
|
1904
|
+
stanza.leave();
|
|
1905
|
+
},
|
|
1906
|
+
serverTime: () => stanza.serverTime()
|
|
1907
|
+
};
|
|
1908
|
+
};
|
|
1909
|
+
const attendiMatch = (url, options) => new Promise((resolve, reject) => {
|
|
1910
|
+
let socket;
|
|
1911
|
+
let conclusa = false;
|
|
1912
|
+
const pulisci = () => {
|
|
1913
|
+
socket.removeEventListener("message", ricevi);
|
|
1914
|
+
socket.removeEventListener("close", chiuso);
|
|
1915
|
+
socket.removeEventListener("error", caduto);
|
|
1916
|
+
options.signal?.removeEventListener("abort", annulla);
|
|
1917
|
+
};
|
|
1918
|
+
const chiudi = () => {
|
|
1919
|
+
try {
|
|
1920
|
+
socket.close(1e3);
|
|
1921
|
+
} catch {
|
|
1922
|
+
}
|
|
1923
|
+
};
|
|
1924
|
+
const fallisci = (errore, chiudiSocket) => {
|
|
1925
|
+
if (conclusa) return;
|
|
1926
|
+
conclusa = true;
|
|
1927
|
+
pulisci();
|
|
1928
|
+
if (chiudiSocket) chiudi();
|
|
1929
|
+
reject(errore);
|
|
1930
|
+
};
|
|
1931
|
+
function annulla() {
|
|
1932
|
+
fallisci(
|
|
1933
|
+
creaErrore("cancelled", "The matchmaking search was cancelled."),
|
|
1934
|
+
true
|
|
1935
|
+
);
|
|
1936
|
+
}
|
|
1937
|
+
function chiuso() {
|
|
1938
|
+
fallisci(erroreOffline(), false);
|
|
1939
|
+
}
|
|
1940
|
+
function caduto() {
|
|
1941
|
+
fallisci(erroreOffline(), true);
|
|
1942
|
+
}
|
|
1943
|
+
function ricevi(evento) {
|
|
1944
|
+
let dati = null;
|
|
1945
|
+
try {
|
|
1946
|
+
dati = typeof evento.data === "string" ? record2(JSON.parse(evento.data)) : null;
|
|
1947
|
+
} catch {
|
|
1948
|
+
}
|
|
1949
|
+
if (dati === null || typeof dati.t !== "string") {
|
|
1950
|
+
fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);
|
|
1951
|
+
return;
|
|
1952
|
+
}
|
|
1953
|
+
if (dati.t === "waiting") {
|
|
1954
|
+
if (!Number.isInteger(dati.players) || !Number.isInteger(dati.min) || !Number.isInteger(dati.max)) {
|
|
1955
|
+
fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);
|
|
1956
|
+
return;
|
|
1957
|
+
}
|
|
1958
|
+
try {
|
|
1959
|
+
options.onWaiting?.({
|
|
1960
|
+
players: dati.players,
|
|
1961
|
+
min: dati.min,
|
|
1962
|
+
max: dati.max
|
|
1963
|
+
});
|
|
1964
|
+
} catch {
|
|
1965
|
+
}
|
|
1966
|
+
return;
|
|
1967
|
+
}
|
|
1968
|
+
if (dati.t === "matched") {
|
|
1969
|
+
if (!ingressoValido(dati)) {
|
|
1970
|
+
fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);
|
|
1971
|
+
return;
|
|
1972
|
+
}
|
|
1973
|
+
conclusa = true;
|
|
1974
|
+
pulisci();
|
|
1975
|
+
chiudi();
|
|
1976
|
+
resolve(dati);
|
|
1977
|
+
return;
|
|
1978
|
+
}
|
|
1979
|
+
if (dati.t === "no_match") {
|
|
1980
|
+
fallisci(creaErrore("no_match", "No match was found before the timeout."), true);
|
|
1981
|
+
return;
|
|
1982
|
+
}
|
|
1983
|
+
if (dati.t === "error") {
|
|
1984
|
+
fallisci(creaErrore(
|
|
1985
|
+
typeof dati.code === "string" ? dati.code : "internal_error",
|
|
1986
|
+
typeof dati.message === "string" ? dati.message : "The matchmaking service could not complete the search."
|
|
1987
|
+
), true);
|
|
1988
|
+
return;
|
|
1989
|
+
}
|
|
1990
|
+
if (dati.t !== "pong") {
|
|
1991
|
+
fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
try {
|
|
1995
|
+
socket = input.apriSocket(url);
|
|
1996
|
+
} catch {
|
|
1997
|
+
reject(erroreOffline());
|
|
1998
|
+
return;
|
|
1999
|
+
}
|
|
2000
|
+
socket.addEventListener("message", ricevi);
|
|
2001
|
+
socket.addEventListener("close", chiuso);
|
|
2002
|
+
socket.addEventListener("error", caduto);
|
|
2003
|
+
options.signal?.addEventListener("abort", annulla, { once: true });
|
|
2004
|
+
if (options.signal?.aborted === true) annulla();
|
|
2005
|
+
});
|
|
1534
2006
|
return {
|
|
1535
2007
|
invited,
|
|
1536
2008
|
async create(options) {
|
|
@@ -1542,6 +2014,23 @@ function creaGestoreStanze(input, invited) {
|
|
|
1542
2014
|
throw creaErrore("invalid_request", "A room invitation code is required.");
|
|
1543
2015
|
}
|
|
1544
2016
|
return collega(await api.joinCode(scelto));
|
|
2017
|
+
},
|
|
2018
|
+
async watch(code) {
|
|
2019
|
+
if (typeof code !== "string" || code.length === 0) {
|
|
2020
|
+
throw creaErrore("invalid_request", "A room invitation code is required.");
|
|
2021
|
+
}
|
|
2022
|
+
return guarda(await api.watchCode(code));
|
|
2023
|
+
},
|
|
2024
|
+
async match(options) {
|
|
2025
|
+
const annullata = () => options.signal?.aborted === true;
|
|
2026
|
+
if (annullata()) {
|
|
2027
|
+
throw creaErrore("cancelled", "The matchmaking search was cancelled.");
|
|
2028
|
+
}
|
|
2029
|
+
const risposta = await api.match(options);
|
|
2030
|
+
if (annullata()) {
|
|
2031
|
+
throw creaErrore("cancelled", "The matchmaking search was cancelled.");
|
|
2032
|
+
}
|
|
2033
|
+
return collega(await attendiMatch(risposta.url, options));
|
|
1545
2034
|
}
|
|
1546
2035
|
};
|
|
1547
2036
|
}
|
|
@@ -1655,7 +2144,8 @@ function dipendenzeReali2() {
|
|
|
1655
2144
|
archivio: archivioReale(),
|
|
1656
2145
|
hostname: typeof location === "undefined" ? "" : location.hostname,
|
|
1657
2146
|
subtle: globalThis.crypto.subtle,
|
|
1658
|
-
ora: Date.now
|
|
2147
|
+
ora: Date.now,
|
|
2148
|
+
sonda: () => probeDevice()
|
|
1659
2149
|
};
|
|
1660
2150
|
}
|
|
1661
2151
|
async function connetti(input) {
|
|
@@ -1710,7 +2200,13 @@ async function connetti(input) {
|
|
|
1710
2200
|
clearTimeout: (id) => globalThis.clearTimeout(id),
|
|
1711
2201
|
setInterval: (handler, timeout) => globalThis.setInterval(handler, timeout),
|
|
1712
2202
|
clearInterval: (id) => globalThis.clearInterval(id),
|
|
1713
|
-
voce: input.voce
|
|
2203
|
+
voce: input.voce,
|
|
2204
|
+
segnalaStanza(room2) {
|
|
2205
|
+
try {
|
|
2206
|
+
handshake.porta.postMessage({ type: "caisual:room", room: room2 });
|
|
2207
|
+
} catch {
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
1714
2210
|
}, handshake.invite);
|
|
1715
2211
|
return {
|
|
1716
2212
|
connected: true,
|
|
@@ -1737,11 +2233,38 @@ async function connetti(input) {
|
|
|
1737
2233
|
room
|
|
1738
2234
|
};
|
|
1739
2235
|
}
|
|
2236
|
+
function dispositivoSconosciuto() {
|
|
2237
|
+
return {
|
|
2238
|
+
webgl2: false,
|
|
2239
|
+
webgpu: false,
|
|
2240
|
+
wasm: false,
|
|
2241
|
+
threads: false,
|
|
2242
|
+
isolated: false,
|
|
2243
|
+
gpu: "none",
|
|
2244
|
+
memoryMb: null,
|
|
2245
|
+
cores: null,
|
|
2246
|
+
mobile: false,
|
|
2247
|
+
tier: "low"
|
|
2248
|
+
};
|
|
2249
|
+
}
|
|
2250
|
+
async function attendiSonda(sonda) {
|
|
2251
|
+
let timer;
|
|
2252
|
+
try {
|
|
2253
|
+
return await Promise.race([
|
|
2254
|
+
Promise.resolve().then(sonda).catch(() => dispositivoSconosciuto()),
|
|
2255
|
+
new Promise((resolve) => {
|
|
2256
|
+
timer = globalThis.setTimeout(() => resolve(dispositivoSconosciuto()), 1500);
|
|
2257
|
+
})
|
|
2258
|
+
]);
|
|
2259
|
+
} finally {
|
|
2260
|
+
if (timer !== void 0) globalThis.clearTimeout(timer);
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
1740
2263
|
function creaKit(input = dipendenzeReali2()) {
|
|
1741
2264
|
let promessa = null;
|
|
1742
2265
|
return {
|
|
1743
2266
|
connect() {
|
|
1744
|
-
promessa ?? (promessa = connetti(input));
|
|
2267
|
+
promessa ?? (promessa = Promise.all([connetti(input), attendiSonda(input.sonda)]).then(([connessione, device]) => ({ ...connessione, device })));
|
|
1745
2268
|
return promessa;
|
|
1746
2269
|
}
|
|
1747
2270
|
};
|