[Protocol] Standardize fields (#84)

* [Protocol] Standardize The Ship fields

* [Protocol] Standardize FFOW fields

* [Protocol] Rename Valve's protocol field to protocol_version

* [Protocol] Rename Minecraft's version_protocol field to protocol_version

* [Protocol] Rename Valve's version field to game_version

* [Protocol] Rename Minecraft java version_name to game_version

* [Crate] Reformat RESPONSES.md

* [Protocol] Renamed Minecraft Java players_sample to players

* [Protocol] Rename Quake (1,2,3) version field to game_version

* [Protocol] Rename quake (1 and 2) game_type field to game_mode

* [Protocol] Rename Valve, FFOW, TS game field to game_mode

* [Generics] Rename game field/function to game_mode

* [Protocol] Change players_minimum, _maximum and _bots from those who werent u8 or u32 to u32

* [Protocol] Change instances of player score field type from u32 to i32

* [Crate] Nicer gramar in CHANGELOG

* [Protocol] Apply clippy fixes
This commit is contained in:
CosminPerRam 2023-08-15 20:44:18 +03:00 committed by GitHub
parent 65c56dc196
commit 9d8fb1ba94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 249 additions and 200 deletions

View file

@ -35,7 +35,7 @@ pub fn query(address: &IpAddr, port: Option<u16>) -> GDResult<game::Response> {
}
if let Some(bat_gamemode) = rules.get("bat_gamemode_s") {
valve_response.info.game = bat_gamemode.clone();
valve_response.info.game_mode = bat_gamemode.clone();
rules.remove("bat_gamemode_s");
}

View file

@ -13,17 +13,17 @@ use std::net::{IpAddr, SocketAddr};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Response {
/// Protocol used by the server.
pub protocol: u8,
pub protocol_version: u8,
/// Name of the server.
pub name: String,
/// Map name.
pub active_mod: String,
/// Running game mode.
pub game_mode: String,
/// The version that the server is running on.
pub game_version: String,
/// Description of the server.
pub description: String,
/// The version that the server is running on.
pub version: String,
/// Current map.
pub map: String,
/// Number of players on the server.
@ -50,13 +50,13 @@ impl CommonResponse for Response {
fn as_original(&self) -> GenericResponse { GenericResponse::FFOW(self) }
fn name(&self) -> Option<&str> { Some(&self.name) }
fn game(&self) -> Option<&str> { Some(&self.game_mode) }
fn game_mode(&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 game_version(&self) -> Option<&str> { Some(&self.game_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() }
fn players_maximum(&self) -> u32 { self.players_maximum.into() }
fn players_online(&self) -> u32 { self.players_online.into() }
}
pub fn query(address: &IpAddr, port: Option<u16>) -> GDResult<Response> { query_with_timeout(address, port, None) }
@ -79,13 +79,13 @@ pub fn query_with_timeout(
let mut buffer = Buffer::<LittleEndian>::new(&data);
let protocol = buffer.read::<u8>()?;
let protocol_version = buffer.read::<u8>()?;
let name = buffer.read_string::<Utf8Decoder>(None)?;
let map = buffer.read_string::<Utf8Decoder>(None)?;
let active_mod = buffer.read_string::<Utf8Decoder>(None)?;
let game_mode = buffer.read_string::<Utf8Decoder>(None)?;
let description = buffer.read_string::<Utf8Decoder>(None)?;
let version = buffer.read_string::<Utf8Decoder>(None)?;
let game_version = buffer.read_string::<Utf8Decoder>(None)?;
buffer.move_cursor(2)?;
let players_online = buffer.read::<u8>()?;
let players_maximum = buffer.read::<u8>()?;
@ -99,12 +99,12 @@ pub fn query_with_timeout(
let time_left = buffer.read::<u16>()?;
Ok(Response {
protocol,
protocol_version,
name,
active_mod,
game_mode,
game_version,
description,
version,
map,
players_online,
players_maximum,

View file

@ -27,28 +27,24 @@ impl CommonPlayer for Player {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Response {
version: String,
game_version: String,
description: String,
name: String,
has_password: bool,
players: Vec<Player>,
players_maximum: usize,
players_online: usize,
players_maximum: u32,
players_online: u32,
}
impl CommonResponse for Response {
fn as_original(&self) -> GenericResponse { GenericResponse::JC2MP(self) }
fn game_version(&self) -> Option<&str> { Some(&self.version) }
fn game_version(&self) -> Option<&str> { Some(&self.game_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_maximum(&self) -> u32 { self.players_maximum }
fn players_online(&self) -> u32 { self.players_online }
fn players(&self) -> Option<Vec<&dyn crate::protocols::types::CommonPlayer>> {
Some(
@ -113,10 +109,10 @@ pub fn query_with_timeout(
false => reported_players,
}
}
};
} as u32;
Ok(Response {
version: server_vars
game_version: server_vars
.remove("version")
.ok_or(GDErrorKind::PacketBad)?,
description: server_vars

View file

@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct TheShipPlayer {
pub name: String,
pub score: u32,
pub score: i32,
pub duration: f32,
pub deaths: u32,
pub money: u32,
@ -39,24 +39,24 @@ 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) }
fn score(&self) -> Option<i32> { Some(self.score) }
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Response {
pub protocol: u8,
pub protocol_version: u8,
pub name: String,
pub map: String,
pub game: String,
pub players: u8,
pub players_details: Vec<TheShipPlayer>,
pub max_players: u8,
pub bots: u8,
pub game_mode: String,
pub game_version: String,
pub players: Vec<TheShipPlayer>,
pub players_online: u8,
pub players_maximum: u8,
pub players_bots: u8,
pub server_type: Server,
pub has_password: bool,
pub vac_secured: bool,
pub version: String,
pub port: Option<u16>,
pub steam_id: Option<u64>,
pub tv_port: Option<u16>,
@ -73,15 +73,15 @@ impl CommonResponse for Response {
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 game_mode(&self) -> Option<&str> { Some(&self.game_mode) }
fn players_maximum(&self) -> u32 { self.players_maximum.into() }
fn players_online(&self) -> u32 { self.players_online.into() }
fn players_bots(&self) -> Option<u32> { Some(self.players_bots.into()) }
fn has_password(&self) -> Option<bool> { Some(self.has_password) }
fn players(&self) -> Option<Vec<&dyn CommonPlayer>> {
Some(
self.players_details
self.players
.iter()
.map(|p| p as &dyn CommonPlayer)
.collect(),
@ -96,23 +96,23 @@ impl Response {
let the_unwrapped_ship = response.info.the_ship.unwrap();
Self {
protocol: response.info.protocol,
protocol_version: response.info.protocol_version,
name: response.info.name,
map: response.info.map,
game: response.info.game,
players: response.info.players_online,
players_details: response
game_mode: response.info.game_mode,
game_version: response.info.game_version,
players_online: response.info.players_online,
players: response
.players
.unwrap()
.iter()
.map(TheShipPlayer::new_from_valve_player)
.collect(),
max_players: response.info.players_maximum,
bots: response.info.players_bots,
players_maximum: response.info.players_maximum,
players_bots: response.info.players_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,