Skip to content

Commit

Permalink
feat: initial minetest support
Browse files Browse the repository at this point in the history
  • Loading branch information
CosminPerRam committed Jul 21, 2024
1 parent 41a3d88 commit ed8eceb
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/lib/src/games/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub static GAMES: Map<&'static str, Game> = phf_map! {
"zps" => game!("Zombie Panic: Source", 27015, Protocol::Valve(Engine::new(17_500))),
"moe" => game!("Myth Of Empires", 12888, Protocol::Valve(Engine::new(1_371_580))),
"mordhau" => game!("Mordhau", 27015, Protocol::Valve(Engine::new(629_760))),
// "minetest" => game!("Minetest", 30000, Protocol::PROPRIETARY(ProprietaryProtocol::Minetest)),
"mindustry" => game!("Mindustry", crate::games::mindustry::DEFAULT_PORT, Protocol::PROPRIETARY(ProprietaryProtocol::Mindustry)),
"nla" => game!("Nova-Life: Amboise", 27015, Protocol::Valve(Engine::new(885_570))),
};
8 changes: 8 additions & 0 deletions crates/lib/src/games/minetest/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// The implementation.
/// Reference: [Node-GameGig](https://github.com/gamedig/node-gamedig/blob/master/protocols/minetest.js)
pub mod protocol;
/// All types used by the implementation.
pub mod types;

pub use protocol::*;
pub use types::*;
23 changes: 23 additions & 0 deletions crates/lib/src/games/minetest/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::minetest_master_server::Server;
use crate::{minetest_master_server, GDErrorKind, GDResult, TimeoutSettings};
use std::net::IpAddr;

pub fn query(address: &IpAddr, port: Option<u16>) -> GDResult<Server> { query_with_timeout(address, port, &None) }

pub fn query_with_timeout(
address: &IpAddr,
port: Option<u16>,
timeout_settings: &Option<TimeoutSettings>,
) -> GDResult<Server> {
let address = address.to_string();
let port = port.unwrap_or(30000);

let servers = minetest_master_server::query(timeout_settings.unwrap_or_default())?;
for server in servers.list {
if server.ip == address && server.port == port {
return Ok(server);
}
}

Err(GDErrorKind::AutoQuery.context("Server not found in the master query list."))
}
43 changes: 43 additions & 0 deletions crates/lib/src/games/minetest/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::minetest_master_server::Server;
use crate::protocols::types::{CommonPlayer, CommonResponse, GenericPlayer};
use crate::protocols::GenericResponse;
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Player {
pub name: String,
}

impl CommonPlayer for Player {
fn as_original(&self) -> GenericPlayer { GenericPlayer::Minetest(self) }

fn name(&self) -> &str { &self.name }
}

impl CommonResponse for Server {
fn as_original(&self) -> GenericResponse { GenericResponse::Minetest(self) }

fn name(&self) -> Option<&str> { Some(&self.name) }

fn description(&self) -> Option<&str> { Some(&self.description) }

fn game_version(&self) -> Option<&str> { Some(&self.version) }

fn players_maximum(&self) -> u32 { self.clients_max }

fn players_online(&self) -> u32 { self.clients }

fn has_password(&self) -> Option<bool> { Some(self.password) }

fn players(&self) -> Option<Vec<&dyn CommonPlayer>> {
None
// Some(
// self.clients_list
// .iter()
// .map(|p| Player { name: p.clone() })
// .map(|p| p as &dyn CommonPlayer)
// .collect(),
// )
}
}
6 changes: 6 additions & 0 deletions crates/lib/src/games/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ pub mod quake;
pub mod unreal2;
pub mod valve;

#[cfg(all(feature = "tls", feature = "serde", feature = "services"))]
pub mod minetest;

#[cfg(feature = "tls")]
pub use epic::*;
pub use gamespy::*;
pub use quake::*;
pub use unreal2::*;
pub use valve::*;

#[cfg(all(feature = "tls", feature = "serde", feature = "services"))]
pub use minetest::*;

