From 526aef9acc01126fa0286820d3924225a5784347 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 20 Oct 2022 23:08:45 +0300 Subject: [PATCH] Games structures (#2) * tf2 skeleton * tf2 structure done * csgo structure * the_ship structure --- src/games/csgo.rs | 78 +++++++++++++++++++++++++++++++++++-- src/games/tf2.rs | 78 +++++++++++++++++++++++++++++++++++-- src/games/the_ship.rs | 90 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 236 insertions(+), 10 deletions(-) diff --git a/src/games/csgo.rs b/src/games/csgo.rs index cc45ab4..20d0416 100644 --- a/src/games/csgo.rs +++ b/src/games/csgo.rs @@ -1,12 +1,82 @@ use crate::errors::GDError; -use crate::valve::{ValveProtocol, App, GatheringSettings, Response}; +use crate::valve; +use crate::valve::{ValveProtocol, App, GatheringSettings, ServerPlayer, Server}; + +#[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: Vec, + pub online_players: u8, + 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 +} + +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.players.unwrap().iter().map(|p| Player::from_valve_response(p)).collect(), + online_players: response.info.players, + 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 + } + } +} pub fn query(address: &str, port: Option) -> Result { - ValveProtocol::query(App::CSGO, address, match port { + let valve_response = ValveProtocol::query(App::CSGO, address, match port { None => 27015, Some(port) => port }, GatheringSettings { players: true, - rules: true - }) + rules: false // cause csgo doesnt reply with rules anymore + })?; + + Ok(Response::new_from_valve_response(valve_response)) } diff --git a/src/games/tf2.rs b/src/games/tf2.rs index dcee6fb..528961f 100644 --- a/src/games/tf2.rs +++ b/src/games/tf2.rs @@ -1,12 +1,84 @@ use crate::errors::GDError; -use crate::valve::{ValveProtocol, App, GatheringSettings, Response}; +use crate::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: Vec, + pub online_players: u8, + 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.players.unwrap().iter().map(|p| Player::from_valve_response(p)).collect(), + online_players: response.info.players, + 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) -> Result { - ValveProtocol::query(App::TF2, address, match port { + let valve_response = ValveProtocol::query(App::TF2, 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/the_ship.rs b/src/games/the_ship.rs index a9d1fba..0561930 100644 --- a/src/games/the_ship.rs +++ b/src/games/the_ship.rs @@ -1,12 +1,96 @@ use crate::errors::GDError; -use crate::valve::{ValveProtocol, App, GatheringSettings, Response}; +use crate::valve; +use crate::valve::{ValveProtocol, App, GatheringSettings, ServerPlayer, Server, ServerRule}; + +#[derive(Debug)] +pub struct Player { + pub name: String, + pub score: u32, + pub duration: f32, + pub deaths: u32, + pub money: u32 +} + +impl Player { + fn from_valve_response(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 { + pub protocol: u8, + pub name: String, + pub map: String, + pub game: String, + pub players: Vec, + pub online_players: u8, + 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, + pub mode: u8, + pub witnesses: u8, + pub duration: u8 +} + +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 the_unwrapped_ship = response.info.the_ship.unwrap(); + + Self { + protocol: response.info.protocol, + name: response.info.name, + map: response.info.map, + game: response.info.game, + players: response.players.unwrap().iter().map(|p| Player::from_valve_response(p)).collect(), + online_players: response.info.players, + 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(), + mode: the_unwrapped_ship.mode, + witnesses: the_unwrapped_ship.witnesses, + duration: the_unwrapped_ship.duration + } + } +} pub fn query(address: &str, port: Option) -> Result { - ValveProtocol::query(App::TheShip, address, match port { + let valve_response = ValveProtocol::query(App::TheShip, address, match port { None => 27015, Some(port) => port }, GatheringSettings { players: true, rules: true - }) + })?; + + Ok(Response::new_from_valve_response(valve_response)) }