Make improvement to avoid spamming the server

This commit is contained in:
Jeff 2024-07-15 23:26:37 -04:00
parent 22777d43cd
commit 535adb2394
3 changed files with 32 additions and 22 deletions

2
Cargo.lock generated
View File

@ -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",

View File

@ -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" }

View File

@ -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