@zhin.js/plugin-idiom-chain 1.0.0 → 1.0.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.
Files changed (67) hide show
  1. package/README.md +31 -0
  2. package/commands/chain/[action:string=].ts +15 -0
  3. package/lib/chain-command.d.ts +4 -3
  4. package/lib/chain-command.js +5 -1
  5. package/lib/chain-command.js.map +1 -1
  6. package/lib/engine.d.ts +0 -1
  7. package/lib/game-flow.d.ts +6 -7
  8. package/lib/game-flow.js +27 -22
  9. package/lib/game-flow.js.map +1 -1
  10. package/lib/idiom-provider.d.ts +0 -1
  11. package/lib/idiom-provider.js +3 -2
  12. package/lib/idiom-provider.js.map +1 -1
  13. package/lib/index.d.ts +8 -2
  14. package/lib/index.js +8 -25
  15. package/lib/index.js.map +1 -1
  16. package/lib/memory-db.d.ts +6 -0
  17. package/lib/memory-db.js +18 -0
  18. package/lib/memory-db.js.map +1 -0
  19. package/lib/models.d.ts +5 -4
  20. package/lib/models.js +2 -2
  21. package/lib/models.js.map +1 -1
  22. package/lib/runtime-store.d.ts +6 -0
  23. package/lib/runtime-store.js +7 -0
  24. package/lib/runtime-store.js.map +1 -0
  25. package/lib/session-service.d.ts +1 -3
  26. package/lib/session-service.js +3 -9
  27. package/lib/session-service.js.map +1 -1
  28. package/lib/view.d.ts +2 -3
  29. package/lib/view.js +5 -3
  30. package/lib/view.js.map +1 -1
  31. package/middlewares/chain-alias.ts +18 -0
  32. package/middlewares/chain-text.ts +75 -0
  33. package/package.json +45 -10
  34. package/plugin.ts +64 -0
  35. package/schema.json +6 -0
  36. package/src/chain-command.ts +14 -5
  37. package/src/game-flow.ts +33 -29
  38. package/src/idiom-provider.ts +3 -2
  39. package/src/index.ts +9 -32
  40. package/src/memory-db.ts +22 -0
  41. package/src/models.ts +7 -4
  42. package/src/runtime-store.ts +9 -0
  43. package/src/session-service.ts +4 -9
  44. package/src/view.ts +6 -3
  45. package/lib/chain-command.d.ts.map +0 -1
  46. package/lib/commands.d.ts +0 -6
  47. package/lib/commands.d.ts.map +0 -1
  48. package/lib/commands.js +0 -94
  49. package/lib/commands.js.map +0 -1
  50. package/lib/data/idioms.d.ts +0 -8
  51. package/lib/data/idioms.d.ts.map +0 -1
  52. package/lib/data/idioms.js +0 -422
  53. package/lib/data/idioms.js.map +0 -1
  54. package/lib/engine.d.ts.map +0 -1
  55. package/lib/game-flow.d.ts.map +0 -1
  56. package/lib/hub-register.d.ts +0 -5
  57. package/lib/hub-register.d.ts.map +0 -1
  58. package/lib/hub-register.js +0 -31
  59. package/lib/hub-register.js.map +0 -1
  60. package/lib/idiom-provider.d.ts.map +0 -1
  61. package/lib/index.d.ts.map +0 -1
  62. package/lib/models.d.ts.map +0 -1
  63. package/lib/session-service.d.ts.map +0 -1
  64. package/lib/view.d.ts.map +0 -1
  65. package/plugin.yml +0 -2
  66. package/src/commands.ts +0 -112
  67. package/src/hub-register.ts +0 -32
