From aefd8cc43cc55383592bdf363bb8a4fe90d5d226 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Fri, 21 Oct 2022 12:35:33 +0300 Subject: [PATCH] Added support for Counter-Strike: Source --- GAMES.md | 3 +- examples/css.rs | 10 +++++ src/games/css.rs | 83 ++++++++++++++++++++++++++++++++++++++++++ src/games/mod.rs | 1 + src/protocols/valve.rs | 2 + 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 examples/css.rs create mode 100644 src/games/css.rs diff --git a/GAMES.md b/GAMES.md index 777933e..e68a751 100644 --- a/GAMES.md +++ b/GAMES.md @@ -4,7 +4,8 @@ |----------|----------------------------------|----------------|----------------------------------| | TF2 | Team Fortress 2 | Valve Protocol | | | The_Ship | The Ship | Valve Protocol | | -| CSGO | Counter Strike: Global Offensive | Valve Protocol | Rules doesnt work on this title. | +| CSGO | Counter-Strike: Global Offensive | Valve Protocol | Rules doesnt work on this title. | +| CSS | Counter-Strike: Source | Valve Protocol | | ## Planned to add support: All Valve titles. diff --git a/examples/css.rs b/examples/css.rs new file mode 100644 index 0000000..53aea99 --- /dev/null +++ b/examples/css.rs @@ -0,0 +1,10 @@ + +use gamedig::games::css; + +fn main() { + let response = css::query("104.128.58.206", None); + match response { + Err(error) => println!("Couldn't query, error: {error}"), + Ok(r) => println!("{:?}", r) + } +} diff --git a/src/games/css.rs b/src/games/css.rs new file mode 100644 index 0000000..9e23ad6 --- /dev/null +++ b/src/games/css.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::CSS, 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 a0f3e63..fc3ddf8 100644 --- a/src/games/mod.rs +++ b/src/games/mod.rs @@ -4,3 +4,4 @@ pub mod tf2; pub mod the_ship; pub mod csgo; +pub mod css; diff --git a/src/protocols/valve.rs b/src/protocols/valve.rs index 82e1a74..6b0f364 100644 --- a/src/protocols/valve.rs +++ b/src/protocols/valve.rs @@ -124,6 +124,7 @@ pub enum Request { /// Supported app id's #[derive(PartialEq)] pub enum App { + CSS = 240, TF2 = 440, CSGO = 730, TheShip = 2400 @@ -134,6 +135,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::TF2 as u16 => Ok(App::TF2), x if x == App::CSGO as u16 => Ok(App::CSGO), x if x == App::TheShip as u16 => Ok(App::TheShip),