Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tukeJonny committed Feb 4, 2024
1 parent 16e1862 commit f0bcac1
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 32 deletions.
203 changes: 201 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ thiserror = "1.0"
anyhow = "1.0"
bytes = "1"
num-traits = "0.2"
num-derive = "0.4"
num-derive = "0.4"

[dev-dependencies]
clap = { version = "3", features = ["derive"] }
50 changes: 41 additions & 9 deletions examples/palworld.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
//! Palworld client example.
//!
//! # Usage Examples
//!
//! ```bash
//! $ cargo run --example palworld -- --help
//! $ cargo run --example palworld -- -a 127.0.0.1:25575 -p passwrd -c "Info"
//! ```
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;

use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Parser;

use rcon::client::RconClient;

#[derive(Debug, Parser)]
#[clap(name = "palworld")]
struct Args {
#[clap(long, short = 'a', default_value = "127.0.0.1:25575")]
remote_address: String,

#[clap(long, short = 'p', default_value = "password here")]
rcon_password: String,

#[clap(long, short = 'c', default_value = "Info")]
command: String,
}

#[derive(Debug)]
pub struct PalworldClient {
stream: TcpStream,
Expand All @@ -30,14 +52,24 @@ impl RconClient for PalworldClient {
}
}

fn main() {
let mut client =
PalworldClient::new("127.0.0.1:25575".to_string()).expect("failed to initiate client");
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();

client.auth("<password here>").expect("failed to auth");
let mut client = PalworldClient::new(args.remote_address)?;
client.auth(args.rcon_password.as_ref())?;

let result = client
.execute_command("Info")
.expect("failed to execute command");
println!("Result:\n{:?}", result);
// NOTE: Teleport commands is not supported. because these commands often used in case of playing.
if let Some(command) = args.command.split_whitespace().next() {
match command {
"Shutdown" | "DoExit" | "Broadcast" | "KickPlayer" | "BanPlayer" | "ShowPlayers"
| "Info" | "Save" => {
let result = client.execute_command(args.command.as_ref())?;
println!("{}", result);
Ok(())
}
_ => Err(anyhow!("unsupported command has specified: {}", args.command).into()),
}
} else {
Err(anyhow!("invalid command string: {}", args.command).into())
}
}
Loading

0 comments on commit f0bcac1

Please sign in to comment.