Added converting from JSON code to game engine representation
[entelect-challenge-tower-defence.git] / src / main.rs
1 extern crate serde;
2 extern crate serde_json;
3
4 #[macro_use]
5 extern crate serde_derive;
6
7 use std::error::Error;
8
9 const STATE_PATH: &str = "state.json";
10
11 const COMMAND_PATH: &str = "command.txt";
12
13 use std::fs::File;
14 use std::io::prelude::*;
15 use std::process;
16
17 mod json;
18 mod engine;
19 use engine::command::Command;
20
21 fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState) -> Command {
22     state.simulate(&settings, Command::Nothing, Command::Nothing);
23     Command::Nothing
24 }
25
26
27 fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
28     let mut file = File::create(filename)?;
29     write!(file, "{}", command)?;
30
31     Ok(())
32 }
33
34
35 fn main() {
36     let (settings, state) = match json::read_state_from_file(STATE_PATH) {
37         Ok(ok) => ok,
38         Err(error) => {
39             eprintln!("Failed to read the {} file. {}", STATE_PATH, error);
40             process::exit(1);
41         }
42     };
43     let command = choose_move(&settings, &state);
44
45     match write_command(COMMAND_PATH, command) {
46         Ok(()) => {}
47         Err(error) => {
48             eprintln!("Failed to write the {} file. {}", COMMAND_PATH, error);
49             process::exit(1);
50         }
51     }
52 }