Add character field to config; Condense bot fns
This commit is contained in:
parent
1fa74aad14
commit
746e2174e1
@ -1,3 +1,4 @@
|
|||||||
|
character = "crabobot_test"
|
||||||
position = [17689.0, 14964.0, 238.0]
|
position = [17689.0, 14964.0, 238.0]
|
||||||
orientation = "East"
|
orientation = "East"
|
||||||
|
|
||||||
|
72
src/bot.rs
72
src/bot.rs
@ -20,6 +20,8 @@ use veloren_common_net::sync::WorldSyncExt;
|
|||||||
|
|
||||||
const COINS: &str = "common.items.utility.coins";
|
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 {
|
pub struct Bot {
|
||||||
position: [f32; 3],
|
position: [f32; 3],
|
||||||
orientation: String,
|
orientation: String,
|
||||||
@ -38,9 +40,12 @@ pub struct Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Bot {
|
impl Bot {
|
||||||
|
/// Connect to the official veloren server, select the specified character
|
||||||
|
/// return a Bot instance ready to run.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
username: &str,
|
username: &str,
|
||||||
password: &str,
|
password: &str,
|
||||||
|
character: &str,
|
||||||
buy_prices: HashMap<String, u32>,
|
buy_prices: HashMap<String, u32>,
|
||||||
sell_prices: HashMap<String, u32>,
|
sell_prices: HashMap<String, u32>,
|
||||||
position: [f32; 3],
|
position: [f32; 3],
|
||||||
@ -48,8 +53,38 @@ impl Bot {
|
|||||||
) -> Result<Self, String> {
|
) -> Result<Self, String> {
|
||||||
log::info!("Connecting to veloren");
|
log::info!("Connecting to veloren");
|
||||||
|
|
||||||
let client = connect_to_veloren(username, password)?;
|
let mut client = connect_to_veloren(username, password)?;
|
||||||
let clock = Clock::new(Duration::from_secs_f64(1.0 / 30.0));
|
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();
|
let now = Instant::now();
|
||||||
|
|
||||||
Ok(Bot {
|
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> {
|
pub fn tick(&mut self) -> Result<(), String> {
|
||||||
let veloren_events = self
|
let veloren_events = self
|
||||||
.client
|
.client
|
||||||
|
@ -17,6 +17,7 @@ pub struct Secrets {
|
|||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
|
pub character: String,
|
||||||
pub buy_prices: HashMap<String, u32>,
|
pub buy_prices: HashMap<String, u32>,
|
||||||
pub sell_prices: HashMap<String, u32>,
|
pub sell_prices: HashMap<String, u32>,
|
||||||
pub position: [f32; 3],
|
pub position: [f32; 3],
|
||||||
@ -46,6 +47,7 @@ fn main() {
|
|||||||
let mut bot = Bot::new(
|
let mut bot = Bot::new(
|
||||||
&secrets.username,
|
&secrets.username,
|
||||||
&secrets.password,
|
&secrets.password,
|
||||||
|
&config.character,
|
||||||
config.buy_prices,
|
config.buy_prices,
|
||||||
config.sell_prices,
|
config.sell_prices,
|
||||||
config.position,
|
config.position,
|
||||||
@ -53,8 +55,6 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.expect("Failed to create bot");
|
.expect("Failed to create bot");
|
||||||
|
|
||||||
bot.select_character().expect("Failed to select character");
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let _ = bot.tick().inspect_err(|error| eprintln!("{error}"));
|
let _ = bot.tick().inspect_err(|error| eprintln!("{error}"));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user