summaryrefslogtreecommitdiff
path: root/tests/live-comparison.rs
blob: e063959debce192abde9559cf980d36183972f6b (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
extern crate zombot;

use zombot::input::json;
use zombot::engine::command::{Command, BuildingType};
use zombot::engine::geometry::Point;
use zombot::engine::settings::GameSettings;

use std::fs::File;
use std::io::prelude::*;

#[test]
fn it_successfully_simulates_replay() {
    test_from_replay("tests/after_200", 62);
}

fn test_from_replay(replay_folder: &str, length: usize) {
    let (settings, mut state) = json::read_state_from_file(&format!("{}/Round 000/state.json", replay_folder)).unwrap();
    
    for i in 0..length {
        let player = read_player_command(&format!("{}/Round {:03}/PlayerCommand.txt", replay_folder, i));
        let opponent = read_opponent_command(&format!("{}/Round {:03}/OpponentCommand.txt", replay_folder, i), &settings);
        let (_, mut expected_state) = json::read_state_from_file(&format!("{}/Round {:03}/state.json", replay_folder, i+1)).unwrap();
        
        state.simulate_mut(&settings, player, opponent);
        state.sort();
        expected_state.sort();
        assert_eq!(state, expected_state, "\nFailed on state {}\n", i+1);
    }
}

fn read_player_command(filename: &str) -> Command {
    let mut file = File::open(filename).unwrap();
    let mut content = String::new();
    file.read_to_string(&mut content).unwrap();
    if content.trim() == "No Command" {
        Command::Nothing
    }
    else {
        let mut components = content.split(',');
        let point = Point::new(components.next().unwrap().trim().parse().unwrap(),
                               components.next().unwrap().trim().parse().unwrap());
        let action_type = components.next().unwrap().trim().parse().unwrap();
        if action_type == 3 {
            Command::Deconstruct(point)
        } else {
            Command::Build(point, BuildingType::from_u8(action_type).unwrap())
        }
    }
}

fn read_opponent_command(filename: &str, settings: &GameSettings) -> Command {
    match read_player_command(filename) {
        Command::Nothing => Command::Nothing,
        Command::Build(p, b) => Command::Build(Point::new(
            settings.size.x - p.x - 1,
            p.y
        ), b),
        Command::Deconstruct(p) => Command::Deconstruct(Point::new(
            settings.size.x - p.x - 1,
            p.y
        )),
    }
    
}