summaryrefslogtreecommitdiff
path: root/src/global_json.rs
blob: 7ac109ac00f1006a8892935f414253e2653c2a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::convert::TryInto;
use std::fs::File;
use std::io::prelude::*;
use std::rc::Rc;

use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json;
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::state::*;

pub fn read_initial_state_from_global_json_file(filename: &str) -> Result<GameState> {
    let mut state = read_state_from_global_json_file(filename)?;
    state.reset_players_to_start();
    Ok(state)
}

pub fn read_state_from_global_json_file(filename: &str) -> Result<GameState> {
    let mut file = File::open(filename)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    let json_state: JsonState = serde_json::from_str(content.as_ref())?;
    json_state.to_game_state()
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct JsonState {
    // pub current_round: usize,
    // pub max_rounds: usize,
    pub players: [JsonPlayer; 2],
    pub blocks: Vec<JsonBlock>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct JsonPlayer {
    // id: usize,
    position: JsonPosition,
    speed: u16,
    // state: JsonPlayerState,
    powerups: Vec<JsonPowerup>,
    // boosting: bool,
    boost_counter: u8,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct JsonBlock {
    position: JsonPosition,
    surface_object: JsonSurfaceObject,
    // occupied_by_player_id: usize,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct JsonPosition {
    lane: u8,
    block_number: u16,
}

// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
// #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
// pub enum JsonPlayerState {
//     Ready,
//     Nothing,
//     TurningLeft,
//     TurningRight,
//     Accelerating,
//     Decelarating,
//     PickedUpPowerup,
//     UsedBoost,
//     UsedOil,
//     HitMud,
//     HitOil,
//     Finishing,
// }

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum JsonPowerup {
    Boost,
    Oil,
}

#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[repr(u8)]
pub enum JsonSurfaceObject {
    Empty = 0,
    Mud = 1,
    OilSpill = 2,
    OilItem = 3,
    FinishLine = 4,
    Boost = 5,
}

impl JsonState {
    fn to_game_state(&self) -> Result<GameState> {
        Ok(GameState {
            status: GameStatus::Continue,
            players: [self.players[0].to_player()?, self.players[1].to_player()?],
            muds: Rc::new(
                self.blocks
                    .iter()
                    .filter(|cell| cell.surface_object == JsonSurfaceObject::Mud)
                    .map(|cell| cell.position.to_position())
                    .collect(),
            ),
            oil_spills: Rc::new(
                self.blocks
                    .iter()
                    .filter(|cell| cell.surface_object == JsonSurfaceObject::OilSpill)
                    .map(|cell| cell.position.to_position())
                    .collect(),
            ),
            powerup_oils: Rc::new(
                self.blocks
                    .iter()
                    .filter(|cell| cell.surface_object == JsonSurfaceObject::OilItem)
                    .map(|cell| cell.position.to_position())
                    .collect(),
            ),
            powerup_boosts: Rc::new(
                self.blocks
                    .iter()
                    .filter(|cell| cell.surface_object == JsonSurfaceObject::Boost)
                    .map(|cell| cell.position.to_position())
                    .collect(),
            ),
        })
    }
}

impl JsonPlayer {
    fn to_player(&self) -> Result<Player> {
        Ok(Player {
            position: self.position.to_position(),
            speed: self.speed,
            boost_remaining: self.boost_counter,
            oils: self
                .powerups
                .iter()
                .filter(|powerup| **powerup == JsonPowerup::Oil)
                .count()
                .try_into()?,
            boosts: self
                .powerups
                .iter()
                .filter(|powerup| **powerup == JsonPowerup::Boost)
                .count()
                .try_into()?,
        })
    }
}

impl JsonPosition {
    fn to_position(&self) -> Position {
        Position {
            x: self.block_number,
            y: self.lane,
        }
    }
}