@baipiaodajun/mcbots 1.0.7 → 1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +26 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baipiaodajun/mcbots",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Minecraft bot and status dashboard for multi-server management",
5
5
  "main": "server.js",
6
6
  "scripts": {
package/server.js CHANGED
@@ -117,6 +117,8 @@ class MinecraftBotManager {
117
117
  this.timeout= null;
118
118
  this.reachable=false;
119
119
  this.lastReachable = null;
120
+ this.resetTimer=null;
121
+ this.notice=true;
120
122
  // 机器人名称池
121
123
  this.botNames = Array.from({ length: 20 }, () => generateUsername());
122
124
 
@@ -151,6 +153,7 @@ class MinecraftBotManager {
151
153
  console.log(`[${this.host}:${this.port}] MC服务器恢复可达`);
152
154
  }
153
155
  this.lastReachable = this.reachable;
156
+ this.notice=true;
154
157
  }
155
158
  }
156
159
  // 创建Minecraft机器人
@@ -224,21 +227,17 @@ class MinecraftBotManager {
224
227
 
225
228
  bot.on('error', (error) => {
226
229
  console.log(`[${this.host}:${this.port}] 机器人 ${botName} 错误:`, error.message);
227
- if (this.activeBots.has(botName) && this.failedAttempts <= SERVER_CONFIG.maxFailedAttempts) {
230
+ // if (this.activeBots.has(botName) && this.failedAttempts <= SERVER_CONFIG.maxFailedAttempts) {
228
231
  this.handleBotDisconnect(botName);
229
- } else {
230
- this.handleBotFailure(botName);
231
- }
232
+ // } else {
233
+ // this.handleBotFailure(botName);
234
+ // }
232
235
  });
233
236
 
234
237
  bot.on('end', (reason) => {
235
238
  console.log(`[${this.host}:${this.port}] 机器人 ${botName} 断开连接:`, reason);
236
239
  console.log(`[${this.host}:${this.port}] ${botName}:失败次数${this.failedAttempts}`);
237
- if (this.failedAttempts < SERVER_CONFIG.maxFailedAttempts) {
238
- this.handleBotDisconnect(botName);
239
- } else {
240
- this.handleBotFailure(botName);
241
- }
240
+ this.handleBotDisconnect(botName);
242
241
  });
243
242
 
244
243
  bot.on('kicked', (reason) => {
@@ -308,33 +307,28 @@ class MinecraftBotManager {
308
307
  console.log(`[${this.host}:${this.port}] 失败次数过多,${SERVER_CONFIG.resetTimeout / 1000}秒后再次尝试`);
309
308
  setTimeout(() => {
310
309
  this.failedAttempts = 0;
311
- if(botName){
312
- this.reconnect(botName);
313
- }
314
- else{
315
- this.maintainBots();
316
- }
310
+ this.reconnect(botName);
317
311
  }, SERVER_CONFIG.resetTimeout);
318
312
  return;
319
313
  }
320
-
321
314
  setTimeout(() => {
322
- this.maintainBots();
315
+ this.reconnect(botName);
323
316
  }, BOT_CONFIG.reconnectDelay);
324
317
  }
325
318
 
326
319
  // 维护机器人数目 - 增强版本
327
- maintainBots(botName) {
320
+ maintainBots() {
328
321
  const neededBots = this.minBots - this.currentBots;
329
322
 
330
323
  // console.log(`[${this.host}:${this.port}] 当前机器人: ${this.currentBots}, 需要: ${neededBots}, 失败次数: ${this.failedAttempts}`);
331
-
332
324
  if (neededBots > 0 && this.failedAttempts < SERVER_CONFIG.maxFailedAttempts) {
333
- console.log(`[${this.host}:${this.port}] 需要启动 ${neededBots} 个机器人`);
334
-
325
+ if (this.notice) {
326
+ console.log(`[${this.host}:${this.port}] 需要启动 ${neededBots} 个机器人`);
327
+ this.notice=false;
328
+ }
335
329
  for (let i = 0; i < neededBots; i++) {
336
330
  setTimeout(() => {
337
- this.createBot(botName);
331
+ this.createBot();
338
332
  }, i * 8000); // 每隔8秒启动一个
339
333
  }
340
334
  }
@@ -351,6 +345,16 @@ class MinecraftBotManager {
351
345
  this.createBot(botName);
352
346
  }, 5000);
353
347
  } else{
348
+ // 如果还没有设置过 resetTimer,就设置一次
349
+ if (!this.resetTimer) {
350
+ this.resetTimer = setTimeout(() => {
351
+ this.failedAttempts = 0;
352
+ this.resetTimer = null; // 清理标记,允许下次再设置
353
+ console.log(
354
+ `[${this.host}:${this.port}] 失败次数已重置,可以重新尝试创建机器人`
355
+ );
356
+ }, SERVER_CONFIG.resetTimeout);
357
+ }
354
358
  console.log(`[${this.host}:${this.port}] 由于失败次数过多,暂停创建新机器人`);
355
359
  }
356
360
  this.updateStatus();