From 4e9458f1022bd8b4a4965af24b1330ea969e2ab0 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Fri, 21 Oct 2022 13:10:09 +0300 Subject: [PATCH] Added support for Half-Life 2 Deathmatch --- GAMES.md | 19 +++++----- examples/hl2dm.rs | 10 +++++ src/games/hl2dm.rs | 83 ++++++++++++++++++++++++++++++++++++++++++ src/games/mod.rs | 1 + src/protocols/valve.rs | 3 ++ 5 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 examples/hl2dm.rs create mode 100644 src/games/hl2dm.rs diff --git a/GAMES.md b/GAMES.md index 7ea42f0..8d8350a 100644 --- a/GAMES.md +++ b/GAMES.md @@ -1,14 +1,15 @@ # Supported games: -| ID | Name | Protocol | Notes | -|------|----------------------------------|----------------|----------------------------------| -| TF2 | Team Fortress 2 | Valve Protocol | | -| TS | The Ship | Valve Protocol | | -| CSGO | Counter-Strike: Global Offensive | Valve Protocol | Rules doesnt work on this title. | -| CSS | Counter-Strike: Source | Valve Protocol | | -| DODS | Day of Defeat: Source | Valve Protocol | | -| L4D | Left 4 Dead | Valve Protocol | | -| L4D2 | Left 4 Dead 2 | Valve Protocol | | +| ID | Name | Protocol | Notes | +|-------|----------------------------------|----------------|----------------------------------| +| TF2 | Team Fortress 2 | Valve Protocol | | +| TS | The Ship | Valve Protocol | | +| CSGO | Counter-Strike: Global Offensive | Valve Protocol | Rules doesnt work on this title. | +| CSS | Counter-Strike: Source | Valve Protocol | | +| DODS | Day of Defeat: Source | Valve Protocol | | +| L4D | Left 4 Dead | Valve Protocol | | +| L4D2 | Left 4 Dead 2 | Valve Protocol | | +| HL2DM | Half-Life 2 Deathmatch | Valve Protocol | | ## Planned to add support: All Valve titles. diff --git a/examples/hl2dm.rs b/examples/hl2dm.rs new file mode 100644 index 0000000..49d642e --- /dev/null +++ b/examples/hl2dm.rs @@ -0,0 +1,10 @@ + +use gamedig::games::hl2dm; + +fn main() { + let response = hl2dm::query("74.91.118.209", None); + match response { + Err(error) => println!("Couldn't query, error: {error}"), + Ok(r) => println!("{:?}", r) + } +} diff --git a/src/games/hl2dm.rs b/src/games/hl2dm.rs new file mode 100644 index 0000000..2ddf874 --- /dev/null +++ b/src/games/hl2dm.rs @@ -0,0 +1,83 @@ +use crate::{GDResult, valve}; +use crate::valve::{ValveProtocol, App, GatheringSettings, Server, ServerRule, ServerPlayer}; + +#[derive(Debug)] +pub struct Player { + pub name: String, + pub score: u32, + pub duration: f32 +} + +impl Player { + fn from_valve_response(player: &ServerPlayer) -> Self { + Self { + name: player.name.clone(), + score: player.score, + duration: player.duration + } + } +} + +#[derive(Debug)] +pub struct Response { + pub protocol: u8, + pub name: String, + pub map: String, + pub game: String, + pub players: u8, + pub players_details: Vec, + pub max_players: u8, + pub bots: u8, + pub server_type: Server, + pub has_password: bool, + pub vac_secured: bool, + pub version: String, + pub port: Option, + pub steam_id: Option, + pub tv_port: Option, + pub tv_name: Option, + pub keywords: Option, + pub rules: Vec +} + +impl Response { + pub fn new_from_valve_response(response: valve::Response) -> Self { + let (port, steam_id, tv_port, tv_name, keywords) = match response.info.extra_data { + None => (None, None, None, None, None), + Some(ed) => (ed.port, ed.steam_id, ed.tv_port, ed.tv_name, ed.keywords) + }; + + Self { + protocol: response.info.protocol, + name: response.info.name, + map: response.info.map, + game: response.info.game, + players: response.info.players, + players_details: response.players.unwrap().iter().map(|p| Player::from_valve_response(p)).collect(), + max_players: response.info.max_players, + bots: response.info.bots, + server_type: response.info.server_type, + has_password: response.info.has_password, + vac_secured: response.info.vac_secured, + version: response.info.version, + port, + steam_id, + tv_port, + tv_name, + keywords, + rules: response.rules.unwrap() + } + } +} + +pub fn query(address: &str, port: Option) -> GDResult { + let valve_response = ValveProtocol::query(App::HL2DM, address, match port { + None => 27015, + Some(port) => port + }, GatheringSettings { + players: true, + rules: true + })?; + + Ok(Response::new_from_valve_response(valve_response)) +} diff --git a/src/games/mod.rs b/src/games/mod.rs index 4486384..d5d8e73 100644 --- a/src/games/mod.rs +++ b/src/games/mod.rs @@ -9,3 +9,4 @@ pub mod dods; pub mod gm; pub mod l4d; pub mod l4d2; +pub mod hl2dm; diff --git a/src/protocols/valve.rs b/src/protocols/valve.rs index 6fa10ea..23e9d10 100644 --- a/src/protocols/valve.rs +++ b/src/protocols/valve.rs @@ -128,6 +128,8 @@ pub enum App { CSS = 240, /// Day of Defeat: Sourcec DODS = 300, + /// Half-Life 2 Deathmatch + HL2DM = 320, /// Team Fortress 2 TF2 = 440, /// Left 4 Dead @@ -148,6 +150,7 @@ impl TryFrom for App { fn try_from(value: u16) -> GDResult { match value { x if x == App::CSS as u16 => Ok(App::CSS), + x if x == App::HL2DM as u16 => Ok(App::HL2DM), x if x == App::DODS as u16 => Ok(App::DODS), x if x == App::TF2 as u16 => Ok(App::TF2), x if x == App::L4D as u16 => Ok(App::L4D),