koishi-plugin-game-mini 0.0.8 → 0.1.0

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,7 +1,13 @@
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"];
4
5
  export interface Config {
6
+ guessNumber: {
7
+ min: number;
8
+ max: number;
9
+ botParticipateInGroup: boolean;
10
+ };
5
11
  }
6
12
  export declare const Config: Schema<Config>;
7
13
  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.using = exports.name = void 0;
6
+ exports.Config = exports.inject = 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,118 +11,145 @@ 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.Config = koishi_1.Schema.object({}).i18n({ 'zh-CN': {} });
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),
18
+ max: koishi_1.Schema.number().min(1).default(100),
19
+ botParticipateInGroup: koishi_1.Schema.boolean().default(true)
20
+ }).description('猜数字游戏配置')
21
+ }).i18n({ 'zh-CN': {} });
15
22
  function apply(ctx, config) {
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);
20
- const guessNumberStates = new Map();
21
- const getSessionKey = (session) => {
22
- return session.channelId ? `group:${session.channelId}` : `private:${session.userId}`;
23
+ const defaultI18n = {
24
+ guessNumber: {
25
+ usage: '用法错误!正确用法:guess start/stop 或 直接输入数字',
26
+ start: '猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~',
27
+ stop: '猜数字游戏已停止,正确数字是 {0}',
28
+ outOfRange: '数字超出当前范围 [{0}-{1}],请重新输入!',
29
+ tooSmall: '猜小啦!当前范围更新为 [{0}-{1}]',
30
+ tooBig: '猜大啦!当前范围更新为 [{0}-{1}]',
31
+ correct: '恭喜你猜中了!正确数字就是 {0} 🎉',
32
+ botFirstGuess: '我先来猜一个:{0},该你啦~',
33
+ botWin: '哈哈我猜中了!我猜的是 {0},正确数字就是 {0},我赢啦 🎉',
34
+ botGuessTooSmall: '我猜 {0},猜小啦!当前范围 [{1}-{2}]',
35
+ botGuessTooBig: '我猜 {0},猜大啦!当前范围 [{1}-{2}]'
36
+ }
23
37
  };
38
+ try {
39
+ const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
40
+ if (fs_1.default.existsSync(zhCNPath)) {
41
+ const zhCN = yaml_1.default.parse(fs_1.default.readFileSync(zhCNPath, 'utf8'));
42
+ ctx.i18n.define('zh-CN', zhCN);
43
+ }
44
+ else {
45
+ ctx.i18n.define('zh-CN', defaultI18n);
46
+ }
47
+ }
48
+ catch (e) {
49
+ ctx.i18n.define('zh-CN', defaultI18n);
50
+ }
51
+ const guessNumberStates = new Map();
52
+ const getSessionKey = (session) => session.channelId ? `group:${session.channelId}` : `private:${session.userId}`;
24
53
  ctx.middleware(async (session, next) => {
25
54
  if (!session.content)
26
55
  return next();
27
56
  const key = getSessionKey(session);
28
57
  const state = guessNumberStates.get(key);
29
- if (state && state.started) {
30
- const input = session.content.trim();
31
- const num = parseInt(input);
32
- if (!isNaN(num) && Number.isInteger(num)) {
33
- let reply = '';
34
- if (num < state.currentMin || num > state.currentMax) {
35
- reply = `数字超出当前范围 [${state.currentMin}-${state.currentMax}],请重新输入!`;
36
- }
37
- else if (num < state.target) {
38
- state.currentMin = num + 1;
39
- reply = `猜小啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
40
- }
41
- else if (num > state.target) {
42
- state.currentMax = num - 1;
43
- reply = `猜大啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
44
- }
45
- else {
46
- state.started = false;
47
- guessNumberStates.set(key, state);
48
- reply = `恭喜你猜中了!正确数字就是 ${state.target} 🎉`;
49
- await session.send(reply);
50
- return;
51
- }
58
+ if (!state || !state.started)
59
+ return next();
60
+ const num = parseInt(session.content.trim());
61
+ if (isNaN(num) || !Number.isInteger(num))
62
+ return next();
63
+ let reply = '';
64
+ if (num < state.currentMin || num > state.currentMax) {
65
+ reply = session.text('guessNumber.outOfRange', [state.currentMin, state.currentMax]);
66
+ }
67
+ else if (num < state.target) {
68
+ state.currentMin = num + 1;
69
+ reply = session.text('guessNumber.tooSmall', [state.currentMin, state.currentMax]);
70
+ }
71
+ else if (num > state.target) {
72
+ state.currentMax = num - 1;
73
+ reply = session.text('guessNumber.tooBig', [state.currentMin, state.currentMax]);
74
+ }
75
+ else {
76
+ state.started = false;
77
+ guessNumberStates.set(key, state);
78
+ reply = session.text('guessNumber.correct', [state.target]);
79
+ await session.send(reply);
80
+ return;
81
+ }
82
+ guessNumberStates.set(key, state);
83
+ const isPrivate = !session.channelId;
84
+ const botPlay = isPrivate || config.guessNumber.botParticipateInGroup;
85
+ if (botPlay && state.started && state.currentMin <= state.currentMax) {
86
+ let botGuess;
87
+ let safe = 0;
88
+ do {
89
+ botGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
90
+ safe++;
91
+ } while ((botGuess === state.target || state.botGuess.includes(botGuess)) && safe < 20);
92
+ state.botGuess.push(botGuess);
93
+ guessNumberStates.set(key, state);
94
+ if (botGuess === state.target) {
95
+ reply += session.text('guessNumber.botWin', [botGuess]);
96
+ state.started = false;
52
97
  guessNumberStates.set(key, state);
53
- const isPrivate = !session.channelId;
54
- const botShouldParticipate = isPrivate || true;
55
- if (botShouldParticipate && state.started && state.currentMin <= state.currentMax) {
56
- let botGuess;
57
- do {
58
- botGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
59
- } while (state.botGuess.includes(botGuess));
60
- state.botGuess.push(botGuess);
61
- guessNumberStates.set(key, state);
62
- if (botGuess === state.target) {
63
- reply += `\n哈哈我猜中了!正确数字是 ${botGuess},我赢啦 🎉`;
64
- state.started = false;
65
- guessNumberStates.set(key, state);
66
- }
67
- else if (botGuess < state.target) {
68
- state.currentMin = botGuess + 1;
69
- guessNumberStates.set(key, state);
70
- reply += `\n我猜 ${botGuess},猜小啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
71
- }
72
- else {
73
- state.currentMax = botGuess - 1;
74
- guessNumberStates.set(key, state);
75
- reply += `\n我猜 ${botGuess},猜大啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
76
- }
77
- }
78
- await session.send(reply);
79
- return;
98
+ }
99
+ else if (botGuess < state.target) {
100
+ state.currentMin = botGuess + 1;
101
+ guessNumberStates.set(key, state);
102
+ reply += session.text('guessNumber.botGuessTooSmall', [botGuess, state.currentMin, state.currentMax]);
103
+ }
104
+ else {
105
+ state.currentMax = botGuess - 1;
106
+ guessNumberStates.set(key, state);
107
+ reply += session.text('guessNumber.botGuessTooBig', [botGuess, state.currentMin, state.currentMax]);
80
108
  }
81
109
  }
82
- return next();
110
+ await session.send(reply);
83
111
  });
84
- ctx.command('guess <action:string>')
85
- .action(async ({ session }, action) => {
112
+ ctx.command('guess <action:string>').action(async ({ session }, action) => {
86
113
  if (!session)
87
114
  return '';
88
115
  const key = getSessionKey(session);
89
116
  let state = guessNumberStates.get(key) || {
90
117
  target: 0,
91
- currentMin: 0,
92
- currentMax: 100,
118
+ currentMin: config.guessNumber.min,
119
+ currentMax: config.guessNumber.max,
93
120
  started: false,
94
121
  botGuess: []
95
122
  };
96
123
  if (!action) {
97
- await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
124
+ await session.send(session.text('guessNumber.usage'));
98
125
  return '';
99
126
  }
100
127
  if (action === 'start') {
101
- state.target = koishi_1.Random.int(0, 100);
102
- state.currentMin = 0;
103
- state.currentMax = 100;
128
+ state.target = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
129
+ state.currentMin = config.guessNumber.min;
130
+ state.currentMax = config.guessNumber.max;
104
131
  state.started = true;
105
132
  state.botGuess = [];
106
133
  guessNumberStates.set(key, state);
107
- let reply = `猜数字游戏开始!初始范围是 ${state.currentMin}-${state.currentMax},快来猜猜看~`;
134
+ let msg = session.text('guessNumber.start', [config.guessNumber.min, config.guessNumber.max]);
108
135
  const isPrivate = !session.channelId;
109
- const botShouldParticipate = isPrivate || true;
110
- if (botShouldParticipate && state.started) {
111
- const botFirstGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
112
- state.botGuess.push(botFirstGuess);
136
+ const botPlay = isPrivate || config.guessNumber.botParticipateInGroup;
137
+ if (botPlay) {
138
+ const first = koishi_1.Random.int(state.currentMin, state.currentMax);
139
+ state.botGuess.push(first);
113
140
  guessNumberStates.set(key, state);
114
- reply += `\n我先来猜一个:${botFirstGuess},该你啦~`;
141
+ msg += session.text('guessNumber.botFirstGuess', [first]);
115
142
  }
116
- await session.send(reply);
143
+ await session.send(msg);
117
144
  return '';
118
145
  }
119
146
  if (action === 'stop') {
120
147
  state.started = false;
121
148
  guessNumberStates.set(key, state);
122
- await session.send(`猜数字游戏已停止,正确数字是 ${state.target}`);
149
+ await session.send(session.text('guessNumber.stop', [state.target]));
123
150
  return '';
124
151
  }
125
- await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
152
+ await session.send(session.text('guessNumber.usage'));
126
153
  return '';
127
154
  });
128
155
  }
@@ -2,13 +2,11 @@ guessNumber:
2
2
  usage: 用法错误!正确用法:guess start/stop 或 直接输入数字
3
3
  start: 猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~
4
4
  stop: 猜数字游戏已停止,正确数字是 {0}
5
- invalid: 请输入有效的整数!
6
- notStarted: 游戏还未开始,请先发送 guess start 开始游戏
7
5
  outOfRange: 数字超出当前范围 [{0}-{1}],请重新输入!
8
6
  tooSmall: 猜小啦!当前范围更新为 [{0}-{1}]
9
7
  tooBig: 猜大啦!当前范围更新为 [{0}-{1}]
10
8
  correct: 恭喜你猜中了!正确数字就是 {0} 🎉
11
9
  botFirstGuess: 我先来猜一个:{0},该你啦~
12
- botWin: 哈哈我猜中了!正确数字是 {0},我赢啦 🎉
10
+ botWin: 哈哈我猜中了!我猜的是 {0},正确数字就是 {0},我赢啦 🎉
13
11
  botGuessTooSmall: 我猜 {0},猜小啦!当前范围 [{1}-{2}]
14
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.8",
4
- "description": "一款简单的猜数字小游戏,支持Bot 参与猜数",
3
+ "version": "0.1.0",
4
+ "description": "一款简单的猜数字小游戏,支持自定义范围、降低Bot猜中概率、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,136 +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 = `数字超出当前范围 [${state.currentMin}-${state.currentMax}],请重新输入!`;
43
- } else if (num < state.target) {
44
- state.currentMin = num + 1;
45
- reply = `猜小啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
46
- } else if (num > state.target) {
47
- state.currentMax = num - 1;
48
- reply = `猜大啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
49
- } else {
50
- state.started = false;
51
- guessNumberStates.set(key, state);
52
- reply = `恭喜你猜中了!正确数字就是 ${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哈哈我猜中了!正确数字是 ${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我猜 ${botGuess},猜小啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
76
- } else {
77
- state.currentMax = botGuess - 1;
78
- guessNumberStates.set(key, state);
79
- reply += `\n我猜 ${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) {
102
- await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
103
- return '';
104
- }
105
-
106
- if (action === 'start') {
107
- state.target = Random.int(0, 100);
108
- state.currentMin = 0;
109
- state.currentMax = 100;
110
- state.started = true;
111
- state.botGuess = [];
112
- guessNumberStates.set(key, state);
113
- let reply = `猜数字游戏开始!初始范围是 ${state.currentMin}-${state.currentMax},快来猜猜看~`;
114
- const isPrivate = !session.channelId;
115
- const botShouldParticipate = isPrivate || true;
116
- if (botShouldParticipate && state.started) {
117
- const botFirstGuess = Random.int(state.currentMin, state.currentMax);
118
- state.botGuess.push(botFirstGuess);
119
- guessNumberStates.set(key, state);
120
- reply += `\n我先来猜一个:${botFirstGuess},该你啦~`;
121
- }
122
- await session.send(reply);
123
- return '';
124
- }
125
-
126
- if (action === 'stop') {
127
- state.started = false;
128
- guessNumberStates.set(key, state);
129
- await session.send(`猜数字游戏已停止,正确数字是 ${state.target}`);
130
- return '';
131
- }
132
-
133
- await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
134
- return '';
135
- });
136
- }
@@ -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}]