@zaisma/game-server 1.47.0 → 1.49.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/bench/glass.js +685 -89
- package/bin/local-play-bench.js +1 -1
- package/bin/local-play.js +2 -2
- package/package.json +1 -1
- package/sdk/zaisma.d.ts +88 -1
- package/sdk/zaisma.js +510 -3
- package/src/chat.d.ts +11 -0
- package/src/chat.js +11 -0
- package/src/doors.d.ts +1 -1
- package/src/index.d.ts +3 -2
- package/src/index.js +3 -2
- package/src/internal.d.ts +1 -0
- package/src/internal.js +1 -0
- package/src/pass.d.ts +15 -0
- package/src/pass.js +16 -0
- package/src/room-calls.d.ts +9 -0
- package/src/room-calls.js +21 -1
- package/src/room.d.ts +99 -1
- package/src/room.js +93 -8
- package/src/voice-ticket.d.ts +56 -0
- package/src/voice-ticket.js +107 -0
package/sdk/zaisma.js
CHANGED
|
@@ -37,7 +37,30 @@
|
|
|
37
37
|
/** A fast lane that isn't up in this long gives way to the WebSocket, for the rest of the room's life. */
|
|
38
38
|
var FAST_LANE_CONNECT_TIMEOUT_MS = 3000;
|
|
39
39
|
var ROOM_INFO_FIELDS = ["name", "labels"];
|
|
40
|
-
var ROOM_EVENTS = ["message", "bytes", "state", "players", "connectionStatus", "ended", "ending", "endingCancelled", "kicked", "settings", "move", "moved", "idle", "away", "back", "muted", "error", "versionUpdate", "chat", "roomMessage"];
|
|
40
|
+
var ROOM_EVENTS = ["message", "bytes", "state", "players", "connectionStatus", "ended", "ending", "endingCancelled", "kicked", "settings", "move", "moved", "idle", "away", "back", "muted", "error", "versionUpdate", "chat", "roomMessage", "speaking", "voiceSpeakers"];
|
|
41
|
+
/** Voice (room.voice): what the SDK sends (Opus mono, packets this long), when a voice counts as speaking, and the report clip. Zaisma's VOICE_LIMITS has the same. */
|
|
42
|
+
var VOICE_JOIN_OPTIONS = ["isMuted", "ticket"];
|
|
43
|
+
var VOICE_BITRATE_BITS_PER_SEC = 24000;
|
|
44
|
+
var VOICE_PACKET_DURATION_MS = 40;
|
|
45
|
+
var VOICE_SPEAKING_LEVEL_PCT = 30;
|
|
46
|
+
var VOICE_SPEAKING_HOLD_MS = 300;
|
|
47
|
+
var VOICE_LEVEL_REPORT_INTERVAL_MS = 1000;
|
|
48
|
+
var VOICE_REPORT_AUDIO_DURATION_SEC = 30;
|
|
49
|
+
var VOICE_MAXIMUM_REPORT_AUDIO_SIZE_BYTES = 1000000;
|
|
50
|
+
var VOICE_MAXIMUM_TRANSCEIVER_COUNT = 40;
|
|
51
|
+
/** On a game's own server, a fresh pass goes on the voice channel this often: a pass lasts an hour. */
|
|
52
|
+
var VOICE_PASS_REFRESH_INTERVAL_MS = 30 * 60 * 1000;
|
|
53
|
+
/** Fresh sessions tried in a row after a voice connection fails, before the player is told. */
|
|
54
|
+
var VOICE_MAXIMUM_RECONNECT_COUNT = 3;
|
|
55
|
+
var VOICE_ERROR_TEXT = {
|
|
56
|
+
voice_not_allowed: "Voice isn't open to this player here.",
|
|
57
|
+
voice_unavailable: "Voice isn't available right now.",
|
|
58
|
+
voice_ticket_required: "On its own server, a game's voice needs a ticket from that server.",
|
|
59
|
+
voice_ticket_refused: "The voice ticket was refused: ask the game's server for a new one.",
|
|
60
|
+
hosting_paused: "This game's online play is paused, voice included.",
|
|
61
|
+
rented_share_used: "Voice isn't available from this network right now.",
|
|
62
|
+
busy: "Voice is busy. Try again in a minute.",
|
|
63
|
+
};
|
|
41
64
|
/** Chat (room.chat): the longest message, the lines a room keeps in memory for reports, and how many of them a report carries. */
|
|
42
65
|
var MAXIMUM_CHAT_CHARACTER_COUNT = 2000;
|
|
43
66
|
var KEPT_CHAT_LINE_COUNT = 50;
|
|
@@ -102,6 +125,15 @@
|
|
|
102
125
|
var offZaisma = null;
|
|
103
126
|
|
|
104
127
|
/** An Error with a code, as the SDK's errors all have. */
|
|
128
|
+
/** A blob as base64, for a JSON body. */
|
|
129
|
+
function blobBase64(blob) {
|
|
130
|
+
return new Promise(function (resolve, reject) {
|
|
131
|
+
var reader = new FileReader();
|
|
132
|
+
reader.onload = function () { resolve(String(reader.result).slice(String(reader.result).indexOf(",") + 1)); };
|
|
133
|
+
reader.onerror = function () { reject(reader.error); };
|
|
134
|
+
reader.readAsDataURL(blob);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
105
137
|
function codedError(code, message) {
|
|
106
138
|
var error = new Error(message);
|
|
107
139
|
error.code = code;
|
|
@@ -616,6 +648,9 @@
|
|
|
616
648
|
this._chatNumber = 0;
|
|
617
649
|
this._chats = new Map();
|
|
618
650
|
this._chatLines = [];
|
|
651
|
+
// Voice (room.voice): whether this player can talk (from welcome), and the room's voice.
|
|
652
|
+
this.canTalk = false;
|
|
653
|
+
this.voice = new RoomVoice(this);
|
|
619
654
|
}
|
|
620
655
|
Room.prototype = Object.create(Emitter.prototype);
|
|
621
656
|
|
|
@@ -868,6 +903,9 @@
|
|
|
868
903
|
room._probeIntervalSec = typeof message.probeIntervalSec === "number" ? message.probeIntervalSec : null;
|
|
869
904
|
room.spectatorCount = typeof message.spectatorCount === "number" ? message.spectatorCount : 0;
|
|
870
905
|
room.canChat = message.canChat === true && !room.isSpectator;
|
|
906
|
+
room.canTalk = (typeof message.canTalk === "boolean" ? message.canTalk : message.canChat === true) && !room.isSpectator;
|
|
907
|
+
// Back after a lost connection: voice starts again on the new one.
|
|
908
|
+
if (room.voice._wantsJoin && !room.voice._joinWaiter) room.voice._rejoin();
|
|
871
909
|
// A room that doesn't say so (an older game server of the game's own) gets one message a frame, as before.
|
|
872
910
|
room._canCombine = message.canCombineMessages === true;
|
|
873
911
|
// A new connection: the first latest-only send of each kind goes, whatever went before.
|
|
@@ -926,6 +964,8 @@
|
|
|
926
964
|
room._emit("message", message.data, typeof message.fromPlayerId === "string" ? message.fromPlayerId : null);
|
|
927
965
|
} else if (message.type === "chat" || message.type === "chat_sent" || message.type === "chat_stopped" || (message.type === "error" && typeof message.chatNumber === "number")) {
|
|
928
966
|
room._chatAnswer(message);
|
|
967
|
+
} else if (message.type === "voice") {
|
|
968
|
+
room.voice._answer(message);
|
|
929
969
|
} else if (message.type === "state") {
|
|
930
970
|
room._takeState(message);
|
|
931
971
|
} else if (message.type === "room") {
|
|
@@ -1603,7 +1643,9 @@
|
|
|
1603
1643
|
Room.prototype._mutedChanged = function () {
|
|
1604
1644
|
var room = this;
|
|
1605
1645
|
var hidden = new Set(Array.from(this._muted || []).concat(Array.from(blockedSet()), Array.from(lastingBlocked), Array.from(this._blocks || [])));
|
|
1606
|
-
|
|
1646
|
+
var mutedPlayerIds = Array.from(hidden).filter(function (playerId) { return !room.me || playerId !== room.me.id; });
|
|
1647
|
+
this.voice._mutedChanged(mutedPlayerIds);
|
|
1648
|
+
this._emit("muted", mutedPlayerIds);
|
|
1607
1649
|
this._tellPage();
|
|
1608
1650
|
};
|
|
1609
1651
|
/**
|
|
@@ -1770,7 +1812,11 @@
|
|
|
1770
1812
|
var chatMessages = this._chatLines.filter(function (line) {
|
|
1771
1813
|
return line.fromPlayerId === playerId || (line.fromPlayerId === me && (!line.toPlayerIds || line.toPlayerIds.indexOf(playerId) !== -1));
|
|
1772
1814
|
}).slice(-REPORTED_CHAT_LINE_COUNT).map(function (line) { return { fromPlayerId: line.fromPlayerId, text: line.text, sentAt: line.sentAt }; });
|
|
1773
|
-
|
|
1815
|
+
var report = { gameSlug: config.game, roomCode: this.roomCode, playerId: playerId, playerName: reported ? reported.name : "", reason: reason, details: details || "", chatMessages: chatMessages };
|
|
1816
|
+
var clip = this.voice._reportClip();
|
|
1817
|
+
if (!clip) return post("/v1/report", report);
|
|
1818
|
+
// The last 30 seconds of voice this player heard go with it.
|
|
1819
|
+
return blobBase64(clip).then(function (voiceClipBase64) { report.voiceClipBase64 = voiceClipBase64; return post("/v1/report", report); });
|
|
1774
1820
|
};
|
|
1775
1821
|
|
|
1776
1822
|
/**
|
|
@@ -1834,7 +1880,462 @@
|
|
|
1834
1880
|
waiting.forEach(function (chat) { chat.reject(codedError("connection_closed", "The room closed before the message went.")); });
|
|
1835
1881
|
};
|
|
1836
1882
|
|
|
1883
|
+
/**
|
|
1884
|
+
* Voice in a room (zaisma.d.ts: room.voice; PL39). The game holds the microphone; Zaisma's room decides whose voices
|
|
1885
|
+
* reach this player through Cloudflare's media server, by the same rules as chat, and the SDK plays them, each through
|
|
1886
|
+
* its own volume and left-right, which the game can replace with its own effects (audioNodeOf). Nothing of voice is
|
|
1887
|
+
* kept, except the last 30 seconds this player heard, in memory only, which go with a report they send.
|
|
1888
|
+
*/
|
|
1889
|
+
function RoomVoice(room) {
|
|
1890
|
+
this._room = room;
|
|
1891
|
+
this.isJoined = false;
|
|
1892
|
+
this.speakerPlayerIds = [];
|
|
1893
|
+
this.audioContext = null;
|
|
1894
|
+
this.outputNode = null;
|
|
1895
|
+
this._outputElement = null;
|
|
1896
|
+
this._pc = null;
|
|
1897
|
+
this._microphone = null;
|
|
1898
|
+
this._isMicrophoneMuted = false;
|
|
1899
|
+
this._wantsJoin = false;
|
|
1900
|
+
this._joinWaiter = null;
|
|
1901
|
+
this._joinAttemptNumber = 0;
|
|
1902
|
+
this._playerIdByMid = new Map();
|
|
1903
|
+
this._speakers = new Map();
|
|
1904
|
+
this._volumePct = new Map();
|
|
1905
|
+
this._panPct = new Map();
|
|
1906
|
+
this._levelTimer = null;
|
|
1907
|
+
this._ownLevel = { isSpeaking: false, levelPct: 0, sentAtMs: 0 };
|
|
1908
|
+
this._clip = null;
|
|
1909
|
+
}
|
|
1910
|
+
Object.defineProperty(RoomVoice.prototype, "canTalk", { get: function () { return this._channel ? this._channelCanTalk === true : this._room.canTalk === true; } });
|
|
1911
|
+
|
|
1912
|
+
/** The channel a voice ticket names (its server signed it; Zaisma checks it), or null. */
|
|
1913
|
+
function voiceTicketChannel(ticket) {
|
|
1914
|
+
try {
|
|
1915
|
+
var claims = JSON.parse(atob(String(ticket).split(".")[0].replace(/-/g, "+").replace(/_/g, "/")));
|
|
1916
|
+
return typeof claims.channel_name === "string" && /^[a-z0-9_:-]{1,40}$/.test(claims.channel_name) ? claims.channel_name : null;
|
|
1917
|
+
} catch (error) {
|
|
1918
|
+
return null;
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
/** A voice channel on Zaisma for a room on the game's own server: the player's pass and the ticket go first. */
|
|
1922
|
+
RoomVoice.prototype._openChannel = function () {
|
|
1923
|
+
var voice = this;
|
|
1924
|
+
var room = this._room;
|
|
1925
|
+
if (!config.rooms) return Promise.reject(codedError("voice_unavailable", "Voice goes through Zaisma, so it works only in games played on Zaisma."));
|
|
1926
|
+
return getPass().then(function (given) {
|
|
1927
|
+
return new Promise(function (resolve, reject) {
|
|
1928
|
+
var socket = new WebSocket(config.rooms.replace(/^http/, "ws") + "/v1/voice/" + config.game + "/" + voiceTicketChannel(voice._ticket));
|
|
1929
|
+
voice._channel = socket;
|
|
1930
|
+
var isWelcomed = false;
|
|
1931
|
+
socket.onopen = function () { socket.send(JSON.stringify({ type: "hello", pass: given.pass, ticket: voice._ticket })); };
|
|
1932
|
+
socket.onmessage = function (event) {
|
|
1933
|
+
var message;
|
|
1934
|
+
try { message = JSON.parse(event.data); } catch (error) { return; }
|
|
1935
|
+
if (message.type === "welcome") {
|
|
1936
|
+
isWelcomed = true;
|
|
1937
|
+
voice._channelCanTalk = message.canTalk === true;
|
|
1938
|
+
// A pass lasts an hour: a fresh one goes on the channel well before, so a long match keeps its voice.
|
|
1939
|
+
voice._passTimer = setInterval(function () {
|
|
1940
|
+
getPass().then(function (fresh) { if (voice._channel === socket) socket.send(JSON.stringify({ type: "voice", action: "pass", pass: fresh.pass })); }, function () {});
|
|
1941
|
+
}, VOICE_PASS_REFRESH_INTERVAL_MS);
|
|
1942
|
+
return resolve();
|
|
1943
|
+
}
|
|
1944
|
+
if (message.type === "voice") {
|
|
1945
|
+
if (!isWelcomed && message.action === "error") return reject(codedError(message.code, VOICE_ERROR_TEXT[message.code] || "Voice isn't available right now."));
|
|
1946
|
+
voice._answer(message);
|
|
1947
|
+
}
|
|
1948
|
+
};
|
|
1949
|
+
socket.onclose = function () {
|
|
1950
|
+
if (voice._channel !== socket) return;
|
|
1951
|
+
voice._channel = null;
|
|
1952
|
+
if (!isWelcomed) return reject(codedError("voice_unavailable", VOICE_ERROR_TEXT.voice_unavailable));
|
|
1953
|
+
// Lost: start again on a new connection, with the ticket kept (the server sends a fresh one when it's old).
|
|
1954
|
+
if (voice._wantsJoin && !room._done) voice._rejoin();
|
|
1955
|
+
};
|
|
1956
|
+
});
|
|
1957
|
+
});
|
|
1958
|
+
};
|
|
1959
|
+
/** New rules from the game's own server (a team switch, a range): a new ticket on the open channel. */
|
|
1960
|
+
RoomVoice.prototype.updateTicket = function (ticket) {
|
|
1961
|
+
if (typeof ticket !== "string" || voiceTicketChannel(ticket) !== voiceTicketChannel(this._ticket)) throw codedError("voice_ticket_refused", "room.voice.updateTicket takes a ticket for the channel this player joined.");
|
|
1962
|
+
this._ticket = ticket;
|
|
1963
|
+
// With a fresh pass, so what the player's account says now applies too.
|
|
1964
|
+
var voice = this;
|
|
1965
|
+
getPass().then(function (given) { voice._sendVoice({ action: "ticket", ticket: ticket, pass: given.pass }); }, function () { voice._sendVoice({ action: "ticket", ticket: ticket }); });
|
|
1966
|
+
};
|
|
1967
|
+
|
|
1968
|
+
RoomVoice.prototype.join = function (voiceOptions) {
|
|
1969
|
+
var refused = unknownOption(voiceOptions, VOICE_JOIN_OPTIONS, "room.voice.join");
|
|
1970
|
+
if (refused) return Promise.reject(refused);
|
|
1971
|
+
var voice = this;
|
|
1972
|
+
var room = this._room;
|
|
1973
|
+
if (room._done || room.isSpectator || !room.me || room.me.kind !== "account") return Promise.reject(codedError("voice_not_allowed", "Voice is for players signed in to Zaisma."));
|
|
1974
|
+
if (typeof RTCPeerConnection !== "function" || typeof AudioContext !== "function") return Promise.reject(codedError("voice_unavailable", "This browser can't do voice."));
|
|
1975
|
+
if (this._joinWaiter) return this._joinWaiter.promise;
|
|
1976
|
+
if (this.isJoined) return Promise.resolve();
|
|
1977
|
+
// A room on the game's own server talks through a voice channel on Zaisma, joined with a ticket that server signed.
|
|
1978
|
+
var ticket = voiceOptions && voiceOptions.ticket;
|
|
1979
|
+
if (room._serverUrl && !ticket) return Promise.reject(codedError("voice_ticket_required", VOICE_ERROR_TEXT.voice_ticket_required));
|
|
1980
|
+
if (ticket !== undefined && (typeof ticket !== "string" || !voiceTicketChannel(ticket))) return Promise.reject(codedError("voice_ticket_refused", VOICE_ERROR_TEXT.voice_ticket_refused));
|
|
1981
|
+
this._ticket = ticket || null;
|
|
1982
|
+
this._isMicrophoneMuted = Boolean(voiceOptions && voiceOptions.isMuted);
|
|
1983
|
+
this._wantsJoin = true;
|
|
1984
|
+
// Made and woken here, inside the player's tap: made later, after the microphone's prompt, browsers keep it silent.
|
|
1985
|
+
if (!this.audioContext) this.audioContext = new AudioContext();
|
|
1986
|
+
if (!this.outputNode) {
|
|
1987
|
+
// Voices reach the speakers through a media element, not the context's own output: echo cancellation hears what
|
|
1988
|
+
// media elements play (on phones, only that), so the microphone doesn't send the voices back. The game's own sound
|
|
1989
|
+
// can come this way too (outputNode).
|
|
1990
|
+
var output = this.audioContext.createMediaStreamDestination();
|
|
1991
|
+
this._outputElement = new Audio();
|
|
1992
|
+
this._outputElement.srcObject = output.stream;
|
|
1993
|
+
this.outputNode = output;
|
|
1994
|
+
this._unlockSoundOnTap();
|
|
1995
|
+
}
|
|
1996
|
+
this._wakeSound();
|
|
1997
|
+
var attemptNumber = ++this._joinAttemptNumber;
|
|
1998
|
+
var isCurrent = function () { return voice._joinAttemptNumber === attemptNumber && voice._wantsJoin; };
|
|
1999
|
+
var waiter = {};
|
|
2000
|
+
waiter.promise = new Promise(function (resolve, reject) { waiter.resolve = resolve; waiter.reject = reject; });
|
|
2001
|
+
this._joinWaiter = waiter;
|
|
2002
|
+
(this._ticket ? this._openChannel() : Promise.resolve())
|
|
2003
|
+
.then(function () { return voice.canTalk ? navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true, channelCount: 1 } }).catch(function (error) {
|
|
2004
|
+
// Only a no from the player or the browser is a refusal; no microphone, or one another app holds, says so.
|
|
2005
|
+
if (error && error.name === "NotFoundError") throw codedError("voice_unavailable", "No microphone was found. Plug one in, or join to listen.");
|
|
2006
|
+
if (error && error.name === "NotReadableError") throw codedError("voice_unavailable", "The microphone is busy in another app. Close it there and try again.");
|
|
2007
|
+
throw codedError("microphone_refused", "The browser didn't let the game use the microphone. Allow it for this site, then join again.");
|
|
2008
|
+
}) : null; })
|
|
2009
|
+
.then(function (microphone) {
|
|
2010
|
+
// Left while the browser asked: the microphone is let go at once, never kept.
|
|
2011
|
+
if (!isCurrent()) {
|
|
2012
|
+
if (microphone) microphone.getTracks().forEach(function (track) { track.stop(); });
|
|
2013
|
+
return;
|
|
2014
|
+
}
|
|
2015
|
+
return voice._connect(microphone);
|
|
2016
|
+
})
|
|
2017
|
+
.catch(function (error) { if (isCurrent()) voice._failJoin(error && error.code ? error : codedError("voice_unavailable", "Voice couldn't start. Try again.")); });
|
|
2018
|
+
return waiter.promise;
|
|
2019
|
+
};
|
|
2020
|
+
|
|
2021
|
+
RoomVoice.prototype._connect = function (microphone) {
|
|
2022
|
+
var voice = this;
|
|
2023
|
+
this._microphone = microphone;
|
|
2024
|
+
var pc = new RTCPeerConnection({ iceServers: [{ urls: "stun:stun.cloudflare.com:3478" }], bundlePolicy: "max-bundle" });
|
|
2025
|
+
this._pc = pc;
|
|
2026
|
+
pc.ontrack = function () { voice._wire(); };
|
|
2027
|
+
// A connection that fails (a network change, a media server hiccup) starts a fresh session, up to 3 times in a row;
|
|
2028
|
+
// after that the player hears why, rather than sitting in a silent voice.
|
|
2029
|
+
pc.onconnectionstatechange = function () {
|
|
2030
|
+
if (voice._pc !== pc) return;
|
|
2031
|
+
if (pc.connectionState === "connected") voice._failedConnectionCount = 0;
|
|
2032
|
+
if (pc.connectionState !== "failed") return;
|
|
2033
|
+
voice._failedConnectionCount = (voice._failedConnectionCount || 0) + 1;
|
|
2034
|
+
if (voice._failedConnectionCount > VOICE_MAXIMUM_RECONNECT_COUNT) {
|
|
2035
|
+
voice._failedConnectionCount = 0;
|
|
2036
|
+
voice._wantsJoin = false;
|
|
2037
|
+
// The room drops the dead session, so nobody keeps pulling it.
|
|
2038
|
+
voice._sendVoice({ action: "leave" });
|
|
2039
|
+
return voice._failJoin(codedError("voice_unavailable", "Voice couldn't connect. Check the connection and join again."));
|
|
2040
|
+
}
|
|
2041
|
+
voice._rejoin();
|
|
2042
|
+
};
|
|
2043
|
+
if (!microphone) return voice._sendVoice({ action: "join" });
|
|
2044
|
+
var track = microphone.getAudioTracks()[0];
|
|
2045
|
+
track.enabled = !this._isMicrophoneMuted;
|
|
2046
|
+
var transceiver = pc.addTransceiver(track, { direction: "sendonly" });
|
|
2047
|
+
this._watchOwnLevel(microphone);
|
|
2048
|
+
return pc.createOffer().then(function (offer) {
|
|
2049
|
+
// Opus mono at 24 kbps in 40 ms packets, silent when the player is: about 33 kbps talking and 1 kbps silent, on the wire.
|
|
2050
|
+
var sdp = offer.sdp.replace(/a=fmtp:111 ([^\r\n]*)/, "a=fmtp:111 $1;usedtx=1;useinbandfec=1;stereo=0;maxaveragebitrate=" + VOICE_BITRATE_BITS_PER_SEC).replace(/(m=audio[^\r\n]*\r\n)/, "$1a=ptime:" + VOICE_PACKET_DURATION_MS + "\r\n");
|
|
2051
|
+
return pc.setLocalDescription({ type: "offer", sdp: sdp }).then(function () { voice._sendVoice({ action: "join", offerSdp: sdp, mid: transceiver.mid }); });
|
|
2052
|
+
});
|
|
2053
|
+
};
|
|
2054
|
+
|
|
2055
|
+
RoomVoice.prototype._failJoin = function (error) {
|
|
2056
|
+
var waiter = this._joinWaiter;
|
|
2057
|
+
this._joinWaiter = null;
|
|
2058
|
+
this._close();
|
|
2059
|
+
if (waiter) waiter.reject(error);
|
|
2060
|
+
else this._room._emit("error", { code: error.code, message: error.message });
|
|
2061
|
+
};
|
|
2062
|
+
|
|
2063
|
+
/** What the room said about voice. */
|
|
2064
|
+
RoomVoice.prototype._answer = function (message) {
|
|
2065
|
+
var voice = this;
|
|
2066
|
+
var pc = this._pc;
|
|
2067
|
+
if (message.action === "error") return this._failJoin(codedError(typeof message.code === "string" ? message.code : "voice_unavailable", VOICE_ERROR_TEXT[message.code] || "Voice isn't available right now."));
|
|
2068
|
+
if (!pc) return;
|
|
2069
|
+
if (message.action === "joined") {
|
|
2070
|
+
var done = function () {
|
|
2071
|
+
voice.isJoined = true;
|
|
2072
|
+
var waiter = voice._joinWaiter;
|
|
2073
|
+
voice._joinWaiter = null;
|
|
2074
|
+
if (waiter) waiter.resolve();
|
|
2075
|
+
};
|
|
2076
|
+
if (typeof message.answerSdp === "string") pc.setRemoteDescription({ type: "answer", sdp: message.answerSdp }).then(done, function () { voice._failJoin(codedError("voice_unavailable", "Voice couldn't connect.")); });
|
|
2077
|
+
else done();
|
|
2078
|
+
} else if (message.action === "offer" && typeof message.offerSdp === "string") {
|
|
2079
|
+
pc.setRemoteDescription({ type: "offer", sdp: message.offerSdp })
|
|
2080
|
+
.then(function () { return pc.createAnswer(); })
|
|
2081
|
+
.then(function (answer) { return pc.setLocalDescription(answer).then(function () { voice._sendVoice({ action: "answer", answerSdp: answer.sdp }); }); })
|
|
2082
|
+
.catch(function () { voice._rejoin(); });
|
|
2083
|
+
} else if (message.action === "tracks" && Array.isArray(message.tracks)) {
|
|
2084
|
+
this._playerIdByMid = new Map(message.tracks.filter(function (track) { return track && typeof track.mid === "string" && typeof track.playerId === "string"; }).map(function (track) { return [track.mid, track.playerId]; }));
|
|
2085
|
+
this._wire();
|
|
2086
|
+
// A long session's description grows with every voice pulled: past this many, start a fresh one.
|
|
2087
|
+
if (pc.getTransceivers().length > VOICE_MAXIMUM_TRANSCEIVER_COUNT) this._rejoin();
|
|
2088
|
+
}
|
|
2089
|
+
};
|
|
2090
|
+
|
|
2091
|
+
/** Each voice pulled now, through its own volume and left-right to the speakers, and into the report clip's mix. */
|
|
2092
|
+
RoomVoice.prototype._wire = function () {
|
|
2093
|
+
var voice = this;
|
|
2094
|
+
var audioContext = this.audioContext;
|
|
2095
|
+
if (!this._pc || !audioContext) return;
|
|
2096
|
+
var heard = new Set();
|
|
2097
|
+
this._pc.getTransceivers().forEach(function (transceiver) {
|
|
2098
|
+
var playerId = transceiver.mid !== null ? voice._playerIdByMid.get(transceiver.mid) : undefined;
|
|
2099
|
+
if (!playerId || transceiver.direction === "sendonly" || !transceiver.receiver.track) return;
|
|
2100
|
+
heard.add(playerId);
|
|
2101
|
+
var speaker = voice._speakers.get(playerId);
|
|
2102
|
+
if (speaker && speaker.track === transceiver.receiver.track) return;
|
|
2103
|
+
if (speaker) voice._unwire(playerId);
|
|
2104
|
+
var stream = new MediaStream([transceiver.receiver.track]);
|
|
2105
|
+
// Chrome plays a remote voice into Web Audio only while a media element holds it too; the element itself stays silent.
|
|
2106
|
+
var element = new Audio();
|
|
2107
|
+
element.muted = true;
|
|
2108
|
+
element.srcObject = stream;
|
|
2109
|
+
element.play().catch(function () {});
|
|
2110
|
+
var source = audioContext.createMediaStreamSource(stream);
|
|
2111
|
+
var analyser = audioContext.createAnalyser();
|
|
2112
|
+
analyser.fftSize = 512;
|
|
2113
|
+
var gain = audioContext.createGain();
|
|
2114
|
+
var panner = audioContext.createStereoPanner();
|
|
2115
|
+
gain.gain.value = (voice._volumePct.has(playerId) ? voice._volumePct.get(playerId) : 100) / 100;
|
|
2116
|
+
panner.pan.value = (voice._panPct.get(playerId) || 0) / 100;
|
|
2117
|
+
source.connect(analyser);
|
|
2118
|
+
source.connect(gain);
|
|
2119
|
+
gain.connect(panner);
|
|
2120
|
+
panner.connect(voice.outputNode);
|
|
2121
|
+
voice._speakers.set(playerId, { track: transceiver.receiver.track, element: element, source: source, analyser: analyser, gain: gain, panner: panner, isSpeaking: false, quietSinceMs: 0 });
|
|
2122
|
+
voice._recordInto(gain);
|
|
2123
|
+
});
|
|
2124
|
+
Array.from(this._speakers.keys()).forEach(function (playerId) { if (!heard.has(playerId)) voice._unwire(playerId); });
|
|
2125
|
+
var speakerPlayerIds = Array.from(heard).sort();
|
|
2126
|
+
if (speakerPlayerIds.join(",") !== this.speakerPlayerIds.join(",")) {
|
|
2127
|
+
this.speakerPlayerIds = speakerPlayerIds;
|
|
2128
|
+
this._room._emit("voiceSpeakers", { playerIds: speakerPlayerIds.slice() });
|
|
2129
|
+
}
|
|
2130
|
+
this._watchLevels();
|
|
2131
|
+
};
|
|
2132
|
+
|
|
2133
|
+
RoomVoice.prototype._unwire = function (playerId) {
|
|
2134
|
+
var speaker = this._speakers.get(playerId);
|
|
2135
|
+
if (!speaker) return;
|
|
2136
|
+
this._speakers.delete(playerId);
|
|
2137
|
+
try { speaker.source.disconnect(); speaker.gain.disconnect(); speaker.panner.disconnect(); } catch (error) { /* already gone */ }
|
|
2138
|
+
speaker.element.srcObject = null;
|
|
2139
|
+
if (speaker.isSpeaking) this._room._emit("speaking", { playerId: playerId, isSpeaking: false });
|
|
2140
|
+
};
|
|
2141
|
+
|
|
2142
|
+
/** Who's speaking, from each voice's level here, ten times a second: "speaking" events, the player's own included. */
|
|
2143
|
+
RoomVoice.prototype._watchLevels = function () {
|
|
2144
|
+
var voice = this;
|
|
2145
|
+
if (this._levelTimer) return;
|
|
2146
|
+
var samples = new Float32Array(512);
|
|
2147
|
+
var levelOf = function (analyser) {
|
|
2148
|
+
analyser.getFloatTimeDomainData(samples);
|
|
2149
|
+
var squaredSampleSumNumber = 0;
|
|
2150
|
+
for (var index = 0; index < samples.length; index++) squaredSampleSumNumber += samples[index] * samples[index];
|
|
2151
|
+
// Loudness 0 to 100 from the RMS level, about -60 dB to 0 dB.
|
|
2152
|
+
var decibelLevel = 20 * Math.log10(Math.sqrt(squaredSampleSumNumber / samples.length) || 1e-6);
|
|
2153
|
+
return Math.max(0, Math.min(100, Math.round(((decibelLevel + 60) / 60) * 100)));
|
|
2154
|
+
};
|
|
2155
|
+
var step = function (playerId, speaker, levelPct, nowMs) {
|
|
2156
|
+
var isLoud = levelPct >= VOICE_SPEAKING_LEVEL_PCT;
|
|
2157
|
+
if (isLoud) speaker.quietSinceMs = 0;
|
|
2158
|
+
else if (!speaker.quietSinceMs) speaker.quietSinceMs = nowMs;
|
|
2159
|
+
var isSpeaking = isLoud || (speaker.isSpeaking && nowMs - speaker.quietSinceMs < VOICE_SPEAKING_HOLD_MS);
|
|
2160
|
+
if (isSpeaking === speaker.isSpeaking) return false;
|
|
2161
|
+
speaker.isSpeaking = isSpeaking;
|
|
2162
|
+
voice._room._emit("speaking", { playerId: playerId, isSpeaking: isSpeaking });
|
|
2163
|
+
return true;
|
|
2164
|
+
};
|
|
2165
|
+
this._levelTimer = setInterval(function () {
|
|
2166
|
+
var nowMs = Date.now();
|
|
2167
|
+
voice._speakers.forEach(function (speaker, playerId) { step(playerId, speaker, levelOf(speaker.analyser), nowMs); });
|
|
2168
|
+
var own = voice._ownSpeaker;
|
|
2169
|
+
if (own && voice._room.me) {
|
|
2170
|
+
var levelPct = voice._isMicrophoneMuted ? 0 : levelOf(own.analyser);
|
|
2171
|
+
var isChanged = step(voice._room.me.id, own, levelPct, nowMs);
|
|
2172
|
+
// The room hears this player's level when they start and stop, and every second while speaking: only which voices fill a full listener's slots.
|
|
2173
|
+
if (isChanged || (own.isSpeaking && nowMs - voice._ownLevel.sentAtMs >= VOICE_LEVEL_REPORT_INTERVAL_MS)) {
|
|
2174
|
+
voice._ownLevel = { isSpeaking: own.isSpeaking, levelPct: levelPct, sentAtMs: nowMs };
|
|
2175
|
+
voice._sendVoice({ action: "level", isSpeaking: own.isSpeaking, levelPct: levelPct });
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
}, 100);
|
|
2179
|
+
};
|
|
2180
|
+
|
|
2181
|
+
RoomVoice.prototype._watchOwnLevel = function (microphone) {
|
|
2182
|
+
var analyser = this.audioContext.createAnalyser();
|
|
2183
|
+
analyser.fftSize = 512;
|
|
2184
|
+
this.audioContext.createMediaStreamSource(microphone).connect(analyser);
|
|
2185
|
+
this._ownSpeaker = { analyser: analyser, isSpeaking: false, quietSinceMs: 0 };
|
|
2186
|
+
this._watchLevels();
|
|
2187
|
+
};
|
|
2188
|
+
|
|
2189
|
+
/** The last 30 seconds this player heard, in memory only: a recorder over every voice's mix, its pieces overwritten as it goes. */
|
|
2190
|
+
RoomVoice.prototype._recordInto = function (node) {
|
|
2191
|
+
if (typeof MediaRecorder !== "function") return;
|
|
2192
|
+
if (!this._clip) {
|
|
2193
|
+
var mix = this.audioContext.createMediaStreamDestination();
|
|
2194
|
+
var clip = { mix: mix, header: null, pieces: [], recorder: null };
|
|
2195
|
+
try {
|
|
2196
|
+
clip.recorder = new MediaRecorder(mix.stream, { mimeType: "audio/webm;codecs=opus", audioBitsPerSecond: VOICE_BITRATE_BITS_PER_SEC });
|
|
2197
|
+
} catch (error) {
|
|
2198
|
+
return;
|
|
2199
|
+
}
|
|
2200
|
+
clip.recorder.ondataavailable = function (event) {
|
|
2201
|
+
if (!event.data || !event.data.size) return;
|
|
2202
|
+
// The first piece carries the file's header: kept, so the clip plays from any later piece.
|
|
2203
|
+
if (!clip.header) clip.header = event.data;
|
|
2204
|
+
else {
|
|
2205
|
+
clip.pieces.push(event.data);
|
|
2206
|
+
if (clip.pieces.length > VOICE_REPORT_AUDIO_DURATION_SEC) clip.pieces.shift();
|
|
2207
|
+
}
|
|
2208
|
+
};
|
|
2209
|
+
clip.recorder.start(1000);
|
|
2210
|
+
this._clip = clip;
|
|
2211
|
+
}
|
|
2212
|
+
node.connect(this._clip.mix);
|
|
2213
|
+
};
|
|
2214
|
+
|
|
2215
|
+
/** The clip for a report, or null when voice was never heard. */
|
|
2216
|
+
RoomVoice.prototype._reportClip = function () {
|
|
2217
|
+
if (!this._clip || !this._clip.header) return null;
|
|
2218
|
+
var clip = new Blob([this._clip.header].concat(this._clip.pieces), { type: "audio/webm" });
|
|
2219
|
+
return clip.size <= VOICE_MAXIMUM_REPORT_AUDIO_SIZE_BYTES ? clip : null;
|
|
2220
|
+
};
|
|
2221
|
+
|
|
2222
|
+
RoomVoice.prototype.setMicrophoneMuted = function (isMuted) {
|
|
2223
|
+
this._isMicrophoneMuted = Boolean(isMuted);
|
|
2224
|
+
if (this._microphone) this._microphone.getAudioTracks().forEach(function (track) { track.enabled = !isMuted; });
|
|
2225
|
+
};
|
|
2226
|
+
RoomVoice.prototype.setSpeakerVolume = function (playerId, volumePct) {
|
|
2227
|
+
if (typeof playerId !== "string" || typeof volumePct !== "number" || !(volumePct >= 0 && volumePct <= 100)) throw codedError("bad_voice_call", "room.voice.setSpeakerVolume takes a player ID and a volume from 0 to 100.");
|
|
2228
|
+
var wasSilent = this._volumePct.get(playerId) === 0;
|
|
2229
|
+
this._volumePct.set(playerId, volumePct);
|
|
2230
|
+
var speaker = this._speakers.get(playerId);
|
|
2231
|
+
if (speaker) speaker.gain.gain.value = volumePct / 100;
|
|
2232
|
+
// A voice turned all the way down isn't sent at all: nobody pays for what nobody hears.
|
|
2233
|
+
if ((volumePct === 0) !== wasSilent) this._sendVoice({ action: "mute", playerId: playerId, isMuted: volumePct === 0 || this._room.isMuted(playerId) });
|
|
2234
|
+
};
|
|
2235
|
+
RoomVoice.prototype.setSpeakerPan = function (playerId, panPct) {
|
|
2236
|
+
if (typeof playerId !== "string" || typeof panPct !== "number" || !(panPct >= -100 && panPct <= 100)) throw codedError("bad_voice_call", "room.voice.setSpeakerPan takes a player ID and a position from -100 (left) to 100 (right).");
|
|
2237
|
+
this._panPct.set(playerId, panPct);
|
|
2238
|
+
var speaker = this._speakers.get(playerId);
|
|
2239
|
+
if (speaker) speaker.panner.pan.value = panPct / 100;
|
|
2240
|
+
};
|
|
2241
|
+
/** Sound blocked until the player taps (a browser that blocks autoplay): woken on join, and again on any later tap or key. */
|
|
2242
|
+
RoomVoice.prototype._wakeSound = function () {
|
|
2243
|
+
if (this.audioContext && this.audioContext.state === "suspended") this.audioContext.resume().catch(function () {});
|
|
2244
|
+
if (this._outputElement && this._outputElement.paused) this._outputElement.play().catch(function () {});
|
|
2245
|
+
};
|
|
2246
|
+
RoomVoice.prototype._unlockSoundOnTap = function () {
|
|
2247
|
+
var voice = this;
|
|
2248
|
+
var wake = function () { if (voice.isJoined || voice._joinWaiter) voice._wakeSound(); };
|
|
2249
|
+
["pointerdown", "keydown", "touchend"].forEach(function (name) { document.addEventListener(name, wake, true); });
|
|
2250
|
+
this._wakeOnTap = wake;
|
|
2251
|
+
};
|
|
2252
|
+
/** The room is left: its taps unheard and its sound let go, so a game that makes a room per match doesn't pile them up. */
|
|
2253
|
+
RoomVoice.prototype._dispose = function () {
|
|
2254
|
+
var wake = this._wakeOnTap;
|
|
2255
|
+
if (wake) ["pointerdown", "keydown", "touchend"].forEach(function (name) { document.removeEventListener(name, wake, true); });
|
|
2256
|
+
this._wakeOnTap = null;
|
|
2257
|
+
if (this._outputElement) this._outputElement.srcObject = null;
|
|
2258
|
+
this._outputElement = null;
|
|
2259
|
+
this.outputNode = null;
|
|
2260
|
+
if (this._clip && this._clip.recorder && this._clip.recorder.state !== "inactive") this._clip.recorder.stop();
|
|
2261
|
+
if (this.audioContext) this.audioContext.close().catch(function () {});
|
|
2262
|
+
this.audioContext = null;
|
|
2263
|
+
};
|
|
2264
|
+
/** A voice's sound after its volume, for the game's own effects: disconnect it and route it through your own nodes to outputNode. Null when not heard. */
|
|
2265
|
+
RoomVoice.prototype.audioNodeOf = function (playerId) {
|
|
2266
|
+
var speaker = this._speakers.get(playerId);
|
|
2267
|
+
return speaker ? speaker.gain : null;
|
|
2268
|
+
};
|
|
2269
|
+
/** Players this one muted or blocked, or whose game said so: their voices aren't sent (room "muted"). */
|
|
2270
|
+
RoomVoice.prototype._mutedChanged = function (mutedPlayerIds) {
|
|
2271
|
+
var voice = this;
|
|
2272
|
+
if (!this._pc) return;
|
|
2273
|
+
var muted = new Set(mutedPlayerIds);
|
|
2274
|
+
var told = this._toldMuted || new Set();
|
|
2275
|
+
muted.forEach(function (playerId) { if (!told.has(playerId)) voice._sendVoice({ action: "mute", playerId: playerId, isMuted: true }); });
|
|
2276
|
+
told.forEach(function (playerId) { if (!muted.has(playerId) && voice._volumePct.get(playerId) !== 0) voice._sendVoice({ action: "mute", playerId: playerId, isMuted: false }); });
|
|
2277
|
+
this._toldMuted = muted;
|
|
2278
|
+
};
|
|
2279
|
+
|
|
2280
|
+
RoomVoice.prototype.leave = function () {
|
|
2281
|
+
this._wantsJoin = false;
|
|
2282
|
+
this._joinAttemptNumber++;
|
|
2283
|
+
if (this._pc || this._channel) this._sendVoice({ action: "leave" });
|
|
2284
|
+
var waiter = this._joinWaiter;
|
|
2285
|
+
this._joinWaiter = null;
|
|
2286
|
+
this._close();
|
|
2287
|
+
if (waiter) waiter.reject(codedError("voice_unavailable", "Left voice before it started."));
|
|
2288
|
+
};
|
|
2289
|
+
/** A fresh session: after a reconnect, or when a long one grew too big. */
|
|
2290
|
+
RoomVoice.prototype._rejoin = function () {
|
|
2291
|
+
if (!this._wantsJoin) return;
|
|
2292
|
+
var isMuted = this._isMicrophoneMuted;
|
|
2293
|
+
var ticket = this._ticket;
|
|
2294
|
+
// A join still on its way settles with the fresh one.
|
|
2295
|
+
var waiter = this._joinWaiter;
|
|
2296
|
+
this._joinWaiter = null;
|
|
2297
|
+
this._close();
|
|
2298
|
+
var voice = this;
|
|
2299
|
+
this.join(ticket ? { isMuted: isMuted, ticket: ticket } : { isMuted: isMuted }).then(function () { if (waiter) waiter.resolve(); }, function (error) {
|
|
2300
|
+
if (waiter) waiter.reject(error);
|
|
2301
|
+
else if (voice._wantsJoin) voice._room._emit("error", { code: error.code, message: error.message });
|
|
2302
|
+
});
|
|
2303
|
+
};
|
|
2304
|
+
RoomVoice.prototype._close = function () {
|
|
2305
|
+
var voice = this;
|
|
2306
|
+
// A microphone the browser grants after this is let go at once (join's isCurrent).
|
|
2307
|
+
this._joinAttemptNumber++;
|
|
2308
|
+
Array.from(this._speakers.keys()).forEach(function (playerId) { voice._unwire(playerId); });
|
|
2309
|
+
if (this._ownSpeaker && this._ownSpeaker.isSpeaking && this._room.me) this._room._emit("speaking", { playerId: this._room.me.id, isSpeaking: false });
|
|
2310
|
+
this._ownSpeaker = null;
|
|
2311
|
+
if (this._levelTimer) clearInterval(this._levelTimer);
|
|
2312
|
+
this._levelTimer = null;
|
|
2313
|
+
if (this._pc) this._pc.close();
|
|
2314
|
+
this._pc = null;
|
|
2315
|
+
if (this._microphone) this._microphone.getTracks().forEach(function (track) { track.stop(); });
|
|
2316
|
+
this._microphone = null;
|
|
2317
|
+
this._toldMuted = null;
|
|
2318
|
+
if (this._passTimer) clearInterval(this._passTimer);
|
|
2319
|
+
this._passTimer = null;
|
|
2320
|
+
if (this._channel) {
|
|
2321
|
+
var channel = this._channel;
|
|
2322
|
+
this._channel = null;
|
|
2323
|
+
try { channel.close(1000, "Left"); } catch (error) { /* already closed */ }
|
|
2324
|
+
}
|
|
2325
|
+
this.isJoined = false;
|
|
2326
|
+
if (this.speakerPlayerIds.length) {
|
|
2327
|
+
this.speakerPlayerIds = [];
|
|
2328
|
+
this._room._emit("voiceSpeakers", { playerIds: [] });
|
|
2329
|
+
}
|
|
2330
|
+
};
|
|
2331
|
+
RoomVoice.prototype._sendVoice = function (fields) {
|
|
2332
|
+
var socket = this._channel || this._room._ws;
|
|
2333
|
+
if (socket && socket.readyState === 1) socket.send(JSON.stringify(Object.assign({ type: "voice" }, fields)));
|
|
2334
|
+
};
|
|
2335
|
+
|
|
1837
2336
|
Room.prototype.leave = function () {
|
|
2337
|
+
this.voice.leave();
|
|
2338
|
+
this.voice._dispose();
|
|
1838
2339
|
if (!this._done) left(this);
|
|
1839
2340
|
this._done = true;
|
|
1840
2341
|
this._queue = [];
|
|
@@ -2890,6 +3391,12 @@
|
|
|
2890
3391
|
|
|
2891
3392
|
// Zaisma's report sheet asks for a player's last chat lines on this screen, for the player to tick the ones to send.
|
|
2892
3393
|
window.addEventListener("message", function (event) {
|
|
3394
|
+
if (config.site && event.origin === config.site && event.source === window.parent && event.data && event.data.type === "zaisma:voice-clip-request") {
|
|
3395
|
+
// Zaisma's report sheet asks for the last 30 seconds of voice this player heard in the room, to send with the report.
|
|
3396
|
+
var heardIn = joined.filter(function (room) { return room.roomCode === event.data.code; })[0];
|
|
3397
|
+
window.parent.postMessage({ type: "zaisma:voice-clip", id: event.data.id, clip: heardIn ? heardIn.voice._reportClip() : null }, config.site);
|
|
3398
|
+
return;
|
|
3399
|
+
}
|
|
2893
3400
|
if (!config.site || event.origin !== config.site || event.source !== window.parent || !event.data || event.data.type !== "zaisma:chat-lines-request") return;
|
|
2894
3401
|
var playerId = event.data.playerId, lines = [];
|
|
2895
3402
|
joined.forEach(function (room) {
|
package/src/chat.d.ts
CHANGED
|
@@ -58,6 +58,17 @@ export declare const CHAT_ROOM_LIMITS: {
|
|
|
58
58
|
readonly maximumChatMessageCount: 20;
|
|
59
59
|
readonly chatWindowDurationMs: 10000;
|
|
60
60
|
};
|
|
61
|
+
/** Voice in rooms (room.voice in the game; room.setVoiceGroup, setHeardSpeakers and setHeardSpeakerCount on the server). */
|
|
62
|
+
export declare const VOICE_ROOM_LIMITS: {
|
|
63
|
+
/** Voices each player hears at once unless the server sets it; past it, the loudest. */
|
|
64
|
+
readonly defaultHeardSpeakerCount: 16;
|
|
65
|
+
/** The most a server can set: a phone's browser plays this many voices at once without trouble. */
|
|
66
|
+
readonly maximumHeardSpeakerCount: 32;
|
|
67
|
+
/** A voice group's name: a team, a squad, a radio channel. */
|
|
68
|
+
readonly maximumVoiceGroupNameCharacterCount: 40;
|
|
69
|
+
/** How long a voice ticket from your own server lasts (voiceTicket). */
|
|
70
|
+
readonly voiceTicketDurationSec: 300;
|
|
71
|
+
};
|
|
61
72
|
export declare const CHAT_CHECK_LIMITS: {
|
|
62
73
|
/** The longest message checked, in characters: the whole text is checked, and a longer one is refused (too_long), never cut. */
|
|
63
74
|
readonly maximumMessageCharacterCount: 2000;
|
package/src/chat.js
CHANGED
|
@@ -26,6 +26,17 @@ export const CHAT_ROOM_LIMITS = {
|
|
|
26
26
|
maximumChatMessageCount: 20,
|
|
27
27
|
chatWindowDurationMs: 10_000,
|
|
28
28
|
};
|
|
29
|
+
/** Voice in rooms (room.voice in the game; room.setVoiceGroup, setHeardSpeakers and setHeardSpeakerCount on the server). */
|
|
30
|
+
export const VOICE_ROOM_LIMITS = {
|
|
31
|
+
/** Voices each player hears at once unless the server sets it; past it, the loudest. */
|
|
32
|
+
defaultHeardSpeakerCount: 16,
|
|
33
|
+
/** The most a server can set: a phone's browser plays this many voices at once without trouble. */
|
|
34
|
+
maximumHeardSpeakerCount: 32,
|
|
35
|
+
/** A voice group's name: a team, a squad, a radio channel. */
|
|
36
|
+
maximumVoiceGroupNameCharacterCount: 40,
|
|
37
|
+
/** How long a voice ticket from your own server lasts (voiceTicket). */
|
|
38
|
+
voiceTicketDurationSec: 300,
|
|
39
|
+
};
|
|
29
40
|
export const CHAT_CHECK_LIMITS = {
|
|
30
41
|
/** The longest message checked, in characters: the whole text is checked, and a longer one is refused (too_long), never cut. */
|
|
31
42
|
maximumMessageCharacterCount: 2000,
|
package/src/doors.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export type DoorCaller = DoorCallerProvenByGuard | DoorCallerProvenByFunction;
|
|
|
42
42
|
*/
|
|
43
43
|
export type DoorReach = "account" | "player_save" | "game" | "webhook" | "sign_in_app" | "forum_topic" | "nothing_private" | "any_game" | "any_account" | "zaisma_settings" | "room" | "purchase";
|
|
44
44
|
/** What a door takes from another door, as the security map names them (scripts/security-map/doors.mjs). */
|
|
45
|
-
export type DoorTrust = "session" | "api_key" | "oauth_code" | "oauth_access" | "tester_link" | "preview_link" | "player_pass" | "admin_access" | "player_id" | "stripe_signature" | "github_signature" | "game_origin" | "purchase_receipt" | "passkey" | "email_code";
|
|
45
|
+
export type DoorTrust = "session" | "api_key" | "oauth_code" | "oauth_access" | "tester_link" | "preview_link" | "player_pass" | "admin_access" | "player_id" | "stripe_signature" | "github_signature" | "game_origin" | "purchase_receipt" | "voice_ticket" | "passkey" | "email_code";
|
|
46
46
|
/** The function that proves a caller; a test checks it exists. */
|
|
47
47
|
export interface DoorProof {
|
|
48
48
|
source_path: string;
|
package/src/index.d.ts
CHANGED
|
@@ -4,8 +4,9 @@ export { SCHEDULE_LIMITS, type Schedule, type ScheduledRun } from "./schedules.t
|
|
|
4
4
|
export { WEBHOOK_LIMITS, verifyWebhook, type WebhookError, type WebhookErrorCode, type WebhookEvent, type WebhookEventKind, type WebhookHeaders } from "./webhooks.ts";
|
|
5
5
|
export { DATABASE_LIMITS, DatabaseError, type DatabaseErrorCode, type DatabaseRow, type DatabaseStatement, type DatabaseValue, type GameDatabase } from "./database.ts";
|
|
6
6
|
export { OUTSIDE_CALL_LIMITS, OutsideCallError, type OutsideCallErrorCode } from "./outside-calls.ts";
|
|
7
|
-
export { PASS_ISSUER_URL, PASS_TOKEN_TYPE, PASS_VERSION_NUMBER, PassError, RECEIPT_ISSUER_URL, RECEIPT_TOKEN_TYPE, RECEIPT_VERSION_NUMBER, ReceiptError, canChat, passesAreFriends, passesHaveBlockBetween, verifyPass, verifyReceipt, type PassClaims, type PassErrorCode, type PassExpectations, type PlayerKind, type PublicKey, type ReceiptClaims, type ReceiptErrorCode, type ReceiptExpectations, type WhoCanChat, type WhoReadsMyChat, type ChatReach } from "./pass.ts";
|
|
8
|
-
export {
|
|
7
|
+
export { PASS_ISSUER_URL, PASS_TOKEN_TYPE, PASS_VERSION_NUMBER, PassError, RECEIPT_ISSUER_URL, RECEIPT_TOKEN_TYPE, RECEIPT_VERSION_NUMBER, ReceiptError, canChat, canVoiceChat, passesAreFriends, passesHaveBlockBetween, verifyPass, verifyReceipt, type PassClaims, type PassErrorCode, type PassExpectations, type PlayerKind, type PublicKey, type ReceiptClaims, type ReceiptErrorCode, type ReceiptExpectations, type WhoCanChat, type WhoReadsMyChat, type ChatReach } from "./pass.ts";
|
|
8
|
+
export { VOICE_CHANNEL_NAME_PATTERN, VoiceTicketError, voiceTicket, type VoiceTicketClaims, type VoiceTicketErrorCode, type VoiceTicketOptions } from "./voice-ticket.ts";
|
|
9
|
+
export { CHAT_CHECK_LIMITS, CHAT_ROOM_LIMITS, VOICE_ROOM_LIMITS, ChatCheckError, checkChat, type ChatCheck, type ChatCheckOptions, type ChatCheckReason } from "./chat.ts";
|
|
9
10
|
export { guestName, isRoomCode } from "./names.ts";
|
|
10
11
|
export { guestPlayer, signInWithZaismaPlayers, type IdentifyPlayer, type OutsidePlayer, type SignInWithZaismaPlayersOptions } from "./outside-players.ts";
|
|
11
12
|
export { relay } from "./relay.ts";
|
package/src/index.js
CHANGED
|
@@ -6,8 +6,9 @@ export { SCHEDULE_LIMITS } from "./schedules.js";
|
|
|
6
6
|
export { WEBHOOK_LIMITS, verifyWebhook } from "./webhooks.js";
|
|
7
7
|
export { DATABASE_LIMITS, DatabaseError } from "./database.js";
|
|
8
8
|
export { OUTSIDE_CALL_LIMITS, OutsideCallError } from "./outside-calls.js";
|
|
9
|
-
export { PASS_ISSUER_URL, PASS_TOKEN_TYPE, PASS_VERSION_NUMBER, PassError, RECEIPT_ISSUER_URL, RECEIPT_TOKEN_TYPE, RECEIPT_VERSION_NUMBER, ReceiptError, canChat, passesAreFriends, passesHaveBlockBetween, verifyPass, verifyReceipt } from "./pass.js";
|
|
10
|
-
export {
|
|
9
|
+
export { PASS_ISSUER_URL, PASS_TOKEN_TYPE, PASS_VERSION_NUMBER, PassError, RECEIPT_ISSUER_URL, RECEIPT_TOKEN_TYPE, RECEIPT_VERSION_NUMBER, ReceiptError, canChat, canVoiceChat, passesAreFriends, passesHaveBlockBetween, verifyPass, verifyReceipt } from "./pass.js";
|
|
10
|
+
export { VOICE_CHANNEL_NAME_PATTERN, VoiceTicketError, voiceTicket } from "./voice-ticket.js";
|
|
11
|
+
export { CHAT_CHECK_LIMITS, CHAT_ROOM_LIMITS, VOICE_ROOM_LIMITS, ChatCheckError, checkChat } from "./chat.js";
|
|
11
12
|
export { guestName, isRoomCode } from "./names.js";
|
|
12
13
|
export { guestPlayer, signInWithZaismaPlayers } from "./outside-players.js";
|
|
13
14
|
export { relay } from "./relay.js";
|