diff options
| author | troido <troido@protonmail.com> | 2020-04-01 13:27:42 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-01 13:27:42 +0200 |
| commit | 1ceb4c6f23287bca98f0c3946d5678dce5d0457c (patch) | |
| tree | c354643711d0a6066b149206a29d2182b281f50f /src/persistence.rs | |
| parent | 69ac6eb6153b016c39bbe55c85f15e3478032182 (diff) | |
better time handling for growing plants
Diffstat (limited to 'src/persistence.rs')
| -rw-r--r-- | src/persistence.rs | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/persistence.rs b/src/persistence.rs index 2903ec6..bbf9595 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -3,12 +3,13 @@ use std::path::{PathBuf, Path}; use std::fs; use std::env; use serde_json; -use serde_json::Value; +use serde_json::{Value, json}; use crate::{ PlayerId, RoomId, savestate::SaveState, playerstate::PlayerState, + Timestamp, Result, aerr }; @@ -16,13 +17,12 @@ use crate::{ pub trait PersistentStorage { fn load_room(&self, id: RoomId) -> Result<SaveState>; - fn load_player(&self, id: PlayerId) -> Result<PlayerState>; + fn load_world_meta(&self) -> Result<Timestamp>; fn save_room(&self, id: RoomId, state: SaveState) -> Result<()>; - fn save_player(&self, id: PlayerId, sate: PlayerState) -> Result<()>; - + fn save_world_meta(&self, time: Timestamp) -> Result<()>; } @@ -78,6 +78,20 @@ impl PersistentStorage for FileStorage { PlayerState::from_json(&json) } + fn load_world_meta(&self) -> Result<Timestamp> { + let mut path = self.directory.clone(); + path.push("world.save.json"); + let text = fs::read_to_string(path)?; + let json: Value = serde_json::from_str(&text)?; + Ok( + Timestamp( + json + .get("steps").ok_or(aerr!("world data does not have steps"))? + .as_i64().ok_or(aerr!("timestamp not an int"))? + ) + ) + } + fn save_room(&self, id: RoomId, state: SaveState) -> Result<()> { let mut path = self.directory.clone(); path.push("rooms"); @@ -85,7 +99,6 @@ impl PersistentStorage for FileStorage { let fname = id.to_string() + ".save.json"; path.push(fname); let text = state.to_json().to_string(); - // todo: write to a temp file first write_file_safe(path, text)?; Ok(()) } @@ -97,10 +110,17 @@ impl PersistentStorage for FileStorage { let fname = id.to_string() + ".save.json"; path.push(fname); let text = state.to_json().to_string(); - // todo: write to a temp file first write_file_safe(path, text)?; Ok(()) } + + fn save_world_meta(&self, time: Timestamp) -> Result<()> { + let mut path = self.directory.clone(); + fs::create_dir_all(&path)?; + path.push("world.save.json"); + write_file_safe(path, json!({"steps": time.0}).to_string())?; + Ok(()) + } } fn write_file_safe<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> { |
