[Protocol] Implement generic response with dyn (#56)

* Implement generic response as enum

* First draft of implementing into_common()

* Make common response type generic

* Use macros and generics to reduce repetition

* [Games] Add dynamically dispatched CommonResponse trait

This adds two traits: "CommonResponse", and "CommonPlayer", when the
generic game query function returns a response it returns a pointer to
its original response type that implements "CommonResponse".

Both common traits require that "as_original()" be implemented, this
returns an enum containing a pointer to the original type.

Both traits have a concrete method "as_json()" that returns a struct
containing data fetched from all of its methods as. This struct
implements serde and can hence be serialized as required.

The traits require a few other methods be implemented, those being the
fields that are common across all types. All other methods have a
default None implementation so that each response type only needs to
implement methods for fields that it has.

* [Game] Implement common traits for JCMP2 response

* [Fmt] Run cargo fmt

* Fix doctest failing

* Run cargo fmt
This commit is contained in:
Tom 2023-06-25 13:31:23 +00:00 committed by GitHub
parent bf14ecb4a4
commit b368877031
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 405 additions and 430 deletions

View file

@ -19,7 +19,6 @@ use crate::{
SteamApp,
},
},
socket::{Socket, UdpSocket},
utils::u8_lower_upper,
GDError::{BadGame, Decompress, UnknownEnumCast},

View file

@ -1,11 +1,9 @@
use std::collections::HashMap;
use crate::protocols::types::{CommonPlayer, CommonResponse, GenericPlayer};
use crate::GDError::UnknownEnumCast;
use crate::GDResult;
use crate::{
bufferer::Bufferer,
protocols::{types::SpecificResponse, GenericResponse},
};
use crate::{bufferer::Bufferer, protocols::GenericResponse};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -58,61 +56,22 @@ pub struct Response {
pub rules: Option<HashMap<String, String>>,
}
/// Non-generic valve response
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct ExtraResponse {
pub players: Option<Vec<ServerPlayer>>,
pub rules: Option<HashMap<String, String>>,
/// Protocol used by the server.
pub protocol: u8,
/// Name of the folder containing the game files.
pub folder: String,
/// [Steam Application ID](https://developer.valvesoftware.com/wiki/Steam_Application_ID) of game.
pub appid: u32,
/// Dedicated, NonDedicated or SourceTV
pub server_type: Server,
/// The Operating System that the server is on.
pub environment_type: Environment,
/// Indicates whether the server uses VAC.
pub vac_secured: bool,
/// [The ship](https://developer.valvesoftware.com/wiki/The_Ship) extra data
pub the_ship: Option<TheShip>,
/// Some extra data that the server might provide or not.
pub extra_data: Option<ExtraData>,
/// GoldSrc only: Indicates whether the hosted game is a mod.
pub is_mod: bool,
/// GoldSrc only: If the game is a mod, provide additional data.
pub mod_data: Option<ModData>,
}
impl CommonResponse for Response {
fn as_original(&self) -> GenericResponse { GenericResponse::Valve(self) }
impl From<Response> for GenericResponse {
fn from(r: Response) -> Self {
GenericResponse {
name: Some(r.info.name),
description: None,
game: Some(r.info.game),
game_version: Some(r.info.version),
map: Some(r.info.map),
players_maximum: r.info.players_maximum.into(),
players_online: r.info.players_online.into(),
players_bots: Some(r.info.players_bots.into()),
has_password: Some(r.info.has_password),
inner: SpecificResponse::Valve(ExtraResponse {
players: r.players,
rules: r.rules,
protocol: r.info.protocol,
folder: r.info.folder,
appid: r.info.appid,
server_type: r.info.server_type,
environment_type: r.info.environment_type,
vac_secured: r.info.vac_secured,
the_ship: r.info.the_ship,
extra_data: r.info.extra_data,
is_mod: r.info.is_mod,
mod_data: r.info.mod_data,
}),
}
fn name(&self) -> Option<&str> { Some(&self.info.name) }
fn game(&self) -> Option<&str> { Some(&self.info.game) }
fn game_version(&self) -> Option<&str> { Some(&self.info.version) }
fn map(&self) -> Option<&str> { Some(&self.info.map) }
fn players_maximum(&self) -> u64 { self.info.players_maximum.into() }
fn players_online(&self) -> u64 { self.info.players_online.into() }
fn players_bots(&self) -> Option<u64> { Some(self.info.players_bots.into()) }
fn has_password(&self) -> Option<bool> { Some(self.info.has_password) }
fn players(&self) -> Option<Vec<&dyn CommonPlayer>> {
self.players
.as_ref()
.map(|p| p.iter().map(|p| p as &dyn CommonPlayer).collect())
}
}
@ -174,6 +133,12 @@ pub struct ServerPlayer {
pub money: Option<u32>, // the_ship
}
impl CommonPlayer for ServerPlayer {
fn as_original(&self) -> GenericPlayer { GenericPlayer::Valve(self) }
fn name(&self) -> &str { &self.name }
fn score(&self) -> Option<u32> { Some(self.score) }
}
/// Only present for [the ship](https://developer.valvesoftware.com/wiki/The_Ship).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]