[Games] Programmatic games by storing information as data (#45)

* Define games as structs

* Create table of response types

* Ensure serde is always included

* Remove server_ prefix in GenericResponse

* Make players online/max non-optional in generic response

* Use already existing minecraft server enum

* Implement ExtraResponses to prevent cloning when creating generic

* Add game definitions

* Add doc comments to generic types

* Include players in gamespy extra responses

* Add custom response types for TheShip and FFOW

* Cargo format differing files

* Final cleanup
This commit is contained in:
Tom 2023-06-13 18:49:58 +00:00 committed by GitHub
parent 26ad1f5d19
commit d853189e06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 806 additions and 102 deletions

View file

@ -1,5 +1,6 @@
use crate::protocols::types::TimeoutSettings;
use crate::protocols::types::{SpecificResponse, TimeoutSettings};
use crate::protocols::valve::{Engine, Environment, Server, ValveProtocol};
use crate::protocols::GenericResponse;
use crate::GDResult;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -43,6 +44,53 @@ 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 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,
}),
}
}
}
pub fn query(address: &IpAddr, port: Option<u16>) -> GDResult<Response> {
query_with_timeout(address, port, TimeoutSettings::default())
}