koishi-plugin-prism 0.3.3 → 0.3.4

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/README.md CHANGED
@@ -39,6 +39,7 @@ Koishi 里只填怎么连到店铺:
39
39
  | 账单私聊店员 | 玩家退场或店员覆盖结账后,把同一份账单私聊给同一聊天平台上的店员。 |
40
40
  | 闲置提醒频道、提前分钟 | 闲置自动关机的提醒发到哪里(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`),以及提前几分钟;0 或不填频道则不提醒。 |
41
41
  | 门锁 | `lock` 打开的门(带门锁能力的资源);店里只有一扇门时默认就是它。没有门时 `lock` 回复本店还没有设置。 |
42
+ | 店内动态播报 | 把店里发生的事播到「播报群」(格式同闲置提醒频道):开机/关机(含闲置自动关机)、上机、刷卡,各自开关,默认都关。刷卡同时上机时只发一条:开了「上机」按上机播报,否则按刷卡播报。这个 Bot 自己执行的命令已经回复过,不再播报;别的 Bot、网页和机台上发生的照常播报。下机不播报。 |
42
43
 
43
44
  **店员**是在这家店工作、并在网页里绑定了聊天平台身份的人:Bot 按「平台 + 账号」认人,`onebot:114514` 是店员不代表 `telegram:114514` 也是。**货币名称**取店铺的余额资产名,**麻将桌**和**门锁**取店铺的资源,都不需要在 Koishi 里重复填写。店铺没有整店计费开关。Bot 属于一个资源组,`login`/`入场` 开始的是这个资源组(或 Bot 资源专属)的计费方案;平台下发的 `billingEnabled` 只表示通过这个 Bot 入场是否计费。为 `false` 时 Bot 不请求平台,直接回复「这里不计费,无需入场」;平台拒绝时(例如资源组刚改成免费)回复同样的话。钱包、账单、结账、兑换、上下桌、`list` 等命令属于玩家或各自的资源,跟 Bot 所在资源组无关,一律交给平台处理。旧平台整店未启用计费时返回的 `BILLING_DISABLED` 仍提示「本店未启用计费」。
44
45
 
@@ -81,7 +82,7 @@ Bot 设置开启「账单私聊店员」时,玩家退场或店员覆盖结账
81
82
  * `billing` - 预览当前玩家本场计费的消费费用。
82
83
  * `wallet` - 查看当前玩家的钱包余额。
83
84
  * `items` - 查看当前玩家持有的道具或资产。
84
- * `list [资源或方案]` - 查看在店的人:正在玩(占用某个资源)或有任意进行中计费的人都算;刷了本店认不出主人的卡不算占用,`[总计 N 人]` 就是这个人数。每人只列一处:正在占用的资源下(显示当前人数和容量),否则列在最具体的计费方案下(例如同时计费「普通游玩」和依赖它的「麻将」时列在「麻将」)。带名称时只列这一项:资源名或别名列出上面的人,方案名列出所有在计费这个方案的人,例如 `list maimai`、`list 普通游玩`。
85
+ * `list [资源或方案]` - 查看在店的人:正在玩(占用某个资源)或有任意进行中计费的人都算;刷了本店认不出主人的卡不算占用,`[总计 N 人]` 就是这个人数。每人只列一处:正在占用的资源下(显示当前人数和容量),没有占用资源的人统一列在「其他」下,不再按计费方案分组。带名称时只列这一项:资源名或别名列出上面的人,方案名列出所有在计费这个方案的人,例如 `list maimai`、`list 普通游玩`。
85
86
  * `show [deviceId]` - 查看设备电源与连接状态。
86
87
  设备状态按后端的普通字符串契约显示;插件仍兼容历史数据库中的 JSON 字符串状态,避免旧记录显示为 `unknown`。
87
88
  * `history` - 查看自己的历史游玩记录。
package/lib/index.d.ts CHANGED
@@ -33,6 +33,11 @@ export type BotConfig = {
33
33
  idleChannelIds: string[];
34
34
  /** How many minutes before an idle power-off to tell them; 0 for never. */
35
35
  idleReminderMinutes: number;
36
+ /** Channels (`platform:channelId`) told what happens at the shop, and which of it. */
37
+ eventChannelIds: string[];
38
+ announcePower: boolean;
39
+ announceCards: boolean;
40
+ announceStarts: boolean;
36
41
  };
37
42
  /** Which shop this credential belongs to; the shop code is no longer configured by hand. */
38
43
  shop?: {
package/lib/index.js CHANGED
@@ -57,6 +57,8 @@ const USAGE = {
57
57
  list: "/list [资源或方案]",
58
58
  show: "/show [设备ID]",
59
59
  };
60
+ /** Where `list` puts players who occupy no resource. */
61
+ const OTHER_PLAYERS_LABEL = "其他";
60
62
  function applyPrismKoishiPlugin(ctx, config) {
61
63
  const service = new PrismKoishiService(ctx, config);
62
64
  const wrap = (handler, gate = {}) => async (context, ...args) => {
@@ -591,6 +593,8 @@ class PrismKoishiService {
591
593
  watcher = null;
592
594
  /** The bot's settings as last read; null until read, and again once the shop disables the bot. */
593
595
  bot = null;
596
+ /** Announcements still being sent; each waits for the one before. */
597
+ announcing = Promise.resolve();
594
598
  /** The read in flight, shared by commands arriving together. */
595
599
  reading = null;
596
600
  reads = 0;
@@ -621,6 +625,11 @@ class PrismKoishiService {
621
625
  this.socket = new PrismSocket(open, platformSocketUrl(options.baseUrl), options.integrationToken, () => this.hello(), (event) => {
622
626
  if (event.event === "idle.reminder")
623
627
  void this.remindIdle(event.data);
628
+ // One at a time, so the channels read them in the order they happened.
629
+ else if (event.event === "shop.event")
630
+ this.announcing = this.announcing
631
+ .then(() => this.announce(event.data))
632
+ .catch((error) => this.logger?.warn("PRiSM event announcement failed", error));
624
633
  }, this.logger, this.watcher
625
634
  ? {
626
635
  // Only a bot whose settings allow it watches, whatever the platform asks.
@@ -719,6 +728,59 @@ class PrismKoishiService {
719
728
  throw error;
720
729
  }
721
730
  }
731
+ /**
732
+ * Tell the configured channels what happened, if this bot announces that kind
733
+ * of thing. A card that put its owner on the cabinet is one message: a start
734
+ * when starts are announced, otherwise a card. What this bot's own command
735
+ * did is left out: the bot has answered the command already.
736
+ */
737
+ async announce(event) {
738
+ const bot = this.bot;
739
+ const channels = bot?.settings.eventChannelIds ?? [];
740
+ if (!bot || !channels.length || typeof this.ctx.broadcast !== "function")
741
+ return;
742
+ if (event.botId && event.botId === bot.bot.id)
743
+ return;
744
+ const message = await this.announcement(event, bot.settings);
745
+ if (!message)
746
+ return;
747
+ try {
748
+ await this.ctx.broadcast(channels, message);
749
+ }
750
+ catch (error) {
751
+ this.logger?.warn("PRiSM event announcement failed", error);
752
+ }
753
+ }
754
+ async announcement(event, settings) {
755
+ const place = event.resourceName || event.resourceId;
756
+ const who = async () => {
757
+ if (!event.player)
758
+ return "未绑定的卡";
759
+ // Named as the platform the channels are on would name them, when it can.
760
+ const provider = event.player.identities[0]?.provider ?? "";
761
+ return this.displayNameForPlayer({
762
+ playerId: event.player.playerId,
763
+ displayName: event.player.displayName ?? undefined,
764
+ sessions: [{ playerDisplayName: event.player.displayName ?? undefined, identities: event.player.identities }],
765
+ }, provider);
766
+ };
767
+ if (event.kind === "power") {
768
+ if (!settings.announcePower)
769
+ return null;
770
+ if (event.by === "system")
771
+ return event.state === "off" ? `💤 ${place} 闲置,已自动关机` : `✅ ${place} 已开机`;
772
+ const by = event.player ? `(${await who()})` : "";
773
+ return event.state === "on" ? `✅ ${place} 已开机${by}` : `🛑 ${place} 已关机${by}`;
774
+ }
775
+ if (event.kind === "card") {
776
+ if (event.started && settings.announceStarts)
777
+ return `🎮 ${await who()} 在 ${place} 刷卡上机`;
778
+ return settings.announceCards ? `💳 ${await who()} 在 ${place} 刷卡` : null;
779
+ }
780
+ if (event.kind === "occupancy")
781
+ return settings.announceStarts ? `🎮 ${await who()} 在 ${place} 上机` : null;
782
+ return null;
783
+ }
722
784
  /** Tell the configured channels a power-off is coming. Short: they can read a device name. */
723
785
  async remindIdle(reminder) {
724
786
  const channels = this.bot?.settings.idleChannelIds ?? [];
@@ -1158,7 +1220,13 @@ class PrismKoishiService {
1158
1220
  if (wanted && only && !only.players.length) {
1159
1221
  return only.capacity !== undefined ? `${only.label} ( 0/${only.capacity} )` : `${only.label} ( 0人 )`;
1160
1222
  }
1161
- return formatPlayerGroups(groups);
1223
+ if (wanted)
1224
+ return formatPlayerGroups(groups);
1225
+ // The whole shop: players on a resource are listed there, everyone else together,
1226
+ // whatever plan they bill. Asking for a plan by name still lists that plan.
1227
+ const onResources = groups.filter((group) => group.capacity !== undefined);
1228
+ const others = groups.filter((group) => group.capacity === undefined).flatMap((group) => group.players);
1229
+ return formatPlayerGroups([...onResources, ...(others.length ? [{ label: OTHER_PLAYERS_LABEL, players: others }] : [])]);
1162
1230
  }
1163
1231
  async listDeviceStates(rawAlias) {
1164
1232
  const alias = cleanText(rawAlias);
@@ -1571,6 +1639,10 @@ function parseBotConfig(value) {
1571
1639
  receiptsToStaff: settings.receiptsToStaff === true,
1572
1640
  idleChannelIds: strings(settings.idleChannelIds),
1573
1641
  idleReminderMinutes: Math.max(0, Number(settings.idleReminderMinutes ?? 5) || 0),
1642
+ eventChannelIds: strings(settings.eventChannelIds),
1643
+ announcePower: settings.announcePower === true,
1644
+ announceCards: settings.announceCards === true,
1645
+ announceStarts: settings.announceStarts === true,
1574
1646
  },
1575
1647
  billingEnabled: record.billingEnabled !== false,
1576
1648
  currencyName: cleanText(record.currencyName) || "余额",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {