From 96c2c8a335769f2961bb92430e3081fa8f2c1ba8 Mon Sep 17 00:00:00 2001 From: cosminperram Date: Sun, 23 Oct 2022 18:09:13 +0300 Subject: [PATCH] Fixed uncomplete the ship player struct and added some docs... --- src/games/csgo.rs | 7 ++----- src/games/ts.rs | 33 +++++++++++++++++++++++++-------- src/protocols/valve/types.rs | 17 +++++++++++++---- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/games/csgo.rs b/src/games/csgo.rs index 9af139b..4b18678 100644 --- a/src/games/csgo.rs +++ b/src/games/csgo.rs @@ -1,6 +1,6 @@ use crate::GDResult; use crate::protocols::valve; -use crate::protocols::valve::{App, Server, ServerPlayer, GatheringSettings}; +use crate::protocols::valve::{App, Server, GatheringSettings, get_optional_extracted_data}; use crate::protocols::valve::game::Player; #[derive(Debug)] @@ -26,10 +26,7 @@ pub struct Response { 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) - }; + let (port, steam_id, tv_port, tv_name, keywords) = get_optional_extracted_data(response.info.extra_data); Self { protocol: response.info.protocol, diff --git a/src/games/ts.rs b/src/games/ts.rs index ecc9baa..ad7ee8c 100644 --- a/src/games/ts.rs +++ b/src/games/ts.rs @@ -1,7 +1,27 @@ use crate::GDResult; use crate::protocols::valve; -use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer}; -use crate::protocols::valve::game::Player; +use crate::protocols::valve::{App, Server, ServerRule, ServerPlayer, get_optional_extracted_data}; + +#[derive(Debug)] +pub struct TheShipPlayer { + pub name: String, + pub score: u32, + pub duration: f32, + pub deaths: u32, + pub money: u32 +} + +impl TheShipPlayer { + pub fn new_from_valve_player(player: &ServerPlayer) -> Self { + Self { + name: player.name.clone(), + score: player.score, + duration: player.duration, + deaths: player.deaths.unwrap(), + money: player.money.unwrap() + } + } +} #[derive(Debug)] pub struct Response { @@ -10,7 +30,7 @@ pub struct Response { pub map: String, pub game: String, pub players: u8, - pub players_details: Vec, + pub players_details: Vec, pub max_players: u8, pub bots: u8, pub server_type: Server, @@ -30,10 +50,7 @@ pub struct Response { 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) - }; + let (port, steam_id, tv_port, tv_name, keywords) = get_optional_extracted_data(response.info.extra_data); let the_unwrapped_ship = response.info.the_ship.unwrap(); @@ -43,7 +60,7 @@ impl Response { 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(), + players_details: response.players.unwrap().iter().map(|p| TheShipPlayer::new_from_valve_player(p)).collect(), max_players: response.info.max_players, bots: response.info.bots, server_type: response.info.server_type, diff --git a/src/protocols/valve/types.rs b/src/protocols/valve/types.rs index 9636197..b06c793 100644 --- a/src/protocols/valve/types.rs +++ b/src/protocols/valve/types.rs @@ -1,4 +1,6 @@ +//! All types used by the Valve protocol + /// The type of the server. #[derive(Debug)] pub enum Server { @@ -107,6 +109,13 @@ pub struct ExtraData { pub game_id: Option } +pub fn get_optional_extracted_data(data: Option) -> (Option, Option, Option, Option, Option) { + match data { + None => (None, None, None, None, None), + Some(ed) => (ed.port, ed.steam_id, ed.tv_port, ed.tv_name, ed.keywords) + } +} + /// The type of the request, see the [protocol](https://developer.valvesoftware.com/wiki/Server_queries). #[derive(PartialEq, Clone)] #[repr(u8)] @@ -158,7 +167,10 @@ pub struct GatheringSettings { pub rules: bool } +/// Generic response types that are used by many games, they are the protocol ones, but without the +/// unnecessary bits (example: the **The Ship**-only fields) pub mod game { + use crate::protocols::valve::types::get_optional_extracted_data; use super::{Server, ServerRule, ServerPlayer}; #[derive(Debug)] @@ -202,10 +214,7 @@ pub mod game { impl Response { pub fn new_from_valve_response(response: super::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) - }; + let (port, steam_id, tv_port, tv_name, keywords) = get_optional_extracted_data(response.info.extra_data); Self { protocol: response.info.protocol,