@zhin.js/plugin-blackjack 0.0.0 → 0.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 (60) hide show
  1. package/commands/bj/[action:string=].ts +23 -0
  2. package/lib/bj-command.d.ts +4 -3
  3. package/lib/bj-command.js +4 -0
  4. package/lib/bj-command.js.map +1 -1
  5. package/lib/engine.d.ts +0 -1
  6. package/lib/engine.js +2 -5
  7. package/lib/engine.js.map +1 -1
  8. package/lib/game-flow.d.ts +5 -6
  9. package/lib/game-flow.js +20 -16
  10. package/lib/game-flow.js.map +1 -1
  11. package/lib/index.d.ts +8 -2
  12. package/lib/index.js +8 -25
  13. package/lib/index.js.map +1 -1
  14. package/lib/memory-db.d.ts +6 -0
  15. package/lib/memory-db.js +18 -0
  16. package/lib/memory-db.js.map +1 -0
  17. package/lib/models.d.ts +5 -4
  18. package/lib/models.js +2 -2
  19. package/lib/models.js.map +1 -1
  20. package/lib/runtime-store.d.ts +6 -0
  21. package/lib/runtime-store.js +7 -0
  22. package/lib/runtime-store.js.map +1 -0
  23. package/lib/session-service.d.ts +1 -3
  24. package/lib/session-service.js +1 -4
  25. package/lib/session-service.js.map +1 -1
  26. package/lib/view.d.ts +1 -2
  27. package/lib/view.js +1 -1
  28. package/lib/view.js.map +1 -1
  29. package/middlewares/bj-alias.ts +22 -0
  30. package/middlewares/bj-choice.ts +80 -0
  31. package/package.json +45 -10
  32. package/plugin.ts +62 -0
  33. package/schema.json +6 -0
  34. package/src/bj-command.ts +12 -3
  35. package/src/engine.ts +3 -5
  36. package/src/game-flow.ts +29 -26
  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/runtime-store.ts +9 -0
  41. package/src/session-service.ts +2 -5
  42. package/src/view.ts +2 -2
  43. package/lib/bj-command.d.ts.map +0 -1
  44. package/lib/commands.d.ts +0 -6
  45. package/lib/commands.d.ts.map +0 -1
  46. package/lib/commands.js +0 -98
  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 -3
  51. package/lib/hub-register.d.ts.map +0 -1
  52. package/lib/hub-register.js +0 -29
  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/session-service.d.ts.map +0 -1
  57. package/lib/view.d.ts.map +0 -1
  58. package/plugin.yml +0 -2
  59. package/src/commands.ts +0 -117
  60. package/src/hub-register.ts +0 -28
