diff options
Diffstat (limited to 'src/json.rs')
-rw-r--r-- | src/json.rs | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/json.rs b/src/json.rs index fa90e06..6cdc48f 100644 --- a/src/json.rs +++ b/src/json.rs @@ -1,3 +1,4 @@ +use std::convert::TryInto; use std::fs::File; use std::io::prelude::*; use std::rc::Rc; @@ -14,7 +15,7 @@ pub fn read_state_from_json_file(filename: &str) -> Result<GameState> { let mut content = String::new(); file.read_to_string(&mut content)?; let json_state: JsonState = serde_json::from_str(content.as_ref())?; - Ok(json_state.to_game_state()) + json_state.to_game_state() } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -32,11 +33,11 @@ pub struct JsonState { pub struct JsonPlayer { // id: usize, position: JsonPosition, - speed: usize, + speed: u16, // state: JsonPlayerState, powerups: Vec<JsonPowerup>, // boosting: bool, - boost_counter: usize, + boost_counter: u8, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -44,7 +45,7 @@ pub struct JsonPlayer { pub struct JsonOpponent { // id: usize, position: JsonPosition, - speed: usize, + speed: u16, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -58,8 +59,8 @@ pub struct JsonWorldMapCell { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct JsonPosition { - x: usize, - y: usize, + y: u8, + x: u16, } // #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -99,10 +100,10 @@ pub enum JsonSurfaceObject { } impl JsonState { - fn to_game_state(&self) -> GameState { - GameState { + fn to_game_state(&self) -> Result<GameState> { + Ok(GameState { status: GameStatus::Continue, - players: [self.player.to_player(), self.opponent.to_player()], + players: [self.player.to_player()?, self.opponent.to_player()], obstacles: Rc::new( self.world_map .iter() @@ -138,13 +139,13 @@ impl JsonState { .map(|cell| cell.position.to_position()) .collect(), ), - } + }) } } impl JsonPlayer { - fn to_player(&self) -> Player { - Player { + fn to_player(&self) -> Result<Player> { + Ok(Player { position: self.position.to_position(), next_position: self.position.to_position(), speed: self.speed, @@ -153,14 +154,16 @@ impl JsonPlayer { .powerups .iter() .filter(|powerup| **powerup == JsonPowerup::Oil) - .count(), + .count() + .try_into()?, boosts: self .powerups .iter() .filter(|powerup| **powerup == JsonPowerup::Boost) - .count(), + .count() + .try_into()?, finished: false, - } + }) } } |