Fixed uncomplete the ship player struct and added some docs...

This commit is contained in:
cosminperram 2022-10-23 18:09:13 +03:00
parent 9df4bddc09
commit 96c2c8a335
3 changed files with 40 additions and 17 deletions

View file

@ -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,

View file

@ -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<Player>,
pub players_details: Vec<TheShipPlayer>,
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,

View file

@ -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<u64>
}
pub fn get_optional_extracted_data(data: Option<ExtraData>) -> (Option<u16>, Option<u64>, Option<u16>, Option<String>, Option<String>) {
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,