koishi-plugin-game-mini 0.0.5 → 0.0.7
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 +33 -23
- package/lib/locales/locales/zh-CN.yml +10 -10
- package/package.json +16 -24
- package/src/index.ts +138 -0
- package/src/locales/zh-CN.yml +14 -0
package/lib/index.js
CHANGED
|
@@ -24,18 +24,18 @@ function apply(ctx, config) {
|
|
|
24
24
|
const defaultI18n = {
|
|
25
25
|
guessNumber: {
|
|
26
26
|
usage: '用法错误!正确用法:guess start/stop 或 直接输入数字',
|
|
27
|
-
start: '
|
|
27
|
+
start: '猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~',
|
|
28
28
|
stop: '猜数字游戏已停止,正确答案是 {0}',
|
|
29
|
-
invalid: '
|
|
29
|
+
invalid: '请输入有效的整数!',
|
|
30
30
|
notStarted: '游戏还未开始,请先发送 guess start 开始游戏',
|
|
31
|
-
outOfRange: '
|
|
32
|
-
tooSmall: '
|
|
33
|
-
tooBig: '
|
|
34
|
-
correct: '
|
|
31
|
+
outOfRange: '数字超出当前范围 [{0}-{1}],请重新输入!',
|
|
32
|
+
tooSmall: '猜小啦!当前范围更新为 [{0}-{1}]',
|
|
33
|
+
tooBig: '猜大啦!当前范围更新为 [{0}-{1}]',
|
|
34
|
+
correct: '恭喜你猜中了!数字就是 {0} 🎉',
|
|
35
35
|
botFirstGuess: '我先来猜一个:{0},该你啦~',
|
|
36
|
-
botWin: '
|
|
37
|
-
botGuessTooSmall: '我猜 {0}
|
|
38
|
-
botGuessTooBig: '我猜 {0}
|
|
36
|
+
botWin: '哈哈我猜中了!数字是 {0},我赢啦 🎉',
|
|
37
|
+
botGuessTooSmall: '我猜 {0},猜小啦!当前范围 [{1}-{2}]',
|
|
38
|
+
botGuessTooBig: '我猜 {0},猜大啦!当前范围 [{1}-{2}]'
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
41
|
if (fs_1.default.existsSync(zhCNPath)) {
|
|
@@ -61,17 +61,20 @@ function apply(ctx, config) {
|
|
|
61
61
|
const key = getSessionKey(session);
|
|
62
62
|
const state = guessNumberStates.get(key);
|
|
63
63
|
if (state && state.started) {
|
|
64
|
-
const
|
|
65
|
-
|
|
64
|
+
const input = session.content.trim();
|
|
65
|
+
const num = parseInt(input);
|
|
66
|
+
if (!isNaN(num) && Number.isInteger(num)) {
|
|
66
67
|
let reply = '';
|
|
67
|
-
if (num < state.
|
|
68
|
-
reply = session.text('guessNumber.outOfRange', [state.
|
|
68
|
+
if (num < state.currentMin || num > state.currentMax) {
|
|
69
|
+
reply = session.text('guessNumber.outOfRange', [state.currentMin, state.currentMax]);
|
|
69
70
|
}
|
|
70
71
|
else if (num < state.target) {
|
|
71
|
-
|
|
72
|
+
state.currentMin = num + 1;
|
|
73
|
+
reply = session.text('guessNumber.tooSmall', [state.currentMin, state.currentMax]);
|
|
72
74
|
}
|
|
73
75
|
else if (num > state.target) {
|
|
74
|
-
|
|
76
|
+
state.currentMax = num - 1;
|
|
77
|
+
reply = session.text('guessNumber.tooBig', [state.currentMin, state.currentMax]);
|
|
75
78
|
}
|
|
76
79
|
else {
|
|
77
80
|
state.started = false;
|
|
@@ -79,12 +82,13 @@ function apply(ctx, config) {
|
|
|
79
82
|
reply = session.text('guessNumber.correct', [state.target]);
|
|
80
83
|
return session.send(reply);
|
|
81
84
|
}
|
|
85
|
+
guessNumberStates.set(key, state);
|
|
82
86
|
const isPrivate = !session.channelId;
|
|
83
87
|
const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
|
|
84
|
-
if (botShouldParticipate && state.started) {
|
|
88
|
+
if (botShouldParticipate && state.started && state.currentMin <= state.currentMax) {
|
|
85
89
|
let botGuess;
|
|
86
90
|
do {
|
|
87
|
-
botGuess = koishi_1.Random.int(
|
|
91
|
+
botGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
|
|
88
92
|
} while (state.botGuess.includes(botGuess));
|
|
89
93
|
state.botGuess.push(botGuess);
|
|
90
94
|
guessNumberStates.set(key, state);
|
|
@@ -94,10 +98,14 @@ function apply(ctx, config) {
|
|
|
94
98
|
guessNumberStates.set(key, state);
|
|
95
99
|
}
|
|
96
100
|
else if (botGuess < state.target) {
|
|
97
|
-
|
|
101
|
+
state.currentMin = botGuess + 1;
|
|
102
|
+
guessNumberStates.set(key, state);
|
|
103
|
+
reply += '\n' + session.text('guessNumber.botGuessTooSmall', [botGuess, state.currentMin, state.currentMax]);
|
|
98
104
|
}
|
|
99
105
|
else {
|
|
100
|
-
|
|
106
|
+
state.currentMax = botGuess - 1;
|
|
107
|
+
guessNumberStates.set(key, state);
|
|
108
|
+
reply += '\n' + session.text('guessNumber.botGuessTooBig', [botGuess, state.currentMin, state.currentMax]);
|
|
101
109
|
}
|
|
102
110
|
}
|
|
103
111
|
return session.send(reply);
|
|
@@ -113,8 +121,8 @@ function apply(ctx, config) {
|
|
|
113
121
|
const key = getSessionKey(session);
|
|
114
122
|
let state = guessNumberStates.get(key) || {
|
|
115
123
|
target: 0,
|
|
116
|
-
|
|
117
|
-
|
|
124
|
+
currentMin: config.guessNumber.min,
|
|
125
|
+
currentMax: config.guessNumber.max,
|
|
118
126
|
started: false,
|
|
119
127
|
botGuess: []
|
|
120
128
|
};
|
|
@@ -122,14 +130,16 @@ function apply(ctx, config) {
|
|
|
122
130
|
return t('guessNumber.usage');
|
|
123
131
|
if (action === 'start') {
|
|
124
132
|
state.target = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
|
|
133
|
+
state.currentMin = config.guessNumber.min;
|
|
134
|
+
state.currentMax = config.guessNumber.max;
|
|
125
135
|
state.started = true;
|
|
126
136
|
state.botGuess = [];
|
|
127
137
|
guessNumberStates.set(key, state);
|
|
128
138
|
let reply = t('guessNumber.start', [config.guessNumber.min, config.guessNumber.max]);
|
|
129
139
|
const isPrivate = !session.channelId;
|
|
130
140
|
const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
|
|
131
|
-
if (botShouldParticipate) {
|
|
132
|
-
const botFirstGuess = koishi_1.Random.int(
|
|
141
|
+
if (botShouldParticipate && state.started) {
|
|
142
|
+
const botFirstGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
|
|
133
143
|
state.botGuess.push(botFirstGuess);
|
|
134
144
|
guessNumberStates.set(key, state);
|
|
135
145
|
reply += '\n' + t('guessNumber.botFirstGuess', [botFirstGuess]);
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
guessNumber:
|
|
2
2
|
usage: 用法错误!正确用法:guess start/stop 或 直接输入数字
|
|
3
|
-
start:
|
|
3
|
+
start: 猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~
|
|
4
4
|
stop: 猜数字游戏已停止,正确答案是 {0}
|
|
5
|
-
invalid:
|
|
6
|
-
notStarted:
|
|
7
|
-
outOfRange:
|
|
8
|
-
tooSmall:
|
|
9
|
-
tooBig:
|
|
10
|
-
correct:
|
|
5
|
+
invalid: 请输入有效的整数!
|
|
6
|
+
notStarted: 猜数字游戏还未开始,请先发送 guess start 开始游戏
|
|
7
|
+
outOfRange: 数字超出当前范围 [{0}-{1}],请重新输入!
|
|
8
|
+
tooSmall: 猜小啦!当前范围更新为 [{0}-{1}]
|
|
9
|
+
tooBig: 猜大啦!当前范围更新为 [{0}-{1}]
|
|
10
|
+
correct: 恭喜你猜中了!正确数字就是 {0} 🎉
|
|
11
11
|
botFirstGuess: 我先来猜一个:{0},该你啦~
|
|
12
|
-
botWin:
|
|
13
|
-
botGuessTooSmall: 我猜 {0}
|
|
14
|
-
botGuessTooBig: 我猜 {0}
|
|
12
|
+
botWin: 哈哈我猜中了!正确数字是 {0},我赢啦 🎉
|
|
13
|
+
botGuessTooSmall: 我猜 {0},猜小啦!当前范围 [{1}-{2}]
|
|
14
|
+
botGuessTooBig: 我猜 {0},猜大啦!当前范围 [{1}-{2}]s
|
package/package.json
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-game-mini",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.7",
|
|
4
|
+
"description": "一款简单的猜数字小游戏,支持Bot 参与猜数",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"lib",
|
|
9
|
-
"
|
|
9
|
+
"src"
|
|
10
10
|
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc && copyfiles -u 1 src/locales/*.yml lib/locales/"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"koishi",
|
|
16
|
+
"plugin",
|
|
17
|
+
"game",
|
|
18
|
+
"猜数字"
|
|
19
|
+
],
|
|
20
|
+
"author": "Minecraft-1314",
|
|
21
|
+
"license": "MIT",
|
|
11
22
|
"repository": {
|
|
12
23
|
"type": "git",
|
|
13
24
|
"url": "git+https://github.com/Minecraft-1314/koishi-plugin-game-mini.git"
|
|
@@ -16,31 +27,12 @@
|
|
|
16
27
|
"url": "https://github.com/Minecraft-1314/koishi-plugin-game-mini/issues"
|
|
17
28
|
},
|
|
18
29
|
"homepage": "https://github.com/Minecraft-1314/koishi-plugin-game-mini#readme",
|
|
19
|
-
"
|
|
20
|
-
"description": {
|
|
21
|
-
"zh-CN": "Koishi猜数字小游戏插件"
|
|
22
|
-
},
|
|
23
|
-
"locales": [
|
|
24
|
-
"zh-CN"
|
|
25
|
-
],
|
|
26
|
-
"service": {
|
|
27
|
-
"required": [
|
|
28
|
-
"console"
|
|
29
|
-
]
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"@koishijs/plugin-console": "^5.0.0",
|
|
30
|
+
"peerDependencies": {
|
|
34
31
|
"koishi": "^4.0.0",
|
|
35
|
-
"yaml": "^2.
|
|
32
|
+
"yaml": "^2.0.0"
|
|
36
33
|
},
|
|
37
34
|
"devDependencies": {
|
|
38
|
-
"@types/node": "^20.0.0",
|
|
39
35
|
"typescript": "^5.0.0",
|
|
40
36
|
"copyfiles": "^2.4.1"
|
|
41
|
-
},
|
|
42
|
-
"scripts": {
|
|
43
|
-
"build": "tsc && copyfiles -u 1 src/locales/*.yml lib/locales/",
|
|
44
|
-
"dev": "tsc -w && copyfiles -u 1 src/locales/*.yml lib/locales/ -w"
|
|
45
37
|
}
|
|
46
38
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Context, Schema, Random } from 'koishi';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import yaml from 'yaml';
|
|
5
|
+
|
|
6
|
+
export const name = 'game-mini';
|
|
7
|
+
export const using = ['i18n'] as const;
|
|
8
|
+
|
|
9
|
+
export interface Config {}
|
|
10
|
+
export const Config: Schema<Config> = Schema.object({}).i18n({ 'zh-CN': {} });
|
|
11
|
+
|
|
12
|
+
interface GuessNumberState {
|
|
13
|
+
target: number
|
|
14
|
+
currentMin: number
|
|
15
|
+
currentMax: number
|
|
16
|
+
started: boolean
|
|
17
|
+
botGuess: number[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function apply(ctx: Context, config: Config) {
|
|
21
|
+
const zhCNPath = path.join(__dirname, './locales/zh-CN.yml');
|
|
22
|
+
const zhCNContent = fs.readFileSync(zhCNPath, 'utf8');
|
|
23
|
+
const zhCN = yaml.parse(zhCNContent);
|
|
24
|
+
ctx.i18n.define('zh-CN', zhCN);
|
|
25
|
+
|
|
26
|
+
const guessNumberStates = new Map<string, GuessNumberState>();
|
|
27
|
+
|
|
28
|
+
const getSessionKey = (session: any) => {
|
|
29
|
+
return session.channelId ? `group:${session.channelId}` : `private:${session.userId}`;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
ctx.middleware(async (session, next) => {
|
|
33
|
+
if (!session.content) return next();
|
|
34
|
+
const key = getSessionKey(session);
|
|
35
|
+
const state = guessNumberStates.get(key);
|
|
36
|
+
if (state && state.started) {
|
|
37
|
+
const input = session.content.trim();
|
|
38
|
+
const num = parseInt(input);
|
|
39
|
+
if (!isNaN(num) && Number.isInteger(num)) {
|
|
40
|
+
let reply = '';
|
|
41
|
+
if (num < state.currentMin || num > state.currentMax) {
|
|
42
|
+
reply = session.text('guessNumber.outOfRange', [state.currentMin, state.currentMax]);
|
|
43
|
+
} else if (num < state.target) {
|
|
44
|
+
state.currentMin = num + 1;
|
|
45
|
+
reply = session.text('guessNumber.tooSmall', [state.currentMin, state.currentMax]);
|
|
46
|
+
} else if (num > state.target) {
|
|
47
|
+
state.currentMax = num - 1;
|
|
48
|
+
reply = session.text('guessNumber.tooBig', [state.currentMin, state.currentMax]);
|
|
49
|
+
} else {
|
|
50
|
+
state.started = false;
|
|
51
|
+
guessNumberStates.set(key, state);
|
|
52
|
+
reply = session.text('guessNumber.correct', [state.target]);
|
|
53
|
+
await session.send(reply);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
guessNumberStates.set(key, state);
|
|
58
|
+
const isPrivate = !session.channelId;
|
|
59
|
+
const botShouldParticipate = isPrivate || true;
|
|
60
|
+
if (botShouldParticipate && state.started && state.currentMin <= state.currentMax) {
|
|
61
|
+
let botGuess;
|
|
62
|
+
do {
|
|
63
|
+
botGuess = Random.int(state.currentMin, state.currentMax);
|
|
64
|
+
} while (state.botGuess.includes(botGuess));
|
|
65
|
+
state.botGuess.push(botGuess);
|
|
66
|
+
guessNumberStates.set(key, state);
|
|
67
|
+
|
|
68
|
+
if (botGuess === state.target) {
|
|
69
|
+
reply += '\n' + session.text('guessNumber.botWin', [botGuess]);
|
|
70
|
+
state.started = false;
|
|
71
|
+
guessNumberStates.set(key, state);
|
|
72
|
+
} else if (botGuess < state.target) {
|
|
73
|
+
state.currentMin = botGuess + 1;
|
|
74
|
+
guessNumberStates.set(key, state);
|
|
75
|
+
reply += '\n' + session.text('guessNumber.botGuessTooSmall', [botGuess, state.currentMin, state.currentMax]);
|
|
76
|
+
} else {
|
|
77
|
+
state.currentMax = botGuess - 1;
|
|
78
|
+
guessNumberStates.set(key, state);
|
|
79
|
+
reply += '\n' + session.text('guessNumber.botGuessTooBig', [botGuess, state.currentMin, state.currentMax]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
await session.send(reply);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return next();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
ctx.command('guess <action:string>')
|
|
90
|
+
.action(async ({ session }, action) => {
|
|
91
|
+
if (!session) return '';
|
|
92
|
+
const key = getSessionKey(session);
|
|
93
|
+
let state = guessNumberStates.get(key) || {
|
|
94
|
+
target: 0,
|
|
95
|
+
currentMin: 0,
|
|
96
|
+
currentMax: 100,
|
|
97
|
+
started: false,
|
|
98
|
+
botGuess: []
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
if (!action) return session.text('guessNumber.usage');
|
|
102
|
+
|
|
103
|
+
if (action === 'start') {
|
|
104
|
+
state.target = Random.int(0, 100);
|
|
105
|
+
state.currentMin = 0;
|
|
106
|
+
state.currentMax = 100;
|
|
107
|
+
state.started = true;
|
|
108
|
+
state.botGuess = [];
|
|
109
|
+
guessNumberStates.set(key, state);
|
|
110
|
+
// 修复:手动拼接范围字符串,避免模板解析异常
|
|
111
|
+
const startText = `猜数字游戏开始!初始范围是 ${state.currentMin}-${state.currentMax},快来猜猜看~`;
|
|
112
|
+
let reply = startText;
|
|
113
|
+
const isPrivate = !session.channelId;
|
|
114
|
+
const botShouldParticipate = isPrivate || true;
|
|
115
|
+
if (botShouldParticipate && state.started) {
|
|
116
|
+
const botFirstGuess = Random.int(state.currentMin, state.currentMax);
|
|
117
|
+
state.botGuess.push(botFirstGuess);
|
|
118
|
+
guessNumberStates.set(key, state);
|
|
119
|
+
reply += '\n' + `我先来猜一个:${botFirstGuess},该你啦~`;
|
|
120
|
+
}
|
|
121
|
+
// 修复:使用 send 方法而非直接返回,避免附带无关数字
|
|
122
|
+
await session.send(reply);
|
|
123
|
+
return '';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (action === 'stop') {
|
|
127
|
+
state.started = false;
|
|
128
|
+
guessNumberStates.set(key, state);
|
|
129
|
+
const stopText = `猜数字游戏已停止,正确数字是 ${state.target}`;
|
|
130
|
+
await session.send(stopText);
|
|
131
|
+
return '';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const usageText = session.text('guessNumber.usage');
|
|
135
|
+
await session.send(usageText);
|
|
136
|
+
return '';
|
|
137
|
+
});
|
|
138
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
guessNumber:
|
|
2
|
+
usage: 用法错误!正确用法:guess start/stop 或 直接输入数字
|
|
3
|
+
start: 猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~
|
|
4
|
+
stop: 猜数字游戏已停止,正确数字是 {0}
|
|
5
|
+
invalid: 请输入有效的整数!
|
|
6
|
+
notStarted: 游戏还未开始,请先发送 guess start 开始游戏
|
|
7
|
+
outOfRange: 数字超出当前范围 [{0}-{1}],请重新输入!
|
|
8
|
+
tooSmall: 猜小啦!当前范围更新为 [{0}-{1}]
|
|
9
|
+
tooBig: 猜大啦!当前范围更新为 [{0}-{1}]
|
|
10
|
+
correct: 恭喜你猜中了!正确数字就是 {0} 🎉
|
|
11
|
+
botFirstGuess: 我先来猜一个:{0},该你啦~
|
|
12
|
+
botWin: 哈哈我猜中了!正确数字是 {0},我赢啦 🎉
|
|
13
|
+
botGuessTooSmall: 我猜 {0},猜小啦!当前范围 [{1}-{2}]
|
|
14
|
+
botGuessTooBig: 我猜 {0},猜大啦!当前范围 [{1}-{2}]
|