@zhin.js/game-kit 1.0.1

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.
Files changed (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -0
  3. package/lib/board-sender.d.ts +14 -0
  4. package/lib/board-sender.d.ts.map +1 -0
  5. package/lib/board-sender.js +7 -0
  6. package/lib/board-sender.js.map +1 -0
  7. package/lib/choice-keyboard.d.ts +44 -0
  8. package/lib/choice-keyboard.d.ts.map +1 -0
  9. package/lib/choice-keyboard.js +67 -0
  10. package/lib/choice-keyboard.js.map +1 -0
  11. package/lib/command-alias.d.ts +21 -0
  12. package/lib/command-alias.d.ts.map +1 -0
  13. package/lib/command-alias.js +22 -0
  14. package/lib/command-alias.js.map +1 -0
  15. package/lib/command-help.d.ts +19 -0
  16. package/lib/command-help.d.ts.map +1 -0
  17. package/lib/command-help.js +91 -0
  18. package/lib/command-help.js.map +1 -0
  19. package/lib/command-message.d.ts +25 -0
  20. package/lib/command-message.d.ts.map +1 -0
  21. package/lib/command-message.js +58 -0
  22. package/lib/command-message.js.map +1 -0
  23. package/lib/game-action-alias.d.ts +8 -0
  24. package/lib/game-action-alias.d.ts.map +1 -0
  25. package/lib/game-action-alias.js +117 -0
  26. package/lib/game-action-alias.js.map +1 -0
  27. package/lib/game-commands.d.ts +3 -0
  28. package/lib/game-commands.d.ts.map +1 -0
  29. package/lib/game-commands.js +5 -0
  30. package/lib/game-commands.js.map +1 -0
  31. package/lib/game-hub-feature.d.ts +58 -0
  32. package/lib/game-hub-feature.d.ts.map +1 -0
  33. package/lib/game-hub-feature.js +100 -0
  34. package/lib/game-hub-feature.js.map +1 -0
  35. package/lib/game-hub-flow.d.ts +9 -0
  36. package/lib/game-hub-flow.d.ts.map +1 -0
  37. package/lib/game-hub-flow.js +90 -0
  38. package/lib/game-hub-flow.js.map +1 -0
  39. package/lib/game-hub-menu-context.d.ts +25 -0
  40. package/lib/game-hub-menu-context.d.ts.map +1 -0
  41. package/lib/game-hub-menu-context.js +64 -0
  42. package/lib/game-hub-menu-context.js.map +1 -0
  43. package/lib/game-hub-menu.d.ts +37 -0
  44. package/lib/game-hub-menu.d.ts.map +1 -0
  45. package/lib/game-hub-menu.js +162 -0
  46. package/lib/game-hub-menu.js.map +1 -0
  47. package/lib/game-hub-mount.d.ts +9 -0
  48. package/lib/game-hub-mount.d.ts.map +1 -0
  49. package/lib/game-hub-mount.js +108 -0
  50. package/lib/game-hub-mount.js.map +1 -0
  51. package/lib/game-interactive.d.ts +25 -0
  52. package/lib/game-interactive.d.ts.map +1 -0
  53. package/lib/game-interactive.js +62 -0
  54. package/lib/game-interactive.js.map +1 -0
  55. package/lib/game-middleware.d.ts +6 -0
  56. package/lib/game-middleware.d.ts.map +1 -0
  57. package/lib/game-middleware.js +9 -0
  58. package/lib/game-middleware.js.map +1 -0
  59. package/lib/game-onboarding.d.ts +8 -0
  60. package/lib/game-onboarding.d.ts.map +1 -0
  61. package/lib/game-onboarding.js +66 -0
  62. package/lib/game-onboarding.js.map +1 -0
  63. package/lib/game-records.d.ts +52 -0
  64. package/lib/game-records.d.ts.map +1 -0
  65. package/lib/game-records.js +206 -0
  66. package/lib/game-records.js.map +1 -0
  67. package/lib/game-session.d.ts +64 -0
  68. package/lib/game-session.d.ts.map +1 -0
  69. package/lib/game-session.js +44 -0
  70. package/lib/game-session.js.map +1 -0
  71. package/lib/grid-keyboard.d.ts +67 -0
  72. package/lib/grid-keyboard.d.ts.map +1 -0
  73. package/lib/grid-keyboard.js +104 -0
  74. package/lib/grid-keyboard.js.map +1 -0
  75. package/lib/help-card.d.ts +12 -0
  76. package/lib/help-card.d.ts.map +1 -0
  77. package/lib/help-card.js +129 -0
  78. package/lib/help-card.js.map +1 -0
  79. package/lib/index.d.ts +29 -0
  80. package/lib/index.d.ts.map +1 -0
  81. package/lib/index.js +29 -0
  82. package/lib/index.js.map +1 -0
  83. package/lib/interaction-profiles.d.ts +16 -0
  84. package/lib/interaction-profiles.d.ts.map +1 -0
  85. package/lib/interaction-profiles.js +35 -0
  86. package/lib/interaction-profiles.js.map +1 -0
  87. package/lib/memory-db.d.ts +41 -0
  88. package/lib/memory-db.d.ts.map +1 -0
  89. package/lib/memory-db.js +92 -0
  90. package/lib/memory-db.js.map +1 -0
  91. package/lib/random.d.ts +6 -0
  92. package/lib/random.d.ts.map +1 -0
  93. package/lib/random.js +29 -0
  94. package/lib/random.js.map +1 -0
  95. package/lib/runtime-hub.d.ts +28 -0
  96. package/lib/runtime-hub.d.ts.map +1 -0
  97. package/lib/runtime-hub.js +66 -0
  98. package/lib/runtime-hub.js.map +1 -0
  99. package/lib/send-content-text.d.ts +5 -0
  100. package/lib/send-content-text.d.ts.map +1 -0
  101. package/lib/send-content-text.js +24 -0
  102. package/lib/send-content-text.js.map +1 -0
  103. package/package.json +49 -0
  104. package/src/board-sender.ts +16 -0
  105. package/src/choice-keyboard.ts +117 -0
  106. package/src/command-alias.ts +45 -0
  107. package/src/command-help.ts +117 -0
  108. package/src/command-message.ts +76 -0
  109. package/src/game-action-alias.ts +129 -0
  110. package/src/game-commands.ts +4 -0
  111. package/src/game-hub-feature.ts +151 -0
  112. package/src/game-hub-flow.ts +114 -0
  113. package/src/game-hub-menu-context.ts +94 -0
  114. package/src/game-hub-menu.ts +223 -0
  115. package/src/game-hub-mount.ts +172 -0
  116. package/src/game-interactive.ts +88 -0
  117. package/src/game-middleware.ts +14 -0
  118. package/src/game-onboarding.ts +67 -0
  119. package/src/game-records.ts +287 -0
  120. package/src/game-session.ts +97 -0
  121. package/src/grid-keyboard.ts +180 -0
  122. package/src/help-card.ts +188 -0
  123. package/src/index.ts +29 -0
  124. package/src/interaction-profiles.ts +56 -0
  125. package/src/memory-db.ts +128 -0
  126. package/src/random.ts +32 -0
  127. package/src/runtime-hub.ts +86 -0
  128. package/src/send-content-text.ts +21 -0
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@zhin.js/game-kit",
3
+ "version": "1.0.1",
4
+ "description": "Zhin.js 游戏插件通用工具包",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "development": "./src/index.ts",
11
+ "import": "./lib/index.js",
12
+ "types": "./lib/index.d.ts"
13
+ }
14
+ },
15
+ "peerDependencies": {
16
+ "zhin.js": "^4.1.2",
17
+ "@zhin.js/html-renderer": "1.0.3"
18
+ },
19
+ "peerDependenciesMeta": {
20
+ "@zhin.js/html-renderer": {
21
+ "optional": true
22
+ }
23
+ },
24
+ "dependencies": {
25
+ "@zhin.js/core": "1.3.4",
26
+ "@zhin.js/satori": "1.0.16",
27
+ "@zhin.js/middleware": "1.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "typescript": "^6.0.3",
31
+ "zhin.js": "4.1.2"
32
+ },
33
+ "files": [
34
+ "lib",
35
+ "src"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/zhinjs/zhin.git",
44
+ "directory": "packages/game-kit"
45
+ },
46
+ "scripts": {
47
+ "build": "tsc"
48
+ }
49
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 游戏棋盘消息辅助工具
3
+ * 提供会话键生成和消息 ID 更新回调封装
4
+ *
5
+ * 注意:实际的发送/编辑逻辑由 Adapter.editMessage 统一处理
6
+ * - 支持编辑的平台:调用平台 API 编辑
7
+ * - 不支持编辑的平台:自动 fallback 到发送新消息
8
+ */
9
+ import type { Message } from '@zhin.js/core';
10
+
11
+ /**
12
+ * 构建频道唯一键(用于会话查找)
13
+ */
14
+ export function channelKey(message: Message<any>): string {
15
+ return `${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`;
16
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * 选项式交互键盘(文字冒险、问答、剧情分支等)
3
+ */
4
+ import { segment, type SendContent } from '@zhin.js/core';
5
+ import { applyInteractionProfile, type InteractionProfile } from './interaction-profiles.js';
6
+
7
+ export interface ChoiceOption {
8
+ /** 选项 ID,写入 payload */
9
+ id: string;
10
+ label: string;
11
+ disabled?: boolean;
12
+ style?: 'primary' | 'danger' | 'secondary';
13
+ /** 终局菜单(如「再来一局」)保持可点 */
14
+ keepEnabledWhenTerminal?: boolean;
15
+ }
16
+
17
+ export interface ChoiceKeyboardOptions {
18
+ gamePrefix: string;
19
+ sessionId: string;
20
+ /** 剧情/题干正文 */
21
+ narrative: string;
22
+ choices: ChoiceOption[];
23
+ terminal?: boolean;
24
+ /** 文本 fallback 提示 */
25
+ fallbackHint?: string;
26
+ /** 每行最多几个按钮(默认全部一行) */
27
+ buttonsPerRow?: number;
28
+ /** 交互 profile:menu→command,gameplay→callback,terminal→command */
29
+ interactionProfile?: InteractionProfile;
30
+ /** terminal profile 私聊 auto-enter 用 */
31
+ channelType?: string;
32
+ }
33
+
34
+ export function buildChoiceFallbackMap(
35
+ gamePrefix: string,
36
+ sessionId: string,
37
+ choices: ChoiceOption[],
38
+ ): Record<string, string> {
39
+ const map: Record<string, string> = {};
40
+ let n = 1;
41
+ for (const choice of choices) {
42
+ if (!choice.disabled) {
43
+ map[String(n)] = `${gamePrefix}:${sessionId}:${choice.id}`;
44
+ n++;
45
+ }
46
+ }
47
+ return map;
48
+ }
49
+
50
+ function chunkChoices<T>(items: T[], size: number): T[][] {
51
+ if (size <= 0 || items.length <= size) return [items];
52
+ const rows: T[][] = [];
53
+ for (let i = 0; i < items.length; i += size) {
54
+ rows.push(items.slice(i, i + size));
55
+ }
56
+ return rows;
57
+ }
58
+
59
+ /**
60
+ * 构建选项键盘消息:正文 + 一行或多行按钮
61
+ */
62
+ export function buildChoiceKeyboard(options: ChoiceKeyboardOptions): SendContent {
63
+ const {
64
+ gamePrefix,
65
+ sessionId,
66
+ narrative,
67
+ choices,
68
+ terminal,
69
+ fallbackHint,
70
+ buttonsPerRow,
71
+ interactionProfile,
72
+ channelType,
73
+ } = options;
74
+
75
+ const enabled = choices.filter((c) => !c.disabled);
76
+ const buttonRows = chunkChoices(enabled, buttonsPerRow ?? enabled.length).map((row) =>
77
+ row.map((choice) => {
78
+ const base = segment.button({
79
+ id: choice.id,
80
+ label: choice.label,
81
+ payload: `${gamePrefix}:${sessionId}:${choice.id}`,
82
+ disabled: choice.disabled || (terminal && !choice.keepEnabledWhenTerminal),
83
+ style: choice.style ?? 'secondary',
84
+ });
85
+ const data = base.data;
86
+ if (!interactionProfile) return base;
87
+ return segment.button(
88
+ applyInteractionProfile(data, { profile: interactionProfile, channelType }),
89
+ );
90
+ }),
91
+ );
92
+
93
+ const fallback = buildChoiceFallbackMap(gamePrefix, sessionId, choices);
94
+
95
+ return [
96
+ segment.text(narrative),
97
+ segment.keyboard(buttonRows, {
98
+ fallback: fallbackHint && Object.keys(fallback).length > 0
99
+ ? { hint: fallbackHint, map: fallback }
100
+ : undefined,
101
+ }).toElement(),
102
+ ];
103
+ }
104
+
105
+ /**
106
+ * 解析选项 payload:`{prefix}:{sessionId}:{choiceId}`
107
+ */
108
+ export function parseChoicePayload(
109
+ payload: string,
110
+ expectedPrefix?: string,
111
+ ): { prefix: string; sessionId: string; choiceId: string } | null {
112
+ const m = /^([a-z0-9_]+):([^:]+):([a-z0-9_-]+)$/i.exec(payload);
113
+ if (!m) return null;
114
+ const prefix = m[1]!;
115
+ if (expectedPrefix && prefix !== expectedPrefix) return null;
116
+ return { prefix, sessionId: m[2]!, choiceId: m[3]! };
117
+ }
@@ -0,0 +1,45 @@
1
+ import { defineMiddleware, type MiddlewareContext } from '@zhin.js/middleware';
2
+ import type { Message } from '@zhin.js/core/runtime';
3
+
4
+ /**
5
+ * 游戏 legacy 命令别名路由。
6
+ *
7
+ * 插件运行时迁移后,约定式命令以 `<instanceKey> <localName>` 限定名注册(ADR 0043),
8
+ * 旧的短命令(如 `/21点 开始`、`/猜数 开始`)在 runtime 中不存在。
9
+ * 各游戏插件用本 helper 在 `middlewares/` 下注册一个入站中间件,
10
+ * 把命中别名的首词消息转发到与限定名命令完全相同的处理逻辑。
11
+ */
12
+ export interface GameCommandAliasRoute {
13
+ /** 首词命中即接管(不含前导 `/`),如 `['21点', 'bj']`。 */
14
+ readonly aliases: readonly string[];
15
+ /**
16
+ * 与 `commands/<cmd>/[action:string=].ts` 的 execute 逻辑保持一致:
17
+ * action 为别名后剩余文本(无参数时为 '');返回回复文本,null/undefined 表示放行。
18
+ */
19
+ run(
20
+ action: string,
21
+ input: unknown,
22
+ context: MiddlewareContext<Message>,
23
+ ): Promise<string | null | undefined>;
24
+ }
25
+
26
+ export function defineGameCommandAliasMiddleware(route: GameCommandAliasRoute) {
27
+ return defineMiddleware<Message>({
28
+ target: 'inbound',
29
+ async handle(context, next) {
30
+ const raw = context.input.content?.trim() ?? '';
31
+ const text = raw.startsWith('/') ? raw.slice(1) : raw;
32
+ const [head, ...rest] = text.split(/\s+/).filter(Boolean);
33
+ if (!head || !route.aliases.includes(head)) {
34
+ await next();
35
+ return;
36
+ }
37
+ const reply = await route.run(rest.join(' '), context.input, context);
38
+ if (reply) {
39
+ await context.input.$reply(reply);
40
+ return;
41
+ }
42
+ await next();
43
+ },
44
+ });
45
+ }
@@ -0,0 +1,117 @@
1
+ import type { Message, MessageCommand, Plugin } from '@zhin.js/core';
2
+ import type { RegisteredGame } from './game-hub-feature.js';
3
+
4
+ export interface CommandHelpSource {
5
+ pattern: string;
6
+ helpInfo: {
7
+ desc: string[];
8
+ usage?: string[];
9
+ examples?: string[];
10
+ };
11
+ }
12
+
13
+ export function commandToHelpSource(cmd: Pick<MessageCommand, 'pattern' | 'helpInfo'>): CommandHelpSource {
14
+ return { pattern: cmd.pattern, helpInfo: cmd.helpInfo };
15
+ }
16
+
17
+ /** 按 MessageCommand.permit 与当前消息上下文过滤(与 handle 一致) */
18
+ export async function filterHelpCommands(
19
+ commands: readonly MessageCommand[],
20
+ message: Message<any>,
21
+ plugin: Plugin,
22
+ ): Promise<CommandHelpSource[]> {
23
+ const out: CommandHelpSource[] = [];
24
+ for (const cmd of commands) {
25
+ if (await cmd.checkPermits(message, plugin)) {
26
+ out.push(commandToHelpSource(cmd));
27
+ }
28
+ }
29
+ return out;
30
+ }
31
+
32
+ function commandLeadingToken(pattern: string): string {
33
+ return pattern.split(/\s/)[0] ?? '';
34
+ }
35
+
36
+ function normalizeToken(token: string): string {
37
+ return token.replace(/^\//, '').toLowerCase();
38
+ }
39
+
40
+ /** 不在「其他命令」中重复展示的 pattern(大厅入口 / 帮助自身) */
41
+ const HELP_EXCLUDE_EXACT = new Set([
42
+ '/帮助',
43
+ '/help',
44
+ '帮助',
45
+ '/游戏 帮助',
46
+ '/游戏',
47
+ '/game',
48
+ '游戏',
49
+ 'game',
50
+ ]);
51
+
52
+ function isHubListedGameCommand(pattern: string, games: readonly RegisteredGame[]): boolean {
53
+ const lead = normalizeToken(commandLeadingToken(pattern));
54
+ return games.some((g) => normalizeToken(g.commandPrefix) === lead);
55
+ }
56
+
57
+ function comparePatterns(a: string, b: string): number {
58
+ const leadDiff = commandLeadingToken(b).length - commandLeadingToken(a).length;
59
+ if (leadDiff !== 0) return leadDiff;
60
+ return b.length - a.length;
61
+ }
62
+
63
+ function formatPatternDisplay(pattern: string): string {
64
+ return `\`${pattern}\``;
65
+ }
66
+
67
+ function formatCommandHelpLine(cmd: CommandHelpSource): string {
68
+ const [label = '', detail = ''] = cmd.helpInfo.desc;
69
+ const display = formatPatternDisplay(cmd.pattern);
70
+ const summary = detail || label || cmd.pattern;
71
+ if (label && label !== commandLeadingToken(cmd.pattern) && !cmd.pattern.startsWith(label)) {
72
+ return `• ${display}(${label})— ${summary}`;
73
+ }
74
+ return `• ${display} — ${summary}`;
75
+ }
76
+
77
+ /** 从 command 服务收集应在 /帮助 中展示的命令(去重、排除已在游戏大厅列出的项) */
78
+ export function collectSupplementaryCommandHelp(
79
+ commands: readonly CommandHelpSource[],
80
+ games: readonly RegisteredGame[],
81
+ ): CommandHelpSource[] {
82
+ const seen = new Set<string>();
83
+ const out: CommandHelpSource[] = [];
84
+ for (const cmd of [...commands].sort((a, b) => comparePatterns(a.pattern, b.pattern))) {
85
+ if (HELP_EXCLUDE_EXACT.has(cmd.pattern)) continue;
86
+ if (isHubListedGameCommand(cmd.pattern, games)) continue;
87
+ if (seen.has(cmd.pattern)) continue;
88
+ seen.add(cmd.pattern);
89
+ out.push(cmd);
90
+ }
91
+ return out;
92
+ }
93
+
94
+ export function formatSupplementaryCommandsHelp(
95
+ commands: readonly CommandHelpSource[],
96
+ games: readonly RegisteredGame[],
97
+ ): string {
98
+ const items = collectSupplementaryCommandHelp(commands, games);
99
+ if (!items.length) return '';
100
+ const lines = items.map(formatCommandHelpLine);
101
+ return ['**其他命令**', '', ...lines].join('\n');
102
+ }
103
+
104
+ /** 大厅入口行:从已注册命令的 .desc() 生成 */
105
+ export function formatHubOpenCommandLines(
106
+ commands: readonly CommandHelpSource[] | undefined,
107
+ ): string[] {
108
+ if (!commands?.length) return [];
109
+ const lines: string[] = [];
110
+ for (const pattern of ['/游戏', '/game'] as const) {
111
+ const cmd = commands.find((c) => c.pattern === pattern);
112
+ if (!cmd) continue;
113
+ const desc = cmd.helpInfo.desc[0] ?? cmd.helpInfo.desc[1] ?? '';
114
+ if (desc) lines.push(`${pattern} — ${desc}`);
115
+ }
116
+ return lines;
117
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Map Plugin Runtime CommandContext.input → classic Message-like shape used by game SessionService.
3
+ *
4
+ * Runtime Message: { adapter, target, sender?, metadata? }
5
+ * Classic Message: { $adapter, $endpoint, $channel, $sender }
6
+ */
7
+
8
+ export interface GameMessageLike {
9
+ $adapter: string;
10
+ $endpoint: string;
11
+ $channel: { type: string; id: string };
12
+ $sender: { id: string; name: string };
13
+ }
14
+
15
+ function isClassicMessage(input: unknown): input is GameMessageLike {
16
+ if (!input || typeof input !== 'object') return false;
17
+ const m = input as Record<string, unknown>;
18
+ return (
19
+ typeof m.$adapter === 'string' &&
20
+ m.$channel != null &&
21
+ typeof m.$channel === 'object' &&
22
+ m.$sender != null &&
23
+ typeof m.$sender === 'object'
24
+ );
25
+ }
26
+
27
+ function isRuntimeMessage(input: unknown): input is {
28
+ adapter?: string;
29
+ target?: string;
30
+ sender?: string;
31
+ metadata?: Readonly<Record<string, unknown>>;
32
+ } {
33
+ if (!input || typeof input !== 'object') return false;
34
+ const m = input as Record<string, unknown>;
35
+ return (
36
+ 'adapter' in m ||
37
+ 'target' in m ||
38
+ 'sender' in m ||
39
+ (m.metadata != null && typeof m.metadata === 'object')
40
+ );
41
+ }
42
+
43
+ /** Smoke message when CommandContext has no usable input (unit tests / CLI). */
44
+ export function smokeGameMessage(): GameMessageLike {
45
+ return {
46
+ $adapter: 'runtime',
47
+ $endpoint: 'default',
48
+ $channel: { type: 'private', id: 'smoke' },
49
+ $sender: { id: 'smoke', name: 'smoke' },
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Convert CommandContext.input to a Message-like object for game-flow / *-command.
55
+ */
56
+ export function messageFromCommandInput(input: unknown): GameMessageLike {
57
+ if (isClassicMessage(input)) return input;
58
+
59
+ if (isRuntimeMessage(input)) {
60
+ const meta = input.metadata ?? {};
61
+ const adapter = String(input.adapter ?? meta.adapter ?? 'runtime');
62
+ const endpoint = String(meta.endpoint ?? meta.endpointId ?? 'default');
63
+ const channelType = String(meta.type ?? meta.channelType ?? 'private');
64
+ const channelId = String(input.target ?? meta.channelId ?? 'smoke');
65
+ const senderId = String(input.sender ?? meta.senderId ?? 'smoke');
66
+ const senderName = String(meta.senderName ?? meta.name ?? senderId);
67
+ return {
68
+ $adapter: adapter,
69
+ $endpoint: endpoint,
70
+ $channel: { type: channelType, id: channelId },
71
+ $sender: { id: senderId, name: senderName },
72
+ };
73
+ }
74
+
75
+ return smokeGameMessage();
76
+ }
@@ -0,0 +1,129 @@
1
+ const TTT_ACTIONS: Record<string, string> = {
2
+ bot: 'bot',
3
+ join: 'join',
4
+ leave: 'leave',
5
+ quit: 'quit',
6
+ spectate: 'spectate',
7
+ help: 'help',
8
+ 人机: 'bot',
9
+ 排队: 'join',
10
+ 加入: 'join',
11
+ 离开: 'leave',
12
+ 认输: 'quit',
13
+ 观战: 'spectate',
14
+ 帮助: 'help',
15
+ };
16
+
17
+ const ADV_ACTIONS: Record<string, string> = {
18
+ start: 'start',
19
+ continue: 'continue',
20
+ map: 'map',
21
+ achievements: 'achievements',
22
+ achievement: 'achievements',
23
+ quit: 'quit',
24
+ help: 'help',
25
+ 开始: 'start',
26
+ 继续: 'continue',
27
+ 地图: 'map',
28
+ 成就: 'achievements',
29
+ 放弃: 'quit',
30
+ 退出: 'quit',
31
+ 帮助: 'help',
32
+ };
33
+
34
+ const RPS_ACTIONS: Record<string, string> = {
35
+ start: 'start',
36
+ continue: 'continue',
37
+ quit: 'quit',
38
+ help: 'help',
39
+ 开始: 'start',
40
+ 继续: 'continue',
41
+ 放弃: 'quit',
42
+ 帮助: 'help',
43
+ };
44
+
45
+ const GUESS_ACTIONS: Record<string, string> = {
46
+ start: 'start',
47
+ quit: 'quit',
48
+ help: 'help',
49
+ 开始: 'start',
50
+ 放弃: 'quit',
51
+ 帮助: 'help',
52
+ };
53
+
54
+ const DICE_ACTIONS: Record<string, string> = {
55
+ start: 'start',
56
+ continue: 'continue',
57
+ quit: 'quit',
58
+ help: 'help',
59
+ 开始: 'start',
60
+ 继续: 'continue',
61
+ 放弃: 'quit',
62
+ 帮助: 'help',
63
+ };
64
+
65
+ function lookup(map: Record<string, string>, raw: string): string {
66
+ const t = raw.trim();
67
+ if (!t) return 'help';
68
+ return map[t] ?? map[t.toLowerCase()] ?? t.toLowerCase();
69
+ }
70
+
71
+ export function normalizeTttAction(raw: string): string {
72
+ return lookup(TTT_ACTIONS, raw);
73
+ }
74
+
75
+ export function normalizeAdvAction(raw: string): string {
76
+ return lookup(ADV_ACTIONS, raw);
77
+ }
78
+
79
+ export function normalizeRpsAction(raw: string): string {
80
+ return lookup(RPS_ACTIONS, raw);
81
+ }
82
+
83
+ export function normalizeGuessAction(raw: string): string {
84
+ return lookup(GUESS_ACTIONS, raw);
85
+ }
86
+
87
+ export function normalizeDiceAction(raw: string): string {
88
+ return lookup(DICE_ACTIONS, raw);
89
+ }
90
+
91
+ const CHAIN_ACTIONS: Record<string, string> = {
92
+ start: 'start_pinyin',
93
+ start_pinyin: 'start_pinyin',
94
+ start_char: 'start_char',
95
+ continue: 'continue',
96
+ quit: 'quit',
97
+ help: 'help',
98
+ pinyin: 'start_pinyin',
99
+ char: 'start_char',
100
+ 开始: 'start_pinyin',
101
+ 同音: 'start_pinyin',
102
+ 同字: 'start_char',
103
+ 继续: 'continue',
104
+ 放弃: 'quit',
105
+ 帮助: 'help',
106
+ };
107
+
108
+ const RIDDLE_ACTIONS: Record<string, string> = {
109
+ start: 'start',
110
+ continue: 'continue',
111
+ quit: 'quit',
112
+ help: 'help',
113
+ char: 'char',
114
+ idiom: 'idiom',
115
+ 开始: 'start',
116
+ 继续: 'continue',
117
+ 放弃: 'quit',
118
+ 帮助: 'help',
119
+ 字谜: 'char',
120
+ 成语: 'idiom',
121
+ };
122
+
123
+ export function normalizeChainAction(raw: string): string {
124
+ return lookup(CHAIN_ACTIONS, raw);
125
+ }
126
+
127
+ export function normalizeRiddleAction(raw: string): string {
128
+ return lookup(RIDDLE_ACTIONS, raw);
129
+ }
@@ -0,0 +1,4 @@
1
+ /** 展示用:`/猜拳` */
2
+ export function slashCommandPrefix(name: string): string {
3
+ return name.startsWith('/') ? name : `/${name}`;
4
+ }