diff --git a/afk_parent.js b/afk_parent.js index be367f5..449986a 100644 --- a/afk_parent.js +++ b/afk_parent.js @@ -3,7 +3,7 @@ const fs = require("fs"); const path = require("path"); // Add all bot usernames here. -const USERNAMES = ["ZareMate", "Tomek"]; +const USERNAMES = ["ZareMate", "Tomek", "Cytrus"]; const AFK_SCRIPT = path.join(__dirname, "afk.js"); const OUTPUT_MODE = ( @@ -12,6 +12,8 @@ const OUTPUT_MODE = ( const SPLIT_LOGS_ENABLED = process.env.PARENT_SPLIT_LOGS === "1"; const RESTART_MIN_MS = 2 * 60 * 1000; const RESTART_MAX_MS = 5 * 60 * 1000; +const STARTUP_GAP_MIN_MS = 30 * 1000; +const STARTUP_GAP_MAX_MS = 60 * 1000; const ALREADY_ONLINE_TEXT = "You are already online, try restarting your game."; const TIMEOUT_REGEX = /timeout|timed out|ETIMEDOUT|ECONNRESET|socket hang up/i; @@ -41,6 +43,13 @@ function randomRestartDelayMs() { ); } +function randomStartupGapMs() { + return ( + STARTUP_GAP_MIN_MS + + Math.floor(Math.random() * (STARTUP_GAP_MAX_MS - STARTUP_GAP_MIN_MS + 1)) + ); +} + function shouldRestartFromState(state) { if (state.sawKick) return true; if (state.sawTimeout) return true; @@ -157,19 +166,37 @@ const botStates = USERNAMES.map((username) => ({ username, child: null, restartTimer: null, + startupTimer: null, sawKick: false, sawTimeout: false, sawAlreadyOnline: false, })); +let startupOffset = 0; for (const state of botStates) { - startBot(state); + const delay = startupOffset; + state.startupTimer = setTimeout(() => { + state.startupTimer = null; + if (isShuttingDown) return; + startBot(state); + }, delay); + + if (delay > 0) { + const seconds = Math.round(delay / 1000); + console.log(`[parent] startup delay for ${state.username}: ${seconds}s`); + } + + startupOffset += randomStartupGapMs(); } function shutdownAll() { isShuttingDown = true; console.log("[parent] shutdown requested"); for (const state of botStates) { + if (state.startupTimer) { + clearTimeout(state.startupTimer); + state.startupTimer = null; + } if (state.restartTimer) { clearTimeout(state.restartTimer); state.restartTimer = null;