koishi-plugin-game-mini 0.0.7 → 0.0.8
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.d.ts +0 -6
- package/lib/index.js +37 -65
- package/lib/locales/locales/zh-CN.yml +3 -3
- package/package.json +1 -1
- package/src/index.ts +15 -17
package/lib/index.d.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { Context, Schema } from 'koishi';
|
|
2
2
|
export declare const name = "game-mini";
|
|
3
3
|
export declare const using: readonly ["i18n"];
|
|
4
|
-
export declare const inject: readonly ["console"];
|
|
5
4
|
export interface Config {
|
|
6
|
-
guessNumber: {
|
|
7
|
-
min: number;
|
|
8
|
-
max: number;
|
|
9
|
-
botParticipateInGroup: boolean;
|
|
10
|
-
};
|
|
11
5
|
}
|
|
12
6
|
export declare const Config: Schema<Config>;
|
|
13
7
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Config = exports.
|
|
6
|
+
exports.Config = exports.using = exports.name = void 0;
|
|
7
7
|
exports.apply = apply;
|
|
8
8
|
const koishi_1 = require("koishi");
|
|
9
9
|
const fs_1 = __importDefault(require("fs"));
|
|
@@ -11,46 +11,12 @@ const path_1 = __importDefault(require("path"));
|
|
|
11
11
|
const yaml_1 = __importDefault(require("yaml"));
|
|
12
12
|
exports.name = 'game-mini';
|
|
13
13
|
exports.using = ['i18n'];
|
|
14
|
-
exports.
|
|
15
|
-
exports.Config = koishi_1.Schema.object({
|
|
16
|
-
guessNumber: koishi_1.Schema.object({
|
|
17
|
-
min: koishi_1.Schema.number().min(0).default(0).description('猜数字最小值(最低为0)'),
|
|
18
|
-
max: koishi_1.Schema.number().min(1).default(100).description('猜数字最大值'),
|
|
19
|
-
botParticipateInGroup: koishi_1.Schema.boolean().default(true).description('群聊中Bot是否参与猜数字(私聊强制参与)'),
|
|
20
|
-
}).description('猜数字游戏配置'),
|
|
21
|
-
}).i18n({ 'zh-CN': {} });
|
|
14
|
+
exports.Config = koishi_1.Schema.object({}).i18n({ 'zh-CN': {} });
|
|
22
15
|
function apply(ctx, config) {
|
|
23
|
-
const zhCNPath = path_1.default.
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
start: '猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~',
|
|
28
|
-
stop: '猜数字游戏已停止,正确答案是 {0}',
|
|
29
|
-
invalid: '请输入有效的整数!',
|
|
30
|
-
notStarted: '游戏还未开始,请先发送 guess start 开始游戏',
|
|
31
|
-
outOfRange: '数字超出当前范围 [{0}-{1}],请重新输入!',
|
|
32
|
-
tooSmall: '猜小啦!当前范围更新为 [{0}-{1}]',
|
|
33
|
-
tooBig: '猜大啦!当前范围更新为 [{0}-{1}]',
|
|
34
|
-
correct: '恭喜你猜中了!数字就是 {0} 🎉',
|
|
35
|
-
botFirstGuess: '我先来猜一个:{0},该你啦~',
|
|
36
|
-
botWin: '哈哈我猜中了!数字是 {0},我赢啦 🎉',
|
|
37
|
-
botGuessTooSmall: '我猜 {0},猜小啦!当前范围 [{1}-{2}]',
|
|
38
|
-
botGuessTooBig: '我猜 {0},猜大啦!当前范围 [{1}-{2}]'
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
if (fs_1.default.existsSync(zhCNPath)) {
|
|
42
|
-
try {
|
|
43
|
-
const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
|
|
44
|
-
const zhCN = yaml_1.default.parse(zhCNContent);
|
|
45
|
-
ctx.i18n.define('zh-CN', zhCN);
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
ctx.i18n.define('zh-CN', defaultI18n);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
ctx.i18n.define('zh-CN', defaultI18n);
|
|
53
|
-
}
|
|
16
|
+
const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
|
|
17
|
+
const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
|
|
18
|
+
const zhCN = yaml_1.default.parse(zhCNContent);
|
|
19
|
+
ctx.i18n.define('zh-CN', zhCN);
|
|
54
20
|
const guessNumberStates = new Map();
|
|
55
21
|
const getSessionKey = (session) => {
|
|
56
22
|
return session.channelId ? `group:${session.channelId}` : `private:${session.userId}`;
|
|
@@ -66,25 +32,26 @@ function apply(ctx, config) {
|
|
|
66
32
|
if (!isNaN(num) && Number.isInteger(num)) {
|
|
67
33
|
let reply = '';
|
|
68
34
|
if (num < state.currentMin || num > state.currentMax) {
|
|
69
|
-
reply =
|
|
35
|
+
reply = `数字超出当前范围 [${state.currentMin}-${state.currentMax}],请重新输入!`;
|
|
70
36
|
}
|
|
71
37
|
else if (num < state.target) {
|
|
72
38
|
state.currentMin = num + 1;
|
|
73
|
-
reply =
|
|
39
|
+
reply = `猜小啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
|
|
74
40
|
}
|
|
75
41
|
else if (num > state.target) {
|
|
76
42
|
state.currentMax = num - 1;
|
|
77
|
-
reply =
|
|
43
|
+
reply = `猜大啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
|
|
78
44
|
}
|
|
79
45
|
else {
|
|
80
46
|
state.started = false;
|
|
81
47
|
guessNumberStates.set(key, state);
|
|
82
|
-
reply =
|
|
83
|
-
|
|
48
|
+
reply = `恭喜你猜中了!正确数字就是 ${state.target} 🎉`;
|
|
49
|
+
await session.send(reply);
|
|
50
|
+
return;
|
|
84
51
|
}
|
|
85
52
|
guessNumberStates.set(key, state);
|
|
86
53
|
const isPrivate = !session.channelId;
|
|
87
|
-
const botShouldParticipate = isPrivate ||
|
|
54
|
+
const botShouldParticipate = isPrivate || true;
|
|
88
55
|
if (botShouldParticipate && state.started && state.currentMin <= state.currentMax) {
|
|
89
56
|
let botGuess;
|
|
90
57
|
do {
|
|
@@ -93,22 +60,23 @@ function apply(ctx, config) {
|
|
|
93
60
|
state.botGuess.push(botGuess);
|
|
94
61
|
guessNumberStates.set(key, state);
|
|
95
62
|
if (botGuess === state.target) {
|
|
96
|
-
reply +=
|
|
63
|
+
reply += `\n哈哈我猜中了!正确数字是 ${botGuess},我赢啦 🎉`;
|
|
97
64
|
state.started = false;
|
|
98
65
|
guessNumberStates.set(key, state);
|
|
99
66
|
}
|
|
100
67
|
else if (botGuess < state.target) {
|
|
101
68
|
state.currentMin = botGuess + 1;
|
|
102
69
|
guessNumberStates.set(key, state);
|
|
103
|
-
reply +=
|
|
70
|
+
reply += `\n我猜 ${botGuess},猜小啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
|
|
104
71
|
}
|
|
105
72
|
else {
|
|
106
73
|
state.currentMax = botGuess - 1;
|
|
107
74
|
guessNumberStates.set(key, state);
|
|
108
|
-
reply +=
|
|
75
|
+
reply += `\n我猜 ${botGuess},猜大啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
|
|
109
76
|
}
|
|
110
77
|
}
|
|
111
|
-
|
|
78
|
+
await session.send(reply);
|
|
79
|
+
return;
|
|
112
80
|
}
|
|
113
81
|
}
|
|
114
82
|
return next();
|
|
@@ -116,41 +84,45 @@ function apply(ctx, config) {
|
|
|
116
84
|
ctx.command('guess <action:string>')
|
|
117
85
|
.action(async ({ session }, action) => {
|
|
118
86
|
if (!session)
|
|
119
|
-
return;
|
|
120
|
-
const t = (key, ...args) => session.text(key, args);
|
|
87
|
+
return '';
|
|
121
88
|
const key = getSessionKey(session);
|
|
122
89
|
let state = guessNumberStates.get(key) || {
|
|
123
90
|
target: 0,
|
|
124
|
-
currentMin:
|
|
125
|
-
currentMax:
|
|
91
|
+
currentMin: 0,
|
|
92
|
+
currentMax: 100,
|
|
126
93
|
started: false,
|
|
127
94
|
botGuess: []
|
|
128
95
|
};
|
|
129
|
-
if (!action)
|
|
130
|
-
|
|
96
|
+
if (!action) {
|
|
97
|
+
await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
|
|
98
|
+
return '';
|
|
99
|
+
}
|
|
131
100
|
if (action === 'start') {
|
|
132
|
-
state.target = koishi_1.Random.int(
|
|
133
|
-
state.currentMin =
|
|
134
|
-
state.currentMax =
|
|
101
|
+
state.target = koishi_1.Random.int(0, 100);
|
|
102
|
+
state.currentMin = 0;
|
|
103
|
+
state.currentMax = 100;
|
|
135
104
|
state.started = true;
|
|
136
105
|
state.botGuess = [];
|
|
137
106
|
guessNumberStates.set(key, state);
|
|
138
|
-
let reply =
|
|
107
|
+
let reply = `猜数字游戏开始!初始范围是 ${state.currentMin}-${state.currentMax},快来猜猜看~`;
|
|
139
108
|
const isPrivate = !session.channelId;
|
|
140
|
-
const botShouldParticipate = isPrivate ||
|
|
109
|
+
const botShouldParticipate = isPrivate || true;
|
|
141
110
|
if (botShouldParticipate && state.started) {
|
|
142
111
|
const botFirstGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
|
|
143
112
|
state.botGuess.push(botFirstGuess);
|
|
144
113
|
guessNumberStates.set(key, state);
|
|
145
|
-
reply +=
|
|
114
|
+
reply += `\n我先来猜一个:${botFirstGuess},该你啦~`;
|
|
146
115
|
}
|
|
147
|
-
|
|
116
|
+
await session.send(reply);
|
|
117
|
+
return '';
|
|
148
118
|
}
|
|
149
119
|
if (action === 'stop') {
|
|
150
120
|
state.started = false;
|
|
151
121
|
guessNumberStates.set(key, state);
|
|
152
|
-
|
|
122
|
+
await session.send(`猜数字游戏已停止,正确数字是 ${state.target}`);
|
|
123
|
+
return '';
|
|
153
124
|
}
|
|
154
|
-
|
|
125
|
+
await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
|
|
126
|
+
return '';
|
|
155
127
|
});
|
|
156
128
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
guessNumber:
|
|
2
2
|
usage: 用法错误!正确用法:guess start/stop 或 直接输入数字
|
|
3
3
|
start: 猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~
|
|
4
|
-
stop:
|
|
4
|
+
stop: 猜数字游戏已停止,正确数字是 {0}
|
|
5
5
|
invalid: 请输入有效的整数!
|
|
6
|
-
notStarted:
|
|
6
|
+
notStarted: 游戏还未开始,请先发送 guess start 开始游戏
|
|
7
7
|
outOfRange: 数字超出当前范围 [{0}-{1}],请重新输入!
|
|
8
8
|
tooSmall: 猜小啦!当前范围更新为 [{0}-{1}]
|
|
9
9
|
tooBig: 猜大啦!当前范围更新为 [{0}-{1}]
|
|
@@ -11,4 +11,4 @@ guessNumber:
|
|
|
11
11
|
botFirstGuess: 我先来猜一个:{0},该你啦~
|
|
12
12
|
botWin: 哈哈我猜中了!正确数字是 {0},我赢啦 🎉
|
|
13
13
|
botGuessTooSmall: 我猜 {0},猜小啦!当前范围 [{1}-{2}]
|
|
14
|
-
botGuessTooBig: 我猜 {0},猜大啦!当前范围 [{1}-{2}]
|
|
14
|
+
botGuessTooBig: 我猜 {0},猜大啦!当前范围 [{1}-{2}]
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -39,17 +39,17 @@ export function apply(ctx: Context, config: Config) {
|
|
|
39
39
|
if (!isNaN(num) && Number.isInteger(num)) {
|
|
40
40
|
let reply = '';
|
|
41
41
|
if (num < state.currentMin || num > state.currentMax) {
|
|
42
|
-
reply =
|
|
42
|
+
reply = `数字超出当前范围 [${state.currentMin}-${state.currentMax}],请重新输入!`;
|
|
43
43
|
} else if (num < state.target) {
|
|
44
44
|
state.currentMin = num + 1;
|
|
45
|
-
reply =
|
|
45
|
+
reply = `猜小啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
|
|
46
46
|
} else if (num > state.target) {
|
|
47
47
|
state.currentMax = num - 1;
|
|
48
|
-
reply =
|
|
48
|
+
reply = `猜大啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
|
|
49
49
|
} else {
|
|
50
50
|
state.started = false;
|
|
51
51
|
guessNumberStates.set(key, state);
|
|
52
|
-
reply =
|
|
52
|
+
reply = `恭喜你猜中了!正确数字就是 ${state.target} 🎉`;
|
|
53
53
|
await session.send(reply);
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
@@ -66,17 +66,17 @@ export function apply(ctx: Context, config: Config) {
|
|
|
66
66
|
guessNumberStates.set(key, state);
|
|
67
67
|
|
|
68
68
|
if (botGuess === state.target) {
|
|
69
|
-
reply +=
|
|
69
|
+
reply += `\n哈哈我猜中了!正确数字是 ${botGuess},我赢啦 🎉`;
|
|
70
70
|
state.started = false;
|
|
71
71
|
guessNumberStates.set(key, state);
|
|
72
72
|
} else if (botGuess < state.target) {
|
|
73
73
|
state.currentMin = botGuess + 1;
|
|
74
74
|
guessNumberStates.set(key, state);
|
|
75
|
-
reply +=
|
|
75
|
+
reply += `\n我猜 ${botGuess},猜小啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
|
|
76
76
|
} else {
|
|
77
77
|
state.currentMax = botGuess - 1;
|
|
78
78
|
guessNumberStates.set(key, state);
|
|
79
|
-
reply +=
|
|
79
|
+
reply += `\n我猜 ${botGuess},猜大啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
await session.send(reply);
|
|
@@ -98,7 +98,10 @@ export function apply(ctx: Context, config: Config) {
|
|
|
98
98
|
botGuess: []
|
|
99
99
|
};
|
|
100
100
|
|
|
101
|
-
if (!action)
|
|
101
|
+
if (!action) {
|
|
102
|
+
await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
|
|
103
|
+
return '';
|
|
104
|
+
}
|
|
102
105
|
|
|
103
106
|
if (action === 'start') {
|
|
104
107
|
state.target = Random.int(0, 100);
|
|
@@ -107,18 +110,15 @@ export function apply(ctx: Context, config: Config) {
|
|
|
107
110
|
state.started = true;
|
|
108
111
|
state.botGuess = [];
|
|
109
112
|
guessNumberStates.set(key, state);
|
|
110
|
-
|
|
111
|
-
const startText = `猜数字游戏开始!初始范围是 ${state.currentMin}-${state.currentMax},快来猜猜看~`;
|
|
112
|
-
let reply = startText;
|
|
113
|
+
let reply = `猜数字游戏开始!初始范围是 ${state.currentMin}-${state.currentMax},快来猜猜看~`;
|
|
113
114
|
const isPrivate = !session.channelId;
|
|
114
115
|
const botShouldParticipate = isPrivate || true;
|
|
115
116
|
if (botShouldParticipate && state.started) {
|
|
116
117
|
const botFirstGuess = Random.int(state.currentMin, state.currentMax);
|
|
117
118
|
state.botGuess.push(botFirstGuess);
|
|
118
119
|
guessNumberStates.set(key, state);
|
|
119
|
-
reply +=
|
|
120
|
+
reply += `\n我先来猜一个:${botFirstGuess},该你啦~`;
|
|
120
121
|
}
|
|
121
|
-
// 修复:使用 send 方法而非直接返回,避免附带无关数字
|
|
122
122
|
await session.send(reply);
|
|
123
123
|
return '';
|
|
124
124
|
}
|
|
@@ -126,13 +126,11 @@ export function apply(ctx: Context, config: Config) {
|
|
|
126
126
|
if (action === 'stop') {
|
|
127
127
|
state.started = false;
|
|
128
128
|
guessNumberStates.set(key, state);
|
|
129
|
-
|
|
130
|
-
await session.send(stopText);
|
|
129
|
+
await session.send(`猜数字游戏已停止,正确数字是 ${state.target}`);
|
|
131
130
|
return '';
|
|
132
131
|
}
|
|
133
132
|
|
|
134
|
-
|
|
135
|
-
await session.send(usageText);
|
|
133
|
+
await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
|
|
136
134
|
return '';
|
|
137
135
|
});
|
|
138
136
|
}
|