diff options
Diffstat (limited to 'src/global_json.rs')
-rw-r--r-- | src/global_json.rs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/global_json.rs b/src/global_json.rs index 189a97b..101a085 100644 --- a/src/global_json.rs +++ b/src/global_json.rs @@ -1,3 +1,4 @@ +use std::convert::TryInto; use std::fs::File; use std::io::prelude::*; use std::rc::Rc; @@ -20,7 +21,7 @@ pub fn read_state_from_global_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)] @@ -37,11 +38,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)] @@ -55,8 +56,8 @@ pub struct JsonBlock { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct JsonPosition { - block_number: usize, - lane: usize, + lane: u8, + block_number: u16, } // #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -96,10 +97,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.players[0].to_player(), self.players[1].to_player()], + players: [self.players[0].to_player()?, self.players[1].to_player()?], obstacles: Rc::new( self.blocks .iter() @@ -131,13 +132,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, @@ -146,14 +147,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, - } + }) } } |