mirror of
https://github.com/Tribufu/rust-gamedig
synced 2026-08-19 03:41:05 +00:00
refator: copy cli into mono
This commit is contained in:
parent
66ae3c296e
commit
80f6b87991
63 changed files with 244 additions and 34 deletions
87
crates/lib/src/protocols/quake/one.rs
Normal file
87
crates/lib/src/protocols/quake/one.rs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
use crate::protocols::quake::client::{client_query, remove_wrapping_quotes, QuakeClient};
|
||||
use crate::protocols::quake::Response;
|
||||
use crate::protocols::types::{CommonPlayer, GenericPlayer, TimeoutSettings};
|
||||
use crate::GDErrorKind::TypeParse;
|
||||
use crate::{GDErrorKind, GDResult};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::net::SocketAddr;
|
||||
use std::slice::Iter;
|
||||
|
||||
use super::QuakePlayerType;
|
||||
|
||||
/// Quake 1 player data.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Player {
|
||||
/// Player's server id.
|
||||
pub id: u8,
|
||||
pub score: u16,
|
||||
pub time: u16,
|
||||
pub ping: u16,
|
||||
pub name: String,
|
||||
pub skin: String,
|
||||
pub color_primary: u8,
|
||||
pub color_secondary: u8,
|
||||
}
|
||||
|
||||
impl QuakePlayerType for Player {
|
||||
fn version(response: &Response<Self>) -> super::VersionedResponse { super::VersionedResponse::One(response) }
|
||||
}
|
||||
|
||||
impl CommonPlayer for Player {
|
||||
fn as_original(&self) -> GenericPlayer { GenericPlayer::QuakeOne(self) }
|
||||
|
||||
fn name(&self) -> &str { &self.name }
|
||||
fn score(&self) -> Option<i32> { Some(self.score.into()) }
|
||||
}
|
||||
|
||||
pub(crate) struct QuakeOne;
|
||||
impl QuakeClient for QuakeOne {
|
||||
type Player = Player;
|
||||
|
||||
fn get_send_header<'a>() -> &'a str { "status" }
|
||||
|
||||
fn get_response_header<'a>() -> &'a str { "n" }
|
||||
|
||||
fn parse_player_string(mut data: Iter<&str>) -> GDResult<Self::Player> {
|
||||
Ok(Player {
|
||||
id: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => v.parse().map_err(|e| TypeParse.context(e))?,
|
||||
},
|
||||
score: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => v.parse().map_err(|e| TypeParse.context(e))?,
|
||||
},
|
||||
time: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => v.parse().map_err(|e| TypeParse.context(e))?,
|
||||
},
|
||||
ping: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => v.parse().map_err(|e| TypeParse.context(e))?,
|
||||
},
|
||||
name: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => remove_wrapping_quotes(v).to_string(),
|
||||
},
|
||||
skin: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => remove_wrapping_quotes(v).to_string(),
|
||||
},
|
||||
color_primary: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => v.parse().map_err(|e| TypeParse.context(e))?,
|
||||
},
|
||||
color_secondary: match data.next() {
|
||||
None => Err(GDErrorKind::PacketBad)?,
|
||||
Some(v) => v.parse().map_err(|e| TypeParse.context(e))?,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn query(address: &SocketAddr, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response<Player>> {
|
||||
client_query::<QuakeOne>(address, timeout_settings)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue