diff --git a/examples/gm.rs b/examples/gm.rs new file mode 100644 index 0000000..0ce21bb --- /dev/null +++ b/examples/gm.rs @@ -0,0 +1,10 @@ + +use gamedig::games::gm; + +fn main() { + let response = gm::query("148.59.74.84", None); + match response { + Err(error) => println!("Couldn't query, error: {error}"), + Ok(r) => println!("{:?}", r) + } +} diff --git a/src/games/gm.rs b/src/games/gm.rs new file mode 100644 index 0000000..12c4a90 --- /dev/null +++ b/src/games/gm.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::GM, 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 af2be47..8e23504 100644 --- a/src/games/mod.rs +++ b/src/games/mod.rs @@ -6,3 +6,4 @@ pub mod ts; pub mod csgo; pub mod css; pub mod dods; +pub mod gm; diff --git a/src/protocols/valve.rs b/src/protocols/valve.rs index 0ebaf83..0c3670d 100644 --- a/src/protocols/valve.rs +++ b/src/protocols/valve.rs @@ -124,11 +124,18 @@ pub enum Request { /// Supported app id's #[derive(PartialEq)] pub enum App { + /// Counter-Strike: Source CSS = 240, + /// Day of Defeat: Sourcec DODS = 300, + /// Team Fortress 2 TF2 = 440, + /// Counter-Strike: Global Offensive CSGO = 730, - TS = 2400 + /// The Ship + TS = 2400, + /// Garry's Mod + GM = 4000, } impl TryFrom for App { @@ -141,6 +148,7 @@ impl TryFrom for App { x if x == App::TF2 as u16 => Ok(App::TF2), x if x == App::CSGO as u16 => Ok(App::CSGO), x if x == App::TS as u16 => Ok(App::TS), + x if x == App::GM as u16 => Ok(App::GM), _ => Err(GDError::UnknownEnumCast), } }