summaryrefslogtreecommitdiff
path: root/src/state.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2017-06-18 15:15:11 +0200
committerJustin Worthe <justin.worthe@gmail.com>2017-06-18 15:15:11 +0200
commit862897ea1d183810c2783eaeeaf40f648ef3dc2d (patch)
treefb168f1122fa2839b52443c54555593045262a2a /src/state.rs
parent29181fce4797b6e4833ab56d1fa7ff9fa865965b (diff)
Added knowledge of weapons
Next step: knowledge of weapon's effects.
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/state.rs b/src/state.rs
index 8c176e1..1756ad0 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -4,17 +4,23 @@ use ships::*;
pub struct State {
pub map_size: u16,
+ pub player_map: PlayerMap,
pub opponent_map: OpponentMap
}
impl State {
pub fn new(json: &json::JsonValue) -> Result<State, String> {
let map_size = State::map_size_from_json(&json)?;
+
+ let ref player_map_json = json["PlayerMap"];
+ let player_map = PlayerMap::new(&player_map_json)?;
+
let ref opponent_map_json = json["OpponentMap"];
let opponent_map = OpponentMap::new(&opponent_map_json, map_size)?;
Ok(State {
map_size: map_size,
+ player_map: player_map,
opponent_map: opponent_map
})
}
@@ -100,3 +106,41 @@ impl Cell {
}
}
}
+
+pub struct PlayerMap {
+ pub ships: HashMap<Ship, PlayerShip>,
+ pub energy: u16
+}
+
+impl PlayerMap {
+ fn new(json: &json::JsonValue) -> Result<PlayerMap, String> {
+ let mut ships = HashMap::new();
+ for json_ship in json["Owner"]["Ships"].members() {
+ let ship_type_string = json_ship["ShipType"]
+ .as_str()
+ .ok_or(String::from("Failed to read ShipType value of player map ship in json file"))?;
+ let ship_type = ship_type_string.parse::<Ship>()?;
+
+ let destroyed = json_ship["Destroyed"]
+ .as_bool()
+ .ok_or(String::from("Failed to read Destroyed value of player map ship in json file"))?;
+ ships.insert(ship_type, PlayerShip {
+ destroyed: destroyed
+ });
+ }
+
+ let energy = json["Owner"]["Energy"]
+ .as_u16()
+ .ok_or(String::from("Did not find the energy in the state json file"))?;
+
+ Ok(PlayerMap {
+ ships: ships,
+ energy: energy
+ })
+ }
+}
+
+
+pub struct PlayerShip {
+ pub destroyed: bool
+}