@@ -1,31 +0,0 @@
1
- import { getPlugin } from 'zhin.js';
2
- import { ensureGameHubService } from '@zhin.js/game-shared';
3
- import { idiomCount } from './engine.js';
4
- import { runChainCommand, CHAIN_HELP } from './chain-command.js';
5
- export function registerChainHub(getServices) {
6
- const plugin = getPlugin();
7
- ensureGameHubService(plugin);
8
- return plugin.registerGame({
9
- id: 'chain',
10
- title: '成语接龙',
11
- icon: '📜',
12
- description: `四字成语接龙(同音/同字),词库 ${idiomCount()} 条`,
13
- commandPrefix: '接龙',
14
- quickStart: 'start_pinyin',
15
- aliases: ['chain'],
16
- menus: [
17
- { id: 'start_pinyin', label: '🎮 同音接龙', style: 'primary' },
18
- { id: 'start_char', label: '📝 同字接龙' },
19
- { id: 'continue', label: '🔄 继续' },
20
- { id: 'help', label: '📖 玩法说明' },
21
- ],
22
- runAction: async (actionId, ctx) => {
23
- const services = getServices();
24
- if (!services)
25
- return '成语接龙需要启用 database 配置。';
26
- return runChainCommand(ctx.plugin, services, ctx.message, actionId);
27
- },
28
- });
29
- }
30
- export { CHAIN_HELP };
31
- //# sourceMappingURL=hub-register.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hub-register.js","sourceRoot":"","sources":["../src/hub-register.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGjE,MAAM,UAAU,gBAAgB,CAAC,WAAwC;IACvE,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAC7B,OAAO,MAAM,CAAC,YAAY,CAAC;QACzB,EAAE,EAAE,OAAO;QACX,KAAK,EAAE,MAAM;QACb,IAAI,EAAE,IAAI;QACV,WAAW,EAAE,oBAAoB,UAAU,EAAE,IAAI;QACjD,aAAa,EAAE,IAAI;QACnB,UAAU,EAAE,cAAc;QAC1B,OAAO,EAAE,CAAC,OAAO,CAAC;QAClB,KAAK,EAAE;YACL,EAAE,EAAE,EAAE,cAAc,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE;YAC1D,EAAE,EAAE,EAAE,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE;YACtC,EAAE,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,EAAE;YAClC,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE;SACjC;QACD,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,EAAE;YACjC,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAC;YAC/B,IAAI,CAAC,QAAQ;gBAAE,OAAO,uBAAuB,CAAC;YAC9C,OAAO,eAAe,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,GAAG,CAAC,OAAO,EAA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,CAAC,GAChB,MAAM,GAAG,IAAI,CAEf;AAED,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,SAAS,EACf,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAChB,cAAc,CAyBhB;AAED,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,MAAM,CAOrE"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../src/models.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,MAAM,kBAAkB,GAAG,QAAQ,GAAG,KAAK,GAAG,MAAM,GAAG,SAAS,CAAC;AAEvE,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,MAAM;QACd,oBAAoB,EAAE;YACpB,EAAE,EAAE,MAAM,CAAC;YACX,OAAO,EAAE,MAAM,CAAC;YAChB,QAAQ,EAAE,MAAM,CAAC;YACjB,YAAY,EAAE,MAAM,CAAC;YACrB,UAAU,EAAE,MAAM,CAAC;YACnB,WAAW,EAAE,MAAM,CAAC;YACpB,SAAS,EAAE,MAAM,CAAC;YAClB,WAAW,EAAE,MAAM,CAAC;YACpB,UAAU,EAAE,MAAM,CAAC;YACnB,SAAS,EAAE,MAAM,CAAC;YAClB,UAAU,EAAE,SAAS,CAAC;YACtB,WAAW,EAAE,MAAM,CAAC;YACpB,YAAY,EAAE,MAAM,CAAC;YACrB,SAAS,EAAE,MAAM,C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O,CAAC,eAAe,GAAG,IAAI,CAAC;IAMlF,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAIpD,yBAAyB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAW7E,aAAa,CACjB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,EACrB,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,SAAS,EAAE,SAAS,CAAA;KAAE,GAChF,OAAO,CAAC,eAAe,CAAC;IA+BrB,aAAa,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzE,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAalD;AAED,wBAAgB,cAAc,CAAC,EAAE,EAAE,aAAa,GAAG,cAAc,CAEhE;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,eAAe,GAAG,aAAa,CAE3E"}
package/lib/view.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"view.d.ts","sourceRoot":"","sources":["../src/view.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGnD,QAAA,MAAM,SAAS,IAAI,CAAC;AAMpB,wBAAgB,cAAc,CAC5B,OAAO,EAAE,eAAe,EACxB,UAAU,GAAE,MAAM,EAAO,GACxB,WAAW,CAgDb;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
package/plugin.yml DELETED
@@ -1,2 +0,0 @@
1
- name: "@zhin.js/plugin-idiom-chain"
2
- description: "成语接龙"
package/src/commands.ts DELETED
@@ -1,112 +0,0 @@
1
- import { Message, MessageCommand, getActionFromMessage, type Plugin } from 'zhin.js';
2
- import {
3
- buildChoiceFallbackMap,
4
- channelKey,
5
- normalizeChainAction,
6
- parseChoicePayload,
7
- registerGameTextMiddleware,
8
- } from '@zhin.js/game-shared';
9
- import { CHAIN_PREFIX, handleChoice, processIdiomText } from './game-flow.js';
10
- import { runChainCommand } from './chain-command.js';
11
- import type { SessionService } from './session-service.js';
12
-
13
- function registerPattern(
14
- plugin: Plugin,
15
- pattern: string,
16
- desc: string,
17
- getServices: () => SessionService | null,
18
- ): void {
19
- plugin.addCommand(
20
- new MessageCommand(pattern)
21
- .desc(desc)
22
- .action(async (message, result) => {
23
- const services = getServices();
24
- if (!services) return '成语接龙需要启用 database 配置。';
25
- const raw = (result.params.action as string | undefined) ?? '';
26
- return runChainCommand(plugin, services, message, normalizeChainAction(raw));
27
- }),
28
- );
29
- }
30
-
31
- export function registerCommands(plugin: Plugin, getServices: () => SessionService | null): void {
32
- registerPattern(plugin, 'chain [action:word]', '成语接龙(chain)', getServices);
33
- registerPattern(plugin, '接龙 [action:word]', '成语接龙(中文)', getServices);
34
- }
35
-
36
- async function resolveChoice(
37
- message: Message<any>,
38
- services: SessionService,
39
- ): Promise<{ sessionId: string; choiceId: string } | null> {
40
- const action = getActionFromMessage(message);
41
- if (!action) return null;
42
-
43
- const parsed = parseChoicePayload(action.payload, CHAIN_PREFIX);
44
- if (parsed) return { sessionId: parsed.sessionId, choiceId: parsed.choiceId };
45
-
46
- const choiceId = action.id || action.payload;
47
- if (!choiceId) return null;
48
-
49
- const ch = channelKey(message);
50
- const session =
51
- await services.getActiveForUser(ch, message.$sender.id)
52
- ?? (action.sourceMessageId
53
- ? await services.getActiveByBoardMessageId(action.sourceMessageId)
54
- : null);
55
- if (!session || session.channel_key !== ch) return null;
56
-
57
- const valid = ['hint', 'skip', 'quit', 'restart'];
58
- if (!valid.includes(choiceId)) return null;
59
- return { sessionId: session.id, choiceId };
60
- }
61
-
62
- export function registerInteractive(plugin: Plugin, getServices: () => SessionService | null): void {
63
- plugin.registerInteractiveHandler(`${CHAIN_PREFIX}:`, async (message) => {
64
- const services = getServices();
65
- if (!services) return false;
66
- const choice = await resolveChoice(message, services);
67
- if (!choice) return false;
68
- const err = await handleChoice(plugin, services, message, choice.sessionId, choice.choiceId);
69
- if (err) await message.$reply?.(err);
70
- return true;
71
- });
72
- }
73
-
74
- export function registerTextMiddleware(plugin: Plugin, getServices: () => SessionService | null): void {
75
- registerGameTextMiddleware(plugin, async (message, next) => {
76
- const services = getServices();
77
- if (!services) return next();
78
-
79
- const action = getActionFromMessage(message);
80
- if (action?.payload.startsWith(`${CHAIN_PREFIX}:`)) return next();
81
-
82
- const ch = channelKey(message);
83
- const session = await services.getActiveForUser(ch, message.$sender.id);
84
- if (!session) return next();
85
-
86
- const raw = message.$raw?.trim() ?? '';
87
- if (!raw) return next();
88
-
89
- const n = /^(\d+)$/.exec(raw);
90
- if (n && session.status === 'active') {
91
- const map = buildChoiceFallbackMap(CHAIN_PREFIX, session.id, [
92
- { id: 'hint', label: '提示' },
93
- { id: 'skip', label: '跳过' },
94
- { id: 'quit', label: '认输' },
95
- ]);
96
- const payload = map[n[1]!];
97
- const parsed = payload ? parseChoicePayload(payload, CHAIN_PREFIX) : null;
98
- if (parsed?.sessionId === session.id) {
99
- const err = await handleChoice(plugin, services, message, session.id, parsed.choiceId);
100
- if (err) await message.$reply?.(err);
101
- return;
102
- }
103
- }
104
-
105
- if (/^[\u4e00-\u9fff]{2,8}$/.test(raw)) {
106
- await processIdiomText(plugin, services, message, raw);
107
- return;
108
- }
109
-
110
- return next();
111
- }, 'idiom-chain:text');
112
- }
@@ -1,32 +0,0 @@
1
- import { getPlugin } from 'zhin.js';
2
- import { ensureGameHubService } from '@zhin.js/game-shared';
3
- import { idiomCount } from './engine.js';
4
- import { runChainCommand, CHAIN_HELP } from './chain-command.js';
5
- import type { SessionService } from './session-service.js';
6
-
7
- export function registerChainHub(getServices: () => SessionService | null): () => void {
8
- const plugin = getPlugin();
9
- ensureGameHubService(plugin);
10
- return plugin.registerGame({
11
- id: 'chain',
12
- title: '成语接龙',
13
- icon: '📜',
14
- description: `四字成语接龙(同音/同字),词库 ${idiomCount()} 条`,
15
- commandPrefix: '接龙',
16
- quickStart: 'start_pinyin',
17
- aliases: ['chain'],
18
- menus: [
19
- { id: 'start_pinyin', label: '🎮 同音接龙', style: 'primary' },
20
- { id: 'start_char', label: '📝 同字接龙' },
21
- { id: 'continue', label: '🔄 继续' },
22
- { id: 'help', label: '📖 玩法说明' },
23
- ],
24
- runAction: async (actionId, ctx) => {
25
- const services = getServices();
26
- if (!services) return '成语接龙需要启用 database 配置。';
27
- return runChainCommand(ctx.plugin, services, ctx.message, actionId);
28
- },
29
- });
30
- }
31
-
32
- export { CHAIN_HELP };