mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[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:
parent
bf14ecb4a4
commit
b368877031
17 changed files with 405 additions and 430 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use crate::protocols::types::{SpecificResponse, TimeoutSettings};
|
||||
use crate::protocols::types::{CommonResponse, TimeoutSettings};
|
||||
use crate::protocols::valve::{Engine, Environment, Server, ValveProtocol};
|
||||
use crate::protocols::GenericResponse;
|
||||
use crate::GDResult;
|
||||
|
|
@ -44,51 +44,17 @@ pub struct Response {
|
|||
pub time_left: u16,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ExtraResponse {
|
||||
/// Protocol used by the server.
|
||||
pub protocol: u8,
|
||||
/// Map name.
|
||||
pub active_mod: String,
|
||||
/// 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,
|
||||
/// Current round index.
|
||||
pub round: u8,
|
||||
/// Maximum amount of rounds.
|
||||
pub rounds_maximum: u8,
|
||||
/// Time left for the current round in seconds.
|
||||
pub time_left: u16,
|
||||
}
|
||||
impl CommonResponse for Response {
|
||||
fn as_original(&self) -> GenericResponse { GenericResponse::FFOW(self) }
|
||||
|
||||
impl From<Response> for GenericResponse {
|
||||
fn from(r: Response) -> Self {
|
||||
Self {
|
||||
name: Some(r.name),
|
||||
description: Some(r.description),
|
||||
game: Some(r.game_mode),
|
||||
game_version: Some(r.version),
|
||||
map: Some(r.map),
|
||||
players_maximum: r.players_maximum.into(),
|
||||
players_online: r.players_online.into(),
|
||||
players_bots: None,
|
||||
has_password: Some(r.has_password),
|
||||
inner: SpecificResponse::FFOW(ExtraResponse {
|
||||
protocol: r.protocol,
|
||||
active_mod: r.active_mod,
|
||||
server_type: r.server_type,
|
||||
environment_type: r.environment_type,
|
||||
vac_secured: r.vac_secured,
|
||||
round: r.round,
|
||||
rounds_maximum: r.rounds_maximum,
|
||||
time_left: r.time_left,
|
||||
}),
|
||||
}
|
||||
}
|
||||
fn name(&self) -> Option<&str> { Some(&self.name) }
|
||||
fn game(&self) -> Option<&str> { Some(&self.game_mode) }
|
||||
fn description(&self) -> Option<&str> { Some(&self.description) }
|
||||
fn game_version(&self) -> Option<&str> { Some(&self.version) }
|
||||
fn map(&self) -> Option<&str> { Some(&self.map) }
|
||||
fn has_password(&self) -> Option<bool> { Some(self.has_password) }
|
||||
fn players_maximum(&self) -> u64 { self.players_maximum.into() }
|
||||
fn players_online(&self) -> u64 { self.players_online.into() }
|
||||
}
|
||||
|
||||
pub fn query(address: &IpAddr, port: Option<u16>) -> GDResult<Response> {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::protocols::gamespy::common::has_password;
|
||||
use crate::protocols::gamespy::three::{data_to_map, GameSpy3};
|
||||
use crate::protocols::types::SpecificResponse;
|
||||
use crate::protocols::types::{CommonPlayer, CommonResponse, GenericPlayer};
|
||||
use crate::protocols::GenericResponse;
|
||||
use crate::{GDError, GDResult};
|
||||
#[cfg(feature = "serde")]
|
||||
|
|
@ -16,6 +16,12 @@ pub struct Player {
|
|||
ping: u16,
|
||||
}
|
||||
|
||||
impl CommonPlayer for Player {
|
||||
fn as_original(&self) -> GenericPlayer { GenericPlayer::JCMP2(self) }
|
||||
|
||||
fn name(&self) -> &str { &self.name }
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Response {
|
||||
|
|
@ -28,20 +34,27 @@ pub struct Response {
|
|||
players_online: usize,
|
||||
}
|
||||
|
||||
impl From<Response> for GenericResponse {
|
||||
fn from(r: Response) -> Self {
|
||||
Self {
|
||||
name: Some(r.name),
|
||||
description: Some(r.description),
|
||||
game: None,
|
||||
game_version: Some(r.version),
|
||||
map: None,
|
||||
players_maximum: r.players_maximum as u64,
|
||||
players_online: r.players_online as u64,
|
||||
players_bots: None,
|
||||
has_password: Some(r.has_password),
|
||||
inner: SpecificResponse::JC2MP,
|
||||
}
|
||||
impl CommonResponse for Response {
|
||||
fn as_original(&self) -> GenericResponse { GenericResponse::JC2MP(self) }
|
||||
|
||||
fn game_version(&self) -> Option<&str> { Some(&self.version) }
|
||||
fn description(&self) -> Option<&str> { Some(&self.description) }
|
||||
fn name(&self) -> Option<&str> { Some(&self.name) }
|
||||
fn has_password(&self) -> Option<bool> { Some(self.has_password) }
|
||||
fn players_maximum(&self) -> u64 {
|
||||
// If usize doesn't fit in u64 silently return 0 as this is extremely unlikely
|
||||
// for a player count
|
||||
self.players_maximum.try_into().unwrap_or(0)
|
||||
}
|
||||
fn players_online(&self) -> u64 { self.players_online.try_into().unwrap_or(0) }
|
||||
|
||||
fn players(&self) -> Option<Vec<&dyn crate::protocols::types::CommonPlayer>> {
|
||||
Some(
|
||||
self.players
|
||||
.iter()
|
||||
.map(|p| p as &dyn CommonPlayer)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -114,6 +114,7 @@ pub mod vr;
|
|||
|
||||
use crate::protocols::gamespy::GameSpyVersion;
|
||||
use crate::protocols::quake::QuakeVersion;
|
||||
use crate::protocols::types::CommonResponse;
|
||||
use crate::protocols::{self, Protocol};
|
||||
use crate::GDResult;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
|
|
@ -137,42 +138,42 @@ mod definitions;
|
|||
pub use definitions::GAMES;
|
||||
|
||||
/// Make a query given a game definition
|
||||
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<protocols::GenericResponse> {
|
||||
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<Box<dyn CommonResponse>> {
|
||||
let socket_addr = SocketAddr::new(*address, port.unwrap_or(game.default_port));
|
||||
Ok(match &game.protocol {
|
||||
Protocol::Valve(steam_app) => {
|
||||
protocols::valve::query(&socket_addr, steam_app.as_engine(), None, None).map(|r| r.into())?
|
||||
protocols::valve::query(&socket_addr, steam_app.as_engine(), None, None).map(Box::new)?
|
||||
}
|
||||
Protocol::Minecraft(version) => {
|
||||
match version {
|
||||
Some(protocols::minecraft::Server::Java) => {
|
||||
protocols::minecraft::query_java(&socket_addr, None).map(|r| r.into())?
|
||||
protocols::minecraft::query_java(&socket_addr, None).map(Box::new)?
|
||||
}
|
||||
Some(protocols::minecraft::Server::Bedrock) => {
|
||||
protocols::minecraft::query_bedrock(&socket_addr, None).map(|r| r.into())?
|
||||
protocols::minecraft::query_bedrock(&socket_addr, None).map(Box::new)?
|
||||
}
|
||||
Some(protocols::minecraft::Server::Legacy(group)) => {
|
||||
protocols::minecraft::query_legacy_specific(*group, &socket_addr, None).map(|r| r.into())?
|
||||
protocols::minecraft::query_legacy_specific(*group, &socket_addr, None).map(Box::new)?
|
||||
}
|
||||
None => protocols::minecraft::query(&socket_addr, None).map(|r| r.into())?,
|
||||
None => protocols::minecraft::query(&socket_addr, None).map(Box::new)?,
|
||||
}
|
||||
}
|
||||
Protocol::Gamespy(version) => {
|
||||
match version {
|
||||
GameSpyVersion::One => protocols::gamespy::one::query(&socket_addr, None).map(|r| r.into())?,
|
||||
GameSpyVersion::Two => protocols::gamespy::two::query(&socket_addr, None).map(|r| r.into())?,
|
||||
GameSpyVersion::Three => protocols::gamespy::three::query(&socket_addr, None).map(|r| r.into())?,
|
||||
GameSpyVersion::One => protocols::gamespy::one::query(&socket_addr, None).map(Box::new)?,
|
||||
GameSpyVersion::Two => protocols::gamespy::two::query(&socket_addr, None).map(Box::new)?,
|
||||
GameSpyVersion::Three => protocols::gamespy::three::query(&socket_addr, None).map(Box::new)?,
|
||||
}
|
||||
}
|
||||
Protocol::Quake(version) => {
|
||||
match version {
|
||||
QuakeVersion::One => protocols::quake::one::query(&socket_addr, None).map(|r| r.into())?,
|
||||
QuakeVersion::Two => protocols::quake::two::query(&socket_addr, None).map(|r| r.into())?,
|
||||
QuakeVersion::Three => protocols::quake::three::query(&socket_addr, None).map(|r| r.into())?,
|
||||
QuakeVersion::One => protocols::quake::one::query(&socket_addr, None).map(Box::new)?,
|
||||
QuakeVersion::Two => protocols::quake::two::query(&socket_addr, None).map(Box::new)?,
|
||||
QuakeVersion::Three => protocols::quake::three::query(&socket_addr, None).map(Box::new)?,
|
||||
}
|
||||
}
|
||||
Protocol::TheShip => ts::query(address, port).map(|r| r.into())?,
|
||||
Protocol::FFOW => ffow::query(address, port).map(|r| r.into())?,
|
||||
Protocol::JC2MP => jc2mp::query(address, port).map(|r| r.into())?,
|
||||
Protocol::TheShip => ts::query(address, port).map(Box::new)?,
|
||||
Protocol::FFOW => ffow::query(address, port).map(Box::new)?,
|
||||
Protocol::JC2MP => jc2mp::query(address, port).map(Box::new)?,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
protocols::{
|
||||
types::SpecificResponse,
|
||||
types::{CommonPlayer, CommonResponse, GenericPlayer},
|
||||
valve::{self, get_optional_extracted_data, Server, ServerPlayer, SteamApp},
|
||||
GenericResponse,
|
||||
},
|
||||
|
|
@ -35,6 +35,13 @@ impl TheShipPlayer {
|
|||
}
|
||||
}
|
||||
|
||||
impl CommonPlayer for TheShipPlayer {
|
||||
fn as_original(&self) -> GenericPlayer { GenericPlayer::TheShip(self) }
|
||||
|
||||
fn name(&self) -> &str { &self.name }
|
||||
fn score(&self) -> Option<u32> { Some(self.score) }
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Response {
|
||||
|
|
@ -61,52 +68,24 @@ pub struct Response {
|
|||
pub duration: u8,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ExtraResponse {
|
||||
pub protocol: u8,
|
||||
pub player_details: Vec<TheShipPlayer>,
|
||||
pub server_type: Server,
|
||||
pub vac_secured: bool,
|
||||
pub port: Option<u16>,
|
||||
pub steam_id: Option<u64>,
|
||||
pub tv_port: Option<u16>,
|
||||
pub tv_name: Option<String>,
|
||||
pub keywords: Option<String>,
|
||||
pub rules: HashMap<String, String>,
|
||||
pub mode: u8,
|
||||
pub witnesses: u8,
|
||||
pub duration: u8,
|
||||
}
|
||||
impl CommonResponse for Response {
|
||||
fn as_original(&self) -> GenericResponse { GenericResponse::TheShip(self) }
|
||||
|
||||
impl From<Response> for GenericResponse {
|
||||
fn from(r: Response) -> Self {
|
||||
Self {
|
||||
name: Some(r.name),
|
||||
description: None,
|
||||
game: Some(r.game),
|
||||
game_version: Some(r.version),
|
||||
map: Some(r.map),
|
||||
players_maximum: r.max_players.into(),
|
||||
players_online: r.players.into(),
|
||||
players_bots: Some(r.bots.into()),
|
||||
has_password: Some(r.has_password),
|
||||
inner: SpecificResponse::TheShip(ExtraResponse {
|
||||
protocol: r.protocol,
|
||||
player_details: r.players_details,
|
||||
server_type: r.server_type,
|
||||
vac_secured: r.vac_secured,
|
||||
steam_id: r.steam_id,
|
||||
port: r.port,
|
||||
tv_port: r.tv_port,
|
||||
tv_name: r.tv_name,
|
||||
keywords: r.keywords,
|
||||
rules: r.rules,
|
||||
mode: r.mode,
|
||||
witnesses: r.witnesses,
|
||||
duration: r.duration,
|
||||
}),
|
||||
}
|
||||
fn name(&self) -> Option<&str> { Some(&self.name) }
|
||||
fn map(&self) -> Option<&str> { Some(&self.map) }
|
||||
fn game(&self) -> Option<&str> { Some(&self.game) }
|
||||
fn players_maximum(&self) -> u64 { self.max_players.into() }
|
||||
fn players_online(&self) -> u64 { self.players.into() }
|
||||
fn players_bots(&self) -> Option<u64> { Some(self.bots.into()) }
|
||||
fn has_password(&self) -> Option<bool> { Some(self.has_password) }
|
||||
|
||||
fn players(&self) -> Option<Vec<&dyn CommonPlayer>> {
|
||||
Some(
|
||||
self.players_details
|
||||
.iter()
|
||||
.map(|p| p as &dyn CommonPlayer)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue