From 746e2174e158b8d197e31be3011ecdf9cbf97d1c Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 8 Jul 2024 16:58:40 -0400 Subject: [PATCH] Add character field to config; Condense bot fns --- config.toml | 1 + src/bot.rs | 72 +++++++++++++++++++++++++++-------------------------- src/main.rs | 4 +-- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/config.toml b/config.toml index fc054b8..d1ffeac 100644 --- a/config.toml +++ b/config.toml @@ -1,3 +1,4 @@ +character = "crabobot_test" position = [17689.0, 14964.0, 238.0] orientation = "East" diff --git a/src/bot.rs b/src/bot.rs index 9b30d29..3e5c460 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -20,6 +20,8 @@ use veloren_common_net::sync::WorldSyncExt; const COINS: &str = "common.items.utility.coins"; +/// A Bot instance represents an active connection to the server and it will +/// attempt to run every time the `tick` function is called. pub struct Bot { position: [f32; 3], orientation: String, @@ -38,9 +40,12 @@ pub struct Bot { } impl Bot { + /// Connect to the official veloren server, select the specified character + /// return a Bot instance ready to run. pub fn new( username: &str, password: &str, + character: &str, buy_prices: HashMap, sell_prices: HashMap, position: [f32; 3], @@ -48,8 +53,38 @@ impl Bot { ) -> Result { log::info!("Connecting to veloren"); - let client = connect_to_veloren(username, password)?; - let clock = Clock::new(Duration::from_secs_f64(1.0 / 30.0)); + let mut client = connect_to_veloren(username, password)?; + let mut clock = Clock::new(Duration::from_secs_f64(1.0 / 30.0)); + + log::info!("Selecting a character"); + + client.load_character_list(); + + while client.character_list().loading { + client + .tick(ControllerInputs::default(), clock.dt()) + .map_err(|error| format!("{error:?}"))?; + clock.tick(); + } + + let character_id = client + .character_list() + .characters + .iter() + .find(|character_item| character_item.character.alias == character) + .expect("No characters to select") + .character + .id + .expect("Failed to get character ID"); + + client.request_character( + character_id, + ViewDistances { + terrain: 4, + entity: 4, + }, + ); + let now = Instant::now(); Ok(Bot { @@ -67,39 +102,6 @@ impl Bot { }) } - pub fn select_character(&mut self) -> Result<(), String> { - log::info!("Selecting a character"); - - self.client.load_character_list(); - - while self.client.character_list().loading { - self.client - .tick(ControllerInputs::default(), self.clock.dt()) - .map_err(|error| format!("{error:?}"))?; - self.clock.tick(); - } - - let character_id = self - .client - .character_list() - .characters - .first() - .expect("No characters to select") - .character - .id - .expect("Failed to get character ID"); - - self.client.request_character( - character_id, - ViewDistances { - terrain: 4, - entity: 4, - }, - ); - - Ok(()) - } - pub fn tick(&mut self) -> Result<(), String> { let veloren_events = self .client diff --git a/src/main.rs b/src/main.rs index 2d2dd32..60e77b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ pub struct Secrets { #[derive(Serialize, Deserialize)] struct Config { + pub character: String, pub buy_prices: HashMap, pub sell_prices: HashMap, pub position: [f32; 3], @@ -46,6 +47,7 @@ fn main() { let mut bot = Bot::new( &secrets.username, &secrets.password, + &config.character, config.buy_prices, config.sell_prices, config.position, @@ -53,8 +55,6 @@ fn main() { ) .expect("Failed to create bot"); - bot.select_character().expect("Failed to select character"); - loop { let _ = bot.tick().inspect_err(|error| eprintln!("{error}")); }