Skip to content

Commit

Permalink
feat: minetest support (#218)
Browse files Browse the repository at this point in the history
* feat: initial minetest support

* move player to master, remove comments

* add markdown lines

* oops, change player namespace

* fix some edge cases

* add entry to responses, tweak field names
  • Loading branch information
CosminPerRam authored Oct 30, 2024
1 parent 30ae60e commit bcc92d1
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 59 deletions.
1 change: 1 addition & 0 deletions GAMES.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ requirements/information.
| Nova-Life: Amboise | NLA | Valve | |
| Abiotic Factor | ABIOTICFACTOR | Valve | |
| Soulmask | SOULMASK | Valve | |
| Minetest | MINETEST | Proprietary | Available on the 'tls', 'serde' and 'services' feature |

## Planned to add support:

Expand Down
124 changes: 70 additions & 54 deletions RESPONSES.md

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion crates/lib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ Who knows what the future holds...

# 0.X.Y - DD/MM/YYYY

To be made...
Games:

- [Minetest](https://www.minetest.net/) support (available on the `tls`, `serde` and `services` features) (#218 by
@CosminPerRam).

# 0.5.2 - 20/10/2024

Expand Down
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::Response;
use crate::{minetest_master_server, GDErrorKind, GDResult, TimeoutSettings};
use std::net::IpAddr;

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

pub fn query_with_timeout(
address: &IpAddr,
port: Option<u16>,
timeout_settings: &Option<TimeoutSettings>,
) -> GDResult<Response> {
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.into());
}
}

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

#[derive(Serialize, Deserialize, 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 }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Response {
pub name: String,
pub description: String,
pub game_version: String,
pub players_maximum: u32,
pub players_online: u32,
pub has_password: Option<bool>,
pub players: Vec<Player>,
pub id: String,
pub ip: String,
pub port: u16,
pub creative: Option<bool>,
pub damage: bool,
pub game_time: u32,
pub lag: Option<f32>,
pub proto_max: u16,
pub proto_min: u16,
pub pvp: bool,
pub uptime: u32,
pub url: Option<String>,
pub update_time: u32,
pub start: u32,
pub clients_top: u32,
pub updates: u32,
pub pop_v: f32,
pub geo_continent: Option<String>,
pub ping: f32,
}

impl From<Server> for Response {
fn from(server: Server) -> Self {
Self {
name: server.name,
description: server.description,
game_version: server.version,
players_maximum: server.clients_max,
players_online: server.total_clients,
has_password: server.password,
players: server
.clients_list
.unwrap_or_default()
.into_iter()
.map(|name| Player { name })
.collect(),
ip: server.address,
creative: server.creative,
damage: server.damage,
game_time: server.game_time,
id: server.gameid,
lag: server.lag,
port: server.port,
proto_max: server.proto_max,
proto_min: server.proto_min,
pvp: server.pvp,
uptime: server.uptime,
url: server.url,
update_time: server.update_time,
start: server.start,
clients_top: server.clients_top,
updates: server.updates,
pop_v: server.pop_v,
geo_continent: server.geo_continent,
ping: server.ping,
}
}
}

impl CommonResponse for Response {
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.game_version) }

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

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

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

fn players(&self) -> Option<Vec<&dyn CommonPlayer>> {
Some(
self.players
.iter()
.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::games::minetest::Response),
}

/// 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
8 changes: 4 additions & 4 deletions crates/lib/src/services/minetest_master_server/types.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Server {
pub address: String,
pub clients: u32,
pub clients_list: Vec<String>,
pub clients_list: Option<Vec<String>>,
pub clients_max: u32,
pub creative: bool,
pub creative: Option<bool>,
pub damage: bool,
pub description: String,
pub game_time: u32,
pub gameid: String,
pub lag: Option<f32>,
pub name: String,
pub password: bool,
pub password: Option<bool>,
pub port: u16,
pub proto_max: u16,
pub proto_min: u16,
Expand Down

0 comments on commit bcc92d1

Please sign in to comment.