Add character field to config; Condense bot fns

This commit is contained in:
Jeff 2024-07-08 16:58:40 -04:00
parent 1fa74aad14
commit 746e2174e1
3 changed files with 40 additions and 37 deletions

View File

@ -1,3 +1,4 @@
character = "crabobot_test"
position = [17689.0, 14964.0, 238.0]
orientation = "East"

View File

@ -20,6 +20,8 @@ use veloren_common_net::sync::WorldSyncExt;
const COINS: &str = "common.items.utility.coins";
/// A Bot instance represents an active connection to the server and it will
/// attempt to run every time the `tick` function is called.
pub struct Bot {
position: [f32; 3],
orientation: String,
@ -38,9 +40,12 @@ pub struct Bot {
}
impl Bot {
/// Connect to the official veloren server, select the specified character
/// return a Bot instance ready to run.
pub fn new(
username: &str,
password: &str,
character: &str,
buy_prices: HashMap<String, u32>,
sell_prices: HashMap<String, u32>,
position: [f32; 3],
@ -48,8 +53,38 @@ impl Bot {
) -> Result<Self, String> {
log::info!("Connecting to veloren");
let client = connect_to_veloren(username, password)?;
let clock = Clock::new(Duration::from_secs_f64(1.0 / 30.0));
let mut client = connect_to_veloren(username, password)?;
let mut clock = Clock::new(Duration::from_secs_f64(1.0 / 30.0));
log::info!("Selecting a character");
client.load_character_list();
while client.character_list().loading {
client
.tick(ControllerInputs::default(), clock.dt())
.map_err(|error| format!("{error:?}"))?;
clock.tick();
}
let character_id = client
.character_list()
.characters
.iter()
.find(|character_item| character_item.character.alias == character)
.expect("No characters to select")
.character
.id
.expect("Failed to get character ID");
client.request_character(
character_id,
ViewDistances {
terrain: 4,
entity: 4,
},
);
let now = Instant::now();
Ok(Bot {
@ -67,39 +102,6 @@ impl Bot {
})
}
pub fn select_character(&mut self) -> Result<(), String> {
log::info!("Selecting a character");
self.client.load_character_list();
while self.client.character_list().loading {
self.client
.tick(ControllerInputs::default(), self.clock.dt())
.map_err(|error| format!("{error:?}"))?;
self.clock.tick();
}
let character_id = self
.client
.character_list()
.characters
.first()
.expect("No characters to select")
.character
.id
.expect("Failed to get character ID");
self.client.request_character(
character_id,
ViewDistances {
terrain: 4,
entity: 4,
},
);
Ok(())
}
pub fn tick(&mut self) -> Result<(), String> {
let veloren_events = self
.client

View File

@ -17,6 +17,7 @@ pub struct Secrets {
#[derive(Serialize, Deserialize)]
struct Config {
pub character: String,
pub buy_prices: HashMap<String, u32>,
pub sell_prices: HashMap<String, u32>,
pub position: [f32; 3],
@ -46,6 +47,7 @@ fn main() {
let mut bot = Bot::new(
&secrets.username,
&secrets.password,
&config.character,
config.buy_prices,
config.sell_prices,
config.position,
@ -53,8 +55,6 @@ fn main() {
)
.expect("Failed to create bot");
bot.select_character().expect("Failed to select character");
loop {
let _ = bot.tick().inspect_err(|error| eprintln!("{error}"));
}