diff options
| author | 1992414357@qq.com <1992414357@qq.com> | 2025-06-09 03:26:09 +0800 |
|---|---|---|
| committer | 1992414357@qq.com <1992414357@qq.com> | 2025-06-09 03:26:09 +0800 |
| commit | 8359c5391f1f87d1a29df94ae90d431d432bc344 (patch) | |
| tree | 129101538b9cc45176e9a345583db22609134fe5 /core/src/data/game/cli | |
| parent | 8db4f6af3421392fb75d4a2257d39a10bcefa493 (diff) | |
完成了控制台部分
Diffstat (limited to 'core/src/data/game/cli')
| -rw-r--r-- | core/src/data/game/cli/cli_command.rs | 169 |
1 files changed, 167 insertions, 2 deletions
diff --git a/core/src/data/game/cli/cli_command.rs b/core/src/data/game/cli/cli_command.rs index 1d79515..9b74831 100644 --- a/core/src/data/game/cli/cli_command.rs +++ b/core/src/data/game/cli/cli_command.rs @@ -1,7 +1,12 @@ +use std::process::exit; use std::sync::{Arc, Mutex}; -use clap::{Parser, Subcommand}; +use clap::{Args, Parser, Subcommand}; +use clearscreen::clear; +use log::{info, warn}; use nogamepads::entry_mutex; use crate::data::game::runtime::structs::GameRuntime; +use crate::data::message::traits::MessageManager; +use crate::data::player::structs::Player; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -16,14 +21,69 @@ enum Commands { #[command(about = "Clean the screen")] Clear, + LockGame, + + UnlockGame, + #[command(about = "Close the game")] - Close + Close, + + #[command(about = "Exit the console")] + Exit, + + OnlineList, + + BannedList, + + Ban(PlayerIndex), + + Pardon(PlayerIndex), + + Kick(PlayerIndex), + + Event(SendEventArgs), + + Message(SendMessageArgs), + + Pop, + + PopAll, +} + +#[derive(Args, Debug)] +struct PlayerIndex { + index: usize, +} + +#[derive(Args, Debug)] +struct SendEventArgs { + index: usize, + event: u8 +} + +#[derive(Args, Debug)] +struct SendMessageArgs { + index: usize, + msg: String } pub fn process_game_cli(runtime: Arc<Mutex<GameRuntime>>, cmd: GameCli) { match cmd.command { Commands::Clear => { + clear().expect("Failed to clear screen"); + + } + + Commands::LockGame => { + entry_mutex!(runtime, |guard| { + guard.lock_game(); + }) + } + Commands::UnlockGame => { + entry_mutex!(runtime, |guard| { + guard.unlock_game(); + }) } Commands::Close => { @@ -31,5 +91,110 @@ pub fn process_game_cli(runtime: Arc<Mutex<GameRuntime>>, cmd: GameCli) { guard.close_game(); }) } + + Commands::Exit => { + exit(1); + } + + Commands::OnlineList => { + entry_mutex!(runtime, |guard| { + let mut i = 0; + for account in guard.data.online_accounts() { + info!("{}.{}", i, account.id); + i += 1; + } + }) + } + + Commands::BannedList => { + entry_mutex!(runtime, |guard| { + let mut i = 0; + for account in guard.data.banned_accounts() { + info!("{}.{}", i, account.id); + i += 1; + } + }) + } + + Commands::Ban(args) => { + entry_mutex!(runtime, |guard| { + if let Some(account) = guard.data.online_accounts().get(args.index) { + if let Some(service_type) = guard.data.get_service_type(account) { + guard.ban_player(&Player::from(account.clone()), service_type); + info!("Account {} banned.", account.id); + } + } else { + warn!("Account number {} not found", args.index); + } + }) + } + + Commands::Pardon(args) => { + entry_mutex!(runtime, |guard| { + if let Some(account) = guard.data.banned_accounts().get(args.index) { + guard.pardon_player(&Player::from(account.clone())); + info!("Account {} pardoned.", account.id); + } else { + warn!("Account number {} not found", args.index); + } + }) + } + + Commands::Kick(args) => { + entry_mutex!(runtime, |guard| { + if let Some(account) = guard.data.online_accounts().get(args.index) { + if let Some(service_type) = guard.data.get_service_type(account) { + guard.kick_player(&Player::from(account.clone()), service_type); + info!("Account {} kicked.", account.id); + } + } else { + warn!("Account number {} not found", args.index); + } + }) + } + + Commands::Event(args) => { + entry_mutex!(runtime, |guard| { + if let Some(account) = guard.data.online_accounts().get(args.index) { + if let Some(service_type) = guard.data.get_service_type(account) { + guard.send_event(account, args.event, service_type); + info!("Sent event {} to {}.", args.event, account.id); + } + } else { + warn!("Account number {} not found", args.index); + } + }) + } + + Commands::Message(args) => { + entry_mutex!(runtime, |guard| { + if let Some(account) = guard.data.online_accounts().get(args.index) { + if let Some(service_type) = guard.data.get_service_type(account) { + guard.send_message(account, args.msg.clone(), service_type); + info!("Sent message \"{}\" to {}.", args.msg, account.id); + } + } else { + warn!("Account number {} not found", args.index); + } + }) + } + + Commands::Pop => { + entry_mutex!(runtime, |guard| { + if let Some((account, message)) = guard.pop_event() { + info!("{}: {:?}", account.id, message); + } else { + info!("None") + } + }) + } + + Commands::PopAll => { + entry_mutex!(runtime, |guard| { + while let Some((account, message)) = guard.pop_event() { + info!("{}: {:?}", account.id, message); + } + }) + } } }
\ No newline at end of file |
