Add host and join commands

This commit is contained in:
Sofia 2026-07-15 20:39:15 +03:00
parent c4f376b9c3
commit 17086a2ef4

View File

@ -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<Panel>) -> Self {
let mut commands: HashMap<String, Rc<Box<dyn Command>>> = 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, &params);
} 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::<Vec<_>>();
@ -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::<SocketAddr>() {
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"
}
}