Add admin feature; Make 'take' command admin-only

This commit is contained in:
Jeff 2024-07-11 00:36:37 -04:00
parent 1f80cd9543
commit a7d9044b03
3 changed files with 32 additions and 34 deletions

View File

@ -1,5 +1,5 @@
position = [17726.0, 14960.0, 237.0] position = [17626.0, 14960.0, 237.0]
orientation = "West" orientation = 90.0
[buy_prices] [buy_prices]
"food.cheese" = 50 "food.cheese" = 50

View File

@ -5,7 +5,7 @@ use std::{
}; };
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use vek::{num_traits::Float, Quaternion}; use vek::Quaternion;
use veloren_client::{addr::ConnectionArgs, Client, Event as VelorenEvent, WorldExt}; use veloren_client::{addr::ConnectionArgs, Client, Event as VelorenEvent, WorldExt};
use veloren_common::{ use veloren_common::{
clock::Clock, clock::Clock,
@ -14,6 +14,7 @@ use veloren_common::{
time::DayPeriod, time::DayPeriod,
trade::{PendingTrade, TradeAction, TradeResult}, trade::{PendingTrade, TradeAction, TradeResult},
uid::Uid, uid::Uid,
uuid::Uuid,
ViewDistances, ViewDistances,
}; };
use veloren_common_net::sync::WorldSyncExt; use veloren_common_net::sync::WorldSyncExt;
@ -24,7 +25,8 @@ const COINS: &str = "common.items.utility.coins";
/// attempt to run every time the `tick` function is called. /// 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: f32,
admins: Vec<String>,
client: Client, client: Client,
clock: Clock, clock: Clock,
@ -45,10 +47,11 @@ impl Bot {
username: &str, username: &str,
password: &str, password: &str,
character: &str, character: &str,
admins: Vec<String>,
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],
orientation: String, orientation: f32,
) -> Result<Self, String> { ) -> Result<Self, String> {
log::info!("Connecting to veloren"); log::info!("Connecting to veloren");
@ -89,6 +92,7 @@ impl Bot {
Ok(Bot { Ok(Bot {
position, position,
orientation, orientation,
admins,
client, client,
clock, clock,
buy_prices, buy_prices,
@ -168,7 +172,15 @@ impl Bot {
} }
} }
"take" => { "take" => {
if !self.client.is_trading() { let sender_uuid = self
.find_uuid(&sender)
.ok_or("Failed to find uuid")?
.to_string();
let sender_name = self.find_name(&sender).ok_or("Failed to find name")?;
let sender_is_admin =
self.admins.contains(&sender_uuid) || self.admins.contains(sender_name);
if sender_is_admin && !self.client.is_trading() {
self.trade_mode = TradeMode::Take; self.trade_mode = TradeMode::Take;
self.client.send_invite(sender, InviteKind::Trade); self.client.send_invite(sender, InviteKind::Trade);
} }
@ -529,10 +541,7 @@ impl Bot {
self.client.send_command( self.client.send_command(
"tell".to_string(), "tell".to_string(),
vec![ vec![player_name, format!("I don't have a price for that item.")],
player_name.clone(),
format!("I don't have a price for that item."),
],
); );
} }
@ -562,22 +571,9 @@ impl Bot {
let ecs = self.client.state_mut().ecs(); let ecs = self.client.state_mut().ecs();
let mut position_state = ecs.write_storage::<Pos>(); let mut position_state = ecs.write_storage::<Pos>();
let mut orientation_state = ecs.write_storage::<Ori>(); let mut orientation_state = ecs.write_storage::<Ori>();
let orientation = match self.orientation.to_lowercase().as_str() { let orientation = Ori::default()
"west" => Ori::default()
.uprighted() .uprighted()
.rotated(Quaternion::rotation_z(90.0.to_radians())), .rotated(Quaternion::rotation_z(self.orientation.to_radians()));
"south" => Ori::default()
.uprighted()
.rotated(Quaternion::rotation_z(180.0.to_radians())),
"east" => Ori::default()
.uprighted()
.rotated(Quaternion::rotation_z(270.0.to_radians())),
"north" => Ori::default(),
_ => {
return Err("Orientation must north, east, south or west".to_string());
}
};
orientation_state orientation_state
.insert(entity, orientation) .insert(entity, orientation)
.map_err(|error| error.to_string())?; .map_err(|error| error.to_string())?;
@ -588,20 +584,20 @@ impl Bot {
Ok(()) Ok(())
} }
fn _find_uid<'a>(&'a self, name: &str) -> Option<&'a Uid> { fn find_uuid(&self, target: &Uid) -> Option<Uuid> {
self.client.player_list().iter().find_map(|(id, info)| { self.client.player_list().iter().find_map(|(uid, info)| {
if info.player_alias == name { if uid == target {
Some(id) Some(info.uuid)
} else { } else {
None None
} }
}) })
} }
fn _find_uuid(&self, name: &str) -> Option<String> { fn _find_uid<'a>(&'a self, name: &str) -> Option<&'a Uid> {
self.client.player_list().iter().find_map(|(_, info)| { self.client.player_list().iter().find_map(|(id, info)| {
if info.player_alias == name { if info.player_alias == name {
Some(info.uuid.to_string()) Some(id)
} else { } else {
None None
} }

View File

@ -10,6 +10,7 @@ pub struct Secrets {
pub username: String, pub username: String,
pub password: String, pub password: String,
pub character: String, pub character: String,
pub admins: Vec<String>,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -17,7 +18,7 @@ struct Config {
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],
pub orientation: String, pub orientation: f32,
} }
fn main() { fn main() {
@ -62,6 +63,7 @@ fn main() {
&secrets.username, &secrets.username,
&secrets.password, &secrets.password,
&secrets.character, &secrets.character,
secrets.admins,
buy_prices_with_full_id, buy_prices_with_full_id,
sell_prices_with_full_id, sell_prices_with_full_id,
config.position, config.position,