From 54b99f7d8025b7c77e8f281d6cb01a356e536f94 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 11 Jul 2024 20:11:44 -0400 Subject: [PATCH] Add announcement with automatic location --- config/config.toml | 1 + src/bot.rs | 74 +++++++++++++++++++++++++++++++++++----------- src/main.rs | 6 ++-- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/config/config.toml b/config/config.toml index 0fc65ef..fab865f 100644 --- a/config/config.toml +++ b/config/config.toml @@ -1,5 +1,6 @@ position = [17626.0, 14960.0, 237.0] orientation = 90.0 +announcement = "Buying cheese and selling stuff" [buy_prices] "food.cheese" = 50 diff --git a/src/bot.rs b/src/bot.rs index 6b8defc..e56d5ef 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -6,7 +6,7 @@ use std::{ use tokio::runtime::Runtime; use vek::Quaternion; -use veloren_client::{addr::ConnectionArgs, Client, Event as VelorenEvent, WorldExt}; +use veloren_client::{addr::ConnectionArgs, Client, Event as VelorenEvent, SiteInfoRich, WorldExt}; use veloren_common::{ clock::Clock, comp::{invite::InviteKind, item::ItemDefinitionIdOwned, ChatType, ControllerInputs, Ori, Pos}, @@ -28,6 +28,7 @@ pub struct Bot { position: [f32; 3], orientation: f32, admins: Vec, + announcement: String, client: Client, clock: Clock, @@ -36,7 +37,7 @@ pub struct Bot { sell_prices: BTreeMap, trade_mode: TradeMode, - last_action: Instant, + last_trade_action: Instant, last_announcement: Instant, last_ouch: Instant, sort_count: u8, @@ -54,6 +55,7 @@ impl Bot { sell_prices: BTreeMap, position: [f32; 3], orientation: f32, + announcement: String, ) -> Result { log::info!("Connecting to veloren"); @@ -101,10 +103,11 @@ impl Bot { buy_prices, sell_prices, trade_mode: TradeMode::Trade, - last_action: now, + last_trade_action: now, last_announcement: now, last_ouch: now, sort_count: 0, + announcement, }) } @@ -119,7 +122,7 @@ impl Bot { self.handle_veloren_event(event)?; } - if self.last_action.elapsed() > Duration::from_millis(300) { + if self.last_trade_action.elapsed() > Duration::from_millis(300) { if self.client.is_dead() { self.client.respawn(); } @@ -147,18 +150,10 @@ impl Bot { self.client.accept_invite(); } - self.last_action = Instant::now(); + self.last_trade_action = Instant::now(); if self.last_announcement.elapsed() > Duration::from_secs(1800) { - log::info!("Making an announcement"); - - self.client.send_command( - "region".to_string(), - vec![format!( - "I'm a bot. You can trade with me or check prices: '/tell {} price [search_term]'.", - self.username - )], - ); + self.handle_announcement()?; self.last_announcement = Instant::now(); } @@ -202,9 +197,7 @@ impl Bot { log::debug!("Sorting inventory {sort_count} times"); - self.client.sort_inventory(); - - self.sort_count = sort_count.saturating_sub(1); + self.sort_count = sort_count; } else { log::debug!("Sorting inventory"); self.client.sort_inventory(); @@ -313,6 +306,40 @@ impl Bot { Ok(()) } + fn handle_announcement(&mut self) -> Result<(), String> { + log::info!("Making an announcement"); + + let location = self + .client + .sites() + .into_iter() + .find_map(|(_, SiteInfoRich { site, .. })| { + let x_difference = self.position[0] - site.wpos[0] as f32; + let y_difference = self.position[1] - site.wpos[1] as f32; + + if x_difference.abs() < 100.0 && y_difference.abs() < 100.0 { + site.name.clone() + } else { + None + } + }) + .unwrap_or(format!("{:?}", self.position)); + + self.client.send_command( + "region".to_string(), + vec![format!( + "I'm a bot. You can trade with me or check prices: '/tell {} price [search_term]'.", + self.username + )], + ); + self.client.send_command( + "world".to_string(), + vec![format!("{} at {location}.", self.announcement)], + ); + + Ok(()) + } + fn handle_lantern(&mut self) { let day_period = self.client.state().get_day_period(); @@ -601,6 +628,19 @@ impl Bot { if let Some(current_position) = current_position { if current_position.0 == self.position.into() { + for (_, SiteInfoRich { site, .. }) in self.client.sites() { + let site_name = &site.name; + let site_position = site.wpos; + + let x_difference = self.position[0] - site_position[0] as f32; + let y_difference = self.position[1] - site_position[1] as f32; + + if x_difference.abs() < 100.0 && y_difference.abs() < 100.0 { + println!("{current_position:?} {site_position}"); + println!("{site_name:?} {site_position}"); + } + } + return Ok(()); } } diff --git a/src/main.rs b/src/main.rs index 241f84c..bdca536 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,10 +15,11 @@ pub struct Secrets { #[derive(Serialize, Deserialize)] struct Config { - pub buy_prices: HashMap, - pub sell_prices: HashMap, pub position: [f32; 3], pub orientation: f32, + pub announcement: String, + pub buy_prices: HashMap, + pub sell_prices: HashMap, } fn main() { @@ -68,6 +69,7 @@ fn main() { sell_prices_with_full_id, config.position, config.orientation, + config.announcement, ) .expect("Failed to create bot");