Compare commits

..

No commits in common. "economy_pricing" and "main" have entirely different histories.

View File

@ -18,20 +18,18 @@ use veloren_client_i18n::LocalizationHandle;
use veloren_common::{ use veloren_common::{
clock::Clock, clock::Clock,
comp::{ comp::{
inventory::trade_pricing::TradePricing,
invite::InviteKind, invite::InviteKind,
item::{ItemDefinitionId, ItemDesc, ItemI18n, MaterialStatManifest}, item::{ItemDefinitionId, ItemDesc, ItemI18n, MaterialStatManifest},
tool::AbilityMap, tool::AbilityMap,
ChatType, ControllerInputs, Item, Ori, Pos, ChatType, ControllerInputs, Item, Ori, Pos,
}, },
time::DayPeriod, time::DayPeriod,
trade::{PendingTrade, SitePrices, TradeAction, TradeResult}, trade::{PendingTrade, TradeAction, TradeResult},
uid::Uid, uid::Uid,
uuid::Uuid, uuid::Uuid,
ViewDistances, ViewDistances,
}; };
use veloren_common_net::sync::WorldSyncExt; use veloren_common_net::sync::WorldSyncExt;
use veloren_world::site::Economy;
use crate::config::PriceList; use crate::config::PriceList;
@ -55,7 +53,6 @@ pub struct Bot {
material_manifest: MaterialStatManifest, material_manifest: MaterialStatManifest,
item_i18n: ItemI18n, item_i18n: ItemI18n,
localization: LocalizationHandle, localization: LocalizationHandle,
site_prices: SitePrices,
buy_prices: PriceList, buy_prices: PriceList,
sell_prices: PriceList, sell_prices: PriceList,
@ -138,9 +135,6 @@ impl Bot {
} else { } else {
client.current::<Ori>().ok_or("Failed to get orientation")? client.current::<Ori>().ok_or("Failed to get orientation")?
}; };
let economy = Economy::default();
let site_prices = economy.get_site_prices();
let now = Instant::now(); let now = Instant::now();
Ok(Bot { Ok(Bot {
@ -153,7 +147,6 @@ impl Bot {
material_manifest: MaterialStatManifest::load().read().clone(), material_manifest: MaterialStatManifest::load().read().clone(),
item_i18n: ItemI18n::new_expect(), item_i18n: ItemI18n::new_expect(),
localization: LocalizationHandle::load_expect("en"), localization: LocalizationHandle::load_expect("en"),
site_prices,
buy_prices, buy_prices,
sell_prices, sell_prices,
trade_mode: TradeMode::Trade, trade_mode: TradeMode::Trade,
@ -557,38 +550,43 @@ impl Bot {
}; };
let (mut my_offered_coin_amount, mut their_offered_coin_amount) = (0, 0); let (mut my_offered_coin_amount, mut their_offered_coin_amount) = (0, 0);
let mut my_item_to_remove = None; let my_offered_items_value =
let my_offered_items_value = my_offer.into_iter().try_fold( my_offer
0, .into_iter()
|acc: i32, (slot_id, quantity)| -> Result<i32, String> { .fold(0, |acc: i32, (slot_id, quantity)| {
if let Some(item) = my_inventory.get(*slot_id) { if let Some(item) = my_inventory.get(*slot_id) {
let item_id = item.item_definition_id(); let item_id = item.item_definition_id();
let item_value: i32 = if item_id == COINS { let item_value = if item_id == COINS {
my_offered_coin_amount = *quantity as i32; my_offered_coin_amount = *quantity as i32;
1 1
} else { } else {
my_item_to_remove = Some((*slot_id, *quantity)); self.sell_prices
.0
0 .get(&item_id)
.map(|int| *int as i32)
.unwrap_or_else(|| {
self.buy_prices
.0
.get(&item_id)
.map(|int| 0 - *int as i32)
.unwrap_or(i32::MIN)
})
}; };
let item_name = self.get_item_name(item_id); let item_name = self.get_item_name(item_id);
receipt.my_items.insert(item_name, *quantity); receipt.my_items.insert(item_name, *quantity);
acc.saturating_add(item_value.saturating_mul(*quantity as i32))
let acc = acc.saturating_add(item_value.saturating_mul(*quantity as i32));
Ok(acc)
} else { } else {
Ok(acc) acc
} }
}, });
)?; let their_offered_items_value =
let their_offered_items_value = their_offer.into_iter().try_fold( their_offer
0, .into_iter()
|acc: i32, (slot_id, quantity)| -> Result<i32, String> { .fold(0, |acc: i32, (slot_id, quantity)| {
if let Some(item) = their_inventory.get(*slot_id) { if let Some(item) = their_inventory.get(*slot_id) {
let item_id = item.item_definition_id(); let item_id = item.item_definition_id();
@ -597,21 +595,57 @@ impl Bot {
1 1
} else { } else {
self.get_npc_price(item_id.clone())? as i32 * *quantity as i32 self.buy_prices
.0
.get(&item_id)
.map(|int| *int as i32)
.unwrap_or_else(|| {
self.sell_prices
.0
.get(&item_id)
.map(|int| 0 - *int as i32)
.unwrap_or(0)
})
}; };
let item_name = self.get_item_name(item_id); let item_name = self.get_item_name(item_id);
receipt.their_items.insert(item_name, *quantity); receipt.their_items.insert(item_name, *quantity);
acc.saturating_add(item_value.saturating_mul(*quantity as i32))
let acc = acc.saturating_add(item_value.saturating_mul(*quantity as i32));
Ok(acc)
} else { } else {
Ok(acc) acc
}
});
let mut my_item_to_remove = None;
for (slot_id, amount) in my_offer {
let item = my_inventory.get(*slot_id).ok_or("Failed to get item")?;
let item_id = item.item_definition_id();
if item_id == COINS {
continue;
}
if !self.sell_prices.0.contains_key(&item_id) {
my_item_to_remove = Some((*slot_id, *amount));
}
}
let mut their_item_to_remove = None;
for (slot_id, amount) in their_offer {
let item = their_inventory.get(*slot_id).ok_or("Failed to get item")?;
let item_id = item.item_definition_id();
if item_id == COINS {
continue;
}
if !self.buy_prices.0.contains_key(&item_id) {
their_item_to_remove = Some((*slot_id, *amount));
}
} }
},
)?;
drop(inventories); drop(inventories);
@ -641,6 +675,16 @@ impl Bot {
return Ok(()); return Ok(());
} }
if let Some((slot_id, quantity)) = their_item_to_remove {
self.client.perform_trade_action(TradeAction::RemoveItem {
item: slot_id,
quantity,
ours: false,
});
return Ok(());
}
let difference = their_offered_items_value - my_offered_items_value; let difference = their_offered_items_value - my_offered_items_value;
// The if/else statements below implement the bot's main feature: buying, selling and // The if/else statements below implement the bot's main feature: buying, selling and
@ -956,26 +1000,6 @@ impl Bot {
} }
}) })
} }
pub fn get_npc_price(&self, item_definition_id: ItemDefinitionId<'_>) -> Result<f32, String> {
let materials = TradePricing::get_materials(&item_definition_id)
.ok_or("Failed to get NPC price for item.")?;
let buy_price: f32 = materials
.iter()
.map(|(value, good)| {
self.site_prices
.values
.get(good)
.cloned()
.unwrap_or_default()
* value
* good.trade_margin()
* 5.0
})
.sum();
Ok(buy_price)
}
} }
#[derive(Debug)] #[derive(Debug)]