diff --git a/crates/lib/src/games/mod.rs b/crates/lib/src/games/mod.rs index 1d255060..c8a9fc3c 100644 --- a/crates/lib/src/games/mod.rs +++ b/crates/lib/src/games/mod.rs @@ -1,5 +1,11 @@ //! Currently supported games. +#[cfg(feature = "serde")] +pub mod palworld; + +#[cfg(feature = "serde")] +pub use palworld::*; + #[cfg(feature = "tls")] pub mod epic; pub mod gamespy; diff --git a/crates/lib/src/games/palworld/mod.rs b/crates/lib/src/games/palworld/mod.rs new file mode 100644 index 00000000..96ab935f --- /dev/null +++ b/crates/lib/src/games/palworld/mod.rs @@ -0,0 +1,2 @@ +mod protocol; +mod types; diff --git a/crates/lib/src/games/palworld/protocol.rs b/crates/lib/src/games/palworld/protocol.rs new file mode 100644 index 00000000..1793a592 --- /dev/null +++ b/crates/lib/src/games/palworld/protocol.rs @@ -0,0 +1,39 @@ +use crate::http::HttpClient; +use crate::palworld::types::{Endpoint, Response}; +use crate::GDErrorKind::SocketConnect; +use crate::{GDResult, TimeoutSettings}; +use base64::prelude::BASE64_STANDARD; +use base64::Engine; +use serde_json::Value; +use std::net::IpAddr; +use url::Url; + +fn make_call(client: &mut HttpClient, endpoint: Endpoint) -> GDResult { + client.get_json(endpoint.into(), Default::default()) +} + +pub fn query( + address: &IpAddr, + port: Option, + username: String, + password: String, + timeout_settings: TimeoutSettings, +) -> GDResult { + let url = Url::parse(&format!("{address}:{}", port.unwrap_or(8212))).map_err(|e| SocketConnect.context(e))?; + + let auth_format = format!("{}:{}", username, password); + let auth_base = BASE64_STANDARD.encode(auth_format); + let auth = format!("Basic {}", auth_base.as_str()); + let authorization = auth.as_str(); + let headers = [ + ("Authorization", authorization), + ("Accept", "application/json"), + ]; + + let mut client = HttpClient::from_url(url, &Some(timeout_settings), Some((&headers).to_vec()))?; + + let info = make_call(&mut client, Endpoint::Info)?; + println!("{info:#?}"); + + Ok(Response {}) +} diff --git a/crates/lib/src/games/palworld/types.rs b/crates/lib/src/games/palworld/types.rs new file mode 100644 index 00000000..c41736c2 --- /dev/null +++ b/crates/lib/src/games/palworld/types.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Response {} + +pub enum Endpoint { + Info, + Players, + Settings, + Metrics, +} + +impl<'a> Into<&'a str> for Endpoint { + fn into(self) -> &'a str { + match self { + Endpoint::Info => "info", + Endpoint::Players => "players", + Endpoint::Settings => "settings", + Endpoint::Metrics => "metrics", + } + } +}