koishi-plugin-game-mini 0.0.7 → 0.0.9

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 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.inject = exports.using = exports.name = void 0;
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,44 +11,35 @@ 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.inject = ['console'];
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.resolve(__dirname, './locales/zh-CN.yml');
24
16
  const defaultI18n = {
25
17
  guessNumber: {
26
18
  usage: '用法错误!正确用法:guess start/stop 或 直接输入数字',
27
19
  start: '猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~',
28
- stop: '猜数字游戏已停止,正确答案是 {0}',
29
- invalid: '请输入有效的整数!',
30
- notStarted: '游戏还未开始,请先发送 guess start 开始游戏',
20
+ stop: '猜数字游戏已停止,正确数字是 {0}',
31
21
  outOfRange: '数字超出当前范围 [{0}-{1}],请重新输入!',
32
22
  tooSmall: '猜小啦!当前范围更新为 [{0}-{1}]',
33
23
  tooBig: '猜大啦!当前范围更新为 [{0}-{1}]',
34
- correct: '恭喜你猜中了!数字就是 {0} 🎉',
24
+ correct: '恭喜你猜中了!正确数字就是 {0} 🎉',
35
25
  botFirstGuess: '我先来猜一个:{0},该你啦~',
36
- botWin: '哈哈我猜中了!数字是 {0},我赢啦 🎉',
26
+ botWin: '哈哈我猜中了!正确数字是 {0},我赢啦 🎉',
37
27
  botGuessTooSmall: '我猜 {0},猜小啦!当前范围 [{1}-{2}]',
38
28
  botGuessTooBig: '我猜 {0},猜大啦!当前范围 [{1}-{2}]'
39
29
  }
40
30
  };
41
- if (fs_1.default.existsSync(zhCNPath)) {
42
- try {
31
+ try {
32
+ const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
33
+ if (fs_1.default.existsSync(zhCNPath)) {
43
34
  const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
44
35
  const zhCN = yaml_1.default.parse(zhCNContent);
45
36
  ctx.i18n.define('zh-CN', zhCN);
46
37
  }
47
- catch (error) {
38
+ else {
48
39
  ctx.i18n.define('zh-CN', defaultI18n);
49
40
  }
50
41
  }
51
- else {
42
+ catch (error) {
52
43
  ctx.i18n.define('zh-CN', defaultI18n);
53
44
  }
54
45
  const guessNumberStates = new Map();
@@ -80,11 +71,12 @@ function apply(ctx, config) {
80
71
  state.started = false;
81
72
  guessNumberStates.set(key, state);
82
73
  reply = session.text('guessNumber.correct', [state.target]);
83
- return session.send(reply);
74
+ await session.send(reply);
75
+ return;
84
76
  }
85
77
  guessNumberStates.set(key, state);
86
78
  const isPrivate = !session.channelId;
87
- const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
79
+ const botShouldParticipate = isPrivate || true;
88
80
  if (botShouldParticipate && state.started && state.currentMin <= state.currentMax) {
89
81
  let botGuess;
90
82
  do {
@@ -108,7 +100,8 @@ function apply(ctx, config) {
108
100
  reply += '\n' + session.text('guessNumber.botGuessTooBig', [botGuess, state.currentMin, state.currentMax]);
109
101
  }
110
102
  }
111
- return session.send(reply);
103
+ await session.send(reply);
104
+ return;
112
105
  }
113
106
  }
114
107
  return next();
@@ -116,41 +109,45 @@ function apply(ctx, config) {
116
109
  ctx.command('guess <action:string>')
117
110
  .action(async ({ session }, action) => {
118
111
  if (!session)
119
- return;
120
- const t = (key, ...args) => session.text(key, args);
112
+ return '';
121
113
  const key = getSessionKey(session);
122
114
  let state = guessNumberStates.get(key) || {
123
115
  target: 0,
124
- currentMin: config.guessNumber.min,
125
- currentMax: config.guessNumber.max,
116
+ currentMin: 0,
117
+ currentMax: 100,
126
118
  started: false,
127
119
  botGuess: []
128
120
  };
129
- if (!action)
130
- return t('guessNumber.usage');
121
+ if (!action) {
122
+ await session.send(session.text('guessNumber.usage'));
123
+ return '';
124
+ }
131
125
  if (action === 'start') {
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;
126
+ state.target = koishi_1.Random.int(0, 100);
127
+ state.currentMin = 0;
128
+ state.currentMax = 100;
135
129
  state.started = true;
136
130
  state.botGuess = [];
137
131
  guessNumberStates.set(key, state);
138
- let reply = t('guessNumber.start', [config.guessNumber.min, config.guessNumber.max]);
132
+ let reply = session.text('guessNumber.start', [0, 100]);
139
133
  const isPrivate = !session.channelId;
140
- const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
134
+ const botShouldParticipate = isPrivate || true;
141
135
  if (botShouldParticipate && state.started) {
142
136
  const botFirstGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
143
137
  state.botGuess.push(botFirstGuess);
144
138
  guessNumberStates.set(key, state);
145
- reply += '\n' + t('guessNumber.botFirstGuess', [botFirstGuess]);
139
+ reply += '\n' + session.text('guessNumber.botFirstGuess', [botFirstGuess]);
146
140
  }
147
- return reply;
141
+ await session.send(reply);
142
+ return '';
148
143
  }
149
144
  if (action === 'stop') {
150
145
  state.started = false;
151
146
  guessNumberStates.set(key, state);
152
- return t('guessNumber.stop', [state.target]);
147
+ await session.send(session.text('guessNumber.stop', [state.target]));
148
+ return '';
153
149
  }
154
- return t('guessNumber.usage');
150
+ await session.send(session.text('guessNumber.usage'));
151
+ return '';
155
152
  });
156
153
  }
@@ -1,9 +1,7 @@
1
1
  guessNumber:
2
2
  usage: 用法错误!正确用法:guess start/stop 或 直接输入数字
3
3
  start: 猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~
4
- stop: 猜数字游戏已停止,正确答案是 {0}
5
- invalid: 请输入有效的整数!
6
- notStarted: 猜数字游戏还未开始,请先发送 guess start 开始游戏
4
+ stop: 猜数字游戏已停止,正确数字是 {0}
7
5
  outOfRange: 数字超出当前范围 [{0}-{1}],请重新输入!
8
6
  tooSmall: 猜小啦!当前范围更新为 [{0}-{1}]
9
7
  tooBig: 猜大啦!当前范围更新为 [{0}-{1}]
@@ -11,4 +9,4 @@ guessNumber:
11
9
  botFirstGuess: 我先来猜一个:{0},该你啦~
12
10
  botWin: 哈哈我猜中了!正确数字是 {0},我赢啦 🎉
13
11
  botGuessTooSmall: 我猜 {0},猜小啦!当前范围 [{1}-{2}]
14
- botGuessTooBig: 我猜 {0},猜大啦!当前范围 [{1}-{2}]s
12
+ botGuessTooBig: 我猜 {0},猜大啦!当前范围 [{1}-{2}]
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "koishi-plugin-game-mini",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "一款简单的猜数字小游戏,支持Bot 参与猜数",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
8
- "lib",
9
- "src"
8
+ "lib"
10
9
  ],
11
10
  "scripts": {
12
11
  "build": "tsc && copyfiles -u 1 src/locales/*.yml lib/locales/"
package/src/index.ts DELETED
@@ -1,138 +0,0 @@
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
- }
@@ -1,14 +0,0 @@
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}]