mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-20 19:25:56 +00:00
Merge pull request #230 from Corfirean/feat/startup-fleet-top-up
Companions: the startup limit creates the missing ones
This commit is contained in:
12 files changed
+221
-34
No files matched your search
+89
-32
@@ -2,7 +2,7 @@
|
||||
//! registry; the frontend can never ask the backend to touch an arbitrary path (scan is read-only).
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
mod browse_preferences;
|
||||
@@ -597,6 +597,91 @@ async fn run_verb(
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Creates the companions a realm's startup fleet is missing (when its auto-login is on and its world runs) and
|
||||
/// carries out the realm's pending companion job. Returns how many companions were queued to top the fleet up.
|
||||
fn reconcile_realm_companions(
|
||||
root: &Path,
|
||||
meta: &Path,
|
||||
instance_id: &str,
|
||||
mode: coa_core::realms::Mode,
|
||||
top_up: bool,
|
||||
) -> Result<u32> {
|
||||
let target = coa_core::multiworld::resolve_bot_operation_target(root, mode)?;
|
||||
let mut queued = 0;
|
||||
if top_up && target.running {
|
||||
match queue_startup_fleet(root, meta, instance_id, mode, &target.world_root) {
|
||||
Ok(n) => queued = n,
|
||||
Err(e) => tracing::warn!("startup fleet of realm {} was not topped up: {e}", mode.name()),
|
||||
}
|
||||
}
|
||||
let r_snap = root.to_path_buf();
|
||||
let chars_db = target.chars_db;
|
||||
let r_ra = root.to_path_buf();
|
||||
coa_core::companions::process_pending_job_with(
|
||||
meta,
|
||||
mode,
|
||||
instance_id,
|
||||
target.running,
|
||||
|since_guid| {
|
||||
let admin_db = coa_core::db::Db::from_repack(&r_snap, coa_core::db::Account::Admin)?;
|
||||
let _ = coa_core::companions::ensure_templates_for_schema(&admin_db, chars_db);
|
||||
coa_core::companions::query_bot_db_snapshot(&r_snap, &admin_db, chars_db, since_guid)
|
||||
},
|
||||
|delta, job| {
|
||||
let mut ra = Ra::connect_target(&r_ra, Some(mode))?;
|
||||
if let Some(lvl) = job.level {
|
||||
ra.spawn_custom(delta, lvl, job.class_id, job.spec_id, job.build_id.as_deref())
|
||||
} else {
|
||||
ra.spawn_bots(delta)
|
||||
}
|
||||
},
|
||||
)?;
|
||||
Ok(queued)
|
||||
}
|
||||
|
||||
/// Queues the job that creates the companions missing from a realm's startup fleet. The realm's auto-login limit is
|
||||
/// the fleet size: companions that exist already count, so only the difference is created. Nothing is created when
|
||||
/// the number of existing companions cannot be read.
|
||||
fn queue_startup_fleet(
|
||||
root: &Path,
|
||||
meta: &Path,
|
||||
instance_id: &str,
|
||||
mode: coa_core::realms::Mode,
|
||||
world_root: &Path,
|
||||
) -> Result<u32> {
|
||||
let cfg = config::load(world_root, Scope::Bots)?;
|
||||
let value = |key: &str| cfg.settings.iter().find(|s| s.meta.key == key).map(|s| s.value.clone());
|
||||
let on = value("CoaBots.AutoLoginOnStartup").and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
let limit = value("CoaBots.AutoLogin.MaxCount").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
|
||||
if !on || limit == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let existing = coa_core::population::query_for_realm(root, mode)?.bots_total;
|
||||
let job = coa_core::companions::queue_fleet_top_up(meta, instance_id, mode, limit, existing)?;
|
||||
Ok(job.map_or(0, |j| j.count))
|
||||
}
|
||||
|
||||
/// After the startup fleet settings are saved: create the companions the fleet is missing right away when the realm's
|
||||
/// world is running (otherwise it happens at the next start). Returns how many were queued.
|
||||
#[tauri::command]
|
||||
async fn top_up_startup_fleet(
|
||||
state: State<'_, AppState>,
|
||||
id: String,
|
||||
realm: Option<coa_core::realms::Mode>,
|
||||
) -> std::result::Result<u32, UiError> {
|
||||
let root = path_of(&state, &id)?;
|
||||
let _guard = BusyGuard::acquire(&state, &id)?;
|
||||
blocking(move || {
|
||||
if coa_core::realms::guard_module(&root, "companions").is_err() {
|
||||
return Ok(0);
|
||||
}
|
||||
let meta = meta_dir(&root)?;
|
||||
let mode = realm.unwrap_or_else(|| coa_core::realms::view(&root).map(|v| v.active).unwrap_or_default());
|
||||
reconcile_realm_companions(&root, &meta, &id, mode, true)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn start_server(
|
||||
state: State<'_, AppState>,
|
||||
@@ -643,37 +728,8 @@ async fn start_server(
|
||||
coa_core::realms::Mode::Wildcard,
|
||||
coa_core::realms::Mode::Reborn,
|
||||
] {
|
||||
let target = match coa_core::multiworld::resolve_bot_operation_target(&r, mode) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
tracing::warn!("cannot resolve target for realm {}: {e}", mode.name());
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let r_snap = r.clone();
|
||||
let chars_db = target.chars_db;
|
||||
let r_ra = r.clone();
|
||||
let res = coa_core::companions::process_pending_job_with(
|
||||
&meta,
|
||||
mode,
|
||||
&instance_id,
|
||||
target.running,
|
||||
|since_guid| {
|
||||
let admin_db = coa_core::db::Db::from_repack(&r_snap, coa_core::db::Account::Admin)?;
|
||||
let _ = coa_core::companions::ensure_templates_for_schema(&admin_db, chars_db);
|
||||
coa_core::companions::query_bot_db_snapshot(&r_snap, &admin_db, chars_db, since_guid)
|
||||
},
|
||||
|delta, job| {
|
||||
let mut ra = Ra::connect_target(&r_ra, Some(mode))?;
|
||||
if let Some(lvl) = job.level {
|
||||
ra.spawn_custom(delta, lvl, job.class_id, job.spec_id, job.build_id.as_deref())
|
||||
} else {
|
||||
ra.spawn_bots(delta)
|
||||
}
|
||||
},
|
||||
);
|
||||
if let Err(e) = res {
|
||||
tracing::warn!("pending companions for realm {} failed during reconciliation: {e}", mode.name());
|
||||
if let Err(e) = reconcile_realm_companions(&r, &meta, &instance_id, mode, true) {
|
||||
tracing::warn!("companions for realm {} failed during start-up: {e}", mode.name());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -3152,6 +3208,7 @@ pub fn run() {
|
||||
report_targets,
|
||||
companion_sizes,
|
||||
add_companions,
|
||||
top_up_startup_fleet,
|
||||
get_wildcard_archetypes,
|
||||
remote_connection,
|
||||
remote_connect,
|
||||
|
||||
Reference in new issue
Block a user