Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/lib/src/games/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 2 additions & 0 deletions crates/lib/src/games/palworld/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod protocol;
mod types;
39 changes: 39 additions & 0 deletions crates/lib/src/games/palworld/protocol.rs
Original file line number Diff line number Diff line change
@@ -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<Value> {
client.get_json(endpoint.into(), Default::default())
}

pub fn query(
address: &IpAddr,
port: Option<u16>,
username: String,
password: String,
timeout_settings: TimeoutSettings,
) -> GDResult<Response> {
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 {})
}
22 changes: 22 additions & 0 deletions crates/lib/src/games/palworld/types.rs
Original file line number Diff line number Diff line change
@@ -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",
}
}
}