mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
Merge branch 'main' into feat/cli
This commit is contained in:
commit
963040fb84
28 changed files with 1071 additions and 348 deletions
|
|
@ -2,23 +2,21 @@ use gamedig::{
|
|||
protocols::types::{CommonResponse, ExtraRequestSettings, TimeoutSettings},
|
||||
query_with_timeout_and_extra_settings,
|
||||
GDResult,
|
||||
Game,
|
||||
GAMES,
|
||||
};
|
||||
|
||||
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
|
||||
|
||||
/// Make a query given the name of a game
|
||||
/// The `game` argument is taken from the [GAMES](gamedig::GAMES) map.
|
||||
fn generic_query(
|
||||
game_name: &str,
|
||||
game: &Game,
|
||||
addr: &IpAddr,
|
||||
port: Option<u16>,
|
||||
timeout_settings: Option<TimeoutSettings>,
|
||||
extra_settings: Option<ExtraRequestSettings>,
|
||||
) -> GDResult<Box<dyn CommonResponse>> {
|
||||
let game = GAMES
|
||||
.get(game_name)
|
||||
.expect("Game doesn't exist, run without arguments to see a list of games");
|
||||
|
||||
println!("Querying {:#?} with game {:#?}.", addr, game);
|
||||
|
||||
let response = query_with_timeout_and_extra_settings(game, addr, port, timeout_settings, extra_settings)?;
|
||||
|
|
@ -51,14 +49,18 @@ fn main() {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let extra_settings = ExtraRequestSettings::default()
|
||||
let game = GAMES
|
||||
.get(&game_name)
|
||||
.expect("Game doesn't exist, run without arguments to see a list of games");
|
||||
|
||||
let extra_settings = game
|
||||
.request_settings
|
||||
.clone()
|
||||
.set_hostname(hostname.to_string())
|
||||
.set_gather_rules(true)
|
||||
.set_gather_players(true)
|
||||
.set_check_app_id(false);
|
||||
|
||||
generic_query(
|
||||
&game_name,
|
||||
game,
|
||||
&addr.ip(),
|
||||
port,
|
||||
Some(timeout_settings),
|
||||
|
|
@ -67,8 +69,7 @@ fn main() {
|
|||
.unwrap();
|
||||
} else {
|
||||
// Without arguments print a list of games
|
||||
|
||||
for (name, game) in gamedig::games::GAMES.entries() {
|
||||
for (name, game) in GAMES.entries() {
|
||||
println!("{}\t{}", name, game.name);
|
||||
}
|
||||
}
|
||||
|
|
@ -95,7 +96,12 @@ mod test {
|
|||
)
|
||||
.unwrap(),
|
||||
);
|
||||
assert!(generic_query(game_name, &ADDR, None, timeout_settings, None).is_err());
|
||||
|
||||
let game = GAMES
|
||||
.get(game_name)
|
||||
.expect("Game doesn't exist, run without arguments to see a list of games");
|
||||
|
||||
assert!(generic_query(game, &ADDR, None, timeout_settings, None).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -108,7 +114,7 @@ mod test {
|
|||
fn teamfortress2() { test_game("teamfortress2"); }
|
||||
|
||||
#[test]
|
||||
fn quake() { test_game("quake3"); }
|
||||
fn quake2() { test_game("quake2"); }
|
||||
|
||||
#[test]
|
||||
fn all_games() {
|
||||
|
|
|
|||
28
crates/lib/examples/valve_protocol_query.rs
Normal file
28
crates/lib/examples/valve_protocol_query.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use gamedig::protocols::types::TimeoutSettings;
|
||||
use gamedig::protocols::valve;
|
||||
use gamedig::protocols::valve::{Engine, GatheringSettings};
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
let address = &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 27015);
|
||||
let engine = Engine::Source(None); // We don't specify a steam app id, let the query try to find it.
|
||||
let gather_settings = GatheringSettings {
|
||||
players: true, // We want to query for players
|
||||
rules: false, // We don't want to query for rules
|
||||
check_app_id: false, // Loosen up the query a bit by not checking app id
|
||||
};
|
||||
|
||||
let read_timeout = Duration::from_secs(2);
|
||||
let write_timeout = Duration::from_secs(3);
|
||||
let retries = 1; // does another request if the first one fails.
|
||||
let timeout_settings = TimeoutSettings::new(Some(read_timeout), Some(write_timeout), retries).unwrap();
|
||||
|
||||
let response = valve::query(
|
||||
address,
|
||||
engine,
|
||||
Some(gather_settings),
|
||||
Some(timeout_settings),
|
||||
);
|
||||
println!("{response:#?}");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue