Compare commits

..

2 Commits

Author SHA1 Message Date
3a3aafc296 Notify player of unbalanced trade value 2024-07-18 02:52:38 -04:00
9f0db64482 Clean up 2024-07-17 23:56:07 -04:00

View File

@ -17,6 +17,7 @@ use veloren_common::{
comp::{ comp::{
invite::InviteKind, invite::InviteKind,
item::{ItemDefinitionId, ItemDesc, ItemI18n, MaterialStatManifest}, item::{ItemDefinitionId, ItemDesc, ItemI18n, MaterialStatManifest},
slot::EquipSlot,
tool::AbilityMap, tool::AbilityMap,
ChatType, ControllerInputs, Item, Ori, Pos, ChatType, ControllerInputs, Item, Ori, Pos,
}, },
@ -39,13 +40,6 @@ const TRADE_ACTION_DELAY: Duration = Duration::from_millis(300);
const ACCOUNCEMENT_DELAY: Duration = Duration::from_mins(45); const ACCOUNCEMENT_DELAY: Duration = Duration::from_mins(45);
const OUCH_DELAY: Duration = Duration::from_secs(2); const OUCH_DELAY: Duration = Duration::from_secs(2);
/// TODO: Implement Display
#[derive(Debug)]
pub struct Reciept {
pub my_items: HashMap<String, u32>,
pub their_items: HashMap<String, u32>,
}
/// An active connection to the Veloren server that will attempt to run every time the `tick` /// An active connection to the Veloren server that will attempt to run every time the `tick`
/// function is called. /// function is called.
/// ///
@ -524,6 +518,8 @@ impl Bot {
/// 6. If the total value of my offer is greater than the total value of their offer: /// 6. If the total value of my offer is greater than the total value of their offer:
/// 1. If I am offering coins, remove them to balance. /// 1. If I am offering coins, remove them to balance.
/// 2. If I am not offering coins, add theirs to balance. /// 2. If I am not offering coins, add theirs to balance.
/// 7. If the trade is still unbalanced, tell them the value of the greater offer and the
/// other party's total coin amount.
/// ///
/// See the inline comments for more details. /// See the inline comments for more details.
#[allow(clippy::comparison_chain)] #[allow(clippy::comparison_chain)]
@ -532,6 +528,7 @@ impl Bot {
return Ok(()); return Ok(());
} }
let phase = trade.phase;
let my_offer_index = trade let my_offer_index = trade
.which_party(self.client.uid().ok_or("Failed to get uid")?) .which_party(self.client.uid().ok_or("Failed to get uid")?)
.ok_or("Failed to get offer index")?; .ok_or("Failed to get offer index")?;
@ -542,6 +539,11 @@ impl Bot {
&trade.offers[their_offer_index], &trade.offers[their_offer_index],
) )
}; };
let their_uid = trade.parties[their_offer_index];
let their_name = self
.find_player_alias(&their_uid)
.ok_or("Failed to find player name")?
.clone();
let inventories = self.client.inventories(); let inventories = self.client.inventories();
let me = self.client.entity(); let me = self.client.entity();
@ -559,6 +561,16 @@ impl Bot {
let coins_owned = COINS.to_owned(); let coins_owned = COINS.to_owned();
let get_my_coins = my_inventory.get_slot_of_item_by_def_id(&coins_owned); let get_my_coins = my_inventory.get_slot_of_item_by_def_id(&coins_owned);
let get_their_coins = their_inventory.get_slot_of_item_by_def_id(&coins_owned); let get_their_coins = their_inventory.get_slot_of_item_by_def_id(&coins_owned);
let my_coin_amount = if let Some(coins) = get_my_coins {
my_inventory.get(coins).unwrap().amount()
} else {
0
};
let their_coin_amount = if let Some(coins) = get_their_coins {
their_inventory.get(coins).unwrap().amount()
} else {
0
};
let mut receipt = Reciept { let mut receipt = Reciept {
my_items: HashMap::new(), my_items: HashMap::new(),
their_items: HashMap::new(), their_items: HashMap::new(),
@ -664,8 +676,6 @@ impl Bot {
drop(inventories); drop(inventories);
let phase = trade.phase;
// If the trade hasn't changed, do nothing to avoid spamming the server. // If the trade hasn't changed, do nothing to avoid spamming the server.
if let Some(previous) = &self.previous_trade { if let Some(previous) = &self.previous_trade {
if previous == &trade { if previous == &trade {
@ -677,6 +687,8 @@ impl Bot {
// offer now. The trade action is infallible from here. // offer now. The trade action is infallible from here.
self.previous_trade = Some(trade); self.previous_trade = Some(trade);
log::info!("Performing trade action with {their_name}");
// Before running any actual trade logic, remove items that are not for sale or not being // Before running any actual trade logic, remove items that are not for sale or not being
// purchased. End this trade action if an item was removed. // purchased. End this trade action if an item was removed.
@ -704,8 +716,7 @@ impl Bot {
// 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
// trading items according to the values set in the configuration file. Coins are used to // trading items according to the values set in the configuration file. Coins are used to
// balance the value of the trade. In the case that we try to add more coins than are // balance the value of the trade.
// available, the server will correct it by adding all of the available coins.
// If the trade is balanced // If the trade is balanced
if difference == 0 { if difference == 0 {
@ -713,48 +724,88 @@ impl Bot {
// Accept // Accept
self.client.perform_trade_action(TradeAction::Accept(phase)); self.client.perform_trade_action(TradeAction::Accept(phase));
// If they are offering more
return Ok(());
// If they are offering more
} else if difference > 0 { } else if difference > 0 {
// If they are offering coins // If they are offering coins
if their_offered_coin_amount > 0 { if their_offered_coin_amount > 0 {
if let Some(their_coins) = get_their_coins { if let Some(their_coins) = get_their_coins {
// Remove their coins to balance if their_coin_amount >= difference as u32 {
self.client.perform_trade_action(TradeAction::RemoveItem { // Remove their coins to balance
item: their_coins, self.client.perform_trade_action(TradeAction::RemoveItem {
quantity: difference as u32, item: their_coins,
ours: false, quantity: difference as u32,
}); ours: false,
});
return Ok(());
}
} }
// If they are not offering coins // If they are not offering coins
} else if let Some(my_coins) = get_my_coins { } else if let Some(my_coins) = get_my_coins {
// Add my coins to balanace if my_coin_amount >= difference as u32 {
self.client.perform_trade_action(TradeAction::AddItem { // Add my coins to balanace
item: my_coins, self.client.perform_trade_action(TradeAction::AddItem {
quantity: difference as u32, item: my_coins,
ours: true, quantity: difference as u32,
}); ours: true,
});
return Ok(());
}
} }
self.client.send_command(
"tell".to_string(),
vec![
their_name,
format!(
"The value of your offer is {their_offered_items_value} coins. I only have {my_coin_amount} coins.",
),
],
);
// If I am offering more // If I am offering more
} else if difference < 0 { } else if difference < 0 {
// If I am offering coins // If I am offering coins
if my_offered_coin_amount > 0 { if my_offered_coin_amount > 0 {
if let Some(my_coins) = get_my_coins { if let Some(my_coins) = get_my_coins {
// Remove my coins to balance if my_coin_amount >= difference.unsigned_abs() {
self.client.perform_trade_action(TradeAction::RemoveItem { // Remove my coins to balance
item: my_coins, self.client.perform_trade_action(TradeAction::RemoveItem {
quantity: difference.unsigned_abs(), item: my_coins,
ours: true, quantity: difference.unsigned_abs(),
}); ours: true,
});
return Ok(());
}
} }
// If I am not offering coins // If I am not offering coins
} else if let Some(their_coins) = get_their_coins { } else if let Some(their_coins) = get_their_coins {
// Add their coins to balance if their_coin_amount >= difference.unsigned_abs() {
self.client.perform_trade_action(TradeAction::AddItem { // Add their coins to balance
item: their_coins, self.client.perform_trade_action(TradeAction::AddItem {
quantity: difference.unsigned_abs(), item: their_coins,
ours: false, quantity: difference.unsigned_abs(),
}); ours: false,
});
return Ok(());
}
} }
let their_name = self.find_player_alias(&their_uid).unwrap().clone();
self.client.send_command(
"tell".to_string(),
vec![
their_name,
format!(
"The value of my offer is {my_offered_items_value} coins. You only have {their_coin_amount} coins.",
),
],
);
} }
Ok(()) Ok(())
@ -955,6 +1006,12 @@ impl Bot {
} }
} }
#[derive(Debug)]
pub struct Reciept {
pub my_items: HashMap<String, u32>,
pub their_items: HashMap<String, u32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TradeMode { enum TradeMode {
AdminAccess, AdminAccess,