Add admin command

This commit is contained in:
Jeff 2024-06-12 20:08:58 -04:00
parent ace573358d
commit 158feb17c5

View File

@ -1,11 +1,16 @@
use std::{env::var, fs::read_to_string, sync::Arc, time::Duration}; use std::{
env::var,
fs::{read_to_string, write},
sync::Arc,
time::Duration,
};
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use veloren_client::{addr::ConnectionArgs, Client, Event}; 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, uuid::Uuid,
ViewDistances, ViewDistances,
}; };
@ -16,6 +21,11 @@ fn main() {
let password = read_to_string(&password_file) let password = read_to_string(&password_file)
.expect(&format!("Failed to read password from {password_file}")); .expect(&format!("Failed to read password from {password_file}"));
let mut admin_list = vec![Uuid::parse_str(CRABO_UUID).unwrap()];
write("admin_list.txt", format!("{admin_list:?}"))
.expect("Failed to write initial admin list.");
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));
@ -30,10 +40,13 @@ fn main() {
if let Event::Chat(message) = event { if let Event::Chat(message) = event {
match message.chat_type { match message.chat_type {
ChatType::Tell(sender, _) | ChatType::Group(sender, _) => { ChatType::Tell(sender, _) | ChatType::Group(sender, _) => {
let sender_uuid = client.player_list().get(&sender).unwrap().uuid;
handle_message( handle_message(
&mut client, &mut client,
&mut admin_list,
message.into_content().as_plain().unwrap_or(""), message.into_content().as_plain().unwrap_or(""),
sender, sender_uuid,
); );
} }
_ => {} _ => {}
@ -99,16 +112,24 @@ fn select_character(client: &mut Client, clock: &mut Clock) {
); );
} }
fn handle_message(mut client: &mut Client, content: &str, sender: Uid) { fn handle_message(
mut client: &mut Client,
mut admin_list: &mut Vec<Uuid>,
content: &str,
sender: Uuid,
) {
let mut words = content.split_whitespace(); let mut words = content.split_whitespace();
if let Some(command) = words.next() { if let Some(command) = words.next() {
match command { match command {
"admin" => {
if admin_list.contains(&sender) {
adminify_players(&mut client, &mut admin_list, words)
}
}
"inv" => invite_players(&mut client, words), "inv" => invite_players(&mut client, words),
"kick" => { "kick" => {
let sender_info = client.player_list().get(&sender).unwrap(); if admin_list.contains(&sender) {
if sender_info.uuid.to_string() == CRABO_UUID {
kick_players(&mut client, words) kick_players(&mut client, words)
} }
} }
@ -117,6 +138,26 @@ fn handle_message(mut client: &mut Client, content: &str, sender: Uid) {
} }
} }
fn adminify_players<'a, T: Iterator<Item = &'a str>>(
client: &mut Client,
admin_list: &mut Vec<Uuid>,
names: T,
) {
for name in names {
let find_id = client.player_list().iter().find_map(|(_, info)| {
if info.player_alias == name {
Some(info.uuid)
} else {
None
}
});
if let Some(player_id) = find_id {
admin_list.push(player_id);
}
}
}
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)| {