fix(companions): reuse offline bot pool before spawning and preserve AutoLogin.MaxCount

This commit is contained in:
Дмитрий committed 2026-11-18 10:38:31 +05:00
1 parent 7cdf854eb0
commit 74473471db
2 files changed
+55 -17

No files matched your search

+25
View File
@@ -249,6 +249,17 @@ impl Ra {
Ok(out.lines().last().unwrap_or("").to_string())
}
/// Log in up to `count` existing offline bots without creating new characters.
pub fn login_offline_bots(&mut self, count: u32) -> Result<String> {
if !(1..=5000).contains(&count) {
return Err(Error::Invalid(
"Choose between 1 and 5000 companions.".into(),
));
}
let out = self.command(&format!("botcmd loginall {count}"))?;
Ok(out.lines().last().unwrap_or("").to_string())
}
/// Set a new password for an existing account.
pub fn set_account_password(&mut self, name: &str, password: &str) -> Result<()> {
validate_account(name, password)?;
@@ -547,6 +558,20 @@ 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(),
"BotMgr: queued 500 offline bot(s) for login."
);
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 duplicate_account_and_input_validation() {
let _lock = serial();
+30 -17
View File
@@ -1780,7 +1780,7 @@ async fn add_companions(
let meta = meta_dir(&root)?;
let mut changes = BTreeMap::new();
changes.insert("CoaBots.AutoLoginOnStartup".to_string(), Value::Bool(true));
// Every companion, old and new, should come back at the next start, so the start-up limit only ever grows here.
// Preserve the user's configured Startup Limit (CoaBots.AutoLogin.MaxCount); only initialize it if 0/unset.
let existing_max = config::load(&root, Scope::Bots)
.ok()
.and_then(|v| {
@@ -1790,13 +1790,12 @@ async fn add_companions(
.and_then(|s| s.value.as_u64())
})
.unwrap_or(0) as u32;
let already = coa_core::population::query(&root)
.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(&root, &meta, Scope::Bots, &changes)?;
let o = coa_core::process::observe(&root, &layout::read_ports(&root));
// Large batches while the server is stopped: create them in the database right now, fully equipped.
@@ -1837,15 +1836,29 @@ async fn add_companions(
}
let mut baseline = None;
let spawned = if o.world.state == coa_core::process::ServiceState::Running {
// Older servers have no template characters to copy bots from: add them first.
coa_core::companions::ensure_templates(&coa_core::db::Db::from_repack(
&root,
coa_core::db::Account::Admin,
)?)?;
baseline = coa_core::population::query(&root)
.ok()
.map(|p| p.bots_total);
Some(Ra::connect(&root)?.spawn_bots(count)?)
let pop = coa_core::population::query(&root).ok();
baseline = pop.as_ref().map(|p| p.bots_total);
let offline = pop
.as_ref()
.map(|p| p.bots_total.saturating_sub(p.bots_online))
.unwrap_or(0);
let from_pool = count.min(offline);
let remaining = count.saturating_sub(from_pool);
let mut ra = Ra::connect(&root)?;
let mut reply = String::new();
if from_pool > 0 {
reply = ra.login_offline_bots(from_pool)?;
}
if remaining > 0 {
// Older servers have no template characters to copy bots from: add them first.
coa_core::companions::ensure_templates(&coa_core::db::Db::from_repack(
&root,
coa_core::db::Account::Admin,
)?)?;
reply = ra.spawn_bots(remaining)?;
}
Some(reply)
} else {
// The server is stopped: creating bots needs it running, so remember the request and do it after the next start.
coa_core::fsx::atomic_write_json(