From 535adb23946e617096a370313832568c207c68dd Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 15 Jul 2024 23:26:37 -0400 Subject: [PATCH] Make improvement to avoid spamming the server --- Cargo.lock | 2 ++ Cargo.toml | 1 + src/bot.rs | 51 +++++++++++++++++++++++++++++---------------------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 739dc9a..2b32976 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1037,6 +1037,7 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ "ahash 0.8.11", "allocator-api2", + "equivalent", "rayon", "serde", ] @@ -2826,6 +2827,7 @@ name = "trade-bot" version = "0.1.0" dependencies = [ "env_logger", + "hashbrown 0.14.5", "log", "serde", "tokio", diff --git a/Cargo.toml b/Cargo.toml index f1cedac..e923028 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ serde = { version = "1.0.203", features = ["derive"] } log = "0.4.22" env_logger = "0.11.3" vek = "0.17.0" +hashbrown = { version = "0.14.5", features = ["equivalent"] } [patch.crates-io] specs = { git = "https://github.com/amethyst/specs.git", rev = "4e2da1df29ee840baa9b936593c45592b7c9ae27" } diff --git a/src/bot.rs b/src/bot.rs index 4e2e555..f832bc1 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -8,11 +8,11 @@ announce its presence and respond to chat messages. See [main.rs] for an example of how to run this bot. **/ use std::{ - collections::HashMap, sync::Arc, time::{Duration, Instant}, }; +use hashbrown::HashMap; use tokio::runtime::Runtime; use vek::Quaternion; use veloren_client::{addr::ConnectionArgs, Client, Event as VelorenEvent, SiteInfoRich, WorldExt}; @@ -467,7 +467,7 @@ impl Bot { /// /// The bot's trading logic is as follows: /// - /// 1. If the trade is empty, do nothing. + /// 1. If the trade is empty or hasn't changed, do nothing. /// 2. If my offer includes items I am not selling, remove those items unless they are coins. /// 3. If their offer includes items I am not buying, remove those items unless they are coins. /// 4. If the trade is balanced, accept it. @@ -507,6 +507,33 @@ impl Bot { let get_my_coins = my_inventory .get_slot_of_item_by_def_id(&ItemDefinitionIdOwned::Simple(COINS.to_string())); let their_inventory = inventories.get(them).ok_or("Failed to find inventory")?; + let item_offers = { + let mut their_items = HashMap::with_capacity(their_offer.len()); + + for (slot_id, quantity) in their_offer { + if let Some(item) = their_inventory.get(*slot_id) { + their_items.insert(item.persistence_item_id(), *quantity); + } + } + + let mut my_items = HashMap::with_capacity(my_offer.len()); + + for (slot_id, quantity) in my_offer { + if let Some(item) = my_inventory.get(*slot_id) { + my_items.insert(item.persistence_item_id(), *quantity); + } + } + + (my_items, their_items) + }; + + // If the trade hasn't changed, do nothing to avoid spamming the server. + if let Some(previous) = &self.previous_offer { + if previous == &item_offers { + return Ok(()); + } + } + let get_their_coins = their_inventory .get_slot_of_item_by_def_id(&ItemDefinitionIdOwned::Simple(COINS.to_string())); let (mut their_offered_coin_amount, mut my_offered_coin_amount) = (0, 0); @@ -597,26 +624,6 @@ impl Bot { } } - let item_offers = { - let mut their_items = HashMap::with_capacity(their_offer.len()); - - for (slot_id, quantity) in their_offer { - if let Some(item) = their_inventory.get(*slot_id) { - their_items.insert(item.persistence_item_id(), *quantity); - } - } - - let mut my_items = HashMap::with_capacity(my_offer.len()); - - for (slot_id, quantity) in my_offer { - if let Some(item) = my_inventory.get(*slot_id) { - my_items.insert(item.persistence_item_id(), *quantity); - } - } - - (my_items, their_items) - }; - drop(inventories); // Before running any actual trade logic, remove items that are not for sale or not being