@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
@@ -0,0 +1,151 @@
1
+ import { Feature, getPlugin, type FeatureJSON, type Plugin, type PluginLike, type DatabaseFeature } from '@zhin.js/core';
2
+ import { mountGameHubUi, markGameHubUiMounted } from './game-hub-mount.js';
3
+ import { registerGameRecordModels, initGameRecordDatabase } from './game-records.js';
4
+
5
+ export interface GameHubContext {
6
+ plugin: Plugin;
7
+ message: import('zhin.js').Message<any>;
8
+ }
9
+
10
+ export interface GameMenuAction {
11
+ id: string;
12
+ label: string;
13
+ style?: 'primary' | 'danger' | 'secondary';
14
+ groupOnly?: boolean;
15
+ privateOnly?: boolean;
16
+ }
17
+
18
+ export interface RegisteredGame {
19
+ id: string;
20
+ title: string;
21
+ icon: string;
22
+ description: string;
23
+ commandPrefix: string;
24
+ quickStart?: string;
25
+ aliases?: string[];
26
+ menus: GameMenuAction[];
27
+ runAction: (actionId: string, ctx: GameHubContext) => Promise<string | undefined | void>;
28
+ }
29
+
30
+ export interface GameHubContextExtensions {
31
+ registerGame(game: RegisteredGame): () => void;
32
+ }
33
+
34
+ declare module '@zhin.js/core' {
35
+ namespace Plugin {
36
+ interface Extensions extends GameHubContextExtensions {}
37
+ interface Contexts {
38
+ game: GameHubFeature;
39
+ }
40
+ }
41
+ }
42
+
43
+ let activeFeature: GameHubFeature | null = null;
44
+
45
+ export class GameHubFeature extends Feature<RegisteredGame> {
46
+ readonly name = 'game' as const;
47
+ readonly icon = 'Gamepad2';
48
+ readonly desc = '游戏';
49
+
50
+ private readonly byId = new Map<string, RegisteredGame>();
51
+ private hubDisposers: (() => void)[] = [];
52
+
53
+ register(game: RegisteredGame, pluginName: string): () => void {
54
+ const prev = this.byId.get(game.id);
55
+ if (prev) {
56
+ this.remove(prev);
57
+ }
58
+ this.byId.set(game.id, game);
59
+ return this.add(game, pluginName);
60
+ }
61
+
62
+ getGame(id: string): RegisteredGame | undefined {
63
+ return this.byId.get(id);
64
+ }
65
+
66
+ override remove(item: RegisteredGame, pluginName?: string): boolean {
67
+ this.byId.delete(item.id);
68
+ return super.remove(item, pluginName);
69
+ }
70
+
71
+ toJSON(pluginName?: string): FeatureJSON {
72
+ const list = pluginName ? this.getByPlugin(pluginName) : [...this.items];
73
+ return {
74
+ name: this.name,
75
+ icon: this.icon,
76
+ desc: this.desc,
77
+ count: list.length,
78
+ items: list.map((g) => ({
79
+ name: g.id,
80
+ desc: g.title,
81
+ usage: `${g.commandPrefix} ${g.quickStart ?? '开始'}`,
82
+ })),
83
+ };
84
+ }
85
+
86
+ get extensions() {
87
+ const feature = this;
88
+ return {
89
+ registerGame(game: RegisteredGame) {
90
+ const plugin = getPlugin();
91
+ ensureGameHubService(plugin);
92
+ const dispose = feature.register(game, plugin.name);
93
+ plugin.recordFeatureContribution(feature.name, game.id);
94
+ plugin.onDispose(dispose);
95
+ return dispose;
96
+ },
97
+ };
98
+ }
99
+
100
+ mounted(plugin: PluginLike): void {
101
+ activeFeature = this;
102
+ const root = plugin as Plugin;
103
+ const host = root.root ?? root;
104
+ registerGameRecordModels(host);
105
+ if (host.contextIsReady('database')) {
106
+ initGameRecordDatabase(host.inject('database') as DatabaseFeature);
107
+ } else {
108
+ host.useContext('database', (dbFeature: DatabaseFeature) => {
109
+ initGameRecordDatabase(dbFeature);
110
+ });
111
+ }
112
+ this.hubDisposers = mountGameHubUi(host);
113
+ markGameHubUiMounted();
114
+ }
115
+
116
+ dispose(): void {
117
+ for (const d of this.hubDisposers) d();
118
+ this.hubDisposers = [];
119
+ if (activeFeature === this) {
120
+ activeFeature = null;
121
+ }
122
+ }
123
+ }
124
+
125
+ /**
126
+ * 在 root 上注册 `game` 服务(幂等)。游戏插件初始化时调用一次即可。
127
+ */
128
+ export function ensureGameHubService(plugin: Plugin): GameHubFeature {
129
+ const root = plugin.root;
130
+ if (root.contextIsReady('game')) {
131
+ return root.inject('game') as GameHubFeature;
132
+ }
133
+
134
+ const feature = new GameHubFeature();
135
+ root.provide(feature);
136
+ activeFeature = feature;
137
+
138
+ if (root.started && feature.mounted) {
139
+ feature.mounted(root);
140
+ }
141
+
142
+ return root.inject('game') as GameHubFeature;
143
+ }
144
+
145
+ export function getRegisteredGames(): readonly RegisteredGame[] {
146
+ return activeFeature?.items ?? [];
147
+ }
148
+
149
+ export function getRegisteredGame(id: string): RegisteredGame | undefined {
150
+ return activeFeature?.getGame(id);
151
+ }
@@ -0,0 +1,114 @@
1
+ import type { Message, Plugin } from '@zhin.js/core';
2
+ import { parseChoicePayload } from './choice-keyboard.js';
3
+ import {
4
+ buildGameHubMenu,
5
+ buildMainHubMenu,
6
+ formatHubEmptyMessage,
7
+ HUB_PREFIX,
8
+ hubActionChoiceId,
9
+ hubGameChoiceId,
10
+ parseHubChoiceId,
11
+ } from './game-hub-menu.js';
12
+ import { getRegisteredGame, getRegisteredGames } from './game-hub-feature.js';
13
+ import {
14
+ createHubScope,
15
+ getHubContext,
16
+ rememberHubMenu,
17
+ type HubMenuChoice,
18
+ } from './game-hub-menu-context.js';
19
+
20
+ function mainMenuChoices(): HubMenuChoice[] {
21
+ return getRegisteredGames().map((g) => ({
22
+ id: hubGameChoiceId(g.id),
23
+ label: `${g.icon} ${g.title}`,
24
+ }));
25
+ }
26
+
27
+ function gameMenuChoices(gameId: string, channelType: string): HubMenuChoice[] {
28
+ const game = getRegisteredGame(gameId);
29
+ if (!game) return [];
30
+ const isGroup = channelType !== 'private';
31
+ const menus = game.menus.filter((m) => {
32
+ if (m.groupOnly && !isGroup) return false;
33
+ if (m.privateOnly && isGroup) return false;
34
+ return true;
35
+ });
36
+ return [
37
+ ...menus.map((m) => ({
38
+ id: hubActionChoiceId(game.id, m.id),
39
+ label: m.label,
40
+ })),
41
+ { id: 'back', label: '↩️ 返回大厅' },
42
+ ];
43
+ }
44
+
45
+ export function openMainMenu(message: Message<any>): ReturnType<typeof buildMainHubMenu> | string {
46
+ const games = getRegisteredGames();
47
+ if (!games.length) {
48
+ return formatHubEmptyMessage();
49
+ }
50
+ const scopeId = createHubScope(message);
51
+ const choices = mainMenuChoices();
52
+ rememberHubMenu(message, scopeId, choices);
53
+ return buildMainHubMenu(scopeId, games, message.$channel.type);
54
+ }
55
+
56
+ export async function handleHubChoice(
57
+ plugin: Plugin,
58
+ message: Message<any>,
59
+ scopeId: string,
60
+ choiceId: string,
61
+ ): Promise<boolean> {
62
+ const ctx = getHubContext(scopeId);
63
+ if (!ctx) {
64
+ await message.$reply?.('菜单已过期,请重新发送「游戏」。');
65
+ return true;
66
+ }
67
+ const ch = `${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`;
68
+ if (ctx.channelKey !== ch) {
69
+ await message.$reply?.('请在本频道使用游戏大厅。');
70
+ return true;
71
+ }
72
+
73
+ const parsed = parseHubChoiceId(choiceId);
74
+ if (!parsed) return false;
75
+
76
+ // 大厅导航(选游戏 / 返回):同频道任何人都可点;具体开局由 runAction 用当前点击者 message
77
+ if (parsed.kind === 'back') {
78
+ const games = getRegisteredGames();
79
+ const choices = mainMenuChoices();
80
+ rememberHubMenu(message, scopeId, choices);
81
+ await message.$reply?.(buildMainHubMenu(scopeId, games, message.$channel.type));
82
+ return true;
83
+ }
84
+
85
+ if (parsed.kind === 'game') {
86
+ const game = getRegisteredGame(parsed.gameId);
87
+ if (!game) {
88
+ await message.$reply?.('该游戏未安装。');
89
+ return true;
90
+ }
91
+ const choices = gameMenuChoices(parsed.gameId, message.$channel.type);
92
+ rememberHubMenu(message, scopeId, choices);
93
+ await message.$reply?.(
94
+ buildGameHubMenu(scopeId, game, message.$channel.type),
95
+ );
96
+ return true;
97
+ }
98
+
99
+ const game = getRegisteredGame(parsed.gameId);
100
+ if (!game) {
101
+ await message.$reply?.('该游戏未安装。');
102
+ return true;
103
+ }
104
+
105
+ const result = await game.runAction(parsed.actionId, { plugin, message });
106
+ if (typeof result === 'string') await message.$reply?.(result);
107
+ return true;
108
+ }
109
+
110
+ export function parseHubPayload(payload: string): { scopeId: string; choiceId: string } | null {
111
+ const parsed = parseChoicePayload(payload, HUB_PREFIX);
112
+ if (!parsed) return null;
113
+ return { scopeId: parsed.sessionId, choiceId: parsed.choiceId };
114
+ }
@@ -0,0 +1,94 @@
1
+ import type { Message } from '@zhin.js/core';
2
+ import { channelKey } from './board-sender.js';
3
+ import { generateCompactId } from './random.js';
4
+
5
+ export interface HubMenuContext {
6
+ channelKey: string;
7
+ /** 打开菜单的用户(仅记录,大厅导航不限制点击者) */
8
+ openerId: string;
9
+ message: Message<any>;
10
+ expiresAt: number;
11
+ }
12
+
13
+ export interface HubMenuChoice {
14
+ id: string;
15
+ label: string;
16
+ disabled?: boolean;
17
+ }
18
+
19
+ export interface LastHubMenu {
20
+ scopeId: string;
21
+ choices: HubMenuChoice[];
22
+ expiresAt: number;
23
+ }
24
+
25
+ const TTL_MS = 60 * 60 * 1000;
26
+ const contexts = new Map<string, HubMenuContext>();
27
+ const lastMenus = new Map<string, LastHubMenu>();
28
+
29
+ /** 大厅 fallback 按频道共享,任意成员可回复数字或点按钮 */
30
+ function hubMenuKey(message: Message<any>): string {
31
+ return channelKey(message);
32
+ }
33
+
34
+ export function createHubScope(message: Message<any>): string {
35
+ const scopeId = generateCompactId('h');
36
+ contexts.set(scopeId, {
37
+ channelKey: channelKey(message),
38
+ openerId: message.$sender.id,
39
+ message,
40
+ expiresAt: Date.now() + TTL_MS,
41
+ });
42
+ pruneExpired();
43
+ return scopeId;
44
+ }
45
+
46
+ export function rememberHubMenu(
47
+ message: Message<any>,
48
+ scopeId: string,
49
+ choices: HubMenuChoice[],
50
+ ): void {
51
+ lastMenus.set(hubMenuKey(message), {
52
+ scopeId,
53
+ choices,
54
+ expiresAt: Date.now() + TTL_MS,
55
+ });
56
+ pruneExpired();
57
+ }
58
+
59
+ export function getLastHubMenu(message: Message<any>): LastHubMenu | null {
60
+ pruneExpired();
61
+ const key = hubMenuKey(message);
62
+ const last = lastMenus.get(key);
63
+ if (!last || last.expiresAt < Date.now()) {
64
+ lastMenus.delete(key);
65
+ return null;
66
+ }
67
+ return last;
68
+ }
69
+
70
+ export function getHubContext(scopeId: string): HubMenuContext | null {
71
+ pruneExpired();
72
+ const ctx = contexts.get(scopeId);
73
+ if (!ctx || ctx.expiresAt < Date.now()) {
74
+ contexts.delete(scopeId);
75
+ return null;
76
+ }
77
+ return ctx;
78
+ }
79
+
80
+ function pruneExpired(): void {
81
+ const now = Date.now();
82
+ for (const [id, ctx] of contexts) {
83
+ if (ctx.expiresAt < now) contexts.delete(id);
84
+ }
85
+ for (const [key, menu] of lastMenus) {
86
+ if (menu.expiresAt < now) lastMenus.delete(key);
87
+ }
88
+ }
89
+
90
+ /** 测试专用:清空菜单上下文 */
91
+ export function resetHubMenuContextForTests(): void {
92
+ contexts.clear();
93
+ lastMenus.clear();
94
+ }
@@ -0,0 +1,223 @@
1
+ import { segment, type SendContent } from '@zhin.js/core';
2
+
3
+ import { buildChoiceKeyboard } from './choice-keyboard.js';
4
+ import { slashCommandPrefix } from './game-commands.js';
5
+ import type { RegisteredGame } from './game-hub-feature.js';
6
+ import {
7
+ formatHubOpenCommandLines,
8
+ formatSupplementaryCommandsHelp,
9
+ type CommandHelpSource,
10
+ } from './command-help.js';
11
+ import {
12
+ buildBotHelpHtml,
13
+ buildHubHelpHtml,
14
+ HELP_CARD_CANVAS,
15
+ HELP_CARD_WIDTH,
16
+ } from './help-card.js';
17
+
18
+ export const HUB_PREFIX = 'hub';
19
+
20
+ export function hubGameChoiceId(gameId: string): string {
21
+ return `g_${gameId}`;
22
+ }
23
+
24
+ export function hubActionChoiceId(gameId: string, actionId: string): string {
25
+ return `a_${gameId}_${actionId}`;
26
+ }
27
+
28
+ export function parseHubChoiceId(
29
+ choiceId: string,
30
+ ):
31
+ | { kind: 'game'; gameId: string }
32
+ | { kind: 'action'; gameId: string; actionId: string }
33
+ | { kind: 'back' }
34
+ | null {
35
+ if (choiceId === 'back') return { kind: 'back' };
36
+ if (choiceId.startsWith('g_')) return { kind: 'game', gameId: choiceId.slice(2) };
37
+ if (choiceId.startsWith('a_')) {
38
+ const rest = choiceId.slice(2);
39
+ const sep = rest.indexOf('_');
40
+ if (sep <= 0) return null;
41
+ return {
42
+ kind: 'action',
43
+ gameId: rest.slice(0, sep),
44
+ actionId: rest.slice(sep + 1),
45
+ };
46
+ }
47
+ return null;
48
+ }
49
+
50
+ export function buildMainHubMenu(
51
+ scopeId: string,
52
+ games: readonly RegisteredGame[],
53
+ channelType: string,
54
+ ): SendContent {
55
+ const cmdHint = formatCommandPrefixHint(games);
56
+ const lines = [
57
+ '🎮 **游戏大厅**',
58
+ '',
59
+ cmdHint,
60
+ '',
61
+ '_同频道均可浏览;开局后仅对局玩家可操作棋盘。_',
62
+ '',
63
+ ];
64
+ for (const g of games) {
65
+ lines.push(`• ${g.icon} **${g.title}** — ${g.description}`);
66
+ }
67
+
68
+ return buildChoiceKeyboard({
69
+ gamePrefix: HUB_PREFIX,
70
+ sessionId: scopeId,
71
+ narrative: lines.join('\n'),
72
+ choices: games.map((g) => ({
73
+ id: hubGameChoiceId(g.id),
74
+ label: `${g.icon} ${g.title}`,
75
+ style: 'primary' as const,
76
+ })),
77
+ buttonsPerRow: 2,
78
+ fallbackHint: '回复数字进入对应游戏',
79
+ interactionProfile: 'menu',
80
+ channelType,
81
+ });
82
+ }
83
+
84
+ export function buildGameHubMenu(
85
+ scopeId: string,
86
+ game: RegisteredGame,
87
+ channelType: string,
88
+ ): SendContent {
89
+ const isGroup = channelType !== 'private';
90
+ const menus = game.menus.filter((m) => {
91
+ if (m.groupOnly && !isGroup) return false;
92
+ if (m.privateOnly && isGroup) return false;
93
+ return true;
94
+ });
95
+
96
+ return buildChoiceKeyboard({
97
+ gamePrefix: HUB_PREFIX,
98
+ sessionId: scopeId,
99
+ narrative: `${game.icon} **${game.title}**\n\n${game.description}\n\n请选择:`,
100
+ choices: [
101
+ ...menus.map((m) => ({
102
+ id: hubActionChoiceId(game.id, m.id),
103
+ label: m.label,
104
+ style: m.style,
105
+ })),
106
+ { id: 'back', label: '↩️ 返回大厅' },
107
+ ],
108
+ buttonsPerRow: 2,
109
+ fallbackHint: '回复数字选择操作',
110
+ interactionProfile: 'menu',
111
+ channelType,
112
+ });
113
+ }
114
+
115
+ /** 大厅主菜单:命令前缀提示 */
116
+ export function formatCommandPrefixHint(games: readonly RegisteredGame[]): string {
117
+ if (!games.length) return '暂无可用游戏。';
118
+ const sample = games.slice(0, 5).map((g) => slashCommandPrefix(g.commandPrefix.replace(/^\//, '')));
119
+ const tail = games.length > sample.length ? '等' : '';
120
+ return `选择想玩的游戏(也可直接发送斜杠命令,如「${sample.join('」「')}」${tail}):`;
121
+ }
122
+
123
+ /** 游戏大厅帮助文案(随 registerGame 动态生成) */
124
+ export function formatHubHelp(
125
+ games: readonly RegisteredGame[],
126
+ commands?: readonly CommandHelpSource[],
127
+ ): string {
128
+ const lines = [
129
+ '🎮 **游戏大厅**',
130
+ '',
131
+ ...formatHubOpenCommandLines(commands),
132
+ ];
133
+
134
+ if (!games.length) {
135
+ lines.push('', '当前暂无已注册游戏。', '请安装任意游戏插件(registerGame 会自动挂载大厅)。');
136
+ return lines.join('\n');
137
+ }
138
+
139
+ lines.push('', `当前已注册 **${games.length}** 款游戏:`, '');
140
+
141
+ for (const g of games) {
142
+ const quick = g.quickStart ?? '开始';
143
+ const alias = g.aliases?.length ? ` · ${g.aliases.join(' / ')}` : '';
144
+ lines.push(`• ${g.icon} **${g.title}**${alias}`);
145
+ lines.push(` 命令:\`${slashCommandPrefix(g.commandPrefix.replace(/^\//, ''))} ${quick}\` — ${g.description}`);
146
+ }
147
+
148
+ return lines.join('\n');
149
+ }
150
+
151
+ /** 顶层 /帮助:游戏大厅 + 已注册命令自动汇总 */
152
+ export function formatBotHelp(
153
+ games: readonly RegisteredGame[],
154
+ options?: {
155
+ channelType?: string;
156
+ commands?: readonly CommandHelpSource[];
157
+ },
158
+ ): string {
159
+ const commands = options?.commands;
160
+ const sections = [formatHubHelp(games, commands)];
161
+
162
+ const other = commands?.length
163
+ ? formatSupplementaryCommandsHelp(commands, games)
164
+ : '';
165
+ if (other) {
166
+ sections.push('', '---', '', other);
167
+ }
168
+
169
+ if (options?.channelType === 'group') {
170
+ sections.push(
171
+ '',
172
+ '**QQ 群用法**',
173
+ '• 公域群需 **@ 机器人** 后发送命令,或直接 **点击消息按钮**',
174
+ '• 发送 `/游戏` 打开大厅,同群成员均可点击按钮',
175
+ );
176
+ }
177
+ return sections.join('\n');
178
+ }
179
+
180
+ /** 顶层 /帮助:HTML 卡片(QQ 等出站 policy 为 image 时渲染为 PNG;无 renderer 时回退文本) */
181
+ export function buildBotHelpReply(
182
+ games: readonly RegisteredGame[],
183
+ options?: {
184
+ channelType?: string;
185
+ commands?: readonly CommandHelpSource[];
186
+ },
187
+ ): SendContent {
188
+ const text = formatBotHelp(games, options);
189
+ return segment.html({
190
+ html: buildBotHelpHtml({
191
+ games,
192
+ commands: options?.commands,
193
+ channelType: options?.channelType,
194
+ }),
195
+ text,
196
+ width: HELP_CARD_WIDTH,
197
+ backgroundColor: HELP_CARD_CANVAS,
198
+ fileName: 'bot-help.png',
199
+ });
200
+ }
201
+
202
+ /** /游戏 帮助:大厅专用卡片 */
203
+ export function buildHubHelpReply(
204
+ games: readonly RegisteredGame[],
205
+ commands?: readonly CommandHelpSource[],
206
+ ): SendContent {
207
+ const text = formatHubHelp(games, commands);
208
+ return segment.html({
209
+ html: buildHubHelpHtml(games, commands),
210
+ text,
211
+ width: HELP_CARD_WIDTH,
212
+ backgroundColor: HELP_CARD_CANVAS,
213
+ fileName: 'game-hub-help.png',
214
+ });
215
+ }
216
+
217
+ /** 无游戏时的提示 */
218
+ export function formatHubEmptyMessage(): string {
219
+ return [
220
+ '游戏大厅暂无可用游戏。',
221
+ '请安装任意游戏插件;首个游戏 registerGame 时会自动挂载大厅命令。',
222
+ ].join('');
223
+ }