koishi-plugin-game-mini 0.0.8 → 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.js CHANGED
@@ -13,10 +13,35 @@ exports.name = 'game-mini';
13
13
  exports.using = ['i18n'];
14
14
  exports.Config = koishi_1.Schema.object({}).i18n({ 'zh-CN': {} });
15
15
  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);
16
+ const defaultI18n = {
17
+ guessNumber: {
18
+ usage: '用法错误!正确用法:guess start/stop 或 直接输入数字',
19
+ start: '猜数字游戏开始!初始范围是 {0}-{1},快来猜猜看~',
20
+ stop: '猜数字游戏已停止,正确数字是 {0}',
21
+ outOfRange: '数字超出当前范围 [{0}-{1}],请重新输入!',
22
+ tooSmall: '猜小啦!当前范围更新为 [{0}-{1}]',
23
+ tooBig: '猜大啦!当前范围更新为 [{0}-{1}]',
24
+ correct: '恭喜你猜中了!正确数字就是 {0} 🎉',
25
+ botFirstGuess: '我先来猜一个:{0},该你啦~',
26
+ botWin: '哈哈我猜中了!正确数字是 {0},我赢啦 🎉',
27
+ botGuessTooSmall: '我猜 {0},猜小啦!当前范围 [{1}-{2}]',
28
+ botGuessTooBig: '我猜 {0},猜大啦!当前范围 [{1}-{2}]'
29
+ }
30
+ };
31
+ try {
32
+ const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
33
+ if (fs_1.default.existsSync(zhCNPath)) {
34
+ const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
35
+ const zhCN = yaml_1.default.parse(zhCNContent);
36
+ ctx.i18n.define('zh-CN', zhCN);
37
+ }
38
+ else {
39
+ ctx.i18n.define('zh-CN', defaultI18n);
40
+ }
41
+ }
42
+ catch (error) {
43
+ ctx.i18n.define('zh-CN', defaultI18n);
44
+ }
20
45
  const guessNumberStates = new Map();
21
46
  const getSessionKey = (session) => {
22
47
  return session.channelId ? `group:${session.channelId}` : `private:${session.userId}`;
@@ -32,20 +57,20 @@ function apply(ctx, config) {
32
57
  if (!isNaN(num) && Number.isInteger(num)) {
33
58
  let reply = '';
34
59
  if (num < state.currentMin || num > state.currentMax) {
35
- reply = `数字超出当前范围 [${state.currentMin}-${state.currentMax}],请重新输入!`;
60
+ reply = session.text('guessNumber.outOfRange', [state.currentMin, state.currentMax]);
36
61
  }
37
62
  else if (num < state.target) {
38
63
  state.currentMin = num + 1;
39
- reply = `猜小啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
64
+ reply = session.text('guessNumber.tooSmall', [state.currentMin, state.currentMax]);
40
65
  }
41
66
  else if (num > state.target) {
42
67
  state.currentMax = num - 1;
43
- reply = `猜大啦!当前范围更新为 [${state.currentMin}-${state.currentMax}]`;
68
+ reply = session.text('guessNumber.tooBig', [state.currentMin, state.currentMax]);
44
69
  }
45
70
  else {
46
71
  state.started = false;
47
72
  guessNumberStates.set(key, state);
48
- reply = `恭喜你猜中了!正确数字就是 ${state.target} 🎉`;
73
+ reply = session.text('guessNumber.correct', [state.target]);
49
74
  await session.send(reply);
50
75
  return;
51
76
  }
@@ -60,19 +85,19 @@ function apply(ctx, config) {
60
85
  state.botGuess.push(botGuess);
61
86
  guessNumberStates.set(key, state);
62
87
  if (botGuess === state.target) {
63
- reply += `\n哈哈我猜中了!正确数字是 ${botGuess},我赢啦 🎉`;
88
+ reply += '\n' + session.text('guessNumber.botWin', [botGuess]);
64
89
  state.started = false;
65
90
  guessNumberStates.set(key, state);
66
91
  }
67
92
  else if (botGuess < state.target) {
68
93
  state.currentMin = botGuess + 1;
69
94
  guessNumberStates.set(key, state);
70
- reply += `\n我猜 ${botGuess},猜小啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
95
+ reply += '\n' + session.text('guessNumber.botGuessTooSmall', [botGuess, state.currentMin, state.currentMax]);
71
96
  }
72
97
  else {
73
98
  state.currentMax = botGuess - 1;
74
99
  guessNumberStates.set(key, state);
75
- reply += `\n我猜 ${botGuess},猜大啦!当前范围 [${state.currentMin}-${state.currentMax}]`;
100
+ reply += '\n' + session.text('guessNumber.botGuessTooBig', [botGuess, state.currentMin, state.currentMax]);
76
101
  }
77
102
  }
78
103
  await session.send(reply);
@@ -94,7 +119,7 @@ function apply(ctx, config) {
94
119
  botGuess: []
95
120
  };
96
121
  if (!action) {
97
- await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
122
+ await session.send(session.text('guessNumber.usage'));
98
123
  return '';
99
124
  }
100
125
  if (action === 'start') {
@@ -104,14 +129,14 @@ function apply(ctx, config) {
104
129
  state.started = true;
105
130
  state.botGuess = [];
106
131
  guessNumberStates.set(key, state);
107
- let reply = `猜数字游戏开始!初始范围是 ${state.currentMin}-${state.currentMax},快来猜猜看~`;
132
+ let reply = session.text('guessNumber.start', [0, 100]);
108
133
  const isPrivate = !session.channelId;
109
134
  const botShouldParticipate = isPrivate || true;
110
135
  if (botShouldParticipate && state.started) {
111
136
  const botFirstGuess = koishi_1.Random.int(state.currentMin, state.currentMax);
112
137
  state.botGuess.push(botFirstGuess);
113
138
  guessNumberStates.set(key, state);
114
- reply += `\n我先来猜一个:${botFirstGuess},该你啦~`;
139
+ reply += '\n' + session.text('guessNumber.botFirstGuess', [botFirstGuess]);
115
140
  }
116
141
  await session.send(reply);
117
142
  return '';
@@ -119,10 +144,10 @@ function apply(ctx, config) {
119
144
  if (action === 'stop') {
120
145
  state.started = false;
121
146
  guessNumberStates.set(key, state);
122
- await session.send(`猜数字游戏已停止,正确数字是 ${state.target}`);
147
+ await session.send(session.text('guessNumber.stop', [state.target]));
123
148
  return '';
124
149
  }
125
- await session.send('用法错误!正确用法:guess start/stop 或 直接输入数字');
150
+ await session.send(session.text('guessNumber.usage'));
126
151
  return '';
127
152
  });
128
153
  }
@@ -2,8 +2,6 @@ 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}]
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "koishi-plugin-game-mini",
3
- "version": "0.0.8",
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,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}]