From aec44c4fa6af7b236afcdf9d97f7e5bcdb9d2be9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= <kpiotr2005@gmail.com>
Date: Mon, 1 Jul 2019 20:07:30 +0000
Subject: [PATCH] Add /health command

---
 common/src/comp/stats.rs |  1 +
 server/src/cmd.rs        | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/common/src/comp/stats.rs b/common/src/comp/stats.rs
index 10813901ee..96230ca453 100644
--- a/common/src/comp/stats.rs
+++ b/common/src/comp/stats.rs
@@ -6,6 +6,7 @@ pub enum HealthSource {
     Attack { by: Uid }, // TODO: Implement weapon
     Suicide,
     Revive,
+    Command,
     Unknown,
 }
 
diff --git a/server/src/cmd.rs b/server/src/cmd.rs
index 9fe940055d..90fe706299 100644
--- a/server/src/cmd.rs
+++ b/server/src/cmd.rs
@@ -119,7 +119,13 @@ lazy_static! {
              handle_empty,
          ),
         ChatCommand::new(
-            "help", "", "/help: Display this message", handle_help)
+            "help", "", "/help: Display this message", handle_help),
+        ChatCommand::new(
+            "health",
+            "{}",
+            "/health : Set your current health",
+            handle_health,
+        )
     ];
 }
 
@@ -205,6 +211,31 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch
     };
 }
 
+fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
+    let opt_hp = scan_fmt!(&args, action.arg_fmt, u32);
+
+    match server
+        .state
+        .ecs_mut()
+        .write_storage::<comp::Stats>()
+        .get_mut(entity)
+    {
+        Some(stats) => match opt_hp {
+            Some(hp) => stats.health.set_to(hp, comp::HealthSource::Command),
+            None => {
+                server.clients.notify(
+                    entity,
+                    ServerMsg::Chat(String::from("You must specify health amount!")),
+                );
+            }
+        },
+        None => server.clients.notify(
+            entity,
+            ServerMsg::Chat(String::from("You have no position.")),
+        ),
+    }
+}
+
 fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
     let opt_alias = scan_fmt!(&args, action.arg_fmt, String);
     match opt_alias {