Check warning on line 21 in crates/lib/src/games/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

ambiguous glob re-exports

warning: ambiguous glob re-exports --> crates/lib/src/games/mod.rs:21:9 | 21 | pub use minetest::*; | ^^^^^^^^^^^ the name `query_with_timeout` in the value namespace is first re-exported here ... 44 | pub use query::*; | -------- but the name `query_with_timeout` in the value namespace is also re-exported here

Check warning on line 21 in crates/lib/src/games/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

ambiguous glob re-exports

warning: ambiguous glob re-exports --> crates/lib/src/games/mod.rs:21:9 | 21 | pub use minetest::*; | ^^^^^^^^^^^ the name `query` in the value namespace is first re-exported here ... 44 | pub use query::*; | -------- but the name `query` in the value namespace is also re-exported here | = note: `#[warn(ambiguous_glob_reexports)]` on by default

Check warning on line 21 in crates/lib/src/games/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

ambiguous glob re-exports

warning: ambiguous glob re-exports --> crates/lib/src/games/mod.rs:21:9 | 21 | pub use minetest::*; | ^^^^^^^^^^^ the name `query_with_timeout` in the value namespace is first re-exported here ... 44 | pub use query::*; | -------- but the name `query_with_timeout` in the value namespace is also re-exported here

Check warning on line 21 in crates/lib/src/games/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

ambiguous glob re-exports

warning: ambiguous glob re-exports --> crates/lib/src/games/mod.rs:21:9 | 21 | pub use minetest::*; | ^^^^^^^^^^^ the name `query` in the value namespace is first re-exported here ... 44 | pub use query::*; | -------- but the name `query` in the value namespace is also re-exported here | = note: `#[warn(ambiguous_glob_reexports)]` on by default

/// Battalion 1944
pub mod battalion1944;
/// Eco
Expand Down
6 changes: 6 additions & 0 deletions crates/lib/src/games/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use std::net::{IpAddr, SocketAddr};

#[cfg(all(feature = "services", feature = "tls", feature = "serde"))]
use crate::games::minetest;
use crate::games::types::Game;
use crate::games::{eco, ffow, jc2m, mindustry, minecraft, savage2, theship};
use crate::protocols;
Expand Down Expand Up @@ -125,6 +127,10 @@ pub fn query_with_timeout_and_extra_settings(
)
.map(Box::new)?
}
#[cfg(all(feature = "services", feature = "tls", feature = "serde"))]
ProprietaryProtocol::Minetest => {
minetest::query_with_timeout(address, port, &timeout_settings).map(Box::new)?
}
}
}
})
Expand Down
16 changes: 16 additions & 0 deletions crates/lib/src/protocols/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum ProprietaryProtocol {
Savage2,
Eco,
Mindustry,
#[cfg(all(feature = "services", feature = "tls", feature = "serde"))]
Minetest,
}

/// Enumeration of all valid protocol types
Expand Down Expand Up @@ -63,6 +65,13 @@ pub enum GenericResponse<'a> {
Savage2(&'a crate::games::savage2::Response),
#[cfg(feature = "games")]
Eco(&'a crate::games::eco::Response),
#[cfg(all(
feature = "services",
feature = "tls",
feature = "serde",
feature = "games"
))]
Minetest(&'a crate::services::minetest_master_server::Server),
}

/// All player types
Expand All @@ -84,6 +93,13 @@ pub enum GenericPlayer<'a> {
JCMP2(&'a crate::games::jc2m::Player),
#[cfg(feature = "games")]
Eco(&'a crate::games::eco::Player),
#[cfg(all(
feature = "services",
feature = "tls",
feature = "serde",
feature = "games"
))]
Minetest(&'a crate::games::minetest::Player),
}

pub trait CommonResponse {
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/services/minetest_master_server/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Server {
pub address: String,
pub clients: u32,
Expand Down

0 comments on commit ed8eceb

Please sign in to comment.