Compare commits

..

No commits in common. "a66f65058f25370beb025c8df8f47f76476a94f9" and "612451142d39f56f1bba61066e9f66fdbb63f912" have entirely different histories.

View File

@ -127,6 +127,8 @@ impl Bot {
TradeMode::Take => self.handle_take(trade.clone())?, TradeMode::Take => self.handle_take(trade.clone())?,
} }
} else if self.client.pending_invites().is_empty() { } else if self.client.pending_invites().is_empty() {
self.is_player_notified = false;
self.client.accept_invite(); self.client.accept_invite();
} }
@ -135,7 +137,7 @@ impl Bot {
if self.last_announcement.elapsed() > Duration::from_secs(1200) { if self.last_announcement.elapsed() > Duration::from_secs(1200) {
self.client.send_command( self.client.send_command(
"region".to_string(), "region".to_string(),
vec!["I'm a bot. You can trade with me or use /say or /tell to check prices: 'price [item_name]'.".to_string()], vec!["I'm a bot. Trade with me or say 'prices' to see my offers.".to_string()],
); );
self.last_announcement = Instant::now(); self.last_announcement = Instant::now();
@ -151,53 +153,25 @@ impl Bot {
fn handle_veloren_event(&mut self, event: VelorenEvent) -> Result<(), String> { fn handle_veloren_event(&mut self, event: VelorenEvent) -> Result<(), String> {
match event { match event {
VelorenEvent::Chat(message) => { VelorenEvent::Chat(message) => {
let sender = match message.chat_type {
ChatType::Tell(uid, _) => uid,
ChatType::Say(uid) => uid,
_ => return Ok(()),
};
let content = message.content().as_plain().unwrap_or_default(); let content = message.content().as_plain().unwrap_or_default();
let (command, item_name) = content.split_once(' ').unwrap_or((content, ""));
match command { match message.chat_type {
"price" => { ChatType::Tell(sender_uid, _) | ChatType::Say(sender_uid) => {
let player_name = self match content.trim() {
.find_name(&sender) "prices" => self.send_price_info(&sender_uid)?,
.ok_or("Failed to find player name")?
.to_string();
let mut found = false;
for (item_id, price) in &self.buy_prices {
if item_id.contains(item_name) {
self.client.send_command(
"tell".to_string(),
vec![
player_name.clone(),
format!("{item_id} costs {price} coins."),
],
);
found = true;
}
}
if !found {
self.client.send_command(
"tell".to_string(),
vec![player_name.clone(), format!("I don't have that item.")],
);
}
}
"prices" => self.send_all_price_info(&sender)?,
"take" => { "take" => {
if !self.client.is_trading() { if !self.client.is_trading() {
self.trade_mode = TradeMode::Take; self.trade_mode = TradeMode::Take;
self.client.send_invite(sender, InviteKind::Trade); self.client.send_invite(sender_uid, InviteKind::Trade);
} }
} }
_ => {} _ => {}
} }
} }
_ => {}
}
}
VelorenEvent::Outcome(Outcome::ProjectileHit { VelorenEvent::Outcome(Outcome::ProjectileHit {
target: Some(target), target: Some(target),
.. ..
@ -228,8 +202,7 @@ impl Bot {
log::info!("Completed trade: {result:?}"); log::info!("Completed trade: {result:?}");
if let TradeMode::Take = self.trade_mode { if let TradeMode::Take = self.trade_mode {
self.is_player_notified = false; self.trade_mode = TradeMode::Trade
self.trade_mode = TradeMode::Trade;
} }
if let TradeResult::Completed = result { if let TradeResult::Completed = result {
@ -263,21 +236,14 @@ impl Bot {
} }
fn handle_trade(&mut self, trade: PendingTrade) -> Result<(), String> { fn handle_trade(&mut self, trade: PendingTrade) -> Result<(), String> {
let my_offer_index = trade
.which_party(self.client.uid().ok_or("Failed to get uid")?)
.ok_or("Failed to get offer index")?;
let their_offer_index = if my_offer_index == 0 { 1 } else { 0 };
if !self.is_player_notified {
self.send_all_price_info(&trade.parties[their_offer_index])?;
self.is_player_notified = true;
}
if trade.is_empty_trade() { if trade.is_empty_trade() {
return Ok(()); return Ok(());
} }
let my_offer_index = trade
.which_party(self.client.uid().ok_or("Failed to get uid")?)
.ok_or("Failed to get offer index")?;
let their_offer_index = if my_offer_index == 0 { 1 } else { 0 };
let (my_offer, their_offer, them) = { let (my_offer, their_offer, them) = {
( (
&trade.offers[my_offer_index], &trade.offers[my_offer_index],
@ -390,6 +356,12 @@ impl Bot {
drop(inventories); drop(inventories);
if !self.is_player_notified {
self.send_price_info(&trade.parties[their_offer_index])?;
self.is_player_notified = true;
}
if their_offered_items_value == 0 && my_offered_items_value == 0 { if their_offered_items_value == 0 && my_offered_items_value == 0 {
return Ok(()); return Ok(());
} }
@ -490,7 +462,7 @@ impl Bot {
Ok(()) Ok(())
} }
fn send_all_price_info(&mut self, target: &Uid) -> Result<(), String> { fn send_price_info(&mut self, target: &Uid) -> Result<(), String> {
let player_name = self let player_name = self
.find_name(target) .find_name(target)
.ok_or("Failed to find player name")? .ok_or("Failed to find player name")?