diff --git a/src/models/battle_results.rs b/src/models/battle_results.rs index d9c3dd9..de533dd 100644 --- a/src/models/battle_results.rs +++ b/src/models/battle_results.rs @@ -13,6 +13,7 @@ impl BattleResults { } } +/// Parsed battle results. #[derive(Message)] pub struct BattleResults { /// Battle timestamp. @@ -24,6 +25,7 @@ pub struct BattleResults { pub players: Vec, } +/// Battle player. #[derive(Message)] pub struct Player { /// Player's account ID. @@ -35,12 +37,16 @@ pub struct Player { pub info: PlayerInfo, } +/// Player's extended information. #[derive(Message)] pub struct PlayerInfo { /// Player's nickname. #[prost(string, tag = "1")] pub nickname: String, + /// Some sort of platoon ID: + /// - contains same ID for a platoon members, or + /// - `None` for non-platoon players #[prost(uint32, optional, tag = "2")] pub platoon_id: Option, diff --git a/src/models/battle_results_dat.rs b/src/models/battle_results_dat.rs index 42018c6..ed3a66b 100644 --- a/src/models/battle_results_dat.rs +++ b/src/models/battle_results_dat.rs @@ -1,17 +1,29 @@ use std::io::Read; +use prost::Message; use serde::Deserialize; use serde_bytes::ByteBuf; +use crate::models::BattleResults; use crate::{Error, Result}; /// Represents un-pickled `battle_results.dat`. #[derive(Debug, Deserialize)] -pub struct BattleResultsDat(pub(crate) u64, pub(crate) ByteBuf); +pub struct BattleResultsDat( + /// No idea what this is. + u64, + /// Protobuf-serialized battle results. + ByteBuf, +); impl BattleResultsDat { - /// Try and parse the pickled structure from the reader which contains `battle_results.dat`. + /// Parses the pickled structure from the reader which contains `battle_results.dat`. pub fn from_reader(reader: impl Read) -> Result { serde_pickle::from_reader(reader, Default::default()).map_err(Error::UnpickleFailed) } + + /// Decodes the battle results from the internal buffer. + pub fn decode_battle_results(&self) -> Result { + BattleResults::decode(self.1.as_ref()).map_err(Error::DecodeFailed) + } } diff --git a/src/replay.rs b/src/replay.rs index cec713f..942a399 100644 --- a/src/replay.rs +++ b/src/replay.rs @@ -1,6 +1,5 @@ use std::io::{Read, Seek}; -use prost::Message; use zip::ZipArchive; use crate::error::Error; @@ -24,7 +23,6 @@ impl Replay { .0 .by_name("battle_results.dat") .map_err(Error::OpenBattleResultsFailed)?; - let battle_results_dat = BattleResultsDat::from_reader(pickled_battle_results)?; - BattleResults::decode(battle_results_dat.1.as_ref()).map_err(Error::DecodeFailed) + BattleResultsDat::from_reader(pickled_battle_results)?.decode_battle_results() } } diff --git a/tests/battle_results.rs b/tests/battle_results.rs index d2c43a0..c45d85d 100644 --- a/tests/battle_results.rs +++ b/tests/battle_results.rs @@ -1,6 +1,7 @@ use std::fs::File; use anyhow::Result; +use wotbreplay_parser::models::TeamNumber; use wotbreplay_parser::Replay; #[test] @@ -13,11 +14,11 @@ fn battle_results_ok() -> Result<()> { assert_eq!(battle_results.players[0].account_id, 520886428); assert_eq!(battle_results.players[0].info.nickname, "77mmmr"); - assert_eq!(battle_results.players[0].info.team_number, 2); + assert_eq!(battle_results.players[0].info.team_number(), TeamNumber::Two); assert_eq!(battle_results.players[0].info.platoon_id, None); assert_eq!(battle_results.players[1].info.nickname, "SNAK_THE_RIPPER"); - assert_eq!(battle_results.players[1].info.team_number, 1); + assert_eq!(battle_results.players[1].info.team_number(), TeamNumber::One); assert_eq!(battle_results.players[1].info.platoon_id, Some(547466834)); Ok(())