From b609951cc2b5942ffc4834480a05fa1e9a569258 Mon Sep 17 00:00:00 2001 From: Christoffer Lantz Date: Fri, 26 Jul 2019 15:43:39 +0200 Subject: [PATCH 1/4] Adds the ability to change time with /time with HH:MM format. e.g. 12:43 --- server/Cargo.toml | 1 + server/src/cmd.rs | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/server/Cargo.toml b/server/Cargo.toml index 98fab7922b..6b0319dc70 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -18,3 +18,4 @@ ron = "0.5.1" serde = "1.0" serde_derive = "1.0" rand = "0.5.6" +chrono = "0.4.7" diff --git a/server/src/cmd.rs b/server/src/cmd.rs index d648a2689c..ef51ab5ef7 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -9,6 +9,7 @@ use common::{ npc::{get_npc_name, NpcKind}, state::TimeOfDay, }; +use chrono::{NaiveTime, Timelike}; use rand::Rng; use specs::{Builder, Entity as EcsEntity, Join}; use vek::*; @@ -217,12 +218,14 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch Some("dusk") => 17.0 * 3600.0, Some(n) => match n.parse() { Ok(n) => n, - Err(_) => { - server.clients.notify( - entity, - ServerMsg::private(format!("'{}' is not a time!", n)), - ); - return; + Err(_) => match NaiveTime::parse_from_str(n, "%H:%M") { + Ok(time) => time.num_seconds_from_midnight() as f64, + Err(_) => { + server + .clients + .notify(entity, ServerMsg::private(format!("'{}' is not a valid time.", n))); + return; + } } }, None => { From eab81d5692913aa8e698a109981f146337eedc0d Mon Sep 17 00:00:00 2001 From: Christoffer Lantz Date: Fri, 26 Jul 2019 16:03:05 +0200 Subject: [PATCH 2/4] Now tells the client what the time was changed to --- server/src/cmd.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index ef51ab5ef7..e8dd28efa1 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -210,7 +210,7 @@ fn handle_kill(server: &mut Server, entity: EcsEntity, _args: String, _action: & fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { let time = scan_fmt!(&args, action.arg_fmt, String); - server.state.ecs_mut().write_resource::().0 = match time.as_ref().map(|s| s.as_str()) + let new_time = match time.as_ref().map(|s| s.as_str()) { Some("day") => 12.0 * 3600.0, Some("night") => 24.0 * 3600.0, @@ -236,6 +236,15 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch return; } }; + + server.state.ecs_mut().write_resource::().0 = new_time; + + let naive_time = NaiveTime::from_num_seconds_from_midnight(new_time as u32, 0); + + server.clients.notify( + entity, + ServerMsg::private(format!("Time changed to: {}", naive_time.format("%H:%M").to_string())) + ); } fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { From f6334c153252f3149582d3627e7b51803a802f61 Mon Sep 17 00:00:00 2001 From: Christoffer Lantz Date: Fri, 26 Jul 2019 16:16:35 +0200 Subject: [PATCH 3/4] match arms in handle_time now returns a NaiveTime object instead of f64 --- server/src/cmd.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index e8dd28efa1..d51d6b111a 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -212,14 +212,14 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch let time = scan_fmt!(&args, action.arg_fmt, String); let new_time = match time.as_ref().map(|s| s.as_str()) { - Some("day") => 12.0 * 3600.0, - Some("night") => 24.0 * 3600.0, - Some("dawn") => 5.0 * 3600.0, - Some("dusk") => 17.0 * 3600.0, + Some("night") => NaiveTime::from_hms(0, 0, 0), + Some("dawn") => NaiveTime::from_hms(5, 0, 0), + Some("day") => NaiveTime::from_hms(12, 0, 0), + Some("dusk") => NaiveTime::from_hms(17, 0, 0), Some(n) => match n.parse() { Ok(n) => n, Err(_) => match NaiveTime::parse_from_str(n, "%H:%M") { - Ok(time) => time.num_seconds_from_midnight() as f64, + Ok(time) => time, Err(_) => { server .clients @@ -237,13 +237,11 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch } }; - server.state.ecs_mut().write_resource::().0 = new_time; - - let naive_time = NaiveTime::from_num_seconds_from_midnight(new_time as u32, 0); + server.state.ecs_mut().write_resource::().0 = new_time.num_seconds_from_midnight() as f64; server.clients.notify( entity, - ServerMsg::private(format!("Time changed to: {}", naive_time.format("%H:%M").to_string())) + ServerMsg::private(format!("Time changed to: {}", new_time.format("%H:%M").to_string())) ); } From 6bf666bc0506043a484a6ea8d79651ffb6eb32dc Mon Sep 17 00:00:00 2001 From: Christoffer Lantz Date: Fri, 26 Jul 2019 16:38:31 +0200 Subject: [PATCH 4/4] Applied cargo fmt --- server/src/cmd.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index d51d6b111a..15eeb6f1a4 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -3,19 +3,20 @@ //! and provide a handler function. use crate::Server; +use chrono::{NaiveTime, Timelike}; use common::{ comp, msg::ServerMsg, npc::{get_npc_name, NpcKind}, state::TimeOfDay, }; -use chrono::{NaiveTime, Timelike}; use rand::Rng; use specs::{Builder, Entity as EcsEntity, Join}; use vek::*; use lazy_static::lazy_static; use scan_fmt::scan_fmt; + /// Struct representing a command that a user can run from server chat. pub struct ChatCommand { /// The keyword used to invoke the command, omitting the leading '/'. @@ -210,8 +211,7 @@ fn handle_kill(server: &mut Server, entity: EcsEntity, _args: String, _action: & fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { let time = scan_fmt!(&args, action.arg_fmt, String); - let new_time = match time.as_ref().map(|s| s.as_str()) - { + let new_time = match time.as_ref().map(|s| s.as_str()) { Some("night") => NaiveTime::from_hms(0, 0, 0), Some("dawn") => NaiveTime::from_hms(5, 0, 0), Some("day") => NaiveTime::from_hms(12, 0, 0), @@ -221,12 +221,13 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch Err(_) => match NaiveTime::parse_from_str(n, "%H:%M") { Ok(time) => time, Err(_) => { - server - .clients - .notify(entity, ServerMsg::private(format!("'{}' is not a valid time.", n))); + server.clients.notify( + entity, + ServerMsg::private(format!("'{}' is not a valid time.", n)), + ); return; } - } + }, }, None => { server.clients.notify( @@ -237,11 +238,15 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch } }; - server.state.ecs_mut().write_resource::().0 = new_time.num_seconds_from_midnight() as f64; + server.state.ecs_mut().write_resource::().0 = + new_time.num_seconds_from_midnight() as f64; server.clients.notify( entity, - ServerMsg::private(format!("Time changed to: {}", new_time.format("%H:%M").to_string())) + ServerMsg::private(format!( + "Time changed to: {}", + new_time.format("%H:%M").to_string() + )), ); } @@ -579,6 +584,7 @@ fn handle_light(server: &mut Server, entity: EcsEntity, args: String, action: &C .notify(entity, ServerMsg::chat(format!("You have no position!"))); } } + fn handle_lantern(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { if server .state @@ -615,6 +621,7 @@ fn handle_lantern(server: &mut Server, entity: EcsEntity, _args: String, _action ); } } + fn handle_tell(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { let opt_alias = scan_fmt!(&args, action.arg_fmt, String); match opt_alias {