From 17086a2ef4bd6ef30b2d12075ca83748385b7ae6 Mon Sep 17 00:00:00 2001 From: Sofia Date: Wed, 15 Jul 2026 20:39:15 +0300 Subject: [PATCH] Add host and join commands --- rust/src/ui/cli.rs | 64 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/rust/src/ui/cli.rs b/rust/src/ui/cli.rs index 945325e..f9ff172 100644 --- a/rust/src/ui/cli.rs +++ b/rust/src/ui/cli.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::net::SocketAddr; use std::ops::DerefMut; use std::rc::Rc; @@ -13,14 +14,18 @@ use godot::{ pub enum CliColor { Command, - Regular, + Info, + Help, + Error, } impl CliColor { pub fn as_str(&self) -> &str { match self { CliColor::Command => "#a2a2a2", - CliColor::Regular => "#fff", + CliColor::Info => "#fff", + CliColor::Help => "#ffdc2e", + CliColor::Error => "#cc3535", } } } @@ -46,6 +51,8 @@ impl IPanel for CommandLinePanel { fn init(base: Base) -> Self { let mut commands: HashMap>> = HashMap::new(); commands.insert("help".to_owned(), Rc::new(Box::new(HelpCommand))); + commands.insert("host".to_string(), Rc::new(Box::new(HostCommand))); + commands.insert("join".to_string(), Rc::new(Box::new(JoinCommand))); CommandLinePanel { input_field: None, @@ -104,7 +111,7 @@ impl CommandLinePanel { if let Some(command) = self.commands.get(command) { command.clone().execute(self, ¶ms); } else { - self.publish_message(format!("Unknown command: {}", command), CliColor::Regular); + self.publish_message(format!("Unknown command: {}", command), CliColor::Info); } } } @@ -120,12 +127,9 @@ impl Command for HelpCommand { fn execute(&self, cli: &mut CommandLinePanel, params: &[&str]) { if let Some(command_name) = params.first() { if let Some(command) = cli.commands.get(*command_name) { - cli.publish_message(command.help().to_string(), CliColor::Regular); + cli.publish_message(command.help().to_string(), CliColor::Help); } else { - cli.publish_message( - format!("Unknown command: {}", command_name), - CliColor::Regular, - ); + cli.publish_message(format!("Unknown command: {}", command_name), CliColor::Help); } } else { let mut keys = cli.commands.keys().cloned().collect::>(); @@ -133,7 +137,7 @@ impl Command for HelpCommand { for key in keys { cli.publish_message( format!("\t- {}: {}", key, cli.commands.get(&key).unwrap().help()), - CliColor::Regular, + CliColor::Help, ); } } @@ -143,3 +147,45 @@ impl Command for HelpCommand { "Shows this message" } } +#[derive(Clone)] +struct HostCommand; +impl Command for HostCommand { + fn execute(&self, cli: &mut CommandLinePanel, params: &[&str]) { + if let Some(port_str) = params.first() { + if let Ok(port) = u32::from_str_radix(*port_str, 10) { + cli.publish_message(format!("Server hosted at {}", port), CliColor::Info); + // TODO host server + } else { + cli.publish_message(format!("Invalid port: {}", port_str), CliColor::Error); + } + } else { + cli.publish_message(format!("Missing port"), CliColor::Error); + cli.publish_message(format!("Usage: host [port]"), CliColor::Help); + } + } + + fn help(&self) -> &str { + "Hosts a server at a given port" + } +} +#[derive(Clone)] +struct JoinCommand; +impl Command for JoinCommand { + fn execute(&self, cli: &mut CommandLinePanel, params: &[&str]) { + if let Some(address_str) = params.first() { + if let Ok(address) = address_str.parse::() { + cli.publish_message(format!("Joining server at {}", address), CliColor::Info); + // TODO join server + } else { + cli.publish_message(format!("Invalid address: {}", address_str), CliColor::Error); + } + } else { + cli.publish_message(format!("Missing address"), CliColor::Error); + cli.publish_message(format!("Usage: join [address]:[port]"), CliColor::Help); + } + } + + fn help(&self) -> &str { + "Joins server at a given address:port" + } +}