From e757072ddd53987df5d364e9dd760296a3701e9a Mon Sep 17 00:00:00 2001 From: Viktor Persson Date: Sun, 9 Jun 2024 17:05:06 +0200 Subject: [PATCH] Add functions to parse player/spectator clients. --- Cargo.toml | 2 +- src/clients.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c87dcc..aa34c53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ categories = ["parsing"] keywords = ["demos", "mvd", "parser", "quake", "quakeworld"] repository = "https://github.com/vikpe/mvdparser" authors = ["Viktor Persson "] -version = "0.11.1" +version = "0.12.0" edition = "2021" license = "MIT" include = [ diff --git a/src/clients.rs b/src/clients.rs index 3bde143..085ee2d 100644 --- a/src/clients.rs +++ b/src/clients.rs @@ -16,6 +16,24 @@ pub fn clients(data: &[u8]) -> Result> { Ok(clients) } +pub fn player_clients(data: &[u8]) -> Result> { + let players = clients(data)? + .iter() + .filter(|c| !c.is_spectator) + .cloned() + .collect(); + Ok(players) +} + +pub fn spectator_clients(data: &[u8]) -> Result> { + let spectators = clients(data)? + .iter() + .filter(|c| c.is_spectator) + .cloned() + .collect(); + Ok(spectators) +} + #[cfg(test)] mod tests { use std::fs::read; @@ -26,7 +44,7 @@ mod tests { use super::*; #[test] - fn test_clientinfo() -> Result<()> { + fn test_clients() -> Result<()> { assert_eq!( clients(&read( "tests/files/duel_equ_vs_kaboom[povdmm4]20240422-1038.mvd" @@ -61,4 +79,52 @@ mod tests { Ok(()) } + + #[test] + fn test_player_clients() -> Result<()> { + assert_eq!( + player_clients(&read( + "tests/files/duel_equ_vs_kaboom[povdmm4]20240422-1038.mvd" + )?)?, + vec![ + Client { + number: 0, + name: "eQu".to_string(), + team: "red".to_string(), + color: [4, 4], + is_spectator: false, + is_bot: false, + }, + Client { + number: 2, + name: "KabÏÏm".to_string(), + team: "".to_string(), + color: [2, 2], + is_spectator: false, + is_bot: false, + }, + ] + ); + + Ok(()) + } + + #[test] + fn test_spectator_clients() -> Result<()> { + assert_eq!( + spectator_clients(&read( + "tests/files/duel_equ_vs_kaboom[povdmm4]20240422-1038.mvd" + )?)?, + vec![Client { + number: 1, + name: "[ServeMe]".to_string(), + team: "lqwc".to_string(), + color: [12, 11], + is_spectator: true, + is_bot: false, + },] + ); + + Ok(()) + } }