From dbf494c2c2308162ca78a3a1b244c7e49ecae0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 18 Nov 2026 10:38:31 +0500 Subject: [PATCH] fix(companions): reuse offline bot pool before spawning and preserve AutoLogin.MaxCount Ported from 7447347 onto the multi-realm companion flow. The server's queued count is used, an older bots module without a count falls back to creating the bots, and requests for a chosen level or class are always created. (cherry picked from commit 7447347) Co-Authored-By: Claude Sonnet 5.5 --- .../20261125-companions-reuse-offline-bots.md | 7 ++++ crates/coa-core/src/ra.rs | 39 ++++++++++++++++++ src-tauri/src/lib.rs | 41 ++++++++++++++----- 3 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 changelog.d/20261125-companions-reuse-offline-bots.md diff --git a/changelog.d/20261125-companions-reuse-offline-bots.md b/changelog.d/20261125-companions-reuse-offline-bots.md new file mode 100644 index 0000000..0ac604b --- /dev/null +++ b/changelog.d/20261125-companions-reuse-offline-bots.md @@ -0,0 +1,7 @@ +--- +area: manager +type: changed +audience: admins +title: Adding companions reuses offline bots and keeps your startup limit +--- +Bots that already exist and are offline are logged in first and only the missing ones are created. Adding companions no longer raises the number of bots that log in when the server starts. diff --git a/crates/coa-core/src/ra.rs b/crates/coa-core/src/ra.rs index 2df6d72..e85015f 100644 --- a/crates/coa-core/src/ra.rs +++ b/crates/coa-core/src/ra.rs @@ -327,6 +327,21 @@ impl Ra { Ok(out.lines().last().unwrap_or("").to_string()) } + /// Log in up to `count` existing offline bots without creating new characters. Returns how many the server queued. + /// A bots module that takes no count answers with its usage text: that is reported as unsupported. + pub fn login_offline_bots(&mut self, count: u32) -> Result { + if !(1..=5000).contains(&count) { + return Err(Error::Invalid( + "Choose between 1 and 5000 companions.".into(), + )); + } + let out = self.bot_command(&format!("botcmd loginall {count}"))?; + if !out.contains("queued") { + return Err(Error::CompanionCommandUnsupported); + } + Ok(first_number(&out).min(count)) + } + /// Set a new password for an existing account. pub fn set_account_password(&mut self, name: &str, password: &str) -> Result<()> { validate_account(name, password)?; @@ -625,6 +640,30 @@ AC>"; assert_eq!(h.join().unwrap()[2], "botcmd spawnleveled 50"); } + #[test] + fn login_offline_bots_sends_count() { + let _lock = serial(); + let (port, h) = fake_ra("BotMgr: queued 500 offline bot(s) for login."); + let mut ra = Ra::connect_to(port, "u", "p").unwrap(); + assert_eq!(ra.login_offline_bots(500).unwrap(), 500); + assert!(ra.login_offline_bots(0).is_err() && ra.login_offline_bots(6000).is_err()); + drop(ra); + assert_eq!(h.join().unwrap()[2], "botcmd loginall 500"); + } + + #[test] + fn login_offline_bots_reports_a_module_without_a_count_as_unsupported() { + let _lock = serial(); + let (port, h) = fake_ra("### USAGE: .botcmd loginall"); + let mut ra = Ra::connect_to(port, "u", "p").unwrap(); + assert!(matches!( + ra.login_offline_bots(5), + Err(Error::CompanionCommandUnsupported) + )); + drop(ra); + h.join().unwrap(); + } + #[test] fn duplicate_account_and_input_validation() { let _lock = serial(); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index aed9526..852e9cd 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2277,6 +2277,7 @@ async fn add_companions( let meta = meta_dir(&root)?; let mut changes = BTreeMap::new(); changes.insert("CoaBots.AutoLoginOnStartup".to_string(), Value::Bool(true)); + // Preserve the configured startup limit (CoaBots.AutoLogin.MaxCount); only set it when there is none. let existing_cfg = config::load(&target.world_root, Scope::Bots)?; let existing_max = existing_cfg .settings @@ -2284,13 +2285,12 @@ async fn add_companions( .find(|s| s.meta.key == "CoaBots.AutoLogin.MaxCount") .and_then(|s| s.value.as_u64()) .unwrap_or(0) as u32; - let already = coa_core::population::query_for_realm(&root, mode) - .map(|p| p.bots_total) - .unwrap_or(0); - changes.insert( - "CoaBots.AutoLogin.MaxCount".to_string(), - Value::from(existing_max.max(already + count).min(5000)), - ); + if existing_max == 0 { + changes.insert( + "CoaBots.AutoLogin.MaxCount".to_string(), + Value::from(count.min(5000)), + ); + } config::save(&target.world_root, &meta, Scope::Bots, &changes)?; let mut baseline = None; @@ -2298,14 +2298,33 @@ async fn add_companions( if let Ok(admin_db) = coa_core::db::Db::from_repack(&root, coa_core::db::Account::Admin) { let _ = coa_core::companions::ensure_templates_for_schema(&admin_db, target.chars_db); } - baseline = coa_core::population::query_for_realm(&root, mode) - .ok() - .map(|p| p.bots_total); + let population = coa_core::population::query_for_realm(&root, mode).ok(); + baseline = population.as_ref().map(|p| p.bots_total); let mut ra = Ra::connect_target(&root, Some(mode))?; if let Some(lvl) = level { Some(ra.spawn_custom(count, lvl, class_id, spec_id, archetype.as_deref())?) } else { - Some(ra.spawn_bots(count)?) + // Bots that already exist and are offline come back first; only the rest are created. + let offline = population + .as_ref() + .map(|p| p.bots_total.saturating_sub(p.bots_online)) + .unwrap_or(0); + let mut reused = 0; + if offline > 0 { + match ra.login_offline_bots(count.min(offline)) { + Ok(queued) => reused = queued, + // An older bots module cannot log in a given number: create them all, as before. + Err(Error::CompanionCommandUnsupported) => {} + Err(error) => return Err(error), + } + } + // The progress bar counts from the baseline, so reused bots count as done. + baseline = baseline.map(|total| total.saturating_sub(reused)); + if count > reused { + Some(ra.spawn_bots(count - reused)?) + } else { + Some(format!("BotMgr: queued {reused} offline bot(s) for login.")) + } } } else { let _ = coa_core::companions::migrate_legacy_pending_job(&meta, &id);