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 <noreply@anthropic.com>
This commit is contained in:
ДмитрийandClaude Sonnet 5.5 committed 2026-11-25 07:52:58 +05:00
1 parent 086534f849
commit dbf494c2c2
3 files changed
+76 -11

No files matched your search

@@ -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.
+39
View File
@@ -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<u32> {
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();
+30 -11
View File
@@ -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);