diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2017-05-14 21:49:48 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2017-05-14 21:49:48 +0200 |
commit | 10c8ceb168e86a58e38086691ddd519bac63ff03 (patch) | |
tree | a40b433e7cfad492a60b37c5a337758ffe7d1786 /src/files.rs | |
parent | 25a551316e27f4cc52c160d099db9cc3673b3421 (diff) |
Added model for knowledge of the game's state
Will be useful to track deductions that have already been made.
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/files.rs b/src/files.rs index 7f3daed..0810a4e 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,15 +1,21 @@ use json; +use serde_json; + use std::io::prelude::*; use std::fs::File; use std::path::PathBuf; use actions::*; +use knowledge::*; const STATE_FILE: &'static str = "state.json"; const COMMAND_FILE: &'static str = "command.txt"; const PLACE_FILE: &'static str = "place.txt"; +const KNOWLEDGE_FILE: &'static str = "knowledge-state.json"; + + pub fn read_file(working_dir: &PathBuf) -> Result<json::JsonValue, String> { let state_path = working_dir.join(STATE_FILE); let mut file = File::open(state_path.as_path()).map_err(|e| e.to_string())?; @@ -34,3 +40,18 @@ pub fn write_action(working_dir: &PathBuf, is_place_phase: bool, action: Action) Ok(()) } + +pub fn read_knowledge() -> Result<Knowledge, String> { + let mut file = File::open(KNOWLEDGE_FILE).map_err(|e| e.to_string())?; + let mut content = String::new(); + file.read_to_string(&mut content).map_err(|e| e.to_string())?; + serde_json::from_str(content.as_ref()).map_err(|e| e.to_string()) +} + +pub fn write_knowledge(knowledge: &Knowledge) -> Result<(), String> { + let json = serde_json::to_string(knowledge).map_err(|e| e.to_string())?; + let mut file = File::create(KNOWLEDGE_FILE).map_err(|e| e.to_string())?; + write!(file, "{}", json).map_err(|e| e.to_string())?; + + Ok(()) +} |