Add orientation command

This commit is contained in:
Jeff 2024-07-12 11:59:28 -04:00
parent f334391b47
commit c59cff438a
2 changed files with 65 additions and 34 deletions

View File

@ -150,7 +150,7 @@ impl Bot {
self.client.accept_invite(); self.client.accept_invite();
} }
if self.last_announcement.elapsed() > Duration::from_secs(1800) { if self.last_announcement.elapsed() > Duration::from_mins(30) {
self.handle_announcement()?; self.handle_announcement()?;
self.last_announcement = Instant::now(); self.last_announcement = Instant::now();
@ -176,20 +176,17 @@ impl Bot {
let content = message.content().as_plain().unwrap_or_default(); let content = message.content().as_plain().unwrap_or_default();
let mut split_content = content.split(' '); let mut split_content = content.split(' ');
let command = split_content.next().unwrap_or_default(); let command = split_content.next().unwrap_or_default();
let mut is_correct_format = false; let price_correction_message = "Use the format 'price [search_term]'";
let correction_message = match command {
match command {
"price" => { "price" => {
for item_name in split_content { for item_name in split_content {
is_correct_format = true;
self.send_price_info(&sender, &item_name.to_lowercase())?; self.send_price_info(&sender, &item_name.to_lowercase())?;
} }
None
} }
"sort" => { "sort" => {
if self.is_user_admin(&sender)? { if self.is_user_admin(&sender)? {
is_correct_format = true;
if let Some(sort_count) = split_content.next() { if let Some(sort_count) = split_content.next() {
let sort_count = sort_count let sort_count = sort_count
.parse::<u8>() .parse::<u8>()
@ -202,15 +199,22 @@ impl Bot {
log::debug!("Sorting inventory"); log::debug!("Sorting inventory");
self.client.sort_inventory(); self.client.sort_inventory();
} }
None
} else {
Some(price_correction_message)
} }
} }
"admin_access" => { "admin_access" => {
if self.is_user_admin(&sender)? && !self.client.is_trading() { if self.is_user_admin(&sender)? && !self.client.is_trading() {
log::debug!("Providing admin access"); log::debug!("Providing admin access");
is_correct_format = true;
self.trade_mode = TradeMode::AdminAccess; self.trade_mode = TradeMode::AdminAccess;
self.client.send_invite(sender, InviteKind::Trade); self.client.send_invite(sender, InviteKind::Trade);
None
} else {
Some(price_correction_message)
} }
} }
"position" => { "position" => {
@ -226,15 +230,35 @@ impl Bot {
z.parse::<f32>().map_err(|error| error.to_string())?, z.parse::<f32>().map_err(|error| error.to_string())?,
]; ];
is_correct_format = true;
self.position = position; self.position = position;
None
} else {
Some("Use the format 'position [x] [y] [z]'.")
} }
} else {
Some(price_correction_message)
} }
} }
_ => {} "orientation" => {
} if self.is_user_admin(&sender)? {
if let Some(orientation) = split_content.next() {
self.orientation = orientation
.parse::<f32>()
.map_err(|error| error.to_string())?;
if !is_correct_format { None
} else {
Some("Use the format 'orientation [0-360]'")
}
} else {
Some(price_correction_message)
}
}
_ => Some(price_correction_message),
};
if let Some(message) = correction_message {
let player_name = self let player_name = self
.find_name(&sender) .find_name(&sender)
.ok_or("Failed to find player name")? .ok_or("Failed to find player name")?
@ -242,10 +266,7 @@ impl Bot {
self.client.send_command( self.client.send_command(
"tell".to_string(), "tell".to_string(),
vec![ vec![player_name.clone(), message.to_string()],
player_name.clone(),
format!("Use the format 'price [search_term]'."),
],
); );
} }
} }
@ -624,27 +645,35 @@ impl Bot {
} }
fn handle_position_and_orientation(&mut self) -> Result<(), String> { fn handle_position_and_orientation(&mut self) -> Result<(), String> {
let current_position = self.client.current::<Pos>(); if let Some(current_position) = self.client.current::<Pos>() {
let target_position = Pos(self.position.into());
if let Some(current_position) = current_position { if current_position != target_position {
if current_position.0 == self.position.into() { let entity = self.client.entity();
return Ok(()); let ecs = self.client.state_mut().ecs();
let mut position_state = ecs.write_storage::<Pos>();
position_state
.insert(entity, target_position)
.map_err(|error| error.to_string())?;
} }
} }
let entity = self.client.entity(); if let Some(current_orientation) = self.client.current::<Ori>() {
let ecs = self.client.state_mut().ecs(); let target_orientation = Ori::default()
let mut position_state = ecs.write_storage::<Pos>(); .uprighted()
let mut orientation_state = ecs.write_storage::<Ori>(); .rotated(Quaternion::rotation_z(self.orientation.to_radians()));
let orientation = Ori::default()
.uprighted() if current_orientation != target_orientation {
.rotated(Quaternion::rotation_z(self.orientation.to_radians())); let entity = self.client.entity();
orientation_state let ecs = self.client.state_mut().ecs();
.insert(entity, orientation) let mut orientation_state = ecs.write_storage::<Ori>();
.map_err(|error| error.to_string())?;
position_state orientation_state
.insert(entity, Pos(self.position.into())) .insert(entity, target_orientation)
.map_err(|error| error.to_string())?; .map_err(|error| error.to_string())?;
}
}
Ok(()) Ok(())
} }

View File

@ -1,3 +1,5 @@
#![feature(duration_constructors)]
mod bot; mod bot;
use std::{collections::HashMap, env::var, fs::read_to_string}; use std::{collections::HashMap, env::var, fs::read_to_string};