koishi-plugin-prism 0.3.0 → 0.3.2
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 +7 -7
- package/lib/index.d.ts +17 -5
- package/lib/index.js +78 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ Koishi 里只填怎么连到店铺:
|
|
|
22
22
|
| 配置项 | 类型 | 描述 |
|
|
23
23
|
| :--- | :---: | :--- |
|
|
24
24
|
| `baseUrl` | `string` | **必填**。PRiSM Next Server 的访问基准 URL(如 `https://prism-mmw.neri.moe`)。 |
|
|
25
|
-
| `shopCode` | `string` |
|
|
25
|
+
| `shopCode` | `string` | 可选。店铺公开编号;接入凭据已经指明店铺,**不需要填**。旧配置保留也能用。 |
|
|
26
26
|
| `integrationToken` | `string` | **必填**。从 PRiSM 网页后台生成的 Integration API 令牌。 |
|
|
27
27
|
|
|
28
28
|
其余一切都在 PRiSM 网页里设置:每个令牌对应一个 Bot,Bot 在店铺里是一个**资源**。在**商户工作台 → 资源 → 这个 Bot → Bot 设置**里可以改:
|
|
@@ -34,21 +34,21 @@ Koishi 里只填怎么连到店铺:
|
|
|
34
34
|
| 玩家可用的命令 | `全部` 或 `只读`。只读时玩家只能绑定身份(`bind`)、查询(`wallet`、`billing`、`history`、`items`、`list`、`show`、`麻将列表`、`versions`、`api测速`、`ws测速`)、兑换(`redeem`)和延迟关机(`keep`);入场、结账、上下桌和设备操作(`login`/`入场`、`logout`、`mahjong`/`上桌`/`下桌`、`lock`、`on`、`off`、`coin`、`scan`)只有店员能用,包括替其他玩家操作。 |
|
|
35
35
|
| 只读提示 | 只读模式下玩家使用受限命令时 Bot 的回复。 |
|
|
36
36
|
| 店员命令 | 店员可以替其他玩家入场、结账、查询,以及 `add`、`del`、`overwrite`。 |
|
|
37
|
+
| 开关机失败的原因 | 只说玩家该知道的:没有这个设备、设备未连接、设备已关机、设备没有响应;其余情况统一回「操作没有完成,请找店员」,内部原因只写日志。 |
|
|
37
38
|
| 关机仅限店员 | `off` 只允许店员使用(以店员身份越过入场要求);`on` 始终只允许已入场玩家使用。 |
|
|
38
39
|
| 账单私聊店员 | 玩家退场或店员覆盖结账后,把同一份账单私聊给同一聊天平台上的店员。 |
|
|
39
40
|
| 闲置提醒频道、提前分钟 | 闲置自动关机的提醒发到哪里(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`),以及提前几分钟;0 或不填频道则不提醒。 |
|
|
40
41
|
| 门锁 | `lock` 打开的门(带门锁能力的资源);店里只有一扇门时默认就是它。没有门时 `lock` 回复本店还没有设置。 |
|
|
41
|
-
| 监听 Home Assistant | 见下文「监听 Home Assistant」。 |
|
|
42
42
|
|
|
43
|
-
**店员**是在这家店工作、并在网页里绑定了聊天平台身份的人:Bot 按「平台 + 账号」认人,`onebot:114514` 是店员不代表 `telegram:114514` 也是。**货币名称**取店铺的余额资产名,**麻将桌**和**门锁**取店铺的资源,都不需要在 Koishi
|
|
43
|
+
**店员**是在这家店工作、并在网页里绑定了聊天平台身份的人:Bot 按「平台 + 账号」认人,`onebot:114514` 是店员不代表 `telegram:114514` 也是。**货币名称**取店铺的余额资产名,**麻将桌**和**门锁**取店铺的资源,都不需要在 Koishi 里重复填写。店铺没有整店计费开关。Bot 属于一个资源组,`login`/`入场` 开始的是这个资源组(或 Bot 资源专属)的计费方案;平台下发的 `billingEnabled` 只表示通过这个 Bot 入场是否计费。为 `false` 时 Bot 不请求平台,直接回复「这里不计费,无需入场」;平台拒绝时(例如资源组刚改成免费)回复同样的话。钱包、账单、结账、兑换、上下桌、`list` 等命令属于玩家或各自的资源,跟 Bot 所在资源组无关,一律交给平台处理。旧平台整店未启用计费时返回的 `BILLING_DISABLED` 仍提示「本店未启用计费」。
|
|
44
44
|
|
|
45
45
|
Bot 连上平台后先读取这些设置,再向平台打招呼;网页里改了设置、店员绑定了新身份、增删了麻将桌或门锁时,平台会通知 Bot 重新读取,**不需要重启 Koishi**。
|
|
46
46
|
|
|
47
|
-
旧版的 `autoRegister`、`currencyName`、`defaultDoorDeviceId`、`defaultScanProvider`(刷卡固定使用 Aime)、`loginPricingConfigIds`、`loginSessionLabel`、`enableStaffCommands`、`staffUserIds`、`powerCommandsAdminOnly`、`playerCommands`、`readOnlyNotice`、`logoutNotifyUserIds`、`idleNotifyChannelIds`、`idleReminderMinutes`、`watchHomeAssistant
|
|
47
|
+
旧版的 `autoRegister`、`currencyName`、`defaultDoorDeviceId`、`defaultScanProvider`(刷卡固定使用 Aime)、`loginPricingConfigIds`、`loginSessionLabel`、`enableStaffCommands`、`staffUserIds`、`powerCommandsAdminOnly`、`playerCommands`、`readOnlyNotice`、`logoutNotifyUserIds`、`idleNotifyChannelIds`、`idleReminderMinutes`、`watchHomeAssistant`(改由平台调度)、`botName`、`mahjongTableConfigs`、`mahjongTableSize` 已全部移除;升级后请到 Bot 的资源页重新设置。玩家首次使用时是否自动建档由店铺自己的注册开关决定。入场开始这个 Bot 在 PRiSM 里的计费方案:Bot 资源的专属方案,否则它所属资源组的方案;没改过的 Bot 在默认资源组,即店铺默认计费方案。已经在计费时 Bot 回复「您已在「方案名」中」。
|
|
48
48
|
|
|
49
49
|
### 统一平台
|
|
50
50
|
|
|
51
|
-
配置 `baseUrl
|
|
51
|
+
配置 `baseUrl` 和本店的 Integration 令牌后(店铺编号可选),插件直接连 `/integration/ws`:凭据在握手时解析出店铺,和机台连 `/io/ws` 一样,地址里没有店铺编号。插件与 PRiSM 只保持**一条 WebSocket 连接**,所有命令都作为请求帧走这条连接,平台也通过它推送闲置关机提醒。连接断开时命令直接回复「操作失败,暂时连不上服务器。」,不排队、不自动重发;插件在后台自动重连。需要 Koishi 的 `http` 服务(`ctx.http.ws`)。不再支持不填 `shopCode` 的单店兼容部署。
|
|
52
52
|
|
|
53
53
|
玩家在网页取得验证码后,向任意已接入的店铺 Bot 发送 `bind 验证码`(旧命令 `prism.bind` 仍可用)。店家可以在 PRiSM「设置 → 玩家与身份 → 绑定提示」改写玩家看到的绑定提示。所有命令从消息的 `session.platform` 和 `session.userId` 读取身份,发送 `{ code, provider, subject }`,不接受手填或代填身份;`provider` 配置已移除。例如同一实例的 OneBot 和 Telegram 消息分别使用 `onebot:114514` 和 `telegram:114514`。群/私聊过滤交给 Koishi。网页账号可以跨店使用,平台验证、玩家和余额按店铺隔离;同一网页账号追加平台绑定时复用同一个玩家。
|
|
54
54
|
|
|
@@ -60,7 +60,7 @@ Bot 连上平台后先读取这些设置,再向平台打招呼;网页里改
|
|
|
60
60
|
|
|
61
61
|
Bot 一直在线,所以平台会优先让 Bot 当店铺的 Home Assistant **监听器**:连上平台后 Bot 在握手里声明自己能监听,平台把 Home Assistant 的地址、一个**只读令牌**和要关心的设备实体发给它,Bot 订阅这些实体,把每次变化通过同一条连接报给平台。玩家页和商户后台因此能实时看到电源、空调状态,不必再临时去问 Home Assistant。店里的 HINATA IO 机台也能做同样的事;一家店同时监听的数量默认 2 个(由平台管理员按店铺调整),Bot 排在机台前面:一个 Bot 在线就占一个名额,两个 Bot 就占满,机台只补空位,Bot 上线时机台会在 Bot 交出完整状态之后让位,中间没有空窗。商户后台设备页的「HA 监听器」会显示现在是谁在监听。
|
|
62
62
|
|
|
63
|
-
-
|
|
63
|
+
- **谁在监听由平台决定**:能连上 Home Assistant 的 Bot 都会在 hello 里声明,平台按店铺的监听名额挑选和轮换;店家没有开关,也不能指定。不想要某个 Bot 参与,停用这个 Bot 资源即可。
|
|
64
64
|
- **地址**:平台给的地址列表按顺序尝试(店内地址在前、外部地址在后),不区分 Bot 在哪、Home Assistant 在哪,哪个通就用哪个。每个地址最多等 10 秒连接,一轮全部失败才向平台报断开,上次连上的地址下次排第一。
|
|
65
65
|
- **名字**:设备页显示的是 Bot 资源的名字,多个 Bot 请在资源页分别起名。
|
|
66
66
|
- 需要 Koishi 的 `http` 服务(`ctx.http.ws`),和与平台的连接用同一个。
|
|
@@ -155,7 +155,7 @@ npm version minor --no-git-tag-version
|
|
|
155
155
|
|
|
156
156
|
### 0.3.0
|
|
157
157
|
|
|
158
|
-
Koishi 配置只剩 `baseUrl
|
|
158
|
+
Koishi 配置只剩 `baseUrl` 和 `integrationToken`(`shopCode` 可选、已不需要)。Bot 在 PRiSM 里是一个资源,其余设置(只读模式、店员命令、关机权限、账单私聊、闲置提醒、门锁、名字、启用)都在商户工作台的「Bot 设置」里改,连接后读取、修改后由平台通知重新读取。店员改为网页里绑定了平台身份的店员,货币名称、门锁和麻将桌取自店铺。麻将改由平台完成(不必先入场,满桌开局),`下桌` 可指明桌号;`list` 按平台的在店名单输出(每人一处,资源带人数/容量),`list <资源或方案>` 只列这一项;下桌后回复还在计费的方案;入场开始 Bot 所在资源组的方案(或 Bot 的专属方案),重复入场回复「已在「方案名」中」,入场结束了互斥方案时列出被结束的方案。移除插件自己的麻将桌状态与所有旧配置项。需要配套支持 `integration/bot`、`integration/mahjong-tables`、`integration/roster` 和 `config.changed` 的 PRiSM(1.1.0 起);店铺所在的 PRiSM 升级之前不要升级到此版本。
|
|
159
159
|
|
|
160
160
|
### 0.1.39
|
|
161
161
|
|
package/lib/index.d.ts
CHANGED
|
@@ -33,9 +33,13 @@ 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
|
-
/** Whether this bot offers to watch the shop's Home Assistant. */
|
|
37
|
-
watchHomeAssistant: boolean;
|
|
38
36
|
};
|
|
37
|
+
/** Which shop this credential belongs to; the shop code is no longer configured by hand. */
|
|
38
|
+
shop?: {
|
|
39
|
+
code: string;
|
|
40
|
+
name: string;
|
|
41
|
+
};
|
|
42
|
+
/** Whether entering through this bot bills: its own plan, otherwise its resource group's. */
|
|
39
43
|
billingEnabled: boolean;
|
|
40
44
|
currencyName: string;
|
|
41
45
|
/** Staff are these platform identities. */
|
|
@@ -143,6 +147,7 @@ type PlatformEvent = {
|
|
|
143
147
|
*/
|
|
144
148
|
export declare class PrismSocket {
|
|
145
149
|
private readonly open;
|
|
150
|
+
/** Resolved at each connection: the shop is learned from the credential first. */
|
|
146
151
|
private readonly url;
|
|
147
152
|
private readonly token;
|
|
148
153
|
/**
|
|
@@ -169,7 +174,9 @@ export declare class PrismSocket {
|
|
|
169
174
|
/** Bumped by every hello being prepared, so only the newest one is sent. */
|
|
170
175
|
private greeting;
|
|
171
176
|
private readonly pongWaiters;
|
|
172
|
-
constructor(open: SocketFactory,
|
|
177
|
+
constructor(open: SocketFactory,
|
|
178
|
+
/** Resolved at each connection: the shop is learned from the credential first. */
|
|
179
|
+
url: string | (() => string), token: string,
|
|
173
180
|
/**
|
|
174
181
|
* What to say in hello, read afresh each time: the bot's settings live on
|
|
175
182
|
* the platform, so they are fetched over this connection before it is
|
|
@@ -189,6 +196,8 @@ export declare class PrismSocket {
|
|
|
189
196
|
/** Send a frame if the connection is up; a report made while it is down is dropped, as the platform has forgotten the watch by then. */
|
|
190
197
|
send(frame: Record<string, unknown>): void;
|
|
191
198
|
start(): void;
|
|
199
|
+
/** Connect again with what the platform has just told us, such as the shop. */
|
|
200
|
+
reconnect(): void;
|
|
192
201
|
stop(): void;
|
|
193
202
|
private connect;
|
|
194
203
|
/**
|
|
@@ -215,8 +224,11 @@ type SocketReply = {
|
|
|
215
224
|
body: any;
|
|
216
225
|
serverMs?: number | null;
|
|
217
226
|
};
|
|
218
|
-
/**
|
|
219
|
-
|
|
227
|
+
/**
|
|
228
|
+
* The platform connection URL. The credential names the shop, as a device's
|
|
229
|
+
* token names its machine at `/io/ws`, so nothing identifying is in the path.
|
|
230
|
+
*/
|
|
231
|
+
export declare function platformSocketUrl(baseUrl: string): string;
|
|
220
232
|
export type Sender = {
|
|
221
233
|
id: string;
|
|
222
234
|
name: string;
|
package/lib/index.js
CHANGED
|
@@ -21,7 +21,7 @@ exports.version = package_json_1.default.version;
|
|
|
21
21
|
// resource page in PRiSM, read from the platform when the bot connects.
|
|
22
22
|
exports.Config = koishi_1.Schema.object({
|
|
23
23
|
baseUrl: koishi_1.Schema.string().required().description("PRiSM 后端地址,例如 https://link.neri.moe"),
|
|
24
|
-
shopCode: koishi_1.Schema.string().
|
|
24
|
+
shopCode: koishi_1.Schema.string().description("可选:店铺编号。接入凭据已经指明店铺,无需填写;旧配置仍认。"),
|
|
25
25
|
integrationToken: koishi_1.Schema.string().role("secret").required().description("集成 API Token;其余设置在 PRiSM 商户工作台 → 资源 → 这个 Bot → Bot 设置"),
|
|
26
26
|
});
|
|
27
27
|
function apply(ctx, config) {
|
|
@@ -68,8 +68,8 @@ function applyPrismKoishiPlugin(ctx, config) {
|
|
|
68
68
|
if (gate.effect && bot.settings.playerCommands === "readonly" && !service.isStaff(bot, await service.sender(context))) {
|
|
69
69
|
return quoted(bot.settings.readOnlyNotice || DEFAULT_READ_ONLY_NOTICE);
|
|
70
70
|
}
|
|
71
|
-
if (gate.
|
|
72
|
-
return quoted(
|
|
71
|
+
if (gate.entry && !bot.billingEnabled)
|
|
72
|
+
return quoted(RESOURCE_NOT_BILLED_NOTICE);
|
|
73
73
|
}
|
|
74
74
|
return quoted(await handler(context, ...args));
|
|
75
75
|
}
|
|
@@ -78,28 +78,28 @@ function applyPrismKoishiPlugin(ctx, config) {
|
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
80
|
const effect = (handler, gate = {}) => wrap(handler, { ...gate, effect: true });
|
|
81
|
-
const
|
|
81
|
+
const entry = { entry: true };
|
|
82
82
|
const bind = wrap(async (context, code) => service.bind(await service.sender(context), code));
|
|
83
83
|
ctx.command("bind <code:string>", "绑定网页登录账号到当前平台身份").action(bind);
|
|
84
84
|
// Instructions already handed to players may still say prism.bind.
|
|
85
85
|
ctx.command("prism.bind <code:string>", "绑定网页登录账号到当前平台身份(旧命令,同 bind)").action(bind);
|
|
86
|
-
ctx.command("login [target:user]", "开启玩家计费场次").action(effect(async (context, target) => service.loginForTarget(await service.sender(context), target, context.session?.bot),
|
|
87
|
-
ctx.command("入场 [target:user]", "入场").action(effect(async (context, target) => service.loginForTarget(await service.sender(context), target, context.session?.bot),
|
|
88
|
-
ctx.command("mahjong <tableId>", "加入指定麻将桌").action(effect(async (context, tableId) => service.mahjongJoin(await service.sender(context), tableId)
|
|
89
|
-
ctx.command("上桌 [tableId]", "加入指定麻将桌").action(effect(async (context, tableId) => service.mahjongJoin(await service.sender(context), tableId)
|
|
90
|
-
ctx.command("下桌 [tableId]", "离开当前所在麻将桌").action(effect(async (context, tableId) => service.mahjongLeave(await service.sender(context), tableId)
|
|
91
|
-
ctx.command("麻将列表", "查看麻将机状态与别名").action(wrap(async () => service.listMahjongTables()
|
|
92
|
-
ctx.command("logout [target:user]", "结算玩家计费场次").action(effect(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.logout(sender, context.session?.bot), context.session?.bot)
|
|
93
|
-
ctx.command("billing [target:user]", "预览玩家结账费用").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.billing(sender), context.session?.bot)
|
|
94
|
-
ctx.command("wallet [target:user]", "查看玩家钱包").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.wallet(sender), context.session?.bot)
|
|
86
|
+
ctx.command("login [target:user]", "开启玩家计费场次").action(effect(async (context, target) => service.loginForTarget(await service.sender(context), target, context.session?.bot), entry));
|
|
87
|
+
ctx.command("入场 [target:user]", "入场").action(effect(async (context, target) => service.loginForTarget(await service.sender(context), target, context.session?.bot), entry));
|
|
88
|
+
ctx.command("mahjong <tableId>", "加入指定麻将桌").action(effect(async (context, tableId) => service.mahjongJoin(await service.sender(context), tableId)));
|
|
89
|
+
ctx.command("上桌 [tableId]", "加入指定麻将桌").action(effect(async (context, tableId) => service.mahjongJoin(await service.sender(context), tableId)));
|
|
90
|
+
ctx.command("下桌 [tableId]", "离开当前所在麻将桌").action(effect(async (context, tableId) => service.mahjongLeave(await service.sender(context), tableId)));
|
|
91
|
+
ctx.command("麻将列表", "查看麻将机状态与别名").action(wrap(async () => service.listMahjongTables()));
|
|
92
|
+
ctx.command("logout [target:user]", "结算玩家计费场次").action(effect(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.logout(sender, context.session?.bot), context.session?.bot)));
|
|
93
|
+
ctx.command("billing [target:user]", "预览玩家结账费用").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.billing(sender), context.session?.bot)));
|
|
94
|
+
ctx.command("wallet [target:user]", "查看玩家钱包").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.wallet(sender), context.session?.bot)));
|
|
95
95
|
ctx.command("api测速 [count:number]", "测试 Bot 走 HTTP 到 PRiSM 的延迟").action(wrap(async (context, count) => service.benchmarkApi(await service.sender(context), count)));
|
|
96
96
|
ctx.command("ws测速 [count:number]", "测试 Bot 走 WebSocket 到 PRiSM 的延迟").action(wrap(async (context, count) => service.benchmarkWs(await service.sender(context), count)));
|
|
97
97
|
// The Bot's own version stays visible while the platform is out of reach.
|
|
98
98
|
ctx.command("versions", "查看 Bot 与 PRiSM 后端版本").action(wrap(async () => service.versions(), { offline: true }));
|
|
99
|
-
ctx.command("items [target:user]", "查看玩家资产").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.items(sender), context.session?.bot)
|
|
100
|
-
ctx.command("list [name:text]", "查看在店玩家,或某个资源、某个计费方案上的玩家").action(wrap(async (context, name) => service.listActiveSessions(await service.sender(context), name)
|
|
99
|
+
ctx.command("items [target:user]", "查看玩家资产").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.items(sender), context.session?.bot)));
|
|
100
|
+
ctx.command("list [name:text]", "查看在店玩家,或某个资源、某个计费方案上的玩家").action(wrap(async (context, name) => service.listActiveSessions(await service.sender(context), name)));
|
|
101
101
|
ctx.command("show [deviceId]", "查看设备电源状态").action(wrap(async (context, deviceId) => service.listDeviceStates(deviceId)));
|
|
102
|
-
ctx.command("history [target:user]", "查看玩家历史").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.history(sender), context.session?.bot)
|
|
102
|
+
ctx.command("history [target:user]", "查看玩家历史").action(wrap(async (context, target) => service.withTarget(await service.sender(context), target, (sender) => service.history(sender), context.session?.bot)));
|
|
103
103
|
ctx.command("lock", "向本店门锁发送开门指令").action(effect(async (context) => service.lock(await service.sender(context))));
|
|
104
104
|
ctx.command("on <deviceRef>", "请求启动指定设备电源").action(effect(async (context, deviceRef) => service.powerOn(await service.sender(context), deviceRef)));
|
|
105
105
|
ctx.command("off <deviceRef>", "请求关闭指定设备电源").action(effect(async (context, deviceRef) => service.powerOff(await service.sender(context), deviceRef)));
|
|
@@ -108,14 +108,17 @@ function applyPrismKoishiPlugin(ctx, config) {
|
|
|
108
108
|
ctx.command("keep <deviceRef>", "延迟闲置设备的自动关机").action(wrap(async (context, deviceRef) => service.keep(await service.sender(context), deviceRef)));
|
|
109
109
|
ctx.command("coin <deviceRef> [count]", "请求向指定设备投币").action(effect(async (context, deviceRef, count) => service.coin(await service.sender(context), deviceRef, count)));
|
|
110
110
|
ctx.command("scan <deviceRef>", "使用当前玩家绑定的 Aime 卡刷卡").action(effect(async (context, deviceRef) => service.scan(await service.sender(context), deviceRef)));
|
|
111
|
-
ctx.command("redeem <code>", "兑换 PRiSM 礼物码").action(wrap(async (context, code) => service.redeem(await service.sender(context), code)
|
|
112
|
-
ctx.command("add <target:user> <amount:number>", "增加玩家余额").action(wrap(async (context, target, amount) => service.adjustTargetBalance(await service.sender(context), target, amount, 1, context.session?.bot)
|
|
113
|
-
ctx.command("del <target:user> <amount:number>", "扣除玩家余额").action(wrap(async (context, target, amount) => service.adjustTargetBalance(await service.sender(context), target, amount, -1, context.session?.bot)
|
|
114
|
-
ctx.command("overwrite <target:user> <amount:number> [reason:text]", "覆盖结账金额并立即结账").action(wrap(async (context, target, amount, reason) => service.overwriteTargetCheckout(await service.sender(context), target, amount, reason, context.session?.bot)
|
|
111
|
+
ctx.command("redeem <code>", "兑换 PRiSM 礼物码").action(wrap(async (context, code) => service.redeem(await service.sender(context), code)));
|
|
112
|
+
ctx.command("add <target:user> <amount:number>", "增加玩家余额").action(wrap(async (context, target, amount) => service.adjustTargetBalance(await service.sender(context), target, amount, 1, context.session?.bot)));
|
|
113
|
+
ctx.command("del <target:user> <amount:number>", "扣除玩家余额").action(wrap(async (context, target, amount) => service.adjustTargetBalance(await service.sender(context), target, amount, -1, context.session?.bot)));
|
|
114
|
+
ctx.command("overwrite <target:user> <amount:number> [reason:text]", "覆盖结账金额并立即结账").action(wrap(async (context, target, amount, reason) => service.overwriteTargetCheckout(await service.sender(context), target, amount, reason, context.session?.bot)));
|
|
115
115
|
ctx.on?.("dispose", () => service.dispose());
|
|
116
116
|
}
|
|
117
117
|
const DEFAULT_READ_ONLY_NOTICE = "请扫描店内二维码或碰 NFC,在网页或 App Clip 里入场、结账和操作设备。";
|
|
118
|
+
/** Platforms before per-resource billing refused billing commands shop-wide. */
|
|
118
119
|
const BILLING_DISABLED_NOTICE = "本店未启用计费";
|
|
120
|
+
/** The bot's resource has no plan, so there is nothing to enter. */
|
|
121
|
+
const RESOURCE_NOT_BILLED_NOTICE = "这里不计费,无需入场";
|
|
119
122
|
/* --------------------------- platform connection --------------------------- */
|
|
120
123
|
const SOCKET_OPEN = 1;
|
|
121
124
|
const REQUEST_TIMEOUT_MS = 30_000;
|
|
@@ -147,7 +150,9 @@ class PrismSocket {
|
|
|
147
150
|
/** Bumped by every hello being prepared, so only the newest one is sent. */
|
|
148
151
|
greeting = 0;
|
|
149
152
|
pongWaiters = [];
|
|
150
|
-
constructor(open,
|
|
153
|
+
constructor(open,
|
|
154
|
+
/** Resolved at each connection: the shop is learned from the credential first. */
|
|
155
|
+
url, token,
|
|
151
156
|
/**
|
|
152
157
|
* What to say in hello, read afresh each time: the bot's settings live on
|
|
153
158
|
* the platform, so they are fetched over this connection before it is
|
|
@@ -183,6 +188,12 @@ class PrismSocket {
|
|
|
183
188
|
this.stopped = false;
|
|
184
189
|
this.connect();
|
|
185
190
|
}
|
|
191
|
+
/** Connect again with what the platform has just told us, such as the shop. */
|
|
192
|
+
reconnect() {
|
|
193
|
+
this.socket?.close(1000, "shop known");
|
|
194
|
+
this.socket = null;
|
|
195
|
+
this.connect();
|
|
196
|
+
}
|
|
186
197
|
stop() {
|
|
187
198
|
this.stopped = true;
|
|
188
199
|
if (this.retryTimer)
|
|
@@ -196,7 +207,7 @@ class PrismSocket {
|
|
|
196
207
|
connect() {
|
|
197
208
|
let socket;
|
|
198
209
|
try {
|
|
199
|
-
socket = this.open(this.url, { Authorization: `Bearer ${this.token}` });
|
|
210
|
+
socket = this.open(typeof this.url === "function" ? this.url() : this.url, { Authorization: `Bearer ${this.token}` });
|
|
200
211
|
}
|
|
201
212
|
catch (error) {
|
|
202
213
|
this.logger?.warn("PRiSM connection failed", error);
|
|
@@ -383,10 +394,13 @@ function serverLine(timings) {
|
|
|
383
394
|
return [];
|
|
384
395
|
return [`服务端处理:平均 ${Math.round(known.reduce((sum, value) => sum + value, 0) / known.length)} ms`];
|
|
385
396
|
}
|
|
386
|
-
/**
|
|
387
|
-
|
|
397
|
+
/**
|
|
398
|
+
* The platform connection URL. The credential names the shop, as a device's
|
|
399
|
+
* token names its machine at `/io/ws`, so nothing identifying is in the path.
|
|
400
|
+
*/
|
|
401
|
+
function platformSocketUrl(baseUrl) {
|
|
388
402
|
const base = baseUrl.trim().replace(/\/+$/, "").replace(/^http/i, "ws");
|
|
389
|
-
return `${base}/
|
|
403
|
+
return `${base}/integration/ws`;
|
|
390
404
|
}
|
|
391
405
|
/* ----------------------------- api client ---------------------------------- */
|
|
392
406
|
class PrismApiClient {
|
|
@@ -423,6 +437,9 @@ class PrismApiClient {
|
|
|
423
437
|
...(identity.displayName === undefined ? {} : { displayName: identity.displayName }),
|
|
424
438
|
};
|
|
425
439
|
}
|
|
440
|
+
/** The shop this credential belongs to, read before the shop code is known. */
|
|
441
|
+
setShopCode(code) { if (this.http)
|
|
442
|
+
this.http.shopCode = code; }
|
|
426
443
|
/** The bot's own settings and what it needs to know about the shop. */
|
|
427
444
|
async getBotConfig() {
|
|
428
445
|
return this.request("GET", "/rpc/integration/bot", {});
|
|
@@ -475,6 +492,8 @@ class PrismApiClient {
|
|
|
475
492
|
if (!this.http)
|
|
476
493
|
throw new PrismBotClientError("HTTP 测速不可用", "HTTP_UNAVAILABLE", 0, {});
|
|
477
494
|
const { post, baseUrl, shopCode, token } = this.http;
|
|
495
|
+
if (!shopCode)
|
|
496
|
+
throw new PrismBotClientError("店铺尚未读取", "SHOP_UNKNOWN", 0, {});
|
|
478
497
|
const url = `${baseUrl.trim().replace(/\/+$/, "")}/api/v1/shops/${encodeURIComponent(shopCode)}/integration/players/by-identity/wallet`;
|
|
479
498
|
const startedAt = performance.now();
|
|
480
499
|
const response = await post(url, { authorization: `Bearer ${token}`, "content-type": "application/json" }, this.identityBody(identity));
|
|
@@ -585,8 +604,8 @@ class PrismKoishiService {
|
|
|
585
604
|
this.client = options.client;
|
|
586
605
|
return;
|
|
587
606
|
}
|
|
588
|
-
if (!options.
|
|
589
|
-
throw new Error("PRiSM Koishi plugin requires '
|
|
607
|
+
if (!options.baseUrl || !options.integrationToken) {
|
|
608
|
+
throw new Error("PRiSM Koishi plugin requires 'baseUrl' and 'integrationToken'.");
|
|
590
609
|
}
|
|
591
610
|
const open = options.socket ?? ((url, headers) => {
|
|
592
611
|
const http = ctx.http;
|
|
@@ -599,7 +618,7 @@ class PrismKoishiService {
|
|
|
599
618
|
const haOpen = options.haSocket ?? (options.socket ? null : open);
|
|
600
619
|
if (haOpen)
|
|
601
620
|
this.watcher = new ha_watch_js_1.HaWatcher(haOpen, (frame) => this.socket?.send(frame), this.logger);
|
|
602
|
-
this.socket = new PrismSocket(open, platformSocketUrl(options.baseUrl
|
|
621
|
+
this.socket = new PrismSocket(open, platformSocketUrl(options.baseUrl), options.integrationToken, () => this.hello(), (event) => {
|
|
603
622
|
if (event.event === "idle.reminder")
|
|
604
623
|
void this.remindIdle(event.data);
|
|
605
624
|
}, this.logger, this.watcher
|
|
@@ -618,7 +637,7 @@ class PrismKoishiService {
|
|
|
618
637
|
});
|
|
619
638
|
return { status: response.status, headers: response.headers, data: response.data };
|
|
620
639
|
});
|
|
621
|
-
this.client = new PrismApiClient(this.socket, { post, baseUrl: options.baseUrl,
|
|
640
|
+
this.client = new PrismApiClient(this.socket, { post, baseUrl: options.baseUrl, token: options.integrationToken, ...(options.shopCode ? { shopCode: options.shopCode } : {}) });
|
|
622
641
|
this.socket.start();
|
|
623
642
|
}
|
|
624
643
|
dispose() {
|
|
@@ -636,8 +655,11 @@ class PrismKoishiService {
|
|
|
636
655
|
this.reading = (async () => {
|
|
637
656
|
try {
|
|
638
657
|
const bot = parseBotConfig(await this.client.getBotConfig());
|
|
639
|
-
if (read === this.reads)
|
|
658
|
+
if (read === this.reads) {
|
|
640
659
|
this.bot = bot;
|
|
660
|
+
if (bot.shop?.code)
|
|
661
|
+
this.client.setShopCode(bot.shop.code);
|
|
662
|
+
}
|
|
641
663
|
return bot;
|
|
642
664
|
}
|
|
643
665
|
catch (error) {
|
|
@@ -669,8 +691,9 @@ class PrismKoishiService {
|
|
|
669
691
|
return {};
|
|
670
692
|
}
|
|
671
693
|
const { settings } = bot;
|
|
672
|
-
|
|
673
|
-
//
|
|
694
|
+
// Watching is the platform's to schedule: every bot that can offers, and the
|
|
695
|
+
// hub picks among them. A bot with no Home Assistant connection cannot.
|
|
696
|
+
this.watching = this.watcher !== null;
|
|
674
697
|
if (!this.watching)
|
|
675
698
|
this.watcher?.stop();
|
|
676
699
|
return {
|
|
@@ -1199,9 +1222,11 @@ class PrismKoishiService {
|
|
|
1199
1222
|
return `${operation}失败,${powerFailureReason(error)}`;
|
|
1200
1223
|
}
|
|
1201
1224
|
}
|
|
1225
|
+
/** Whether the action failed; the message itself is logged, never shown as it is. */
|
|
1202
1226
|
getCommandFailureMessage(result) {
|
|
1203
1227
|
const action = result?.action;
|
|
1204
|
-
|
|
1228
|
+
// `pending` here is an unanswered request (a timeout): it counts as not done.
|
|
1229
|
+
if (action?.status === "expired" || action?.status === "rejected" || action?.status === "pending") {
|
|
1205
1230
|
return action.payload?.executorFailure?.message || "命令执行被拒绝或超时";
|
|
1206
1231
|
}
|
|
1207
1232
|
return null;
|
|
@@ -1388,8 +1413,9 @@ class PrismKoishiService {
|
|
|
1388
1413
|
handleCommandError(error) {
|
|
1389
1414
|
this.logCommandError(error);
|
|
1390
1415
|
if (error instanceof PrismBotClientError) {
|
|
1391
|
-
|
|
1392
|
-
|
|
1416
|
+
const shopCode = this.options.shopCode ?? this.bot?.shop?.code;
|
|
1417
|
+
if (error.code?.startsWith("LOCATION_") && shopCode && this.options.baseUrl) {
|
|
1418
|
+
return `请在网页完成到店校验:${this.options.baseUrl.replace(/\/+$/, "")}/t/${encodeURIComponent(shopCode)}`;
|
|
1393
1419
|
}
|
|
1394
1420
|
return humanReadableBotError(error);
|
|
1395
1421
|
}
|
|
@@ -1412,6 +1438,8 @@ function humanReadableBotError(error) {
|
|
|
1412
1438
|
return "Bot 已被店铺停用";
|
|
1413
1439
|
if (error.code === "BILLING_DISABLED")
|
|
1414
1440
|
return BILLING_DISABLED_NOTICE;
|
|
1441
|
+
if (error.code === "RESOURCE_NOT_BILLED")
|
|
1442
|
+
return RESOURCE_NOT_BILLED_NOTICE;
|
|
1415
1443
|
if (error.code === "PLAYER_HAS_NO_UNSETTLED_SESSIONS") {
|
|
1416
1444
|
return "您当前没有未结算的账单,无需结账。";
|
|
1417
1445
|
}
|
|
@@ -1451,7 +1479,8 @@ function powerFailureReason(error) {
|
|
|
1451
1479
|
return "玩家未注册";
|
|
1452
1480
|
if (error.code === "BILLING_DISABLED")
|
|
1453
1481
|
return BILLING_DISABLED_NOTICE;
|
|
1454
|
-
|
|
1482
|
+
if (error.code === "DEVICE_NOT_FOUND")
|
|
1483
|
+
return "没有这个设备";
|
|
1455
1484
|
}
|
|
1456
1485
|
return "请稍后再试";
|
|
1457
1486
|
}
|
|
@@ -1470,16 +1499,23 @@ function deviceActionFailureReason(error) {
|
|
|
1470
1499
|
return BILLING_DISABLED_NOTICE;
|
|
1471
1500
|
return "请稍后再试";
|
|
1472
1501
|
}
|
|
1502
|
+
/**
|
|
1503
|
+
* What a player may be told about a device that would not act. Only facts about
|
|
1504
|
+
* the device itself — whether it exists, is connected, is off, answered — never
|
|
1505
|
+
* how the shop configured it or what went wrong inside the platform.
|
|
1506
|
+
*/
|
|
1507
|
+
const DEVICE_REASONS = [
|
|
1508
|
+
[/设备不存在|没有找到这个资源/, "没有这个设备"],
|
|
1509
|
+
[/机台未在线|没有在线客户端|设备未连接|直连中继尚未配置/, "设备未连接"],
|
|
1510
|
+
[/设备已关机/, "设备已关机"],
|
|
1511
|
+
[/设备未响应|机台未响应/, "设备没有响应"],
|
|
1512
|
+
];
|
|
1473
1513
|
function deviceCommandFailureReason(result) {
|
|
1474
1514
|
const action = result?.action;
|
|
1475
|
-
if (!action || (action.status !== "expired" && action.status !== "rejected"))
|
|
1515
|
+
if (!action || (action.status !== "expired" && action.status !== "rejected" && action.status !== "pending"))
|
|
1476
1516
|
return null;
|
|
1477
1517
|
const message = cleanText(action.payload?.executorFailure?.message);
|
|
1478
|
-
|
|
1479
|
-
return "设备未连接";
|
|
1480
|
-
if (message.includes("设备配置不存在"))
|
|
1481
|
-
return "设备未配置";
|
|
1482
|
-
return "请稍后再试";
|
|
1518
|
+
return DEVICE_REASONS.find(([pattern]) => pattern.test(message))?.[1] ?? "操作没有完成,请找店员";
|
|
1483
1519
|
}
|
|
1484
1520
|
function deviceCommandLabel(result) {
|
|
1485
1521
|
return cleanText(result?.action?.payload?.deviceLabel);
|
|
@@ -1535,7 +1571,6 @@ function parseBotConfig(value) {
|
|
|
1535
1571
|
receiptsToStaff: settings.receiptsToStaff === true,
|
|
1536
1572
|
idleChannelIds: strings(settings.idleChannelIds),
|
|
1537
1573
|
idleReminderMinutes: Math.max(0, Number(settings.idleReminderMinutes ?? 5) || 0),
|
|
1538
|
-
watchHomeAssistant: settings.watchHomeAssistant !== false,
|
|
1539
1574
|
},
|
|
1540
1575
|
billingEnabled: record.billingEnabled !== false,
|
|
1541
1576
|
currencyName: cleanText(record.currencyName) || "余额",
|