From c52613fa20e8b01668ffdb62878a4b8742bcb69b Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 16 Jul 2024 11:36:11 -0400 Subject: [PATCH] Add minor optimization --- README.md | 2 +- src/bot.rs | 37 +++++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index dd5083a..6b10e4b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ you are familiar with Rust. intended to be a fun addition to the game that can help players trade items with each other. - This project may have bugs. You are encouraged to report them to the author but the author takes no responsibility for any lost items. **No such incidents have been reported so far.** -- This program **handles your password securely** and does nothing with it expect connecting to the +- This program **handles your password securely** and does nothing with it except connecting to the veloren server during launch. However, third-party video game software is often infected with malware. You should **review the source code** or ask someone you trust to do so for you. - You are welcome to make changes to the code or fork the project. The author is open to diff --git a/src/bot.rs b/src/bot.rs index e4e6c73..d837909 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -443,25 +443,30 @@ impl Bot { ); if let Some(announcement) = &self.announcement { - let location = self - .client - .sites() - .into_iter() - .find_map(|(_, SiteInfoRich { site, .. })| { - let x_difference = self.position.0[0] - site.wpos[0] as f32; - let y_difference = self.position.0[1] - site.wpos[1] as f32; + let announcement = if announcement.contains("{location}") { + let location = self + .client + .sites() + .into_iter() + .find_map(|(_, SiteInfoRich { site, .. })| { + let x_difference = self.position.0[0] - site.wpos[0] as f32; + let y_difference = self.position.0[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)); - let interpolated_announcement = announcement.replace("{location}", &location); + if x_difference.abs() < 100.0 && y_difference.abs() < 100.0 { + site.name.clone() + } else { + None + } + }) + .unwrap_or(format!("{:?}", self.position)); + + announcement.replace("{location}", &location) + } else { + announcement.clone() + }; self.client - .send_command("world".to_string(), vec![interpolated_announcement]); + .send_command("world".to_string(), vec![announcement]); } Ok(())