From d0437cd0039ec1eb725f72d8848503a86658e5f3 Mon Sep 17 00:00:00 2001 From: Michael Mikovsky <77305074+Astatin3@users.noreply.github.com> Date: Fri, 23 May 2025 11:51:20 -0600 Subject: [PATCH] Make command system --- src/bot_task/chat_task.rs | 3 +++ src/bot_task/goto_block.rs | 3 +++ src/command_controler.rs | 23 ++++------------- src/main.rs | 51 +++++++++++++++++++++++++++++++++++--- 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/bot_task/chat_task.rs b/src/bot_task/chat_task.rs index 3b18751..6e2ccf7 100644 --- a/src/bot_task/chat_task.rs +++ b/src/bot_task/chat_task.rs @@ -23,6 +23,9 @@ impl Chat { } impl BotTask for Chat { + fn get_name(&self) -> &str { + "Chat" + } fn on_event(&mut self, bot: &Client, _event: &Event) { if self.last_update.elapsed() >= DELAY { bot.chat(self.messages[self.index].as_str()); diff --git a/src/bot_task/goto_block.rs b/src/bot_task/goto_block.rs index e4f5a0b..072931e 100644 --- a/src/bot_task/goto_block.rs +++ b/src/bot_task/goto_block.rs @@ -44,6 +44,9 @@ impl GotoBlock { } impl BotTask for GotoBlock { + fn get_name(&self) -> &str { + "Goto" + } fn on_event(&mut self, bot: &Client, event: &Event) { if !self.started { self.started = true; diff --git a/src/command_controler.rs b/src/command_controler.rs index f295bdf..686d686 100644 --- a/src/command_controler.rs +++ b/src/command_controler.rs @@ -1,27 +1,14 @@ use azalea::{Client, Event}; - -use crate::bot_task::*; +use std::fmt::Debug; pub trait BotTask: Send { + fn get_name(&self) -> &str; fn on_event(&mut self, bot: &Client, event: &Event); fn end(&self) -> bool; } -pub fn parse_command(command: &str) -> Option> { - let args = command - .split_whitespace() - .map(|s| s.to_string()) - .collect::>(); - - match args[0].as_str() { - "!chat" => Some(Box::new(Chat::init(args[1..].to_vec()))), - "!goto" => { - if let Some(task) = GotoBlock::parse(args[1..].to_vec()) { - Some(Box::new(task)) - } else { - return None; - } - } - _ => None, +impl Debug for dyn BotTask { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "BotTask({})", self.get_name()) } } diff --git a/src/main.rs b/src/main.rs index 10da539..d1489a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{alloc::System, sync::Arc}; pub mod bot_task; pub mod command_controler; @@ -10,6 +10,8 @@ use azalea::{ use command_controler::BotTask; use parking_lot::Mutex; +use crate::bot_task::*; + pub static BOT_COUNT: usize = 3; pub static BOT_PREFIX: &'static str = "bot"; @@ -67,12 +69,29 @@ async fn handle(bot: Client, event: Event, state: BotState) -> anyhow::Result<() Ok(()) } -#[derive(Default, Clone, Component)] +#[derive(Clone, Component)] pub struct BotState { pub task: Arc>>>, // pub messages_received: Arc>, } +impl Default for BotState { + fn default() -> Self { + Self { + task: Arc::new(Mutex::new(None)), + } + } +} + +impl BotState { + fn get_task(&self) -> String { + match self.task.lock().as_ref() { + Some(task) => task.get_name().to_string(), + None => "No task".to_string(), + } + } +} + #[derive(Resource, Default, Clone)] struct SwarmState { pub tasks: Arc>>>, @@ -97,7 +116,33 @@ async fn handle_swarm(swarm: Swarm, event: SwarmEvent, state: SwarmState) -> any println!("Chat message: {}", command); println!("{}", command); - if let Some(command) = command_controler::parse_command(command.as_str()) { + if let Some(command) = { + let args = command + .split_whitespace() + .map(|s| s.to_string()) + .collect::>(); + + match args[0].as_str() { + "!chat" => Some(Box::new(Chat::init(args[1..].to_vec())) as Box), + "!goto" => { + if let Some(task) = GotoBlock::parse(args[1..].to_vec()) { + Some(Box::new(task) as Box) + } else { + None + } + } + "!status" => { + for bot in swarm { + let botstate = &bot.get_component::().unwrap(); + println!("{} - {}", bot.username(), botstate.get_task()); + } + println!("Unstarted: {:?}", state.tasks.lock()); + + None + } + _ => None, + } + } { state.tasks.lock().push(command); }