Tokipo commited on
Commit
6b93999
·
verified ·
1 Parent(s): c0965a7

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +35 -14
server.js CHANGED
@@ -16,7 +16,6 @@ const SHEET_URL = `https://docs.google.com/spreadsheets/d/${SHEET_ID}/export?for
16
 
17
  // Bot management
18
  const bots = new Map();
19
- const botLastReconnect = new Map();
20
  const serverBotMap = new Map(); // Track one bot per server
21
 
22
  class BotManager {
@@ -29,8 +28,10 @@ class BotManager {
29
  this.status = 'Disconnected';
30
  this.deathCount = 0;
31
  this.disconnectTime = null;
 
32
  this.isManualDisconnect = false;
33
  this.startTime = Date.now();
 
34
  }
35
 
36
  async connect() {
@@ -68,7 +69,9 @@ class BotManager {
68
 
69
  this.bot.on('death', () => {
70
  this.deathCount++;
 
71
  console.log(`Bot ${this.botName} died. Total deaths: ${this.deathCount}`);
 
72
  });
73
 
74
  this.bot.on('kicked', (reason) => {
@@ -98,13 +101,13 @@ class BotManager {
98
  serverBotMap.delete(serverKey);
99
  }
100
 
101
- this.status = 'Disconnected';
 
 
102
  this.disconnectTime = Date.now();
103
  this.bot = null;
104
 
105
- if (!this.isManualDisconnect) {
106
- console.log(`Bot ${this.botName} disconnected from ${this.ip}:${this.port}`);
107
- }
108
  }
109
 
110
  disconnect() {
@@ -141,12 +144,13 @@ class BotManager {
141
  }
142
 
143
  canReconnect() {
144
- if (this.status === 'Connected') return false;
 
145
 
146
- const lastReconnect = botLastReconnect.get(this.botName) || 0;
147
- const hourAgo = Date.now() - (60 * 60 * 1000);
148
 
149
- return lastReconnect < hourAgo;
 
150
  }
151
 
152
  async reconnect() {
@@ -154,11 +158,19 @@ class BotManager {
154
  return false;
155
  }
156
 
 
157
  this.isManualDisconnect = false;
158
- botLastReconnect.set(this.botName, Date.now());
159
  return await this.connect();
160
  }
161
 
 
 
 
 
 
 
 
 
162
  getInfo() {
163
  const uptime = this.status === 'Connected' ?
164
  Math.floor((Date.now() - this.startTime) / 1000) : 0;
@@ -169,7 +181,9 @@ class BotManager {
169
  deathCount: this.deathCount,
170
  uptime: uptime,
171
  canReconnect: this.canReconnect(),
172
- disconnectTime: this.disconnectTime
 
 
173
  };
174
  }
175
  }
@@ -197,6 +211,11 @@ async function updateBots() {
197
  const sheetData = await fetchSheetData();
198
  const activeBots = new Set();
199
 
 
 
 
 
 
200
  for (const row of sheetData) {
201
  const botName = row['BOT NAME']?.trim();
202
  const ip = row['IP']?.trim();
@@ -211,16 +230,18 @@ async function updateBots() {
211
  if (!bots.has(botName)) {
212
  const botManager = new BotManager(botName, ip, port, version);
213
  bots.set(botName, botManager);
214
- await botManager.connect();
 
 
 
215
  }
216
  }
217
 
218
  // Remove bots that are no longer in the sheet
219
  for (const [botName, botManager] of bots.entries()) {
220
- if (!activeBots.has(botName)) {
221
  botManager.disconnect();
222
  bots.delete(botName);
223
- botLastReconnect.delete(botName);
224
  }
225
  }
226
  }
 
16
 
17
  // Bot management
18
  const bots = new Map();
 
19
  const serverBotMap = new Map(); // Track one bot per server
20
 
21
  class BotManager {
 
28
  this.status = 'Disconnected';
29
  this.deathCount = 0;
30
  this.disconnectTime = null;
31
+ this.lastReconnectTime = null;
32
  this.isManualDisconnect = false;
33
  this.startTime = Date.now();
34
+ this.inSheet = true; // Track if bot is still in sheet
35
  }
36
 
37
  async connect() {
 
69
 
70
  this.bot.on('death', () => {
71
  this.deathCount++;
72
+ this.status = 'Dead';
73
  console.log(`Bot ${this.botName} died. Total deaths: ${this.deathCount}`);
74
+ this.handleDisconnect();
75
  });
76
 
77
  this.bot.on('kicked', (reason) => {
 
101
  serverBotMap.delete(serverKey);
102
  }
103
 
104
+ if (this.status !== 'Dead') {
105
+ this.status = 'Disconnected';
106
+ }
107
  this.disconnectTime = Date.now();
108
  this.bot = null;
109
 
110
+ console.log(`Bot ${this.botName} disconnected from ${this.ip}:${this.port}`);
 
 
111
  }
112
 
113
  disconnect() {
 
144
  }
145
 
146
  canReconnect() {
147
+ if (this.status === 'Connected' || this.status === 'Connecting...') return false;
148
+ if (!this.inSheet) return false;
149
 
150
+ if (!this.lastReconnectTime) return true;
 
151
 
152
+ const hourAgo = Date.now() - (60 * 60 * 1000);
153
+ return this.lastReconnectTime < hourAgo;
154
  }
155
 
156
  async reconnect() {
 
158
  return false;
159
  }
160
 
161
+ this.lastReconnectTime = Date.now();
162
  this.isManualDisconnect = false;
 
163
  return await this.connect();
164
  }
165
 
166
+ getTimeUntilReconnect() {
167
+ if (!this.lastReconnectTime) return 0;
168
+ const timeElapsed = Date.now() - this.lastReconnectTime;
169
+ const hourInMs = 60 * 60 * 1000;
170
+ const timeRemaining = Math.max(0, hourInMs - timeElapsed);
171
+ return Math.ceil(timeRemaining / 1000); // Return seconds
172
+ }
173
+
174
  getInfo() {
175
  const uptime = this.status === 'Connected' ?
176
  Math.floor((Date.now() - this.startTime) / 1000) : 0;
 
181
  deathCount: this.deathCount,
182
  uptime: uptime,
183
  canReconnect: this.canReconnect(),
184
+ disconnectTime: this.disconnectTime,
185
+ timeUntilReconnect: this.getTimeUntilReconnect(),
186
+ inSheet: this.inSheet
187
  };
188
  }
189
  }
 
211
  const sheetData = await fetchSheetData();
212
  const activeBots = new Set();
213
 
214
+ // Mark all existing bots as not in sheet initially
215
+ for (const [botName, botManager] of bots.entries()) {
216
+ botManager.inSheet = false;
217
+ }
218
+
219
  for (const row of sheetData) {
220
  const botName = row['BOT NAME']?.trim();
221
  const ip = row['IP']?.trim();
 
230
  if (!bots.has(botName)) {
231
  const botManager = new BotManager(botName, ip, port, version);
232
  bots.set(botName, botManager);
233
+ // Don't auto-connect, wait for manual connection
234
+ } else {
235
+ // Mark existing bot as still in sheet
236
+ bots.get(botName).inSheet = true;
237
  }
238
  }
239
 
240
  // Remove bots that are no longer in the sheet
241
  for (const [botName, botManager] of bots.entries()) {
242
+ if (!botManager.inSheet) {
243
  botManager.disconnect();
244
  bots.delete(botName);
 
245
  }
246
  }
247
  }