@zhin.js/plugin-dice-duel 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.
- package/README.md +31 -0
- package/commands/dice/[action:string=].ts +15 -0
- package/lib/dice-command.d.ts +4 -3
- package/lib/dice-command.js +5 -1
- package/lib/dice-command.js.map +1 -1
- package/lib/engine.d.ts +0 -1
- package/lib/engine.js +2 -1
- package/lib/engine.js.map +1 -1
- package/lib/game-flow.d.ts +6 -7
- package/lib/game-flow.js +18 -18
- package/lib/game-flow.js.map +1 -1
- package/lib/index.d.ts +8 -2
- package/lib/index.js +8 -25
- package/lib/index.js.map +1 -1
- package/lib/memory-db.d.ts +6 -0
- package/lib/memory-db.js +18 -0
- package/lib/memory-db.js.map +1 -0
- package/lib/models.d.ts +5 -4
- package/lib/models.js +2 -2
- package/lib/models.js.map +1 -1
- package/lib/runtime-store.d.ts +6 -0
- package/lib/runtime-store.js +7 -0
- package/lib/runtime-store.js.map +1 -0
- package/lib/session-service.d.ts +1 -3
- package/lib/session-service.js +3 -9
- package/lib/session-service.js.map +1 -1
- package/lib/view.d.ts +2 -3
- package/lib/view.js +5 -3
- package/lib/view.js.map +1 -1
- package/middlewares/dice-alias.ts +18 -0
- package/middlewares/dice-choice.ts +62 -0
- package/package.json +45 -10
- package/plugin.ts +63 -0
- package/schema.json +6 -0
- package/src/dice-command.ts +12 -3
- package/src/engine.ts +3 -1
- package/src/game-flow.ts +21 -24
- package/src/index.ts +9 -36
- package/src/memory-db.ts +22 -0
- package/src/models.ts +7 -4
- package/src/runtime-store.ts +9 -0
- package/src/session-service.ts +4 -9
- package/src/view.ts +6 -3
- package/lib/commands.d.ts +0 -6
- package/lib/commands.d.ts.map +0 -1
- package/lib/commands.js +0 -82
- package/lib/commands.js.map +0 -1
- package/lib/dice-command.d.ts.map +0 -1
- package/lib/engine.d.ts.map +0 -1
- package/lib/game-flow.d.ts.map +0 -1
- package/lib/hub-register.d.ts +0 -5
- package/lib/hub-register.d.ts.map +0 -1
- package/lib/hub-register.js +0 -26
- package/lib/hub-register.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/models.d.ts.map +0 -1
- package/lib/session-service.d.ts.map +0 -1
- package/lib/view.d.ts.map +0 -1
- package/plugin.yml +0 -2
- package/src/commands.ts +0 -98
- package/src/hub-register.ts +0 -30
package/src/commands.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { Message, MessageCommand, getActionFromMessage, type Plugin } from 'zhin.js';
|
|
2
|
-
import {
|
|
3
|
-
buildChoiceFallbackMap,
|
|
4
|
-
channelKey,
|
|
5
|
-
normalizeDiceAction,
|
|
6
|
-
parseChoicePayload,
|
|
7
|
-
registerGameTextMiddleware,
|
|
8
|
-
} from '@zhin.js/game-shared';
|
|
9
|
-
import { DICE_PREFIX, handleChoice } from './game-flow.js';
|
|
10
|
-
import { runDiceCommand } from './dice-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 runDiceCommand(plugin, services, message, normalizeDiceAction(raw));
|
|
27
|
-
}),
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function registerCommands(plugin: Plugin, getServices: () => SessionService | null): void {
|
|
32
|
-
registerPattern(plugin, 'dice [action:word]', '骰子对决(dice)', 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, DICE_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
|
-
if (!['roll', 'restart'].includes(choiceId)) return null;
|
|
58
|
-
return { sessionId: session.id, choiceId };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function registerInteractive(plugin: Plugin, getServices: () => SessionService | null): void {
|
|
62
|
-
plugin.registerInteractiveHandler(`${DICE_PREFIX}:`, async (message) => {
|
|
63
|
-
const services = getServices();
|
|
64
|
-
if (!services) return false;
|
|
65
|
-
const choice = await resolveChoice(message, services);
|
|
66
|
-
if (!choice) return false;
|
|
67
|
-
const err = await handleChoice(plugin, services, message, choice.sessionId, choice.choiceId);
|
|
68
|
-
if (err) await message.$reply?.(err);
|
|
69
|
-
return true;
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function registerTextFallback(plugin: Plugin, getServices: () => SessionService | null): void {
|
|
74
|
-
registerGameTextMiddleware(plugin, async (message, next) => {
|
|
75
|
-
const services = getServices();
|
|
76
|
-
if (!services) return next();
|
|
77
|
-
|
|
78
|
-
const action = getActionFromMessage(message);
|
|
79
|
-
if (action?.payload.startsWith(`${DICE_PREFIX}:`)) return next();
|
|
80
|
-
|
|
81
|
-
const ch = channelKey(message);
|
|
82
|
-
const session = await services.getActiveForUser(ch, message.$sender.id);
|
|
83
|
-
if (!session || session.status !== 'active') return next();
|
|
84
|
-
|
|
85
|
-
const raw = message.$raw?.trim() ?? '';
|
|
86
|
-
if (raw !== '1') return next();
|
|
87
|
-
|
|
88
|
-
const map = buildChoiceFallbackMap(DICE_PREFIX, session.id, [
|
|
89
|
-
{ id: 'roll', label: '掷骰' },
|
|
90
|
-
]);
|
|
91
|
-
const payload = map['1'];
|
|
92
|
-
const parsed = payload ? parseChoicePayload(payload, DICE_PREFIX) : null;
|
|
93
|
-
if (!parsed || parsed.sessionId !== session.id) return next();
|
|
94
|
-
|
|
95
|
-
const err = await handleChoice(plugin, services, message, session.id, 'roll');
|
|
96
|
-
if (err) await message.$reply?.(err);
|
|
97
|
-
}, 'dice:text');
|
|
98
|
-
}
|
package/src/hub-register.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { getPlugin } from 'zhin.js';
|
|
2
|
-
import { ensureGameHubService } from '@zhin.js/game-shared';
|
|
3
|
-
import { runDiceCommand, DICE_HELP } from './dice-command.js';
|
|
4
|
-
import type { SessionService } from './session-service.js';
|
|
5
|
-
|
|
6
|
-
export function registerDiceHub(getServices: () => SessionService | null): () => void {
|
|
7
|
-
const plugin = getPlugin();
|
|
8
|
-
ensureGameHubService(plugin);
|
|
9
|
-
return plugin.registerGame({
|
|
10
|
-
id: 'dice',
|
|
11
|
-
title: '骰子对决',
|
|
12
|
-
icon: '🎲',
|
|
13
|
-
description: '掷骰比大小,三局两胜',
|
|
14
|
-
commandPrefix: '骰子',
|
|
15
|
-
quickStart: '开始',
|
|
16
|
-
aliases: ['dice'],
|
|
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 runDiceCommand(ctx.plugin, services, ctx.message, actionId);
|
|
26
|
-
},
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export { DICE_HELP };
|