koishi-plugin-game-mini 0.0.4 → 0.0.5

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.
Files changed (2) hide show
  1. package/lib/index.js +52 -47
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -55,6 +55,56 @@ function apply(ctx, config) {
55
55
  const getSessionKey = (session) => {
56
56
  return session.channelId ? `group:${session.channelId}` : `private:${session.userId}`;
57
57
  };
58
+ ctx.middleware(async (session, next) => {
59
+ if (!session.content)
60
+ return next();
61
+ const key = getSessionKey(session);
62
+ const state = guessNumberStates.get(key);
63
+ if (state && state.started) {
64
+ const num = parseInt(session.content.trim());
65
+ if (!isNaN(num)) {
66
+ let reply = '';
67
+ if (num < state.min || num > state.max) {
68
+ reply = session.text('guessNumber.outOfRange', [state.min, state.max]);
69
+ }
70
+ else if (num < state.target) {
71
+ reply = session.text('guessNumber.tooSmall');
72
+ }
73
+ else if (num > state.target) {
74
+ reply = session.text('guessNumber.tooBig');
75
+ }
76
+ else {
77
+ state.started = false;
78
+ guessNumberStates.set(key, state);
79
+ reply = session.text('guessNumber.correct', [state.target]);
80
+ return session.send(reply);
81
+ }
82
+ const isPrivate = !session.channelId;
83
+ const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
84
+ if (botShouldParticipate && state.started) {
85
+ let botGuess;
86
+ do {
87
+ botGuess = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
88
+ } while (state.botGuess.includes(botGuess));
89
+ state.botGuess.push(botGuess);
90
+ guessNumberStates.set(key, state);
91
+ if (botGuess === state.target) {
92
+ reply += '\n' + session.text('guessNumber.botWin', [botGuess]);
93
+ state.started = false;
94
+ guessNumberStates.set(key, state);
95
+ }
96
+ else if (botGuess < state.target) {
97
+ reply += '\n' + session.text('guessNumber.botGuessTooSmall', [botGuess]);
98
+ }
99
+ else {
100
+ reply += '\n' + session.text('guessNumber.botGuessTooBig', [botGuess]);
101
+ }
102
+ }
103
+ return session.send(reply);
104
+ }
105
+ }
106
+ return next();
107
+ });
58
108
  ctx.command('guess <action:string>')
59
109
  .action(async ({ session }, action) => {
60
110
  if (!session)
@@ -68,59 +118,14 @@ function apply(ctx, config) {
68
118
  started: false,
69
119
  botGuess: []
70
120
  };
71
- if (!action) {
72
- if (state.started)
73
- return t('guessNumber.invalid');
121
+ if (!action)
74
122
  return t('guessNumber.usage');
75
- }
76
- const num = parseInt(action);
77
- if (!isNaN(num)) {
78
- if (!state.started)
79
- return t('guessNumber.notStarted');
80
- if (num < state.min || num > state.max) {
81
- return t('guessNumber.outOfRange', [state.min, state.max]);
82
- }
83
- let reply = '';
84
- if (num < state.target) {
85
- reply = t('guessNumber.tooSmall');
86
- }
87
- else if (num > state.target) {
88
- reply = t('guessNumber.tooBig');
89
- }
90
- else {
91
- state.started = false;
92
- guessNumberStates.set(key, state);
93
- return t('guessNumber.correct', [state.target]);
94
- }
95
- const isPrivate = !session.channelId;
96
- const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
97
- if (botShouldParticipate && state.started) {
98
- let botGuess;
99
- do {
100
- botGuess = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
101
- } while (state.botGuess.includes(botGuess));
102
- state.botGuess.push(botGuess);
103
- guessNumberStates.set(key, state);
104
- if (botGuess === state.target) {
105
- reply += '\n' + t('guessNumber.botWin', [botGuess]);
106
- state.started = false;
107
- guessNumberStates.set(key, state);
108
- }
109
- else if (botGuess < state.target) {
110
- reply += '\n' + t('guessNumber.botGuessTooSmall', [botGuess]);
111
- }
112
- else {
113
- reply += '\n' + t('guessNumber.botGuessTooBig', [botGuess]);
114
- }
115
- }
116
- return reply;
117
- }
118
123
  if (action === 'start') {
119
124
  state.target = koishi_1.Random.int(config.guessNumber.min, config.guessNumber.max);
120
125
  state.started = true;
121
126
  state.botGuess = [];
122
127
  guessNumberStates.set(key, state);
123
- let reply = t('guessNumber.start', [state.min, state.max]);
128
+ let reply = t('guessNumber.start', [config.guessNumber.min, config.guessNumber.max]);
124
129
  const isPrivate = !session.channelId;
125
130
  const botShouldParticipate = isPrivate || config.guessNumber.botParticipateInGroup;
126
131
  if (botShouldParticipate) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-game-mini",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Koishi猜数字小游戏插件",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",