Skip to content

Commit

Permalink
Add functions to parse player/spectator clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
vikpe committed Jun 9, 2024
1 parent 45ac2e2 commit e757072
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ categories = ["parsing"]
keywords = ["demos", "mvd", "parser", "quake", "quakeworld"]
repository = "https://github.com/vikpe/mvdparser"
authors = ["Viktor Persson <viktor.persson@arcsin.se>"]
version = "0.11.1"
version = "0.12.0"
edition = "2021"
license = "MIT"
include = [
Expand Down
68 changes: 67 additions & 1 deletion src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ pub fn clients(data: &[u8]) -> Result<Vec<Client>> {
Ok(clients)
}

pub fn player_clients(data: &[u8]) -> Result<Vec<Client>> {
let players = clients(data)?
.iter()
.filter(|c| !c.is_spectator)
.cloned()
.collect();
Ok(players)
}

pub fn spectator_clients(data: &[u8]) -> Result<Vec<Client>> {
let spectators = clients(data)?
.iter()
.filter(|c| c.is_spectator)
.cloned()
.collect();
Ok(spectators)
}

#[cfg(test)]
mod tests {
use std::fs::read;
Expand All @@ -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"
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit e757072

Please sign in to comment.