@zhin.js/plugin-word-riddle 1.0.2 → 1.0.4
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/commands/riddle/[action:string=].ts +2 -2
- package/lib/game-flow.d.ts +5 -7
- package/lib/game-flow.js +30 -49
- package/lib/game-flow.js.map +1 -1
- package/lib/riddle-command.d.ts +2 -4
- package/lib/riddle-command.js +3 -7
- package/lib/riddle-command.js.map +1 -1
- package/lib/session-service.d.ts +3 -2
- package/lib/session-service.js.map +1 -1
- package/middlewares/riddle-alias.ts +6 -3
- package/middlewares/riddle-text.ts +3 -3
- package/package.json +6 -6
- package/src/game-flow.ts +35 -56
- package/src/riddle-command.ts +5 -16
- package/src/session-service.ts +3 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineCommand } from '@zhin.js/command';
|
|
2
2
|
import { messageFromCommandInput, normalizeRiddleAction } from '@zhin.js/game-kit';
|
|
3
|
-
import { RIDDLE_HELP,
|
|
3
|
+
import { RIDDLE_HELP, runRiddleCommand } from '../../src/riddle-command.js';
|
|
4
4
|
import { resolveGameServices } from '../../src/runtime-store.js';
|
|
5
5
|
|
|
6
6
|
export default defineCommand({
|
|
@@ -10,6 +10,6 @@ export default defineCommand({
|
|
|
10
10
|
if (!action || action === 'help') return RIDDLE_HELP;
|
|
11
11
|
const services = resolveGameServices({ use, owner });
|
|
12
12
|
const message = messageFromCommandInput(input);
|
|
13
|
-
return
|
|
13
|
+
return runRiddleCommand(services, message, action);
|
|
14
14
|
},
|
|
15
15
|
});
|
package/lib/game-flow.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { type GameMessageLike } from '@zhin.js/game-kit';
|
|
2
2
|
import { RIDDLE_PREFIX } from './engine.js';
|
|
3
|
-
import type { RiddleSessionRow } from './models.js';
|
|
4
3
|
import type { RiddleType } from './riddles-catalog.js';
|
|
5
4
|
import { type SessionService } from './session-service.js';
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function
|
|
8
|
-
export declare function
|
|
9
|
-
export declare function
|
|
10
|
-
export declare function handleChoice(plugin: Plugin | null, services: SessionService, message: Message<any>, sessionId: string, choiceId: string): Promise<string | null>;
|
|
5
|
+
export declare function startGame(services: SessionService, message: GameMessageLike, mode: RiddleType): Promise<string | undefined>;
|
|
6
|
+
export declare function continueGame(services: SessionService, message: GameMessageLike): Promise<string>;
|
|
7
|
+
export declare function processAnswerText(services: SessionService, message: GameMessageLike, raw: string): Promise<string | null>;
|
|
8
|
+
export declare function handleChoice(services: SessionService, message: GameMessageLike, sessionId: string, choiceId: string): Promise<string | null>;
|
|
11
9
|
export { RIDDLE_PREFIX };
|
package/lib/game-flow.js
CHANGED
|
@@ -1,35 +1,15 @@
|
|
|
1
1
|
import { plainTextFromSendContent } from '@zhin.js/game-kit';
|
|
2
|
-
import { checkAnswer, getRiddleById, RIDDLE_PREFIX, typeLabel, } from './engine.js';
|
|
2
|
+
import { answersFor, checkAnswer, getRiddleById, normalizeAnswer, RIDDLE_PREFIX, typeLabel, } from './engine.js';
|
|
3
3
|
import { currentRiddleId, parseQueue, } from './session-service.js';
|
|
4
4
|
import { buildRiddleView, MAX_WRONG } from './view.js';
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Plugin Runtime: render the board as text. Interactive in-place board editing
|
|
7
|
+
* (the old Adapter.editMessage path) is not part of the runtime flow; commands
|
|
8
|
+
* and the text middleware return fresh text each turn.
|
|
9
|
+
*/
|
|
10
|
+
function renderView(message, session, eventLines = []) {
|
|
6
11
|
const content = buildRiddleView(session, eventLines, message.$channel.type);
|
|
7
|
-
|
|
8
|
-
if (!plugin)
|
|
9
|
-
return content;
|
|
10
|
-
await message.$reply?.(content);
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
if (!plugin)
|
|
14
|
-
return plainTextFromSendContent(content);
|
|
15
|
-
const adapter = plugin.root.inject(message.$adapter);
|
|
16
|
-
if (session.board_message_id) {
|
|
17
|
-
const msgId = await adapter.editMessage({
|
|
18
|
-
messageId: session.board_message_id,
|
|
19
|
-
context: String(message.$adapter),
|
|
20
|
-
endpoint: message.$endpoint,
|
|
21
|
-
id: message.$channel.id,
|
|
22
|
-
type: message.$channel.type,
|
|
23
|
-
content,
|
|
24
|
-
});
|
|
25
|
-
if (msgId !== session.board_message_id) {
|
|
26
|
-
await services.updateSession(session.id, { board_message_id: msgId });
|
|
27
|
-
}
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const msgId = await message.$reply?.(content);
|
|
31
|
-
if (msgId)
|
|
32
|
-
await services.updateSession(session.id, { board_message_id: msgId });
|
|
12
|
+
return typeof content === 'string' ? content : plainTextFromSendContent(content);
|
|
33
13
|
}
|
|
34
14
|
async function advanceQuestion(services, session) {
|
|
35
15
|
const queue = parseQueue(session.queue);
|
|
@@ -42,7 +22,7 @@ async function advanceQuestion(services, session) {
|
|
|
42
22
|
}
|
|
43
23
|
return (await services.getById(session.id));
|
|
44
24
|
}
|
|
45
|
-
export async function startGame(
|
|
25
|
+
export async function startGame(services, message, mode) {
|
|
46
26
|
const ch = `${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`;
|
|
47
27
|
const active = await services.getActiveByChannel(ch);
|
|
48
28
|
if (active) {
|
|
@@ -52,19 +32,15 @@ export async function startGame(plugin, services, message, mode) {
|
|
|
52
32
|
return `本频道 ${active.player_name} 正在猜谜。`;
|
|
53
33
|
}
|
|
54
34
|
const session = await services.createSession(message, mode);
|
|
55
|
-
|
|
56
|
-
return typeof text === 'string' ? text : undefined;
|
|
35
|
+
return renderView(message, session);
|
|
57
36
|
}
|
|
58
|
-
export async function continueGame(
|
|
37
|
+
export async function continueGame(services, message) {
|
|
59
38
|
const session = await services.getActiveForUser(`${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`, message.$sender.id);
|
|
60
39
|
if (!session)
|
|
61
40
|
return '你没有进行中的猜谜,发送「猜谜 开始」。';
|
|
62
|
-
|
|
63
|
-
if (typeof text === 'string')
|
|
64
|
-
return text;
|
|
65
|
-
return '已刷新猜谜界面。';
|
|
41
|
+
return renderView(message, session);
|
|
66
42
|
}
|
|
67
|
-
export async function processAnswerText(
|
|
43
|
+
export async function processAnswerText(services, message, raw) {
|
|
68
44
|
const ch = `${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`;
|
|
69
45
|
const session = await services.getActiveForUser(ch, message.$sender.id);
|
|
70
46
|
if (!session || session.status !== 'active')
|
|
@@ -73,6 +49,11 @@ export async function processAnswerText(plugin, services, message, raw) {
|
|
|
73
49
|
const entry = riddleId ? getRiddleById(riddleId) : undefined;
|
|
74
50
|
if (!entry)
|
|
75
51
|
return null;
|
|
52
|
+
// 格式不合规(字数与答案不符)只提示、不扣失误:避免闲聊(「好的」「谢谢」)被累计判负
|
|
53
|
+
const normalized = normalizeAnswer(raw);
|
|
54
|
+
if (!answersFor(entry).some((a) => a.length === normalized.length)) {
|
|
55
|
+
return `本题答案应为 ${entry.answer.length} 个字(这条不算失误)。`;
|
|
56
|
+
}
|
|
76
57
|
if (checkAnswer(entry, raw)) {
|
|
77
58
|
const streak = session.streak + 1;
|
|
78
59
|
const best = Math.max(session.best_streak, streak);
|
|
@@ -84,25 +65,25 @@ export async function processAnswerText(plugin, services, message, raw) {
|
|
|
84
65
|
});
|
|
85
66
|
const after = await advanceQuestion(services, (await services.getById(session.id)));
|
|
86
67
|
const explain = entry.explanation ? `\n📖 ${entry.explanation}` : '';
|
|
87
|
-
return (
|
|
68
|
+
return renderView(message, after, [
|
|
88
69
|
`✅ 正确!答案:**${entry.answer}**${explain}`,
|
|
89
70
|
`+${10 + Math.min(streak, 5)} 分`,
|
|
90
|
-
])
|
|
71
|
+
]);
|
|
91
72
|
}
|
|
92
73
|
const wrong = session.wrong_count + 1;
|
|
93
74
|
if (wrong >= MAX_WRONG) {
|
|
94
75
|
await services.updateSession(session.id, { wrong_count: wrong, streak: 0 });
|
|
95
76
|
const after = await advanceQuestion(services, (await services.getById(session.id)));
|
|
96
|
-
return (
|
|
77
|
+
return renderView(message, after, [
|
|
97
78
|
`❌ 本题答案:**${entry.answer}**`,
|
|
98
79
|
'失误过多,自动下一题。',
|
|
99
|
-
])
|
|
80
|
+
]);
|
|
100
81
|
}
|
|
101
82
|
await services.updateSession(session.id, { wrong_count: wrong, streak: 0 });
|
|
102
83
|
const updated = (await services.getById(session.id));
|
|
103
|
-
return (
|
|
84
|
+
return renderView(message, updated, ['❌ 不对,再想想!']);
|
|
104
85
|
}
|
|
105
|
-
export async function handleChoice(
|
|
86
|
+
export async function handleChoice(services, message, sessionId, choiceId) {
|
|
106
87
|
const session = await services.getById(sessionId);
|
|
107
88
|
if (!session)
|
|
108
89
|
return '会话不存在。';
|
|
@@ -110,11 +91,11 @@ export async function handleChoice(plugin, services, message, sessionId, choiceI
|
|
|
110
91
|
return '这是别人的猜谜。';
|
|
111
92
|
if (choiceId === 'restart_char') {
|
|
112
93
|
await services.updateSession(session.id, { status: 'aborted' });
|
|
113
|
-
return startGame(
|
|
94
|
+
return (await startGame(services, message, 'char')) ?? null;
|
|
114
95
|
}
|
|
115
96
|
if (choiceId === 'restart_idiom') {
|
|
116
97
|
await services.updateSession(session.id, { status: 'aborted' });
|
|
117
|
-
return startGame(
|
|
98
|
+
return (await startGame(services, message, 'idiom')) ?? null;
|
|
118
99
|
}
|
|
119
100
|
if (session.status !== 'active')
|
|
120
101
|
return '本轮已结束。';
|
|
@@ -126,17 +107,17 @@ export async function handleChoice(plugin, services, message, sessionId, choiceI
|
|
|
126
107
|
const hint = entry.hint ?? `答案共 ${entry.answer.length} 个字`;
|
|
127
108
|
await services.updateSession(session.id, { hints_used: session.hints_used + 1, streak: 0 });
|
|
128
109
|
const updated = (await services.getById(session.id));
|
|
129
|
-
return (
|
|
110
|
+
return renderView(message, updated, [
|
|
130
111
|
`💡 提示:${hint}`,
|
|
131
112
|
'(连击清零)',
|
|
132
|
-
])
|
|
113
|
+
]);
|
|
133
114
|
}
|
|
134
115
|
if (choiceId === 'skip') {
|
|
135
116
|
await services.updateSession(session.id, { streak: 0 });
|
|
136
117
|
const after = await advanceQuestion(services, session);
|
|
137
|
-
return (
|
|
118
|
+
return renderView(message, after, [
|
|
138
119
|
`⏭️ 跳过,答案:**${entry.answer}**`,
|
|
139
|
-
])
|
|
120
|
+
]);
|
|
140
121
|
}
|
|
141
122
|
if (choiceId === 'quit') {
|
|
142
123
|
await services.updateSession(session.id, { status: 'aborted' });
|
package/lib/game-flow.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"game-flow.js","sourceRoot":"","sources":["../src/game-flow.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"game-flow.js","sourceRoot":"","sources":["../src/game-flow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAwB,MAAM,mBAAmB,CAAC;AACnF,OAAO,EACL,UAAU,EACV,WAAW,EACX,aAAa,EACb,eAAe,EACf,aAAa,EACb,SAAS,GACV,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,eAAe,EACf,UAAU,GAEX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEvD;;;;GAIG;AACH,SAAS,UAAU,CACjB,OAAwB,EACxB,OAAyB,EACzB,aAAuB,EAAE;IAEzB,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC5E,OAAO,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC;AACnF,CAAC;AAED,KAAK,UAAU,eAAe,CAC5B,QAAwB,EACxB,OAAyB;IAEzB,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;I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,EAAE,KAAK,EAAE;YAChC,cAAc,KAAK,CAAC,MAAM,IAAI;SAC/B,CAAC,CAAC;IACL,CAAC;IAED,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,MAAM,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;QAChE,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
package/lib/riddle-command.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { type GameMessageLike } from '@zhin.js/game-kit';
|
|
2
2
|
import type { SessionService } from './session-service.js';
|
|
3
3
|
export declare const RIDDLE_HELP: string;
|
|
4
|
-
export declare function runRiddleCommand(
|
|
5
|
-
/** Plugin Runtime / smoke: text-only, no Adapter.editMessage. */
|
|
6
|
-
export declare function runRiddleCommandText(services: SessionService, message: Message<any>, action: string): Promise<string>;
|
|
4
|
+
export declare function runRiddleCommand(services: SessionService, message: GameMessageLike, action: string): Promise<string>;
|
package/lib/riddle-command.js
CHANGED
|
@@ -24,7 +24,7 @@ function parseMode(action) {
|
|
|
24
24
|
return 'char';
|
|
25
25
|
return null;
|
|
26
26
|
}
|
|
27
|
-
export async function runRiddleCommand(
|
|
27
|
+
export async function runRiddleCommand(services, message, action) {
|
|
28
28
|
const ch = channelKey(message);
|
|
29
29
|
const userId = message.$sender.id;
|
|
30
30
|
if (!action || action === 'help') {
|
|
@@ -40,9 +40,9 @@ export async function runRiddleCommand(plugin, services, message, action) {
|
|
|
40
40
|
}
|
|
41
41
|
const mode = parseMode(action);
|
|
42
42
|
if (mode)
|
|
43
|
-
return startGame(
|
|
43
|
+
return (await startGame(services, message, mode)) ?? '';
|
|
44
44
|
if (action === 'continue' || action === '继续') {
|
|
45
|
-
return continueGame(
|
|
45
|
+
return continueGame(services, message);
|
|
46
46
|
}
|
|
47
47
|
if (action === 'quit' || action === '放弃') {
|
|
48
48
|
const row = await services.getActiveForUser(ch, userId);
|
|
@@ -53,8 +53,4 @@ export async function runRiddleCommand(plugin, services, message, action) {
|
|
|
53
53
|
}
|
|
54
54
|
return `未知子命令:${action}\n\n${RIDDLE_HELP}`;
|
|
55
55
|
}
|
|
56
|
-
/** Plugin Runtime / smoke: text-only, no Adapter.editMessage. */
|
|
57
|
-
export async function runRiddleCommandText(services, message, action) {
|
|
58
|
-
return (await runRiddleCommand(null, services, message, action)) ?? '';
|
|
59
|
-
}
|
|
60
56
|
//# sourceMappingURL=riddle-command.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"riddle-command.js","sourceRoot":"","sources":["../src/riddle-command.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"riddle-command.js","sourceRoot":"","sources":["../src/riddle-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAwB,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAmB,MAAM,sBAAsB,CAAC;AAEpE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,MAAM,MAAM,GAAG,WAAW,EAAE,CAAC;AAE7B,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,iBAAiB;IACjB,MAAM,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,WAAW,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI;IAC5F,kBAAkB;IAClB,cAAc;IACd,cAAc;IACd,eAAe;IACf,cAAc;IACd,YAAY;IACZ,EAAE;IACF,4BAA4B;CAC7B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEb,SAAS,SAAS,CAAC,MAAc;IAC/B,IAAI,MAAM,KAAK,MAAM,IAAI,MAAM,KAAK,IA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IAED,IAAI,MAAM,KAAK,MAAM,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACzC,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,gBAAgB,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QACxD,IAAI,CAAC,GAAG;YAAE,OAAO,YAAY,CAAC;QAC9B,MAAM,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;QAC5D,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,OAAO,SAAS,MAAM,OAAO,WAAW,EAAE,CAAC;AAC7C,CAAC"}
|
package/lib/session-service.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Database,
|
|
1
|
+
import type { Database, Models } from '@zhin.js/core';
|
|
2
|
+
import { type GameMessageLike } from '@zhin.js/game-kit';
|
|
2
3
|
import { type RiddleType } from './riddles-catalog.js';
|
|
3
4
|
import type { RiddleSessionRow } from './models.js';
|
|
4
5
|
export type RiddleDatabase = Database<unknown, Models, string>;
|
|
@@ -10,7 +11,7 @@ export declare class SessionService {
|
|
|
10
11
|
getActiveForUser(channel: string, userId: string): Promise<RiddleSessionRow | null>;
|
|
11
12
|
getById(id: string): Promise<RiddleSessionRow | null>;
|
|
12
13
|
getActiveByBoardMessageId(messageId: string): Promise<RiddleSessionRow | null>;
|
|
13
|
-
createSession(message:
|
|
14
|
+
createSession(message: GameMessageLike, mode: RiddleType): Promise<RiddleSessionRow>;
|
|
14
15
|
updateSession(id: string, patch: Partial<RiddleSessionRow>): Promise<void>;
|
|
15
16
|
abortStale(idleMs: number): Promise<number>;
|
|
16
17
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-service.js","sourceRoot":"","sources":["../src/session-service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,mBAAmB,
|
|
1
|
+
{"version":3,"file":"session-service.js","s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OAAO,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;QACvD,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,GAAG,CAAC,UAAU,GAAG,MAAM,EAAE,CAAC;gBAC5B,MAAM,KAAK,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;gBACvF,CAAC,EAAE,CAAC;YACN,CAAC;QACH,CAAC;QACD,OAAO,CAAC,CAAC;IACX,CAAC;CACF;AAED,MAAM,UAAU,cAAc,CAAC,EAAkB;IAC/C,OAAO,IAAI,cAAc,CAAC,EAAE,CAAC,CAAC;AAChC,CAAC;AAGD,MAAM,UAAU,eAAe,CAAC,OAAyB;IACvD,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACxC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;AACtC,CAAC"}
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
2
|
defineGameCommandAliasMiddleware,
|
|
3
|
+
isRiddleAction,
|
|
3
4
|
messageFromCommandInput,
|
|
4
5
|
normalizeRiddleAction,
|
|
5
6
|
} from '@zhin.js/game-kit';
|
|
6
|
-
import { RIDDLE_HELP,
|
|
7
|
+
import { RIDDLE_HELP, runRiddleCommand } from '../src/riddle-command.js';
|
|
7
8
|
import { resolveGameServices } from '../src/runtime-store.js';
|
|
8
9
|
|
|
9
10
|
export default defineGameCommandAliasMiddleware({
|
|
10
11
|
aliases: ['猜谜', 'riddle'],
|
|
11
12
|
async run(action, input, context) {
|
|
12
13
|
const normalized = normalizeRiddleAction(String(action ?? ''));
|
|
13
|
-
if (
|
|
14
|
+
if (normalized === 'help') return RIDDLE_HELP;
|
|
15
|
+
// action 无法识别:放行给后续中间件,避免劫持普通聊天
|
|
16
|
+
if (!isRiddleAction(normalized)) return null;
|
|
14
17
|
const services = resolveGameServices(context);
|
|
15
18
|
const message = messageFromCommandInput(input);
|
|
16
|
-
return
|
|
19
|
+
return runRiddleCommand(services, message, normalized);
|
|
17
20
|
},
|
|
18
21
|
});
|
|
@@ -33,7 +33,7 @@ export default defineMiddleware<Message>({
|
|
|
33
33
|
if (parsed) {
|
|
34
34
|
const session = await services.getById(parsed.sessionId);
|
|
35
35
|
if (session?.channel_key === ch) {
|
|
36
|
-
const reply = await handleChoice(
|
|
36
|
+
const reply = await handleChoice(services, message, parsed.sessionId, parsed.choiceId);
|
|
37
37
|
if (reply) await context.input.$reply(reply);
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
@@ -57,7 +57,7 @@ export default defineMiddleware<Message>({
|
|
|
57
57
|
const payload = resolveGameTextPayload(raw, map);
|
|
58
58
|
const parsed = payload ? parseChoicePayload(payload, RIDDLE_PREFIX) : null;
|
|
59
59
|
if (parsed?.sessionId === session.id) {
|
|
60
|
-
const reply = await handleChoice(
|
|
60
|
+
const reply = await handleChoice(services, message, session.id, parsed.choiceId);
|
|
61
61
|
if (reply) await context.input.$reply(reply);
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
@@ -65,7 +65,7 @@ export default defineMiddleware<Message>({
|
|
|
65
65
|
|
|
66
66
|
// 仅把 1-8 个汉字的纯文本当谜底作答,其余放行
|
|
67
67
|
if (/^[\u4e00-\u9fff]{1,8}$/.test(raw)) {
|
|
68
|
-
const reply = await processAnswerText(
|
|
68
|
+
const reply = await processAnswerText(services, message, raw);
|
|
69
69
|
if (reply) await context.input.$reply(reply);
|
|
70
70
|
return;
|
|
71
71
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/plugin-word-riddle",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Zhin.js 猜谜 — 字谜与猜成语 (Plugin Runtime)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"chinese-idiom-chengyu": "^1.1.0",
|
|
28
|
-
"@zhin.js/command": "1.0.
|
|
29
|
-
"@zhin.js/core": "1.
|
|
30
|
-
"@zhin.js/game-kit": "
|
|
31
|
-
"@zhin.js/middleware": "1.0.
|
|
32
|
-
"@zhin.js/plugin-runtime": "1.
|
|
28
|
+
"@zhin.js/command": "1.0.3",
|
|
29
|
+
"@zhin.js/core": "1.4.1",
|
|
30
|
+
"@zhin.js/game-kit": "2.0.1",
|
|
31
|
+
"@zhin.js/middleware": "1.0.3",
|
|
32
|
+
"@zhin.js/plugin-runtime": "1.1.1"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"typescript": "^6.0.3",
|
package/src/game-flow.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { plainTextFromSendContent } from '@zhin.js/game-kit';
|
|
1
|
+
import { plainTextFromSendContent, type GameMessageLike } from '@zhin.js/game-kit';
|
|
3
2
|
import {
|
|
3
|
+
answersFor,
|
|
4
4
|
checkAnswer,
|
|
5
5
|
getRiddleById,
|
|
6
|
+
normalizeAnswer,
|
|
6
7
|
RIDDLE_PREFIX,
|
|
7
8
|
typeLabel,
|
|
8
9
|
} from './engine.js';
|
|
@@ -15,39 +16,18 @@ import {
|
|
|
15
16
|
} from './session-service.js';
|
|
16
17
|
import { buildRiddleView, MAX_WRONG } from './view.js';
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Plugin Runtime: render the board as text. Interactive in-place board editing
|
|
21
|
+
* (the old Adapter.editMessage path) is not part of the runtime flow; commands
|
|
22
|
+
* and the text middleware return fresh text each turn.
|
|
23
|
+
*/
|
|
24
|
+
function renderView(
|
|
25
|
+
message: GameMessageLike,
|
|
22
26
|
session: RiddleSessionRow,
|
|
23
27
|
eventLines: string[] = [],
|
|
24
|
-
):
|
|
28
|
+
): string {
|
|
25
29
|
const content = buildRiddleView(session, eventLines, message.$channel.type);
|
|
26
|
-
|
|
27
|
-
if (!plugin) return content;
|
|
28
|
-
await message.$reply?.(content);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
if (!plugin) return plainTextFromSendContent(content);
|
|
32
|
-
|
|
33
|
-
const adapter = plugin.root.inject(message.$adapter) as Adapter;
|
|
34
|
-
if (session.board_message_id) {
|
|
35
|
-
const msgId = await adapter.editMessage({
|
|
36
|
-
messageId: session.board_message_id,
|
|
37
|
-
context: String(message.$adapter),
|
|
38
|
-
endpoint: message.$endpoint,
|
|
39
|
-
id: message.$channel.id,
|
|
40
|
-
type: message.$channel.type,
|
|
41
|
-
content,
|
|
42
|
-
});
|
|
43
|
-
if (msgId !== session.board_message_id) {
|
|
44
|
-
await services.updateSession(session.id, { board_message_id: msgId });
|
|
45
|
-
}
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const msgId = await message.$reply?.(content);
|
|
50
|
-
if (msgId) await services.updateSession(session.id, { board_message_id: msgId });
|
|
30
|
+
return typeof content === 'string' ? content : plainTextFromSendContent(content);
|
|
51
31
|
}
|
|
52
32
|
|
|
53
33
|
async function advanceQuestion(
|
|
@@ -65,9 +45,8 @@ async function advanceQuestion(
|
|
|
65
45
|
}
|
|
66
46
|
|
|
67
47
|
export async function startGame(
|
|
68
|
-
plugin: Plugin | null,
|
|
69
48
|
services: SessionService,
|
|
70
|
-
message:
|
|
49
|
+
message: GameMessageLike,
|
|
71
50
|
mode: RiddleType,
|
|
72
51
|
): Promise<string | undefined> {
|
|
73
52
|
const ch = `${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`;
|
|
@@ -80,29 +59,24 @@ export async function startGame(
|
|
|
80
59
|
}
|
|
81
60
|
|
|
82
61
|
const session = await services.createSession(message, mode);
|
|
83
|
-
|
|
84
|
-
return typeof text === 'string' ? text : undefined;
|
|
62
|
+
return renderView(message, session);
|
|
85
63
|
}
|
|
86
64
|
|
|
87
65
|
export async function continueGame(
|
|
88
|
-
plugin: Plugin | null,
|
|
89
66
|
services: SessionService,
|
|
90
|
-
message:
|
|
67
|
+
message: GameMessageLike,
|
|
91
68
|
): Promise<string> {
|
|
92
69
|
const session = await services.getActiveForUser(
|
|
93
70
|
`${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`,
|
|
94
71
|
message.$sender.id,
|
|
95
72
|
);
|
|
96
73
|
if (!session) return '你没有进行中的猜谜,发送「猜谜 开始」。';
|
|
97
|
-
|
|
98
|
-
if (typeof text === 'string') return text;
|
|
99
|
-
return '已刷新猜谜界面。';
|
|
74
|
+
return renderView(message, session);
|
|
100
75
|
}
|
|
101
76
|
|
|
102
77
|
export async function processAnswerText(
|
|
103
|
-
plugin: Plugin | null,
|
|
104
78
|
services: SessionService,
|
|
105
|
-
message:
|
|
79
|
+
message: GameMessageLike,
|
|
106
80
|
raw: string,
|
|
107
81
|
): Promise<string | null> {
|
|
108
82
|
const ch = `${message.$adapter}-${message.$endpoint}-${message.$channel.type}:${message.$channel.id}`;
|
|
@@ -113,6 +87,12 @@ export async function processAnswerText(
|
|
|
113
87
|
const entry = riddleId ? getRiddleById(riddleId) : undefined;
|
|
114
88
|
if (!entry) return null;
|
|
115
89
|
|
|
90
|
+
// 格式不合规(字数与答案不符)只提示、不扣失误:避免闲聊(「好的」「谢谢」)被累计判负
|
|
91
|
+
const normalized = normalizeAnswer(raw);
|
|
92
|
+
if (!answersFor(entry).some((a) => a.length === normalized.length)) {
|
|
93
|
+
return `本题答案应为 ${entry.answer.length} 个字(这条不算失误)。`;
|
|
94
|
+
}
|
|
95
|
+
|
|
116
96
|
if (checkAnswer(entry, raw)) {
|
|
117
97
|
const streak = session.streak + 1;
|
|
118
98
|
const best = Math.max(session.best_streak, streak);
|
|
@@ -124,31 +104,30 @@ export async function processAnswerText(
|
|
|
124
104
|
});
|
|
125
105
|
const after = await advanceQuestion(services, (await services.getById(session.id))!);
|
|
126
106
|
const explain = entry.explanation ? `\n📖 ${entry.explanation}` : '';
|
|
127
|
-
return (
|
|
107
|
+
return renderView(message, after, [
|
|
128
108
|
`✅ 正确!答案:**${entry.answer}**${explain}`,
|
|
129
109
|
`+${10 + Math.min(streak, 5)} 分`,
|
|
130
|
-
])
|
|
110
|
+
]);
|
|
131
111
|
}
|
|
132
112
|
|
|
133
113
|
const wrong = session.wrong_count + 1;
|
|
134
114
|
if (wrong >= MAX_WRONG) {
|
|
135
115
|
await services.updateSession(session.id, { wrong_count: wrong, streak: 0 });
|
|
136
116
|
const after = await advanceQuestion(services, (await services.getById(session.id))!);
|
|
137
|
-
return (
|
|
117
|
+
return renderView(message, after, [
|
|
138
118
|
`❌ 本题答案:**${entry.answer}**`,
|
|
139
119
|
'失误过多,自动下一题。',
|
|
140
|
-
])
|
|
120
|
+
]);
|
|
141
121
|
}
|
|
142
122
|
|
|
143
123
|
await services.updateSession(session.id, { wrong_count: wrong, streak: 0 });
|
|
144
124
|
const updated = (await services.getById(session.id))!;
|
|
145
|
-
return (
|
|
125
|
+
return renderView(message, updated, ['❌ 不对,再想想!']);
|
|
146
126
|
}
|
|
147
127
|
|
|
148
128
|
export async function handleChoice(
|
|
149
|
-
plugin: Plugin | null,
|
|
150
129
|
services: SessionService,
|
|
151
|
-
message:
|
|
130
|
+
message: GameMessageLike,
|
|
152
131
|
sessionId: string,
|
|
153
132
|
choiceId: string,
|
|
154
133
|
): Promise<string | null> {
|
|
@@ -158,11 +137,11 @@ export async function handleChoice(
|
|
|
158
137
|
|
|
159
138
|
if (choiceId === 'restart_char') {
|
|
160
139
|
await services.updateSession(session.id, { status: 'aborted' });
|
|
161
|
-
return startGame(
|
|
140
|
+
return (await startGame(services, message, 'char')) ?? null;
|
|
162
141
|
}
|
|
163
142
|
if (choiceId === 'restart_idiom') {
|
|
164
143
|
await services.updateSession(session.id, { status: 'aborted' });
|
|
165
|
-
return startGame(
|
|
144
|
+
return (await startGame(services, message, 'idiom')) ?? null;
|
|
166
145
|
}
|
|
167
146
|
|
|
168
147
|
if (session.status !== 'active') return '本轮已结束。';
|
|
@@ -175,18 +154,18 @@ export async function handleChoice(
|
|
|
175
154
|
const hint = entry.hint ?? `答案共 ${entry.answer.length} 个字`;
|
|
176
155
|
await services.updateSession(session.id, { hints_used: session.hints_used + 1, streak: 0 });
|
|
177
156
|
const updated = (await services.getById(session.id))!;
|
|
178
|
-
return (
|
|
157
|
+
return renderView(message, updated, [
|
|
179
158
|
`💡 提示:${hint}`,
|
|
180
159
|
'(连击清零)',
|
|
181
|
-
])
|
|
160
|
+
]);
|
|
182
161
|
}
|
|
183
162
|
|
|
184
163
|
if (choiceId === 'skip') {
|
|
185
164
|
await services.updateSession(session.id, { streak: 0 });
|
|
186
165
|
const after = await advanceQuestion(services, session);
|
|
187
|
-
return (
|
|
166
|
+
return renderView(message, after, [
|
|
188
167
|
`⏭️ 跳过,答案:**${entry.answer}**`,
|
|
189
|
-
])
|
|
168
|
+
]);
|
|
190
169
|
}
|
|
191
170
|
|
|
192
171
|
if (choiceId === 'quit') {
|
package/src/riddle-command.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { channelKey } from '@zhin.js/game-kit';
|
|
1
|
+
import { channelKey, type GameMessageLike } from '@zhin.js/game-kit';
|
|
3
2
|
import { continueGame, startGame } from './game-flow.js';
|
|
4
3
|
import { riddleCount, type RiddleType } from './riddles-catalog.js';
|
|
5
4
|
|
|
@@ -29,11 +28,10 @@ function parseMode(action: string): RiddleType | null {
|
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
export async function runRiddleCommand(
|
|
32
|
-
plugin: Plugin | null,
|
|
33
31
|
services: SessionService,
|
|
34
|
-
message:
|
|
32
|
+
message: GameMessageLike,
|
|
35
33
|
action: string,
|
|
36
|
-
): Promise<string
|
|
34
|
+
): Promise<string> {
|
|
37
35
|
const ch = channelKey(message);
|
|
38
36
|
const userId = message.$sender.id;
|
|
39
37
|
|
|
@@ -49,10 +47,10 @@ export async function runRiddleCommand(
|
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
const mode = parseMode(action);
|
|
52
|
-
if (mode) return startGame(
|
|
50
|
+
if (mode) return (await startGame(services, message, mode)) ?? '';
|
|
53
51
|
|
|
54
52
|
if (action === 'continue' || action === '继续') {
|
|
55
|
-
return continueGame(
|
|
53
|
+
return continueGame(services, message);
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
if (action === 'quit' || action === '放弃') {
|
|
@@ -64,12 +62,3 @@ export async function runRiddleCommand(
|
|
|
64
62
|
|
|
65
63
|
return `未知子命令:${action}\n\n${RIDDLE_HELP}`;
|
|
66
64
|
}
|
|
67
|
-
|
|
68
|
-
/** Plugin Runtime / smoke: text-only, no Adapter.editMessage. */
|
|
69
|
-
export async function runRiddleCommandText(
|
|
70
|
-
services: SessionService,
|
|
71
|
-
message: Message<any>,
|
|
72
|
-
action: string,
|
|
73
|
-
): Promise<string> {
|
|
74
|
-
return (await runRiddleCommand(null, services, message, action)) ?? '';
|
|
75
|
-
}
|
package/src/session-service.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Database,
|
|
2
|
-
import { channelKey, generateSessionId, boardMessageMatches } from '@zhin.js/game-kit';
|
|
1
|
+
import type { Database, Models, RelatedModel } from '@zhin.js/core';
|
|
2
|
+
import { channelKey, generateSessionId, boardMessageMatches, type GameMessageLike } from '@zhin.js/game-kit';
|
|
3
3
|
import { pickRoundQueue, type RiddleType } from './riddles-catalog.js';
|
|
4
4
|
import type { RiddleSessionRow } from './models.js';
|
|
5
5
|
|
|
@@ -49,7 +49,7 @@ export class SessionService {
|
|
|
49
49
|
return null;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
async createSession(message:
|
|
52
|
+
async createSession(message: GameMessageLike, mode: RiddleType): Promise<RiddleSessionRow> {
|
|
53
53
|
const now = Date.now();
|
|
54
54
|
const queue = pickRoundQueue(mode).map((r) => r.id);
|
|
55
55
|
const row: RiddleSessionRow = {
|