Improve code syntax

This commit is contained in:
Jeff 2024-06-12 19:53:52 -04:00
parent 63cad6daf6
commit ace573358d

View File

@ -5,6 +5,7 @@ use veloren_client::{addr::ConnectionArgs, Client, Event};
use veloren_common::{ use veloren_common::{
clock::Clock, clock::Clock,
comp::{invite::InviteKind, ChatType, ControllerInputs}, comp::{invite::InviteKind, ChatType, ControllerInputs},
uid::Uid,
ViewDistances, ViewDistances,
}; };
@ -18,33 +19,7 @@ fn main() {
let mut client = connect_to_veloren(password); let mut client = connect_to_veloren(password);
let mut clock = Clock::new(Duration::from_secs_f64(1.0)); let mut clock = Clock::new(Duration::from_secs_f64(1.0));
client.load_character_list(); select_character(&mut client, &mut clock);
while client.character_list().loading {
println!("Loading characters...");
client
.tick(ControllerInputs::default(), clock.dt())
.expect("Failed to run client.");
clock.tick();
}
let character_id = client
.character_list()
.characters
.first()
.unwrap()
.character
.id
.unwrap();
client.request_character(
character_id,
ViewDistances {
terrain: 0,
entity: 0,
},
);
loop { loop {
let events = client let events = client
@ -52,30 +27,17 @@ fn main() {
.expect("Failed to run client."); .expect("Failed to run client.");
for event in events { for event in events {
match event { if let Event::Chat(message) = event {
Event::Chat(message) => match message.chat_type { match message.chat_type {
ChatType::Tell(from, _) | ChatType::Group(from, _) => { ChatType::Tell(sender, _) | ChatType::Group(sender, _) => {
if let Some(content) = message.content().as_plain() { handle_message(
let mut words = content.split_whitespace(); &mut client,
message.into_content().as_plain().unwrap_or(""),
if let Some(command) = words.next() { sender,
match command { );
"inv" => invite_players(&mut client, words),
"kick" => {
let sender_info = client.player_list().get(&from).unwrap();
if sender_info.uuid.to_string() == CRABO_UUID {
kick_players(&mut client, words)
}
}
_ => continue,
}
}
}
} }
_ => {} _ => {}
}, }
_ => {}
} }
} }
@ -107,6 +69,54 @@ fn connect_to_veloren(password: String) -> Client {
.expect("Failed to create client instance.") .expect("Failed to create client instance.")
} }
fn select_character(client: &mut Client, clock: &mut Clock) {
client.load_character_list();
while client.character_list().loading {
println!("Loading characters...");
client
.tick(ControllerInputs::default(), clock.dt())
.expect("Failed to run client.");
clock.tick();
}
let character_id = client
.character_list()
.characters
.first()
.unwrap()
.character
.id
.unwrap();
client.request_character(
character_id,
ViewDistances {
terrain: 0,
entity: 0,
},
);
}
fn handle_message(mut client: &mut Client, content: &str, sender: Uid) {
let mut words = content.split_whitespace();
if let Some(command) = words.next() {
match command {
"inv" => invite_players(&mut client, words),
"kick" => {
let sender_info = client.player_list().get(&sender).unwrap();
if sender_info.uuid.to_string() == CRABO_UUID {
kick_players(&mut client, words)
}
}
_ => {}
}
}
}
fn invite_players<'a, T: Iterator<Item = &'a str>>(client: &mut Client, names: T) { fn invite_players<'a, T: Iterator<Item = &'a str>>(client: &mut Client, names: T) {
for name in names { for name in names {
let find_id = client.player_list().iter().find_map(|(id, info)| { let find_id = client.player_list().iter().find_map(|(id, info)| {