Skip to content

Commit

Permalink
Add functions to parse player/team/spectator names.
Browse files Browse the repository at this point in the history
  • Loading branch information
vikpe committed Jun 9, 2024
1 parent e757072 commit d3084b0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 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.12.0"
version = "0.13.0"
edition = "2021"
license = "MIT"
include = [
Expand All @@ -25,7 +25,7 @@ chrono = { version = "0.4.38", default-features = false }
ktxstats = "0.3.1"
quake_clientinfo = "0.3.0"
quake_serverinfo = "0.4.0"
quake_text = "0.1.1"
quake_text = "0.2.0"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false }
Expand Down
93 changes: 93 additions & 0 deletions src/clients.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use quake_text::unicode;

use crate::client::Client;
use crate::clientinfo;
Expand All @@ -25,6 +26,14 @@ pub fn player_clients(data: &[u8]) -> Result<Vec<Client>> {
Ok(players)
}

pub fn player_names(data: &[u8]) -> Result<Vec<String>> {
let names = player_clients(data)?
.iter()
.map(|c| c.name.clone())
.collect::<Vec<String>>();
Ok(unicode::sort(&names))
}

pub fn spectator_clients(data: &[u8]) -> Result<Vec<Client>> {
let spectators = clients(data)?
.iter()
Expand All @@ -34,6 +43,25 @@ pub fn spectator_clients(data: &[u8]) -> Result<Vec<Client>> {
Ok(spectators)
}

pub fn spectator_names(data: &[u8]) -> Result<Vec<String>> {
let names: Vec<String> = spectator_clients(data)?
.iter()
.map(|c| c.name.clone())
.collect();
Ok(unicode::sort(&names))
}

pub fn team_names(data: &[u8]) -> Result<Vec<String>> {
let mut names: Vec<String> = player_clients(data)?
.iter()
.map(|c| c.team.clone())
.collect();

names = unicode::sort(&names);
names.dedup();
Ok(names)
}

#[cfg(test)]
mod tests {
use std::fs::read;
Expand Down Expand Up @@ -127,4 +155,69 @@ mod tests {

Ok(())
}

#[test]
fn test_spectator_names() -> Result<()> {
assert_eq!(
spectator_names(&read(
"tests/files/duel_equ_vs_kaboom[povdmm4]20240422-1038.mvd"
)?)?,
vec!["[ServeMe]".to_string()]
);

Ok(())
}

#[test]
fn test_player_names() -> Result<()> {
assert_eq!(
player_names(&read(
"tests/files/duel_equ_vs_kaboom[povdmm4]20240422-1038.mvd"
)?)?,
vec!["eQu".to_string(), "KabÏÏm".to_string()]
);

assert_eq!(
player_names(&read(
"tests/files/2on2_sf_vs_red[frobodm2]220104-0915.mvd"
)?)?,
vec![
": Sujoy".to_string(),
": Timber".to_string(),
"> MrJustice".to_string(),
"Final".to_string(),
]
);

Ok(())
}

#[test]
fn test_team_names() -> Result<()> {
assert_eq!(
team_names(&read(
"tests/files/2on2_sf_vs_red[frobodm2]220104-0915.mvd"
)?)?,
vec!["=SF=".to_string(), "red".to_string()]
);

assert_eq!(
team_names(&read("tests/files/ctf_blue_vs_red[ctf5]20240520-1925.mvd")?)?,
vec!["blue".to_string(), "red".to_string()]
);

assert_eq!(
team_names(&read("tests/files/4on4_oeks_vs_tsq[dm2]20240426-1716.mvd")?)?,
vec!["oeks".to_string(), "tSÑ".to_string()]
);

assert_eq!(
team_names(&read(
"tests/files/wipeout_red_vs_blue[q3dm6qw]20240406-2028.mvd"
)?)?,
vec!["blue".to_string(), "red".to_string()]
);

Ok(())
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub use server::Server;
pub use team::Team;

pub use crate::clientinfo::clientinfo;
pub use crate::clients::{
player_clients, player_names, spectator_clients, spectator_names, team_names,
};
pub use crate::duration::{countdown_duration, demo_duration, match_duration};
pub use crate::filename::filename;
pub use crate::frags::frags_per_player_name;
Expand Down

0 comments on commit d3084b0

Please sign in to comment.