Moved json parsing to be part of a module, with textmap equivalence
authorJustin Worthe <justin@worthe-it.co.za>
Sun, 3 Jun 2018 15:22:22 +0000 (17:22 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Sun, 3 Jun 2018 15:22:22 +0000 (17:22 +0200)
src/bin/perf-test.rs
src/input/json.rs [moved from src/json.rs with 100% similarity]
src/input/mod.rs [new file with mode: 0644]
src/input/textmap.rs [new file with mode: 0644]
src/lib.rs
src/main.rs
tests/live-comparison.rs

index 03da160..71044ad 100644 (file)
@@ -17,7 +17,7 @@ fn main() {
 fn normal_state() {
     println!("Normal size state file");
     let start_time = PreciseTime::now();
-    let (settings, state) = match json::read_state_from_file(STATE_PATH) {
+    let (settings, state) = match input::json::read_state_from_file(STATE_PATH) {
         Ok(ok) => ok,
         Err(error) => {
             println!("Error while parsing JSON file: {}", error);
@@ -31,7 +31,7 @@ fn normal_state() {
 fn big_state() {
     println!("Big state file");
     let start_time = PreciseTime::now();
-    let (settings, state) = match json::read_state_from_file(STATE_BIG_PATH) {
+    let (settings, state) = match input::json::read_state_from_file(STATE_BIG_PATH) {
         Ok(ok) => ok,
         Err(error) => {
             println!("Error while parsing JSON file: {}", error);
similarity index 100%
rename from src/json.rs
rename to src/input/json.rs
diff --git a/src/input/mod.rs b/src/input/mod.rs
new file mode 100644 (file)
index 0000000..47f359a
--- /dev/null
@@ -0,0 +1,2 @@
+pub mod json;
+pub mod textmap;
diff --git a/src/input/textmap.rs b/src/input/textmap.rs
new file mode 100644 (file)
index 0000000..dbee60a
--- /dev/null
@@ -0,0 +1,67 @@
+use std::fs::File;
+use std::io::prelude::*;
+use std::error::Error;
+
+use ::engine::*;
+use ::engine::settings::*;
+use ::engine::geometry::*;
+
+
+pub fn read_state_from_file(filename: &str) -> Result<(GameSettings, GameState), Box<Error>> {
+    let mut file = File::open(filename)?;
+    let mut content = String::new();
+    file.read_to_string(&mut content)?;
+
+    let engine_settings = GameSettings {
+        size: Point::new(8,4),
+        energy_income: 5,
+        energy: BuildingSettings {
+            price: 20,
+            health: 5,
+            construction_time: 2-2,
+            weapon_damage: 0,
+            weapon_speed: 0,
+            weapon_cooldown_period: 0,
+            energy_generated_per_turn: 3
+        },
+        defence: BuildingSettings {
+            price: 30,
+            health: 20,
+            construction_time: 4-2,
+            weapon_damage: 0,
+            weapon_speed: 0,
+            weapon_cooldown_period: 0,
+            energy_generated_per_turn: 0
+        },
+        attack: BuildingSettings {
+            price: 30,
+            health: 5,
+            construction_time: 2-2,
+            weapon_damage: 5,
+            weapon_speed: 2,
+            weapon_cooldown_period: 3,
+            energy_generated_per_turn: 0
+        }
+    };
+    let engine_state = GameState::new(
+        Player {
+            energy: 20,
+            health: 5,
+            energy_generated: 5
+        },
+        Player {
+            energy: 20,
+            health: 5,
+            energy_generated: 5
+        },
+        Vec::new(),
+        Vec::new(),
+        Vec::new(),
+        Vec::new(),
+        Vec::new(),
+        Vec::new(),
+        &engine_settings
+    );
+    
+    Ok((engine_settings, engine_state))
+}
index 6bcfc00..7b15502 100644 (file)
@@ -9,6 +9,6 @@ extern crate time;
 
 extern crate rayon;
 
-pub mod json;
+pub mod input;
 pub mod engine;
 pub mod strategy;
index 651df35..e84e207 100644 (file)
@@ -32,7 +32,7 @@ fn main() {
     let start_time = PreciseTime::now();
     
     println!("Reading in state.json file");
-    let (settings, state) = match json::read_state_from_file(STATE_PATH) {
+    let (settings, state) = match input::json::read_state_from_file(STATE_PATH) {
         Ok(ok) => ok,
         Err(error) => {
             println!("Error while parsing JSON file: {}", error);
index 72358a7..8d619db 100644 (file)
@@ -1,6 +1,6 @@
 extern crate zombot;
 
-use zombot::json;
+use zombot::input::json;
 use zombot::engine::command::{Command, BuildingType};
 use zombot::engine::geometry::Point;
 use zombot::engine::settings::GameSettings;