@zaisma/game-server 1.43.0 → 1.45.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/src/room.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  */
9
9
  import { type GameDatabase } from "./database.ts";
10
10
  import { type DoorContract, type DoorShape } from "./doors.ts";
11
- import type { PassClaims, PlayerKind, ReceiptClaims } from "./pass.ts";
11
+ import { type ChatReach, type PassClaims, type PlayerKind, type ReceiptClaims } from "./pass.ts";
12
12
  import type { Server } from "./requests.ts";
13
13
  import { type Schedule, type ScheduledRun } from "./schedules.ts";
14
14
  import { type StateChange } from "./state-sync.ts";
@@ -444,9 +444,8 @@ export interface GameServer<S = any> {
444
444
  stateSyncEnabled?: boolean;
445
445
  /**
446
446
  * Check chat between adults (room.chat) with Zaisma's filter too: contact details, moving apps or
447
- * meeting up, personal details and sexual content are stopped, as they always are for under-18s. Off by
448
- * default, when chat between adults is only flagged (hasSwearing, hasSlur) and your game decides. Each
449
- * message the filter's AI looks at counts on your hosting meter (adult_chat_checks).
447
+ * meeting up are stopped, as they always are where an under-18 sends or reads. Off by default, when chat
448
+ * between adults is only flagged (hasSwearing, hasSlur) and your game decides. Free.
450
449
  */
451
450
  adultChatFilterEnabled?: boolean;
452
451
  /**
@@ -1319,20 +1318,23 @@ export declare function errorMessage(code: RoomErrorCode, message?: string, deta
1319
1318
  chatNumber: number;
1320
1319
  }): string;
1321
1320
  /**
1322
- * Who an account player may chat with, from their pass (who_can_chat and chat_group_id): kept by the
1323
- * room's host, never shown to the game. A guest has none and chats with nobody.
1321
+ * How far an account player's chat reaches, from their pass (chatReachOf): who reads what they type (sending),
1322
+ * whose chat they read (reading), their chat group, and their sealed network for Zaisma's witness rule. Kept by
1323
+ * the room's host, never shown to the game. A guest has none and chats with nobody.
1324
1324
  */
1325
1325
  export interface PlayerChatSettings {
1326
- whoCanChat: "nobody" | "friends" | "anyone";
1326
+ sending: ChatReach;
1327
+ reading: ChatReach;
1327
1328
  chatGroupId: string | null;
1329
+ networkSeal: string | null;
1328
1330
  }
1329
1331
  /**
1330
- * The chat settings of a player your own server let in from outside Zaisma (outside_zaisma): they chat with
1331
- * anyone who chats with anyone, in no chat group, and Zaisma's chat check stops any message between them and
1332
- * an under-18 account. Zaisma's chat groups are chat_group_… IDs, so none is ever this one.
1332
+ * The chat settings of a player your own server let in from outside Zaisma (outside_zaisma): they send to and read
1333
+ * everyone, in a chat group of their own, and Zaisma's chat check stops any message between them and an under-18
1334
+ * account. Zaisma's chat groups are chat_group_… IDs, so none is ever this one.
1333
1335
  */
1334
1336
  export declare const OUTSIDE_ZAISMA_CHAT_SETTINGS: PlayerChatSettings;
1335
- /** A checked pass's chat settings, for RoomCore.setChatSettings: null for a guest. A setting this version doesn't know chats with nobody. */
1337
+ /** A checked pass's chat settings, for RoomCore.setChatSettings: null for a guest. A choice this version doesn't know reaches nobody. */
1336
1338
  export declare function chatSettingsOf(claims: PassClaims): PlayerChatSettings | null;
1337
1339
  /** A chat message from a client as readChatMessage found it, for startChat to check. */
1338
1340
  export interface ChatClientMessage {
@@ -1710,13 +1712,14 @@ export declare class RoomCore<S = any> {
1710
1712
  setFriends(playerId: string, friendPlayerIds: readonly string[]): void;
1711
1713
  /** A player's chat settings from their pass, before join: null for a guest, who chats with nobody. */
1712
1714
  setChatSettings(playerId: string, settings: PlayerChatSettings | null): void;
1713
- /** Whether a player can chat at all: an account whose setting isn't nobody, with a chat group. */
1715
+ /** Whether a player can send chat at all: an account whose chat isn't stopped, with a chat group. */
1714
1716
  canChat(playerId: string): boolean;
1717
+ /** A player's sealed network from their pass, for Zaisma's witness rule: null for a guest, a player from outside Zaisma or an older pass. */
1718
+ networkSealOf(playerId: string): string | null;
1715
1719
  /**
1716
- * Whether one player's chat may reach another, canChat's rule on passes (pass.ts): two accounts in the
1717
- * same chat group with no block either way, friends unless both chat with anyone. A player from outside
1718
- * Zaisma is in no chat group: they chat with anyone who chats with anyone, and Zaisma's chat check stops
1719
- * what would reach an under-18 account or come from one.
1720
+ * Whether one player's chat may reach another, canChat's rule on passes (pass.ts), sender to reader: no block
1721
+ * either way, the reader reads chat, and the sender's choice takes the reader in (everyone; the same chat group;
1722
+ * a friend in it). Zaisma's chat check stops what would reach an under-18 account without witnesses.
1720
1723
  */
1721
1724
  private mayChat;
1722
1725
  /** Who a player's chat may reach: the players in the match (or those of toPlayerIds in it), never spectators, kept only where they may chat. */
@@ -1735,13 +1738,16 @@ export declare class RoomCore<S = any> {
1735
1738
  } | null>;
1736
1739
  /**
1737
1740
  * Send a chat message Zaisma's filter let through to its receivers, and tell the sender it went
1738
- * (chat_sent), even when it reached nobody: the sender never learns who can't read them.
1741
+ * (chat_sent), even when it reached nobody: the sender never learns who can't read them. With isEveryReaderNeeded
1742
+ * (Zaisma counted witnesses among them) it sends nothing and answers false when any of them can't read it now.
1739
1743
  */
1740
1744
  deliverChat(senderPlayerId: string, chatNumber: number, receiverPlayerIds: readonly string[], chat: {
1741
1745
  text: string;
1742
1746
  hasSwearing: boolean;
1743
1747
  hasSlur: boolean;
1744
- }): void;
1748
+ }, options?: {
1749
+ isEveryReaderNeeded?: boolean;
1750
+ }): boolean;
1745
1751
  /** Tell the sender their chat message was stopped: reason is the filter's, or stopped_by_game; message is what they see. */
1746
1752
  stopChat(senderPlayerId: string, chatNumber: number, reason: string, message: string): void;
1747
1753
  /** A player's chat messages from the last CHAT_CHECK_LIMITS.recentTextDurationMs, oldest first, up to recentTextCount. */
package/src/room.js CHANGED
@@ -10,6 +10,7 @@ import { CHAT_CHECK_LIMITS, CHAT_ROOM_LIMITS } from "./chat.js";
10
10
  import { DatabaseError } from "./database.js";
11
11
  import { answersJson, checkDoor, fieldsOf, gameData, listOf, orNothing, realNumber, schemaProblems, text as textUpTo, yesOrNo } from "./doors.js";
12
12
  import { isRoomCode, roomCode } from "./names.js";
13
+ import { chatReachOf, isWithinChatReach } from "./pass.js";
13
14
  import { readSchedules } from "./schedules.js";
14
15
  import { roomCallChecks } from "./room-calls.js";
15
16
  import { newStateTracker } from "./state-sync.js";
@@ -475,17 +476,25 @@ export function errorMessage(code, message, details) {
475
476
  return JSON.stringify({ type: "error", code, message: message || ERROR_TEXT[code], ...details });
476
477
  }
477
478
  /**
478
- * The chat settings of a player your own server let in from outside Zaisma (outside_zaisma): they chat with
479
- * anyone who chats with anyone, in no chat group, and Zaisma's chat check stops any message between them and
480
- * an under-18 account. Zaisma's chat groups are chat_group_… IDs, so none is ever this one.
479
+ * The chat settings of a player your own server let in from outside Zaisma (outside_zaisma): they send to and read
480
+ * everyone, in a chat group of their own, and Zaisma's chat check stops any message between them and an under-18
481
+ * account. Zaisma's chat groups are chat_group_… IDs, so none is ever this one.
481
482
  */
482
- export const OUTSIDE_ZAISMA_CHAT_SETTINGS = { whoCanChat: "anyone", chatGroupId: "outside_zaisma" };
483
- /** A checked pass's chat settings, for RoomCore.setChatSettings: null for a guest. A setting this version doesn't know chats with nobody. */
483
+ export const OUTSIDE_ZAISMA_CHAT_SETTINGS = { sending: "everyone", reading: "everyone", chatGroupId: "outside_zaisma", networkSeal: null };
484
+ /** A checked pass's chat settings, for RoomCore.setChatSettings: null for a guest. A choice this version doesn't know reaches nobody. */
484
485
  export function chatSettingsOf(claims) {
485
486
  if (claims.kind !== "account")
486
487
  return null;
487
- const whoCanChat = claims.who_can_chat === "friends" ? "friends" : claims.who_can_chat === "anyone" ? "anyone" : "nobody";
488
- return { whoCanChat, chatGroupId: typeof claims.chat_group_id === "string" ? claims.chat_group_id : null };
488
+ return { ...chatReachOf(claims), chatGroupId: typeof claims.chat_group_id === "string" ? claims.chat_group_id : null, networkSeal: typeof claims.network_seal === "string" ? claims.network_seal : null };
489
+ }
490
+ /** Chat settings a room saved, in today's shape: one saved before who_reads_my_chat reads as v1's two-way setting did. */
491
+ function savedChatSettings(saved) {
492
+ const chatGroupId = typeof saved.chatGroupId === "string" ? saved.chatGroupId : null;
493
+ const networkSeal = typeof saved.networkSeal === "string" ? saved.networkSeal : null;
494
+ if (saved.sending !== undefined && saved.reading !== undefined)
495
+ return { sending: saved.sending, reading: saved.reading, chatGroupId, networkSeal };
496
+ const reach = chatGroupId === OUTSIDE_ZAISMA_CHAT_SETTINGS.chatGroupId ? "everyone" : saved.whoCanChat === "friends" ? "friends" : saved.whoCanChat === "anyone" ? "my_age_band" : "nobody";
497
+ return { sending: reach, reading: reach, chatGroupId, networkSeal };
489
498
  }
490
499
  /** Room traffic of chat, in the room's message types. */
491
500
  export const CHAT_MESSAGE_TYPE = "(chat)";
@@ -955,7 +964,7 @@ export class RoomCore {
955
964
  for (const [first, second] of saved.friendships ?? [])
956
965
  this.friendships.add(pairKey(first, second));
957
966
  for (const [id, settings] of Object.entries(saved.chatSettings ?? {}))
958
- this.chatSettings.set(id, settings);
967
+ this.chatSettings.set(id, savedChatSettings(settings));
959
968
  }
960
969
  else if (startOptions.start) {
961
970
  const start = startOptions.start;
@@ -1181,31 +1190,34 @@ export class RoomCore {
1181
1190
  /** A player's chat settings from their pass, before join: null for a guest, who chats with nobody. */
1182
1191
  setChatSettings(playerId, settings) {
1183
1192
  if (settings)
1184
- this.chatSettings.set(playerId, { whoCanChat: settings.whoCanChat, chatGroupId: settings.chatGroupId });
1193
+ this.chatSettings.set(playerId, { sending: settings.sending, reading: settings.reading, chatGroupId: settings.chatGroupId, networkSeal: settings.networkSeal });
1185
1194
  else if (!this.chatSettings.delete(playerId))
1186
1195
  return;
1187
1196
  this.dirty = true;
1188
1197
  }
1189
- /** Whether a player can chat at all: an account whose setting isn't nobody, with a chat group. */
1198
+ /** Whether a player can send chat at all: an account whose chat isn't stopped, with a chat group. */
1190
1199
  canChat(playerId) {
1191
1200
  const settings = this.chatSettings.get(playerId);
1192
- return settings !== undefined && settings.whoCanChat !== "nobody" && settings.chatGroupId !== null;
1201
+ return settings !== undefined && settings.sending !== "nobody" && settings.chatGroupId !== null;
1202
+ }
1203
+ /** A player's sealed network from their pass, for Zaisma's witness rule: null for a guest, a player from outside Zaisma or an older pass. */
1204
+ networkSealOf(playerId) {
1205
+ return this.chatSettings.get(playerId)?.networkSeal ?? null;
1193
1206
  }
1194
1207
  /**
1195
- * Whether one player's chat may reach another, canChat's rule on passes (pass.ts): two accounts in the
1196
- * same chat group with no block either way, friends unless both chat with anyone. A player from outside
1197
- * Zaisma is in no chat group: they chat with anyone who chats with anyone, and Zaisma's chat check stops
1198
- * what would reach an under-18 account or come from one.
1208
+ * Whether one player's chat may reach another, canChat's rule on passes (pass.ts), sender to reader: no block
1209
+ * either way, the reader reads chat, and the sender's choice takes the reader in (everyone; the same chat group;
1210
+ * a friend in it). Zaisma's chat check stops what would reach an under-18 account without witnesses.
1199
1211
  */
1200
1212
  mayChat(senderPlayerId, receiverPlayerId) {
1201
- if (senderPlayerId === receiverPlayerId || !this.canChat(senderPlayerId) || !this.canChat(receiverPlayerId) || this.hasBlockBetween(senderPlayerId, receiverPlayerId))
1213
+ if (senderPlayerId === receiverPlayerId || !this.canChat(senderPlayerId) || this.hasBlockBetween(senderPlayerId, receiverPlayerId))
1202
1214
  return false;
1203
1215
  const sender = this.chatSettings.get(senderPlayerId);
1204
1216
  const receiver = this.chatSettings.get(receiverPlayerId);
1205
- const isOutside = (settings) => settings.chatGroupId === OUTSIDE_ZAISMA_CHAT_SETTINGS.chatGroupId;
1206
- if (sender.chatGroupId !== receiver.chatGroupId && !isOutside(sender) && !isOutside(receiver))
1217
+ if (!receiver || receiver.chatGroupId === null)
1207
1218
  return false;
1208
- return (sender.whoCanChat === "anyone" && receiver.whoCanChat === "anyone") || this.areFriends(senderPlayerId, receiverPlayerId);
1219
+ const other = { isSameChatGroup: sender.chatGroupId === receiver.chatGroupId, isFriend: this.areFriends(senderPlayerId, receiverPlayerId) };
1220
+ return isWithinChatReach(sender.sending, other) && isWithinChatReach(receiver.reading, other);
1209
1221
  }
1210
1222
  /** Who a player's chat may reach: the players in the match (or those of toPlayerIds in it), never spectators, kept only where they may chat. */
1211
1223
  chatReceivers(senderPlayerId, toPlayerIds) {
@@ -1277,15 +1289,20 @@ export class RoomCore {
1277
1289
  }
1278
1290
  /**
1279
1291
  * Send a chat message Zaisma's filter let through to its receivers, and tell the sender it went
1280
- * (chat_sent), even when it reached nobody: the sender never learns who can't read them.
1292
+ * (chat_sent), even when it reached nobody: the sender never learns who can't read them. With isEveryReaderNeeded
1293
+ * (Zaisma counted witnesses among them) it sends nothing and answers false when any of them can't read it now.
1281
1294
  */
1282
- deliverChat(senderPlayerId, chatNumber, receiverPlayerIds, chat) {
1295
+ deliverChat(senderPlayerId, chatNumber, receiverPlayerIds, chat, options = {}) {
1283
1296
  const sentAt = new Date(this.now()).toISOString();
1284
1297
  // Players who left, or blocked the sender, while the filter looked get nothing.
1285
1298
  const receivers = receiverPlayerIds.filter((id) => this.players.has(id) && this.mayChat(senderPlayerId, id));
1299
+ // A message Zaisma let through only for the witnesses it counted doesn't go to fewer: the host stops it instead.
1300
+ if (options.isEveryReaderNeeded && receivers.length < receiverPlayerIds.length)
1301
+ return false;
1286
1302
  if (receivers.length)
1287
1303
  this.sendEach(receivers, JSON.stringify({ type: "chat", fromPlayerId: senderPlayerId, text: chat.text, hasSwearing: chat.hasSwearing, hasSlur: chat.hasSlur, sentAt }), CHAT_MESSAGE_TYPE);
1288
1304
  this.sendText(senderPlayerId, JSON.stringify({ type: "chat_sent", chatNumber, sentAt }));
1305
+ return true;
1289
1306
  }
1290
1307
  /** Tell the sender their chat message was stopped: reason is the filter's, or stopped_by_game; message is what they see. */
1291
1308
  stopChat(senderPlayerId, chatNumber, reason, message) {