mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-05-06 15:27:28 +00:00
* Implement generic response as enum * First draft of implementing into_common() * Make common response type generic * Use macros and generics to reduce repetition * [Games] Add dynamically dispatched CommonResponse trait This adds two traits: "CommonResponse", and "CommonPlayer", when the generic game query function returns a response it returns a pointer to its original response type that implements "CommonResponse". Both common traits require that "as_original()" be implemented, this returns an enum containing a pointer to the original type. Both traits have a concrete method "as_json()" that returns a struct containing data fetched from all of its methods as. This struct implements serde and can hence be serialized as required. The traits require a few other methods be implemented, those being the fields that are common across all types. All other methods have a default None implementation so that each response type only needs to implement methods for fields that it has. * [Game] Implement common traits for JCMP2 response * [Fmt] Run cargo fmt * Fix doctest failing * Run cargo fmt
63 lines
1.5 KiB
Rust
63 lines
1.5 KiB
Rust
use gamedig::{protocols::types::CommonResponse, query, GDResult, GAMES};
|
|
|
|
use std::net::IpAddr;
|
|
|
|
fn generic_query(game_name: &str, addr: &IpAddr, port: Option<u16>) -> GDResult<Box<dyn CommonResponse>> {
|
|
let game = GAMES.get(game_name).expect("Game doesn't exist");
|
|
|
|
println!("Querying {:#?} with game {:#?}.", addr, game);
|
|
|
|
let response = query(game, addr, port)?;
|
|
println!("Response: {:#?}", response.as_json());
|
|
|
|
let common = response.as_original();
|
|
println!("Common response: {:#?}", common);
|
|
|
|
Ok(response)
|
|
}
|
|
|
|
fn main() {
|
|
let mut args = std::env::args().skip(1);
|
|
|
|
let game_name = args.next().expect("Must provide a game name");
|
|
let addr: IpAddr = args
|
|
.next()
|
|
.map(|s| s.parse().unwrap())
|
|
.expect("Must provide address");
|
|
let port: Option<u16> = args.next().map(|s| s.parse().unwrap());
|
|
|
|
generic_query(&game_name, &addr, port).unwrap();
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use gamedig::GAMES;
|
|
use std::net::{IpAddr, Ipv4Addr};
|
|
|
|
use super::generic_query;
|
|
|
|
const ADDR: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
|
|
|
|
fn test_game(game_name: &str) {
|
|
assert!(generic_query(game_name, &ADDR, None).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn battlefield() { test_game("bf1942"); }
|
|
|
|
#[test]
|
|
fn minecraft() { test_game("mc"); }
|
|
|
|
#[test]
|
|
fn tf2() { test_game("tf2"); }
|
|
|
|
#[test]
|
|
fn quake() { test_game("quake3a"); }
|
|
|
|
#[test]
|
|
fn all_games() {
|
|
for game_name in GAMES.keys() {
|
|
test_game(game_name);
|
|
}
|
|
}
|
|
}
|