koishi-plugin-game-mini 0.0.3 → 0.0.5
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/lib/index.js +58 -49
- package/lib/locales/locales/zh-CN.yml +4 -16
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -23,12 +23,12 @@ function apply(ctx, config) {
|
|
|
23
23
|
const zhCNPath = path_1.default.resolve(__dirname, './locales/zh-CN.yml');
|
|
24
24
|
const defaultI18n = {
|
|
25
25
|
guessNumber: {
|
|
26
|
-
usage: '用法错误!正确用法:guess
|
|
27
|
-
start: '猜数字游戏开始!数字范围是 {0}
|
|
26
|
+
usage: '用法错误!正确用法:guess start/stop 或 直接输入数字',
|
|
27
|
+
start: '猜数字游戏开始!数字范围是 {0}-{1},快来猜猜看~',
|
|
28
28
|
stop: '猜数字游戏已停止,正确答案是 {0}',
|
|
29
29
|
invalid: '请输入有效的数字!',
|
|
30
|
-
notStarted: '游戏还未开始,请先发送 guess
|
|
31
|
-
outOfRange: '数字超出范围!请输入 {0}
|
|
30
|
+
notStarted: '游戏还未开始,请先发送 guess start 开始游戏',
|
|
31
|
+
outOfRange: '数字超出范围!请输入 {0}-{1} 之间的数字',
|
|
32
32
|
tooSmall: '猜小啦,再试试更大的数字~',
|
|
33
33
|
tooBig: '猜大啦,再试试更小的数字~',
|
|
34
34
|
correct: '恭喜你猜对了!正确答案就是 {0} 🎉',
|
|
@@ -55,13 +55,61 @@ function apply(ctx, config) {
|
|
|
55
55
|
const getSessionKey = (session) => {
|
|
56
56
|
return session.channelId ? `group:${session.channelId}` : `private:${session.userId}`;
|
|
57
57
|
};
|
|
58
|
-
ctx.
|
|
58
|
+
ctx.middleware(async (session, next) => {
|
|
59
|
+
if (!session.content)
|
|
60
|
+
return next();
|
|
61
|
+
const key = getSessionKey(session);
|
|
62
|
+
const state = guessNumberStates.get(key);
|
|
63
|
+
if (state && state.started) {
|
|
64
|
+
const num = parseInt(session.content.trim());
|
|
65
|
+
if (!isNaN(num)) {
|
|
66
|
+
let reply = '';
|
|
67
|
+
if (num < state.min || num > state.max) {
|
|
68
|
+
reply = session.text('guessNumber.outOfRange', [state.min, state.max]);
|
|
69
|
+
}
|
|
70
|
+
else if (num < state.target) {
|
|
71
|
+
reply = session.text('guessNumber.tooSmall');
|
|
72
|
+
}
|
|
73
|
+
else if (num > state.target) {
|
|
74
|
+
reply = session.text('guessNumber.tooBig');
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
state.started = false;
|
|
78
|
+
guessNumberStates.set(key, state);
|
|
79
|
+
reply = session.text('guessNumber.correct', [state.target]);
|
|
80
|
+
return session.send(reply);
|
|
81
|
+
}
|
|
82
|
+
const isPrivate = !session.channelId;
|
|
83
|
+
const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
|
|
84
|
+
if (botShouldParticipate && state.started) {
|
|
85
|
+
let botGuess;
|
|
86
|
+
do {
|
|
87
|
+
botGuess = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
|
|
88
|
+
} while (state.botGuess.includes(botGuess));
|
|
89
|
+
state.botGuess.push(botGuess);
|
|
90
|
+
guessNumberStates.set(key, state);
|
|
91
|
+
if (botGuess === state.target) {
|
|
92
|
+
reply += '\n' + session.text('guessNumber.botWin', [botGuess]);
|
|
93
|
+
state.started = false;
|
|
94
|
+
guessNumberStates.set(key, state);
|
|
95
|
+
}
|
|
96
|
+
else if (botGuess < state.target) {
|
|
97
|
+
reply += '\n' + session.text('guessNumber.botGuessTooSmall', [botGuess]);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
reply += '\n' + session.text('guessNumber.botGuessTooBig', [botGuess]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return session.send(reply);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return next();
|
|
107
|
+
});
|
|
108
|
+
ctx.command('guess <action:string>')
|
|
59
109
|
.action(async ({ session }, action) => {
|
|
60
110
|
if (!session)
|
|
61
111
|
return;
|
|
62
112
|
const t = (key, ...args) => session.text(key, args);
|
|
63
|
-
if (!action)
|
|
64
|
-
return t('guessNumber.usage');
|
|
65
113
|
const key = getSessionKey(session);
|
|
66
114
|
let state = guessNumberStates.get(key) || {
|
|
67
115
|
target: 0,
|
|
@@ -70,6 +118,8 @@ function apply(ctx, config) {
|
|
|
70
118
|
started: false,
|
|
71
119
|
botGuess: []
|
|
72
120
|
};
|
|
121
|
+
if (!action)
|
|
122
|
+
return t('guessNumber.usage');
|
|
73
123
|
if (action === 'start') {
|
|
74
124
|
state.target = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
|
|
75
125
|
state.started = true;
|
|
@@ -91,47 +141,6 @@ function apply(ctx, config) {
|
|
|
91
141
|
guessNumberStates.set(key, state);
|
|
92
142
|
return t('guessNumber.stop', [state.target]);
|
|
93
143
|
}
|
|
94
|
-
|
|
95
|
-
if (isNaN(num))
|
|
96
|
-
return t('guessNumber.invalid');
|
|
97
|
-
if (!state.started)
|
|
98
|
-
return t('guessNumber.notStarted');
|
|
99
|
-
if (num < state.min || num > state.max) {
|
|
100
|
-
return t('guessNumber.outOfRange', [state.min, state.max]);
|
|
101
|
-
}
|
|
102
|
-
let reply = '';
|
|
103
|
-
if (num < state.target) {
|
|
104
|
-
reply = t('guessNumber.tooSmall');
|
|
105
|
-
}
|
|
106
|
-
else if (num > state.target) {
|
|
107
|
-
reply = t('guessNumber.tooBig');
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
state.started = false;
|
|
111
|
-
guessNumberStates.set(key, state);
|
|
112
|
-
return t('guessNumber.correct', [state.target]);
|
|
113
|
-
}
|
|
114
|
-
const isPrivate = !session.channelId;
|
|
115
|
-
const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
|
|
116
|
-
if (botShouldParticipate && state.started) {
|
|
117
|
-
let botGuess;
|
|
118
|
-
do {
|
|
119
|
-
botGuess = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
|
|
120
|
-
} while (state.botGuess.includes(botGuess));
|
|
121
|
-
state.botGuess.push(botGuess);
|
|
122
|
-
guessNumberStates.set(key, state);
|
|
123
|
-
if (botGuess === state.target) {
|
|
124
|
-
reply += '\n' + t('guessNumber.botWin', [botGuess]);
|
|
125
|
-
state.started = false;
|
|
126
|
-
guessNumberStates.set(key, state);
|
|
127
|
-
}
|
|
128
|
-
else if (botGuess < state.target) {
|
|
129
|
-
reply += '\n' + t('guessNumber.botGuessTooSmall', [botGuess]);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
reply += '\n' + t('guessNumber.botGuessTooBig', [botGuess]);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return reply;
|
|
144
|
+
return t('guessNumber.usage');
|
|
136
145
|
});
|
|
137
146
|
}
|
|
@@ -1,22 +1,10 @@
|
|
|
1
|
-
commands:
|
|
2
|
-
guess-number:
|
|
3
|
-
description: 猜数字小游戏
|
|
4
|
-
usage: |-
|
|
5
|
-
guess-number start - 开始猜数字游戏
|
|
6
|
-
guess-number <数字> - 输入数字进行猜测
|
|
7
|
-
guess-number stop - 停止猜数字游戏
|
|
8
|
-
examples: |-
|
|
9
|
-
guess-number start
|
|
10
|
-
guess-number 50
|
|
11
|
-
guess-number stop
|
|
12
|
-
|
|
13
1
|
guessNumber:
|
|
14
|
-
usage: 用法错误!正确用法:guess
|
|
15
|
-
start: 猜数字游戏开始!数字范围是 {0}
|
|
2
|
+
usage: 用法错误!正确用法:guess start/stop 或 直接输入数字
|
|
3
|
+
start: 猜数字游戏开始!数字范围是 {0}-{1},快来猜猜看~
|
|
16
4
|
stop: 猜数字游戏已停止,正确答案是 {0}
|
|
17
5
|
invalid: 请输入有效的数字!
|
|
18
|
-
notStarted: 游戏还未开始,请先发送 guess
|
|
19
|
-
outOfRange: 数字超出范围!请输入 {0}
|
|
6
|
+
notStarted: 游戏还未开始,请先发送 guess start 开始游戏
|
|
7
|
+
outOfRange: 数字超出范围!请输入 {0}-{1} 之间的数字
|
|
20
8
|
tooSmall: 猜小啦,再试试更大的数字~
|
|
21
9
|
tooBig: 猜大啦,再试试更小的数字~
|
|
22
10
|
correct: 恭喜你猜对了!正确答案就是 {0} 🎉
|