Initial commit with sample bot and embedded game engine
[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 state_json;
18 mod engine;
19 use engine::command::Command;
20
21 fn choose_move(_state: &state_json::State) -> Option<Command> {
22     None
23 }
24
25
26 fn write_command(filename: &str, command: Option<Command>) -> Result<(), Box<Error> > {
27     let mut file = File::create(filename)?;
28     if let Some(command) = command {
29         write!(file, "{}", command)?;
30     }
31
32     Ok(())
33 }
34
35
36 fn main() {
37     let state = match state_json::read_state_from_file(STATE_PATH) {
38         Ok(state) => state,
39         Err(error) => {
40             eprintln!("Failed to read the {} file. {}", STATE_PATH, error);
41             process::exit(1);
42         }
43     };
44     let command = choose_move(&state);
45
46     match write_command(COMMAND_PATH, command) {
47         Ok(()) => {}
48         Err(error) => {
49             eprintln!("Failed to write the {} file. {}", COMMAND_PATH, error);
50             process::exit(1);
51         }
52     }
53 }