dev #1

Merged
jeff merged 2 commits from dev into main 2024-07-18 12:04:06 +00:00
2 changed files with 26 additions and 10 deletions
Showing only changes of commit 3e47b20101 - Show all commits

View File

@ -163,7 +163,8 @@ impl Bot {
}) })
} }
/// Run the bot for a single tick. This should be called in a loop. /// Run the bot for a single tick. This should be called in a loop. Returns `true` if the loop
/// should continue running.
/// ///
/// There are three timers in this function: /// There are three timers in this function:
/// - The [Clock] runs the Veloren client. At **30 ticks per second** this timer is faster than /// - The [Clock] runs the Veloren client. At **30 ticks per second** this timer is faster than
@ -175,14 +176,18 @@ impl Bot {
/// This function should be modified with care. In addition to being the bot's main loop, it /// This function should be modified with care. In addition to being the bot's main loop, it
/// also accepts incoming trade invites, which has a potential for error if the bot accepts an /// also accepts incoming trade invites, which has a potential for error if the bot accepts an
/// invite while in the wrong trade mode. /// invite while in the wrong trade mode.
pub fn tick(&mut self) -> Result<(), String> { pub fn tick(&mut self) -> Result<bool, String> {
let veloren_events = self let veloren_events = self
.client .client
.tick(ControllerInputs::default(), self.clock.dt()) .tick(ControllerInputs::default(), self.clock.dt())
.map_err(|error| format!("{error:?}"))?; .map_err(|error| format!("{error:?}"))?;
for event in veloren_events { for event in veloren_events {
self.handle_veloren_event(event)?; let should_continue = self.handle_veloren_event(event)?;
if !should_continue {
return Ok(false);
}
} }
if self.last_trade_action.elapsed() > TRADE_ACTION_DELAY { if self.last_trade_action.elapsed() > TRADE_ACTION_DELAY {
@ -236,17 +241,18 @@ impl Bot {
self.clock.tick(); self.clock.tick();
Ok(()) Ok(true)
} }
/// Consume and manage a client-side Veloren event. /// Consume and manage a client-side Veloren event. Returns a boolean indicating whether the
fn handle_veloren_event(&mut self, event: VelorenEvent) -> Result<(), String> { /// bot should continue processing events.
fn handle_veloren_event(&mut self, event: VelorenEvent) -> Result<bool, String> {
match event { match event {
VelorenEvent::Chat(message) => { VelorenEvent::Chat(message) => {
let sender = if let ChatType::Tell(uid, _) = message.chat_type { let sender = if let ChatType::Tell(uid, _) = message.chat_type {
uid uid
} else { } else {
return Ok(()); return Ok(true);
}; };
let content = message.content().as_plain().unwrap_or_default(); let content = message.content().as_plain().unwrap_or_default();
let mut split_content = content.split(' '); let mut split_content = content.split(' ');
@ -375,7 +381,7 @@ impl Bot {
} }
VelorenEvent::Outcome(Outcome::HealthChange { info, .. }) => { VelorenEvent::Outcome(Outcome::HealthChange { info, .. }) => {
if let Some(DamageSource::Buff(_)) = info.cause { if let Some(DamageSource::Buff(_)) = info.cause {
return Ok(()); return Ok(true);
} }
if let Some(uid) = self.client.uid() { if let Some(uid) = self.client.uid() {
@ -424,10 +430,13 @@ impl Bot {
self.trade_mode = TradeMode::Trade; self.trade_mode = TradeMode::Trade;
} }
} }
VelorenEvent::Disconnect => {
return Ok(false);
}
_ => (), _ => (),
} }
Ok(()) Ok(true)
} }
/// Make the bot's trading and help accouncements /// Make the bot's trading and help accouncements

View File

@ -8,6 +8,7 @@ use std::{env::var, fs::read_to_string};
use bot::Bot; use bot::Bot;
use config::{Config, Secrets}; use config::{Config, Secrets};
use env_logger::Env; use env_logger::Env;
use log::error;
fn main() { fn main() {
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init(); env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
@ -48,6 +49,12 @@ fn main() {
.expect("Failed to create bot"); .expect("Failed to create bot");
loop { loop {
let _ = bot.tick().inspect_err(|error| log::error!("{error}")); match bot.tick() {
Ok(true) => return,
Ok(false) => {}
Err(error) => {
error!("{error}");
}
}
} }
} }