diff options
Diffstat (limited to 'core/src/data/controller')
| -rw-r--r-- | core/src/data/controller/cli/cli_command.rs | 111 | ||||
| -rw-r--r-- | core/src/data/controller/runtime/implements.rs | 30 | ||||
| -rw-r--r-- | core/src/data/controller/runtime/structs.rs | 3 |
3 files changed, 133 insertions, 11 deletions
diff --git a/core/src/data/controller/cli/cli_command.rs b/core/src/data/controller/cli/cli_command.rs index b5c0414..d5cda07 100644 --- a/core/src/data/controller/cli/cli_command.rs +++ b/core/src/data/controller/cli/cli_command.rs @@ -1,5 +1,8 @@ +use std::process::exit; use std::sync::{Arc, Mutex}; -use clap::{Parser, Subcommand}; +use clap::{Args, Parser, Subcommand}; +use clearscreen::clear; +use log::info; use nogamepads::entry_mutex; use crate::data::controller::runtime::structs::ControllerRuntime; use crate::data::message::enums::ControlMessage; @@ -19,30 +22,118 @@ enum Commands { #[command(about = "Clean the screen")] Clear, + #[command(about = "Close the controller")] + Close, + + #[command(about = "Exit the console")] + Exit, + #[command(about = "Send a message")] - Message, + Message(MessageArgs), - #[command(about = "Close the controller")] - Close + #[command(about = "Press a button")] + Press(ButtonArgs), + + #[command(about = "Release a button")] + Release(ButtonArgs), + + #[command(about = "Change a axis value")] + Axis(AxisArgs), + + #[command(about = "Change a direction value")] + Direction(DirectionArgs), + + Pop, + + PopAll +} + +#[derive(Args, Debug)] +struct MessageArgs { + message: String, +} + +#[derive(Args, Debug)] +struct ButtonArgs { + button_key: u8 +} + +#[derive(Args, Debug)] +struct AxisArgs { + axis_key: u8, + axis_value: f64 +} + +#[derive(Args, Debug)] +struct DirectionArgs { + dir_key: u8, + x: f64, + y: f64 } pub fn process_controller_cli(runtime: Arc<Mutex<ControllerRuntime>>, cmd: ControllerCli) { match cmd.command { Commands::Clear => { - + clear().expect("Failed to clear screen"); } - Commands::Message => { + Commands::Close => { entry_mutex!(runtime, |guard| { - guard.send(ControlMessage::Msg("fuck".to_string()), 0, TCPConnection); + guard.close(); }); } - Commands::Close => { + Commands::Exit => { + exit(1); + } + + Commands::Message(args) => { entry_mutex!(runtime, |guard| { - guard.close(); - }); + guard.message(args.message); + }) + } + + Commands::Press(args) => { + entry_mutex!(runtime, |guard| { + guard.press_button(args.button_key); + }) + } + + Commands::Release(args) => { + entry_mutex!(runtime, |guard| { + guard.release_button(args.button_key); + }) + } + + Commands::Axis(args) => { + entry_mutex!(runtime, |guard| { + guard.change_axis(args.axis_key, args.axis_value); + }) + } + + Commands::Direction(args) => { + entry_mutex!(runtime, |guard| { + guard.change_direction(args.dir_key, args.x, args.y); + }) + } + + Commands::Pop => { + entry_mutex!(runtime, |guard| { + if let Some(msg) = guard.pop() { + info!("Pop: {:?}", msg); + } else { + info!("None!"); + } + }) + } + + Commands::PopAll => { + entry_mutex!(runtime, |guard| { + while let Some(msg) = guard.pop() { + info!("Pop: {:?}", msg); + } + }) } } }
\ No newline at end of file diff --git a/core/src/data/controller/runtime/implements.rs b/core/src/data/controller/runtime/implements.rs index b79890e..840ea2c 100644 --- a/core/src/data/controller/runtime/implements.rs +++ b/core/src/data/controller/runtime/implements.rs @@ -4,6 +4,7 @@ use log::trace; use crate::data::controller::runtime::structs::ControllerRuntime; use crate::data::message::enums::{ControlMessage, GameMessage}; use crate::data::message::traits::MessageManager; +use crate::data::player::structs::Account; use crate::service::service_types::ServiceType; /// Message manager for controller-side runtime @@ -26,4 +27,33 @@ impl ControllerRuntime { trace!("[Controller Runtime] Closed."); } } + + pub fn message(&mut self, message: String) { + self.send_message(ControlMessage::Msg(message)); + } + + pub fn press_button(&mut self, key: u8) { + self.send_message(ControlMessage::Pressed(key)); + } + + pub fn release_button(&mut self, key: u8) { + self.send_message(ControlMessage::Released(key)); + } + + pub fn change_axis(&mut self, key: u8, ax_val: f64) { + self.send_message(ControlMessage::Axis(key, ax_val)); + } + + pub fn change_direction(&mut self, key: u8, x: f64, y: f64) { + self.send_message(ControlMessage::Dir(key, (x, y))); + } + + pub fn pop(&mut self) -> Option<GameMessage> { + self.receive(0, self.service_type.clone()) + } + + fn send_message (&mut self, msg: ControlMessage) { + let service = self.service_type.clone(); + self.send(msg, 0, service); + } }
\ No newline at end of file diff --git a/core/src/data/controller/runtime/structs.rs b/core/src/data/controller/runtime/structs.rs index 7be2cd6..bfd39c5 100644 --- a/core/src/data/controller/runtime/structs.rs +++ b/core/src/data/controller/runtime/structs.rs @@ -10,11 +10,12 @@ use crate::service::service_types::ServiceType; #[derive(Default)] pub struct ControllerRuntime { + pub(crate) service_type: ServiceType, pub(crate) received: HashMap<(ServiceType, u8), VecDeque<GameMessage>>, pub(crate) send: HashMap<(ServiceType, u8), VecDeque<ControlMessage>>, pub(crate) player: Player, pub game_info: GameInfo, - pub close: AtomicBool + pub close: AtomicBool, }
\ No newline at end of file |
