@zhin.js/plugin-rps 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 (61) hide show
  1. package/README.md +31 -0
  2. package/commands/rps/[action:string=].ts +15 -0
  3. package/lib/engine.d.ts +0 -1
  4. package/lib/engine.js +2 -1
  5. package/lib/engine.js.map +1 -1
  6. package/lib/game-flow.d.ts +6 -7
  7. package/lib/game-flow.js +18 -16
  8. package/lib/game-flow.js.map +1 -1
  9. package/lib/index.d.ts +8 -2
  10. package/lib/index.js +8 -25
  11. package/lib/index.js.map +1 -1
  12. package/lib/memory-db.d.ts +6 -0
  13. package/lib/memory-db.js +18 -0
  14. package/lib/memory-db.js.map +1 -0
  15. package/lib/models.d.ts +5 -4
  16. package/lib/models.js +2 -2
  17. package/lib/models.js.map +1 -1
  18. package/lib/rps-command.d.ts +4 -3
  19. package/lib/rps-command.js +5 -1
  20. package/lib/rps-command.js.map +1 -1
  21. package/lib/runtime-store.d.ts +6 -0
  22. package/lib/runtime-store.js +7 -0
  23. package/lib/runtime-store.js.map +1 -0
  24. package/lib/session-service.d.ts +1 -3
  25. package/lib/session-service.js +3 -12
  26. package/lib/session-service.js.map +1 -1
  27. package/lib/view.d.ts +2 -3
  28. package/lib/view.js +5 -3
  29. package/lib/view.js.map +1 -1
  30. package/middlewares/rps-alias.ts +18 -0
  31. package/middlewares/rps-choice.ts +66 -0
  32. package/package.json +45 -10
  33. package/plugin.ts +63 -0
  34. package/schema.json +6 -0
  35. package/src/engine.ts +3 -1
  36. package/src/game-flow.ts +21 -22
  37. package/src/index.ts +9 -36
  38. package/src/memory-db.ts +22 -0
  39. package/src/models.ts +7 -4
  40. package/src/rps-command.ts +12 -3
  41. package/src/runtime-store.ts +9 -0
  42. package/src/session-service.ts +4 -11
  43. package/src/view.ts +6 -3
  44. package/lib/commands.d.ts +0 -6
  45. package/lib/commands.d.ts.map +0 -1
  46. package/lib/commands.js +0 -86
  47. package/lib/commands.js.map +0 -1
  48. package/lib/engine.d.ts.map +0 -1
  49. package/lib/game-flow.d.ts.map +0 -1
  50. package/lib/hub-register.d.ts +0 -5
  51. package/lib/hub-register.d.ts.map +0 -1
  52. package/lib/hub-register.js +0 -26
  53. package/lib/hub-register.js.map +0 -1
  54. package/lib/index.d.ts.map +0 -1
  55. package/lib/models.d.ts.map +0 -1
  56. package/lib/rps-command.d.ts.map +0 -1
  57. package/lib/session-service.d.ts.map +0 -1
  58. package/lib/view.d.ts.map +0 -1
  59. package/plugin.yml +0 -2
  60. package/src/commands.ts +0 -102
  61. package/src/hub-register.ts +0 -30
package/src/commands.ts DELETED
@@ -1,102 +0,0 @@
1
- import { Message, MessageCommand, getActionFromMessage, type Plugin } from 'zhin.js';
2
- import {
3
- buildChoiceFallbackMap,
4
- channelKey,
5
- normalizeRpsAction,
6
- parseChoicePayload,
7
- registerGameTextMiddleware,
8
- } from '@zhin.js/game-shared';
9
- import { handleChoice, RPS_PREFIX } from './game-flow.js';
10
- import { runRpsCommand } from './rps-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 runRpsCommand(plugin, services, message, normalizeRpsAction(raw));
27
- }),
28
- );
29
- }
30
-
31
- export function registerCommands(plugin: Plugin, getServices: () => SessionService | null): void {
32
- registerPattern(plugin, 'rps [action:word]', '猜拳(rps)', 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, RPS_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 = ['rock', 'paper', 'scissors', '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(`${RPS_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 registerTextFallback(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(`${RPS_PREFIX}:`)) return next();
81
-
82
- const ch = channelKey(message);
83
- const session = await services.getActiveForUser(ch, message.$sender.id);
84
- if (!session || session.status !== 'active') return next();
85
-
86
- const raw = message.$raw?.trim() ?? '';
87
- const n = /^(\d+)$/.exec(raw);
88
- if (!n) return next();
89
-
90
- const map = buildChoiceFallbackMap(RPS_PREFIX, session.id, [
91
- { id: 'rock', label: '石头' },
92
- { id: 'paper', label: '布' },
93
- { id: 'scissors', label: '剪刀' },
94
- ]);
95
- const payload = map[n[1]!];
96
- const parsed = payload ? parseChoicePayload(payload, RPS_PREFIX) : null;
97
- if (!parsed || parsed.sessionId !== session.id) return next();
98
-
99
- const err = await handleChoice(plugin, services, message, session.id, parsed.choiceId);
100
- if (err) await message.$reply?.(err);
101
- }, 'rps:text');
102
- }
@@ -1,30 +0,0 @@
1
- import { getPlugin } from 'zhin.js';
2
- import { ensureGameHubService } from '@zhin.js/game-shared';
3
- import { runRpsCommand, RPS_HELP } from './rps-command.js';
4
- import type { SessionService } from './session-service.js';
5
-
6
- export function registerRpsHub(getServices: () => SessionService | null): () => void {
7
- const plugin = getPlugin();
8
- ensureGameHubService(plugin);
9
- return plugin.registerGame({
10
- id: 'rps',
11
- title: '猜拳对决',
12
- icon: '✊',
13
- description: '石头剪刀布,三局两胜',
14
- commandPrefix: '猜拳',
15
- quickStart: '开始',
16
- aliases: ['rps'],
17
- menus: [
18
- { id: 'start', label: '🎮 开始对局', style: 'primary' },
19
- { id: 'continue', label: '🔄 继续' },
20
- { id: 'help', label: '📖 玩法说明' },
21
- ],
22
- runAction: async (actionId, ctx) => {
23
- const services = getServices();
24
- if (!services) return '猜拳需要启用 database 配置。';
25
- return runRpsCommand(ctx.plugin, services, ctx.message, actionId);
26
- },
27
- });
28
- }
29
-
30
- export { RPS_HELP };