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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Provides the parser for Portal 2 Live Timer splits files.

use crate::{GameTime, Run, Segment, TimeSpan};
use std::io::{self, BufRead};
use std::num::ParseFloatError;
use std::result::Result as StdResult;

quick_error! {
    /// The Error types for splits files that couldn't be parsed by the Portal 2
    /// Live Timer Parser.
    #[derive(Debug)]
    pub enum Error {
        /// Expected another map, but didn't find it.
        ExpectedMap {}
        /// Expected the map's name, but didn't find it.
        ExpectedMapName {}
        /// Expected a different map.
        ExpectedDifferentMapName {}
        /// Expected the start ticks of the map, but didn't find it.
        ExpectedStartTicks {}
        /// Expected the end ticks of the map, but didn't find it.
        ExpectedEndTicks {}
        /// Couldn't parse the amount of ticks.
        Ticks(err: ParseFloatError) {
            from()
        }
        /// Failed to read from the source.
        Io(err: io::Error) {
            from()
        }
    }
}

/// The Result type for the Portal 2 Live Timer Parser.
pub type Result<T> = StdResult<T, Error>;

static CHAPTERS: [(&str, &[&str]); 9] = [
    (
        "Chapter 1 - The Courtesy Call",
        &[
            "sp_a1_intro1",
            "sp_a1_intro2",
            "sp_a1_intro3",
            "sp_a1_intro4",
            "sp_a1_intro5",
            "sp_a1_intro6",
            "sp_a1_intro7",
            "sp_a1_wakeup",
            "sp_a2_intro",
        ],
    ),
    (
        "Chapter 2 - The Cold Boot",
        &[
            "sp_a2_laser_intro",
            "sp_a2_laser_stairs",
            "sp_a2_dual_lasers",
            "sp_a2_laser_over_goo",
            "sp_a2_catapult_intro",
            "sp_a2_trust_fling",
            "sp_a2_pit_flings",
            "sp_a2_fizzler_intro",
        ],
    ),
    (
        "Chapter 3 - The Return",
        &[
            "sp_a2_sphere_peek",
            "sp_a2_ricochet",
            "sp_a2_bridge_intro",
            "sp_a2_bridge_the_gap",
            "sp_a2_turret_intro",
            "sp_a2_laser_relays",
            "sp_a2_turret_blocker",
            "sp_a2_laser_vs_turret",
            "sp_a2_pull_the_rug",
        ],
    ),
    (
        "Chapter 4 - The Surprise",
        &[
            "sp_a2_column_blocker",
            "sp_a2_laser_chaining",
            "sp_a2_triple_laser",
            "sp_a2_bts1",
            "sp_a2_bts2",
        ],
    ),
    (
        "Chapter 5 - The Escape",
        &[
            "sp_a2_bts3",
            "sp_a2_bts4",
            "sp_a2_bts5",
            "sp_a2_bts6",
            "sp_a2_core",
        ],
    ),
    (
        "Chapter 6 - The Fall",
        &[
            "sp_a3_00",
            "sp_a3_01",
            "sp_a3_03",
            "sp_a3_jump_intro",
            "sp_a3_bomb_flings",
            "sp_a3_crazy_box",
            "sp_a3_transition01",
        ],
    ),
    (
        "Chapter 7 - The Reunion",
        &[
            "sp_a3_speed_ramp",
            "sp_a3_speed_flings",
            "sp_a3_portal_intro",
            "sp_a3_end",
        ],
    ),
    (
        "Chapter 8 - The Itch",
        &[
            "sp_a4_intro",
            "sp_a4_tb_intro",
            "sp_a4_tb_trust_drop",
            "sp_a4_tb_wall_button",
            "sp_a4_tb_polarity",
            "sp_a4_tb_catch",
            "sp_a4_stop_the_box",
            "sp_a4_laser_catapult",
            "sp_a4_laser_platform",
            "sp_a4_speed_tb_catch",
            "sp_a4_jump_polarity",
        ],
    ),
    (
        "Chapter 9 - The Part Where...",
        &[
            "sp_a4_finale1",
            "sp_a4_finale2",
            "sp_a4_finale3",
            "sp_a4_finale4",
        ],
    ),
];

/// Attempts to parse a Portal 2 Live Timer splits file.
pub fn parse<R: BufRead>(source: R) -> Result<Run> {
    let mut run = Run::new();

    run.set_game_name("Portal 2");
    run.set_category_name("Any%");

    let mut lines = source.lines();
    lines.next(); // Skip the header

    let mut aggregate_ticks = 0.0;

    let mut line = lines.next().ok_or(Error::ExpectedMap)??;
    for &(chapter_name, maps) in &CHAPTERS {
        for &map in maps {
            {
                let mut splits = line.split(',');
                let map_name = splits.next().ok_or(Error::ExpectedMapName)?;
                if map_name != map {
                    return Err(Error::ExpectedDifferentMapName);
                }
                let start_ticks: f64 = splits.next().ok_or(Error::ExpectedStartTicks)?.parse()?;
                let end_ticks: f64 = splits.next().ok_or(Error::ExpectedEndTicks)?.parse()?;
                let map_ticks = end_ticks - start_ticks;
                aggregate_ticks += map_ticks;
            }

            while let Some(Ok(next_line)) = lines.next() {
                line = next_line;
                let mut splits = line.split(',');
                if splits.next() == Some(map) {
                    let start_ticks: f64 =
                        splits.next().ok_or(Error::ExpectedStartTicks)?.parse()?;
                    let end_ticks: f64 = splits.next().ok_or(Error::ExpectedEndTicks)?.parse()?;
                    let map_ticks = end_ticks - start_ticks;
                    aggregate_ticks += map_ticks;
                } else {
                    break;
                }
            }
        }

        let time = GameTime(Some(TimeSpan::from_seconds(aggregate_ticks / 60.0))).into();
        let mut segment = Segment::new(chapter_name);
        segment.set_personal_best_split_time(time);

        run.push_segment(segment);
    }

    Ok(run)
}