mirror of
https://github.com/eigenein/wotbreplay-parser.git
synced 2026-10-29 21:29:47 +01:00
Break: refactor the models
This commit is contained in:
1 parent
583ca024fc
commit
21206737c0
8 files changed
+83
-37
No files matched your search
@@ -15,6 +15,7 @@ default = []
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
prost = "0.11.3"
|
prost = "0.11.3"
|
||||||
|
serde = { version = "1.0.148", features = ["derive"] }
|
||||||
serde_bytes = "0.11.7"
|
serde_bytes = "0.11.7"
|
||||||
serde-pickle = "1.1.1"
|
serde-pickle = "1.1.1"
|
||||||
thiserror = "1.0.37"
|
thiserror = "1.0.37"
|
||||||
|
|||||||
+24
@@ -1,3 +1,15 @@
|
|||||||
|
//! World of Tanks Blitz replay parser.
|
||||||
|
|
||||||
|
#![warn(
|
||||||
|
clippy::all,
|
||||||
|
clippy::missing_const_for_fn,
|
||||||
|
clippy::trivially_copy_pass_by_ref,
|
||||||
|
clippy::map_unwrap_or,
|
||||||
|
clippy::explicit_into_iter_loop,
|
||||||
|
clippy::unused_self,
|
||||||
|
clippy::needless_pass_by_value
|
||||||
|
)]
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
mod replay;
|
mod replay;
|
||||||
@@ -6,3 +18,15 @@ mod result;
|
|||||||
pub use crate::error::Error;
|
pub use crate::error::Error;
|
||||||
pub use crate::replay::Replay;
|
pub use crate::replay::Replay;
|
||||||
pub use crate::result::Result;
|
pub use crate::result::Result;
|
||||||
|
|
||||||
|
#[cfg(doctest)]
|
||||||
|
mod test_readme {
|
||||||
|
macro_rules! external_doc_test {
|
||||||
|
($x:expr) => {
|
||||||
|
#[doc = $x]
|
||||||
|
extern "C" {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
external_doc_test!(include_str!("../README.md"));
|
||||||
|
}
|
||||||
+4
-30
@@ -1,31 +1,5 @@
|
|||||||
use ::prost::Message;
|
mod battle_results;
|
||||||
|
mod battle_results_dat;
|
||||||
|
|
||||||
#[derive(Message)]
|
pub use self::battle_results::*;
|
||||||
pub struct BattleResults {
|
pub use self::battle_results_dat::*;
|
||||||
#[prost(int64, tag = "2")]
|
|
||||||
pub timestamp: i64,
|
|
||||||
|
|
||||||
#[prost(message, repeated, tag = "201")]
|
|
||||||
pub players: Vec<Player>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Message)]
|
|
||||||
pub struct Player {
|
|
||||||
#[prost(uint32, tag = "1")]
|
|
||||||
pub account_id: u32,
|
|
||||||
|
|
||||||
#[prost(message, required, tag = "2")]
|
|
||||||
pub info: PlayerInfo,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Message)]
|
|
||||||
pub struct PlayerInfo {
|
|
||||||
#[prost(string, tag = "1")]
|
|
||||||
pub nickname: String,
|
|
||||||
|
|
||||||
#[prost(uint32, optional, tag = "2")]
|
|
||||||
pub platoon_id: Option<u32>,
|
|
||||||
|
|
||||||
#[prost(uint32, tag = "3")]
|
|
||||||
pub team_number: u32,
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
use prost::bytes::Buf;
|
||||||
|
use prost::Message;
|
||||||
|
|
||||||
|
use crate::error::Error;
|
||||||
|
use crate::result::Result;
|
||||||
|
|
||||||
|
impl BattleResults {
|
||||||
|
/// Parse battle results from the buffer.
|
||||||
|
///
|
||||||
|
/// Note, that this does NOT parse `battle_results.dat` itself, but the un-pickled tuple element.
|
||||||
|
pub fn parse(buffer: impl Buf) -> Result<Self> {
|
||||||
|
Self::decode(buffer).map_err(Error::DecodeFailed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Message)]
|
||||||
|
pub struct BattleResults {
|
||||||
|
#[prost(int64, tag = "2")]
|
||||||
|
pub timestamp: i64,
|
||||||
|
|
||||||
|
#[prost(message, repeated, tag = "201")]
|
||||||
|
pub players: Vec<Player>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Message)]
|
||||||
|
pub struct Player {
|
||||||
|
#[prost(uint32, tag = "1")]
|
||||||
|
pub account_id: u32,
|
||||||
|
|
||||||
|
#[prost(message, required, tag = "2")]
|
||||||
|
pub info: PlayerInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Message)]
|
||||||
|
pub struct PlayerInfo {
|
||||||
|
#[prost(string, tag = "1")]
|
||||||
|
pub nickname: String,
|
||||||
|
|
||||||
|
#[prost(uint32, optional, tag = "2")]
|
||||||
|
pub platoon_id: Option<u32>,
|
||||||
|
|
||||||
|
#[prost(uint32, tag = "3")]
|
||||||
|
pub team_number: u32,
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use serde_bytes::ByteBuf;
|
||||||
|
|
||||||
|
/// Represents un-pickled `battle_results.dat`.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct BattleResultsDat(pub(crate) u64, pub(crate) ByteBuf);
|
||||||
+3
-4
@@ -1,11 +1,10 @@
|
|||||||
use std::io::{Read, Seek};
|
use std::io::{Read, Seek};
|
||||||
|
|
||||||
use prost::Message;
|
use prost::Message;
|
||||||
use serde_bytes::ByteBuf;
|
|
||||||
use zip::ZipArchive;
|
use zip::ZipArchive;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::models::BattleResults;
|
use crate::models::{BattleResults, BattleResultsDat};
|
||||||
use crate::result::Result;
|
use crate::result::Result;
|
||||||
|
|
||||||
pub struct Replay<R>(pub(crate) ZipArchive<R>);
|
pub struct Replay<R>(pub(crate) ZipArchive<R>);
|
||||||
@@ -27,9 +26,9 @@ impl<R: Read + Seek> Replay<R> {
|
|||||||
pickled_battle_results
|
pickled_battle_results
|
||||||
.read_to_end(&mut serialized_battle_results)
|
.read_to_end(&mut serialized_battle_results)
|
||||||
.map_err(Error::ReadEntryFailed)?;
|
.map_err(Error::ReadEntryFailed)?;
|
||||||
let (_, battle_results): (u64, ByteBuf) =
|
let battle_results_dat: BattleResultsDat =
|
||||||
serde_pickle::from_slice(&serialized_battle_results, Default::default())
|
serde_pickle::from_slice(&serialized_battle_results, Default::default())
|
||||||
.map_err(Error::UnpickleFailed)?;
|
.map_err(Error::UnpickleFailed)?;
|
||||||
BattleResults::decode(battle_results.as_ref()).map_err(Error::DecodeFailed)
|
BattleResults::decode(battle_results_dat.1.as_ref()).map_err(Error::DecodeFailed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,7 @@ use wotbreplay_parser::Replay;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn battle_results_ok() -> Result<()> {
|
fn battle_results_ok() -> Result<()> {
|
||||||
let mut replay = Replay::open(File::open(
|
let mut replay = Replay::open(File::open("tests/replays/battle_results_ok.wotbreplay")?)?;
|
||||||
"tests/replays/20221202_2259__zeekrab_It20_Car_Comb_45t_2308705958773814583.wotbreplay",
|
|
||||||
)?)?;
|
|
||||||
let battle_results = replay.read_battle_results()?;
|
let battle_results = replay.read_battle_results()?;
|
||||||
|
|
||||||
assert_eq!(battle_results.timestamp, 1670018359);
|
assert_eq!(battle_results.timestamp, 1670018359);
|
||||||
|
|||||||
Reference in new issue
Block a user