@@ -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;AAE9C,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;AAE7E,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,MAAM;QACd,WAAW,EAAE;YACX,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,SAAS,EAAE,MAAM,CAAC;YAClB,iBAAiB,EAAE,MAAM,CAAC;YAC1B,iBAAiB,EAAE,MAAM,CAAC;YAC1B,gBAAgB,EAAE,MAAM,CAAC;YACzB,MAAM,EAAE,eAAe,CAAC;YACxB,UAAU,EAAE,MAAM,CAAC;YACnB,UAAU,EAAE,MAAM,CAAC;SAC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ED,wBAAgB,cAAc,CAAC,EAAE,EAAE,UAAU,GAAG,cAAc,CAE7D;AAeD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,CAE5D;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,CAE3D"}
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;AAE3C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,wBAAgB,WAAW,CACzB,OAAO,EAAE,YAAY,EACrB,QAAQ,UAAQ,EAChB,YAAY,UAAQ,EACpB,WAAW,CAAC,EAAE,MAAM,GACnB,WAAW,CAqCb;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAIpE;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,YAAY,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CASpF;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAEzD"}
package/plugin.yml DELETED
@@ -1,2 +0,0 @@
1
- name: "@zhin.js/plugin-blackjack"
2
- description: "21 点 Blackjack"
package/src/commands.ts DELETED
@@ -1,117 +0,0 @@
1
- import { Message, MessageCommand, getActionFromMessage, type Plugin } from 'zhin.js';
2
- import {
3
- buildChoiceFallbackMap,
4
- channelKey,
5
- parseChoicePayload,
6
- registerGameTextMiddleware,
7
- resolveGameChoice,
8
- resolveGameTextPayload,
9
- } from '@zhin.js/game-shared';
10
- import { BJ_PREFIX, handleChoice } from './game-flow.js';
11
- import { runBjCommand } from './bj-command.js';
12
- import type { SessionService } from './session-service.js';
13
-
14
- const VALID_CHOICES = ['hit', 'stand', 'restart'] as const;
15
-
16
- function registerPattern(
17
- plugin: Plugin,
18
- pattern: string,
19
- desc: string,
20
- getServices: () => SessionService | null,
21
- ): void {
22
- plugin.addCommand(
23
- new MessageCommand(pattern)
24
- .desc(desc)
25
- .action(async (message, result) => {
26
- const services = getServices();
27
- if (!services) return '21 点需要启用 database 配置。';
28
- const raw = (result.params.action as string | undefined) ?? '';
29
- return runBjCommand(plugin, services, message, raw);
30
- }),
31
- );
32
- }
33
-
34
- export function registerCommands(plugin: Plugin, getServices: () => SessionService | null): void {
35
- registerPattern(plugin, '/21点 [action:word]', '21 点(中文)', getServices);
36
- registerPattern(plugin, '/bj [action:word]', 'Blackjack (English)', getServices);
37
- }
38
-
39
- async function resolveChoice(
40
- message: Message<any>,
41
- services: SessionService,
42
- ): Promise<{ sessionId: string; choiceId: string } | null> {
43
- return resolveGameChoice({
44
- message,
45
- gamePrefix: BJ_PREFIX,
46
- validChoiceIds: VALID_CHOICES,
47
- getById: (id) => services.getById(id),
48
- getActiveForUser: (ch, uid) => services.getActiveForUser(ch, uid),
49
- getByBoardMessageId: (mid) => services.getActiveByBoardMessageId(mid),
50
- });
51
- }
52
-
53
- export function registerInteractive(plugin: Plugin, getServices: () => SessionService | null): void {
54
- plugin.registerInteractiveHandler(`${BJ_PREFIX}:`, async (message) => {
55
- const services = getServices();
56
- if (!services) return false;
57
- const choice = await resolveChoice(message, services);
58
- if (!choice) return false;
59
- const err = await handleChoice(plugin, services, message, choice.sessionId, choice.choiceId);
60
- if (err) await message.$reply?.(err);
61
- return true;
62
- });
63
- }
64
-
65
- export function registerTextFallback(plugin: Plugin, getServices: () => SessionService | null): void {
66
- registerGameTextMiddleware(plugin, async (message, next) => {
67
- const services = getServices();
68
- if (!services) return next();
69
-
70
- const action = getActionFromMessage(message);
71
- if (action?.payload.startsWith(`${BJ_PREFIX}:`)) return next();
72
-
73
- const raw = message.$raw?.trim() ?? '';
74
- const ch = channelKey(message);
75
-
76
- const payloadFromText = resolveGameTextPayload(raw);
77
- if (payloadFromText?.startsWith(`${BJ_PREFIX}:`)) {
78
- const parsed = parseChoicePayload(payloadFromText, BJ_PREFIX);
79
- if (parsed) {
80
- const session = await services.getById(parsed.sessionId);
81
- if (session?.channel_key === ch) {
82
- const err = await handleChoice(plugin, services, message, parsed.sessionId, parsed.choiceId);
83
- if (err) await message.$reply?.(err);
84
- return;
85
- }
86
- }
87
- }
88
-
89
- const session = await services.getActiveForUser(ch, message.$sender.id);
90
- if (!session) return next();
91
-
92
- if (session.status !== 'active') {
93
- const map = buildChoiceFallbackMap(BJ_PREFIX, session.id, [
94
- { id: 'restart', label: '再来一局', keepEnabledWhenTerminal: true },
95
- ]);
96
- const payload = resolveGameTextPayload(raw, map);
97
- const parsed = payload ? parseChoicePayload(payload, BJ_PREFIX) : null;
98
- if (parsed?.choiceId === 'restart') {
99
- const err = await handleChoice(plugin, services, message, parsed.sessionId, 'restart');
100
- if (err) await message.$reply?.(err);
101
- return;
102
- }
103
- return next();
104
- }
105
-
106
- const map = buildChoiceFallbackMap(BJ_PREFIX, session.id, [
107
- { id: 'hit', label: '要牌' },
108
- { id: 'stand', label: '停牌' },
109
- ]);
110
- const payload = resolveGameTextPayload(raw, map);
111
- const parsed = payload ? parseChoicePayload(payload, BJ_PREFIX) : null;
112
- if (!parsed || parsed.sessionId !== session.id) return next();
113
-
114
- const err = await handleChoice(plugin, services, message, session.id, parsed.choiceId);
115
- if (err) await message.$reply?.(err);
116
- }, 'bj:text');
117
- }
@@ -1,28 +0,0 @@
1
- import { getPlugin } from 'zhin.js';
2
- import { ensureGameHubService } from '@zhin.js/game-shared';
3
- import { runBjCommand, BJ_HELP } from './bj-command.js';
4
- import type { SessionService } from './session-service.js';
5
-
6
- export function registerBjHub(getServices: () => SessionService | null): () => void {
7
- const plugin = getPlugin();
8
- ensureGameHubService(plugin);
9
- return plugin.registerGame({
10
- id: 'blackjack',
11
- title: '21 点',
12
- icon: '🃏',
13
- description: '经典 Blackjack,要牌或停牌挑战庄家',
14
- commandPrefix: '/21点',
15
- quickStart: '开始',
16
- aliases: ['bj', '21点'],
17
- menus: [
18
- { id: 'start', label: '🎮 开始新局', style: 'primary' },
19
- { id: 'help', label: '📖 玩法说明' },
20
- ],
21
- runAction: async (actionId, ctx) => {
22
- const services = getServices();
23
- if (!services) return '21 点需要启用 database 配置。';
24
- if (actionId === 'help') return BJ_HELP;
25
- return runBjCommand(plugin, services, ctx.message, actionId === 'start' ? '开始' : actionId);
26
- },
27
- });
28
- }