Add host and join commands
This commit is contained in:
parent
c4f376b9c3
commit
17086a2ef4
@ -1,4 +1,5 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::net::SocketAddr;
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -13,14 +14,18 @@ use godot::{
|
|||||||
|
|
||||||
pub enum CliColor {
|
pub enum CliColor {
|
||||||
Command,
|
Command,
|
||||||
Regular,
|
Info,
|
||||||
|
Help,
|
||||||
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CliColor {
|
impl CliColor {
|
||||||
pub fn as_str(&self) -> &str {
|
pub fn as_str(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
CliColor::Command => "#a2a2a2",
|
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 {
|
fn init(base: Base<Panel>) -> Self {
|
||||||
let mut commands: HashMap<String, Rc<Box<dyn Command>>> = HashMap::new();
|
let mut commands: HashMap<String, Rc<Box<dyn Command>>> = HashMap::new();
|
||||||
commands.insert("help".to_owned(), Rc::new(Box::new(HelpCommand)));
|
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 {
|
CommandLinePanel {
|
||||||
input_field: None,
|
input_field: None,
|
||||||
@ -104,7 +111,7 @@ impl CommandLinePanel {
|
|||||||
if let Some(command) = self.commands.get(command) {
|
if let Some(command) = self.commands.get(command) {
|
||||||
command.clone().execute(self, ¶ms);
|
command.clone().execute(self, ¶ms);
|
||||||
} else {
|
} 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]) {
|
fn execute(&self, cli: &mut CommandLinePanel, params: &[&str]) {
|
||||||
if let Some(command_name) = params.first() {
|
if let Some(command_name) = params.first() {
|
||||||
if let Some(command) = cli.commands.get(*command_name) {
|
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 {
|
} else {
|
||||||
cli.publish_message(
|
cli.publish_message(format!("Unknown command: {}", command_name), CliColor::Help);
|
||||||
format!("Unknown command: {}", command_name),
|
|
||||||
CliColor::Regular,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut keys = cli.commands.keys().cloned().collect::<Vec<_>>();
|
let mut keys = cli.commands.keys().cloned().collect::<Vec<_>>();
|
||||||
@ -133,7 +137,7 @@ impl Command for HelpCommand {
|
|||||||
for key in keys {
|
for key in keys {
|
||||||
cli.publish_message(
|
cli.publish_message(
|
||||||
format!("\t- {}: {}", key, cli.commands.get(&key).unwrap().help()),
|
format!("\t- {}: {}", key, cli.commands.get(&key).unwrap().help()),
|
||||||
CliColor::Regular,
|
CliColor::Help,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -143,3 +147,45 @@ impl Command for HelpCommand {
|
|||||||
"Shows this message"
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user