summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-03 20:38:51 +0100
committertroido <troido@protonmail.com>2020-02-03 20:38:51 +0100
commit4bfa58f1316a18466d40488e46a6d3c8023f9643 (patch)
treecb4166743caa51cd83ccbd093619c2a84914022f /src
parentdc6ca50ce53d1a9a9ad475e4d82fb3b1fcdfae58 (diff)
don't crash on invalid actions
Diffstat (limited to 'src')
-rw-r--r--src/controls.rs2
-rw-r--r--src/gameserver.rs7
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<Control>{
+ pub fn from_json(val: &Value) -> Option<Control>{
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