From 4bfa58f1316a18466d40488e46a6d3c8023f9643 Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 3 Feb 2020 20:38:51 +0100 Subject: don't crash on invalid actions --- src/controls.rs | 2 +- src/gameserver.rs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/controls.rs b/src/controls.rs index 101b401..1f96180 100644 --- a/src/controls.rs +++ b/src/controls.rs @@ -47,7 +47,7 @@ pub enum Control { impl Control { - pub fn from_json(val: Value) -> Option{ + pub fn from_json(val: &Value) -> Option{ if let Value::String(control_type) = &val[0] { match control_type.as_str() { "move" => match Direction::from_json(&val[1]) { diff --git a/src/gameserver.rs b/src/gameserver.rs index ce4a1e5..c7c5062 100644 --- a/src/gameserver.rs +++ b/src/gameserver.rs @@ -134,7 +134,12 @@ impl GameServer { Message::Input(inp) => { if let Some(nameref) = self.players.get(&id) { let name = nameref.clone(); - Some(Action::Input(name, Control::from_json(inp).unwrap())) + if let Some(control) = Control::from_json(&inp) { + Some(Action::Input(name, control)) + } else { + let _ = self.send_error(id, "invalidaction", &format!("unknown action: {}", inp)); + None + } } else { let _ = self.send_error(id, "invalidaction", &format!("Set a name before you send other messages")); None -- cgit