diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/command.rs | 27 | ||||
-rw-r--r-- | src/json.rs | 96 | ||||
-rw-r--r-- | src/main.rs | 27 |
3 files changed, 150 insertions, 0 deletions
diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..f95ef98 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,27 @@ +use std::fmt; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Command { + Nothing, + Accelerate, + Decelerate, + TurnLeft, + TurnRight, + UseBoost, + UseOil, +} + +impl fmt::Display for Command { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Command::*; + match self { + Nothing => write!(f, "NOTHING"), + Accelerate => write!(f, "ACCELERATE"), + Decelerate => write!(f, "DECELERATE"), + TurnLeft => write!(f, "TURN_LEFT"), + TurnRight => write!(f, "TURN_RIGHT"), + UseBoost => write!(f, "USE_BOOST"), + UseOil => write!(f, "USE_OIL"), + } + } +} diff --git a/src/json.rs b/src/json.rs new file mode 100644 index 0000000..55173bd --- /dev/null +++ b/src/json.rs @@ -0,0 +1,96 @@ +use std::fs::File; +use std::io::prelude::*; + +use anyhow::Result; +use serde::{Deserialize, Serialize}; +use serde_json; +use serde_repr::{Deserialize_repr, Serialize_repr}; + +pub fn read_state_from_json_file(filename: &str) -> Result<State> { + let mut file = File::open(filename)?; + let mut content = String::new(); + file.read_to_string(&mut content)?; + let state: State = serde_json::from_str(content.as_ref())?; + + Ok(state) +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct State { + pub current_round: u32, + pub max_rounds: u32, + pub player: Player, + pub opponent: Opponent, + pub world_map: Vec<Vec<WorldMapCell>>, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct Player { + id: u32, + position: Position, + speed: u32, + state: PlayerState, + powerups: Vec<Powerup>, + boosting: bool, + boost_counter: u32, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct Opponent { + id: u32, + position: Position, + speed: u32, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct WorldMapCell { + position: Position, + surface_object: SurfaceObject, + occupied_by_player_id: u32, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub struct Position { + x: u32, + y: u32, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum PlayerState { + Ready, + Nothing, + TurningLeft, + TurningRight, + Accelerating, + Decelarating, + PickedUpPowerup, + UsedBoost, + UsedOil, + HitMud, + Finishing, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum Powerup { + Boost, + Oil, +} + +#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +#[repr(u8)] +pub enum SurfaceObject { + Empty = 0, + Mud = 1, + OilSpill = 2, + OilItem = 3, + FinishLine = 4, + Boost = 5, +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5d29529 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,27 @@ +use std::io::prelude::*; +use std::io::stdin; + +mod command; +mod json; + +use command::*; +use json::*; + +fn main() { + for line in stdin().lock().lines() { + let round_number = line.expect("Failed to read line from stdin: {}"); + let command = + match read_state_from_json_file(&format!("./rounds/{}/state.json", round_number)) { + Ok(state) => choose_command(state), + Err(e) => { + eprintln!("WARN: State file could not be parsed: {}", e); + Command::Nothing + } + }; + println!("C;{};{}", round_number, command); + } +} + +fn choose_command(state: State) -> Command { + Command::Accelerate +} |