Merchants no longer buy/sell things they don't know the price of

This commit is contained in:
Joshua Barretto 2023-04-01 01:25:35 +01:00
parent ea007ff702
commit 8ba68e30f3
2 changed files with 86 additions and 58 deletions

View File

@ -381,15 +381,16 @@ impl SitePrices {
inventories: &[Option<ReducedInventory>; 2], inventories: &[Option<ReducedInventory>; 2],
who: usize, who: usize,
reduce: bool, reduce: bool,
) -> f32 { ) -> Option<f32> {
offers[who] offers[who]
.iter() .iter()
.map(|(slot, amount)| { .map(|(slot, amount)| {
inventories[who] inventories[who]
.as_ref() .as_ref()
.and_then(|ri| { .map(|ri| {
ri.inventory.get(slot).map(|item| { let item = ri.inventory.get(slot)?;
if let Some(vec) = TradePricing::get_materials(&item.name.as_ref()) { if let Some(vec) = TradePricing::get_materials(&item.name.as_ref()) {
Some(
vec.iter() vec.iter()
.map(|(amount2, material)| { .map(|(amount2, material)| {
self.values.get(material).copied().unwrap_or_default() self.values.get(material).copied().unwrap_or_default()
@ -397,15 +398,15 @@ impl SitePrices {
* (if reduce { material.trade_margin() } else { 1.0 }) * (if reduce { material.trade_margin() } else { 1.0 })
}) })
.sum::<f32>() .sum::<f32>()
* (*amount as f32) * (*amount as f32),
)
} else { } else {
0.0 None
} }
}) })
.unwrap_or(Some(0.0))
}) })
.unwrap_or_default() .try_fold(0.0, |a, p| Some(a + p?))
})
.sum()
} }
} }

View File

@ -524,14 +524,29 @@ pub fn handle_inbox_update_pending_trade(bdata: &mut BehaviorData) -> bool {
let who = usize::from(!agent.behavior.is(BehaviorState::TRADING_ISSUER)); let who = usize::from(!agent.behavior.is(BehaviorState::TRADING_ISSUER));
match agent.behavior.trading_behavior { match agent.behavior.trading_behavior {
TradingBehavior::RequireBalanced { .. } => { TradingBehavior::RequireBalanced { .. } => {
let balance0: f32 = let balance0 = prices.balance(&pending.offers, &inventories, 1 - who, true);
prices.balance(&pending.offers, &inventories, 1 - who, true); let balance1 = prices.balance(&pending.offers, &inventories, who, false);
let balance1: f32 = prices.balance(&pending.offers, &inventories, who, false); match (balance0, balance1) {
(_, None) => {
event_emitter.emit(ServerEvent::Chat(UnresolvedChatMsg::npc_say(
*agent_data.uid,
format!("I'm not willing to sell that item"),
)))
},
(None, _) => {
event_emitter.emit(ServerEvent::Chat(UnresolvedChatMsg::npc_say(
*agent_data.uid,
format!("I'm not willing to buy that item"),
)))
},
(Some(balance0), Some(balance1)) => {
if balance0 >= balance1 { if balance0 >= balance1 {
// If the trade is favourable to us, only send an accept message if we're // If the trade is favourable to us, only send an accept message if
// not already accepting (since otherwise, spam-clicking the accept button // we're not already accepting
// results in lagging and moving to the review phase of an unfavorable trade // (since otherwise, spam-clicking the accept button
// (although since the phase is included in the message, this shouldn't // results in lagging and moving to the review phase of an
// unfavorable trade (although since
// the phase is included in the message, this shouldn't
// result in fully accepting an unfavourable trade)) // result in fully accepting an unfavourable trade))
if !pending.accept_flags[who] && !pending.is_empty_trade() { if !pending.accept_flags[who] && !pending.is_empty_trade() {
event_emitter.emit(ServerEvent::ProcessTradeAction( event_emitter.emit(ServerEvent::ProcessTradeAction(
@ -539,7 +554,12 @@ pub fn handle_inbox_update_pending_trade(bdata: &mut BehaviorData) -> bool {
tradeid, tradeid,
TradeAction::Accept(pending.phase), TradeAction::Accept(pending.phase),
)); ));
tracing::trace!(?tradeid, ?balance0, ?balance1, "Accept Pending Trade"); tracing::trace!(
?tradeid,
?balance0,
?balance1,
"Accept Pending Trade"
);
} }
} else { } else {
if balance1 > 0.0 { if balance1 > 0.0 {
@ -548,10 +568,15 @@ pub fn handle_inbox_update_pending_trade(bdata: &mut BehaviorData) -> bool {
(balance0 / balance1 * 100.0).floor() (balance0 / balance1 * 100.0).floor()
); );
if let Some(tgt_data) = &agent.target { if let Some(tgt_data) = &agent.target {
// If talking with someone in particular, "tell" it only to them // If talking with someone in particular, "tell" it only to
// them
if let Some(with) = read_data.uids.get(tgt_data.target) { if let Some(with) = read_data.uids.get(tgt_data.target) {
event_emitter.emit(ServerEvent::Chat( event_emitter.emit(ServerEvent::Chat(
UnresolvedChatMsg::npc_tell(*agent_data.uid, *with, msg), UnresolvedChatMsg::npc_tell(
*agent_data.uid,
*with,
msg,
),
)); ));
} else { } else {
event_emitter.emit(ServerEvent::Chat( event_emitter.emit(ServerEvent::Chat(
@ -559,14 +584,14 @@ pub fn handle_inbox_update_pending_trade(bdata: &mut BehaviorData) -> bool {
)); ));
} }
} else { } else {
event_emitter.emit(ServerEvent::Chat(UnresolvedChatMsg::npc_say( event_emitter.emit(ServerEvent::Chat(
*agent_data.uid, UnresolvedChatMsg::npc_say(*agent_data.uid, msg),
msg, ));
)));
} }
} }
if pending.phase != TradePhase::Mutate { if pending.phase != TradePhase::Mutate {
// we got into the review phase but without balanced goods, decline // we got into the review phase but without balanced goods,
// decline
agent.behavior.unset(BehaviorState::TRADING); agent.behavior.unset(BehaviorState::TRADING);
agent.target = None; agent.target = None;
event_emitter.emit(ServerEvent::ProcessTradeAction( event_emitter.emit(ServerEvent::ProcessTradeAction(
@ -577,6 +602,8 @@ pub fn handle_inbox_update_pending_trade(bdata: &mut BehaviorData) -> bool {
} }
} }
}, },
}
},
TradingBehavior::AcceptFood => { TradingBehavior::AcceptFood => {
let mut only_food = true; let mut only_food = true;
let ability_map = AbilityMap::load().read(); let ability_map = AbilityMap::load().read();