Skip to content

Commit

Permalink
Started with a basic server time class
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Mar 16, 2023
1 parent 75027d3 commit edc4c12
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
2 changes: 2 additions & 0 deletions core_render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion core_server/src/server/region_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_> {
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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![];
Expand All @@ -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
}
Expand Down
50 changes: 45 additions & 5 deletions core_shared/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ordering> {
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>("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);
}
1 change: 1 addition & 0 deletions core_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions core_shared/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub struct GameUpdate {
pub multi_choice_data : Vec<MultiChoiceData>,

/// Ongoing communications
pub communication : Vec<PlayerCommunication>
pub communication : Vec<PlayerCommunication>,

/// Date
pub date : Date
}

impl GameUpdate {
Expand Down Expand Up @@ -102,7 +105,7 @@ impl GameUpdate {
experience : Experience::new(),
multi_choice_data : vec![],
communication : vec![],

date : Date::new(),
}
}
}
19 changes: 5 additions & 14 deletions creator_lib/src/editor/regionwidget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit edc4c12

Please sign in to comment.