use std::fmt;
use serde::Serialize;
use crate::models::{ServerStatus, ServerData};
use crate::core::fmt::strip_formatting;
use super::ext::StatusExt;
#[derive(Serialize)]
#[serde(transparent)]
pub struct BedrockServerStatus(pub(crate) ServerStatus);
impl BedrockServerStatus {
pub fn motd(&self) -> &str {
match &self.0.data {
ServerData::Bedrock(b) => &b.motd,
_ => "",
}
}
pub fn motd_clean(&self) -> String {
strip_formatting(self.motd())
}
pub fn motd2(&self) -> &str {
match &self.0.data {
ServerData::Bedrock(b) => &b.motd2,
_ => "",
}
}
pub fn motd2_clean(&self) -> String {
strip_formatting(self.motd2())
}
pub fn edition(&self) -> &str {
match &self.0.data {
ServerData::Bedrock(b) => &b.edition,
_ => "",
}
}
pub fn version(&self) -> &str {
match &self.0.data {
ServerData::Bedrock(b) => &b.version,
_ => "",
}
}
pub fn players_online(&self) -> u32 {
match &self.0.data {
ServerData::Bedrock(b) => b.online_players,
_ => 0,
}
}
pub fn players_max(&self) -> u32 {
match &self.0.data {
ServerData::Bedrock(b) => b.max_players,
_ => 0,
}
}
pub fn game_mode(&self) -> &str {
match &self.0.data {
ServerData::Bedrock(b) => &b.game_mode,
_ => "",
}
}
#[inline]
pub fn is_cached(&self) -> bool {
self.0.cached
}
pub fn raw(&self) -> &ServerStatus {
&self.0
}
}
impl StatusExt for BedrockServerStatus {
fn latency_ms(&self) -> f64 { self.0.latency }
fn ip(&self) -> &str { &self.0.ip }
fn hostname(&self) -> &str { &self.0.hostname }
fn port(&self) -> u16 { self.0.port }
fn is_online(&self) -> bool { self.0.online }
fn display_players(&self) -> String {
format!("{}/{}", self.players_online(), self.players_max())
}
}
impl fmt::Display for BedrockServerStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f, "{} [{}] [{} ms] {}/{} — {}",
self.hostname(),
self.edition(),
self.latency_ms() as u32,
self.players_online(),
self.players_max(),
self.motd_clean(),
)
}
}
impl fmt::Debug for BedrockServerStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BedrockServerStatus")
.field("hostname", &self.hostname())
.field("ip", &self.ip())
.field("port", &self.port())
.field("latency_ms", &self.latency_ms())
.field("cached", &self.is_cached())
.field("edition", &self.edition())
.field("version", &self.version())
.field("players", &self.display_players())
.field("game_mode", &self.game_mode())
.field("motd", &self.motd_clean())
.finish()
}
}