Break: refactor the parsers

This commit is contained in:
Pavel Perestoronin committed 2022-12-03 18:21:32 +01:00
1 parent 68e00c5eca
commit ac086b1eb2
4 files changed
+24 -7

No files matched your search

+6
View File
@@ -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<Player>,
}
/// 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<u32>,
+14 -2
View File
@@ -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<Self> {
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> {
BattleResults::decode(self.1.as_ref()).map_err(Error::DecodeFailed)
}
}
+1 -3
View File
@@ -1,6 +1,5 @@
use std::io::{Read, Seek};
use prost::Message;
use zip::ZipArchive;
use crate::error::Error;
@@ -24,7 +23,6 @@ impl<R: Read + Seek> Replay<R> {
.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()
}
}
+3 -2
View File
@@ -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(())