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

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

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

#[test]
fn it_successfully_simulates_replay() {
    test_from_replay(&Path::new("tests/v300_normal_towers"));
}

#[test]
fn it_successfully_simulates_replay_with_iron_curtain() {
    test_from_replay(&Path::new("tests/v300_iron_curtain"));
}

#[test]
fn it_successfully_simulates_replay_with_iron_curtain_with_teslas() {
    test_from_replay(&Path::new("tests/v300_iron_curtain_with_teslas"));
}


fn test_from_replay(replay_folder: &Path) {
    let length = replay_folder.read_dir().unwrap().count()-1;
        
    let  mut state = json::read_bitwise_state_from_file(&format!("{}/Round 000/state.json", replay_folder.display())).unwrap();
    
    for i in 0..length {
        let player = read_player_command(&format!("{}/Round {:03}/PlayerCommand.txt", replay_folder.display(), i));
        let opponent = read_opponent_command(&format!("{}/Round {:03}/OpponentCommand.txt", replay_folder.display(), i));
        let mut expected_state = json::read_bitwise_state_from_file(&format!("{}/Round {:03}/state.json", replay_folder.display(), i+1)).unwrap();
        
        state.simulate(player, opponent);
        state.sort();
        expected_state.sort();

        println!("State {}: {:?}", i+1, state);
        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 == 5 {
            Command::IronCurtain
        } else {
            Command::Build(point, BuildingType::from_u8(action_type).unwrap())
        }
    }
}

fn read_opponent_command(filename: &str) -> Command {
    read_player_command(filename)
}