From edc4c120fa58067ed8802d08e3346461a43b0c97 Mon Sep 17 00:00:00 2001 From: Markus Moenig Date: Thu, 16 Mar 2023 19:56:46 +0700 Subject: [PATCH] Started with a basic server time class --- core_render/src/render.rs | 2 + core_server/src/server/region_instance.rs | 10 ++++- core_shared/src/date.rs | 50 ++++++++++++++++++++--- core_shared/src/lib.rs | 1 + core_shared/src/update.rs | 7 +++- creator_lib/src/editor/regionwidget.rs | 19 +++------ 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/core_render/src/render.rs b/core_render/src/render.rs index 70404c55..ed28c918 100644 --- a/core_render/src/render.rs +++ b/core_render/src/render.rs @@ -143,6 +143,7 @@ impl GameRender<'_> { script_register_gear_api(&mut engine); script_register_weapons_api(&mut engine); script_register_experience_api(&mut engine); + script_register_date_api(&mut engine); let this_map = Map::new(); @@ -426,6 +427,7 @@ impl GameRender<'_> { map.insert("gear".into(), Dynamic::from(update.gear.clone())); map.insert("skills".into(), Dynamic::from(update.skills.clone())); map.insert("experience".into(), Dynamic::from(update.experience.clone())); + map.insert("date".into(), Dynamic::from(update.date.clone())); } None diff --git a/core_server/src/server/region_instance.rs b/core_server/src/server/region_instance.rs index 5c7eb84d..919a6ffc 100644 --- a/core_server/src/server/region_instance.rs +++ b/core_server/src/server/region_instance.rs @@ -120,6 +120,9 @@ pub struct RegionInstance<'a> { pub primary_currency : String, pub hitpoints : String, pub max_hitpoints : String, + + // Date & Time + pub date : Date, } impl RegionInstance<'_> { @@ -284,7 +287,10 @@ impl RegionInstance<'_> { // Variable names primary_currency : "".to_string(), hitpoints : "".to_string(), - max_hitpoints : "".to_string() + max_hitpoints : "".to_string(), + + // Date + date : Date::new() } } @@ -1044,6 +1050,7 @@ impl RegionInstance<'_> { experience : experience.clone(), multi_choice_data : self.instances[inst_index].multi_choice_data.clone(), communication : self.instances[inst_index].communication.clone(), + date : self.date.clone(), }; self.instances[inst_index].messages = vec![]; @@ -1061,6 +1068,7 @@ impl RegionInstance<'_> { //println!("tick time {}", self.get_time() - tick_time); self.tick_count = self.tick_count.wrapping_add(1); + self.date.from_ticks(self.tick_count); messages } diff --git a/core_shared/src/date.rs b/core_shared/src/date.rs index e3e3cc72..e73aef73 100644 --- a/core_shared/src/date.rs +++ b/core_shared/src/date.rs @@ -3,25 +3,65 @@ use crate::prelude::*; /// Holds the current date and time #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] pub struct Date { - pub hours : isize, - pub minutes : isize, + pub total_minutes : usize, + + pub hours : i32, + pub minutes : i32, } impl Date { pub fn new() -> Self { Self { + total_minutes : 0, + hours : 0, minutes : 0, } } - pub fn time_as_24(&self) -> String { - format!("{}:{}", self.hours, self.minutes) + pub fn from_ticks(&mut self, ticks: usize) { + let minutes = ticks / 4; + self.hours = (minutes / 60) as i32; + self.minutes = (minutes % 60) as i32; + + self.total_minutes = minutes; + } + + pub fn get_hours(&mut self) -> i32 { + self.hours + } + + pub fn get_minutes(&mut self) -> i32 { + self.minutes + } + + pub fn time24(&mut self) -> String { + format!("{:0>2}:{:0>2}", self.hours, self.minutes) + } +} + +use std::cmp::*; + +impl PartialOrd for Date { + fn partial_cmp(&self, other: &Date) -> Option { + if self.total_minutes == other.total_minutes { + return Some(Ordering::Equal); + } + if self.total_minutes < other.total_minutes { + return Some(Ordering::Less); + } + if self.total_minutes > other.total_minutes { + return Some(Ordering::Greater); + } + None } } pub fn script_register_date_api(engine: &mut rhai::Engine) { engine.register_type_with_name::("Date") - .register_fn("time_as_24", Date::time_as_24); + .register_get("hours", Date::get_hours) + .register_get("minutes", Date::get_minutes) + + .register_fn("time24", Date::time24); } \ No newline at end of file diff --git a/core_shared/src/lib.rs b/core_shared/src/lib.rs index 92a18f58..a0864d80 100644 --- a/core_shared/src/lib.rs +++ b/core_shared/src/lib.rs @@ -44,6 +44,7 @@ pub mod prelude { pub use crate::dir::get_resource_dir; pub use crate::experience::*; pub use crate::server::*; + pub use crate::date::*; pub use crate::value::Value; pub use rustc_hash::FxHashMap; diff --git a/core_shared/src/update.rs b/core_shared/src/update.rs index bb75ed14..c1b98d88 100644 --- a/core_shared/src/update.rs +++ b/core_shared/src/update.rs @@ -69,7 +69,10 @@ pub struct GameUpdate { pub multi_choice_data : Vec, /// Ongoing communications - pub communication : Vec + pub communication : Vec, + + /// Date + pub date : Date } impl GameUpdate { @@ -102,7 +105,7 @@ impl GameUpdate { experience : Experience::new(), multi_choice_data : vec![], communication : vec![], - + date : Date::new(), } } } \ No newline at end of file diff --git a/creator_lib/src/editor/regionwidget.rs b/creator_lib/src/editor/regionwidget.rs index 5b27ec9b..1c57d2e6 100644 --- a/creator_lib/src/editor/regionwidget.rs +++ b/creator_lib/src/editor/regionwidget.rs @@ -436,20 +436,11 @@ impl EditorContent for RegionWidget { if let Some(instances) = &behavior.data.loot { for instance in instances { if instance.position.region != region.data.id { continue; } - let mut loot = LootData { - id : id.clone(), - item_type : "gear".to_string(), - name : Some(behavior.data.name.clone()), - tile : None, - state : None, - light : None, - slot : None, - amount : instance.amount, - stackable : 1, - static_item : false, - price : 0.0, - weight : 0.0, - }; + + let mut loot = Item::new(*id, behavior.data.name.clone()); + loot.item_type = "gear".into(); + loot.amount = instance.amount; + loot.stackable = 1; for (_index, node) in &behavior.data.nodes { if node.behavior_type == BehaviorNodeType::BehaviorType {