supports config loading
This commit is contained in:
parent
ec981ccb41
commit
f1880424ab
@ -3,5 +3,5 @@ use poise;
|
|||||||
|
|
||||||
struct BotRunner {
|
struct BotRunner {
|
||||||
client: poise::serenity_prelude::Client,
|
client: poise::serenity_prelude::Client,
|
||||||
server_data: dyn ServerInfo<OnlineResponse = dyn ServerOnlineResponse>,
|
server_data: dyn ServerInfo,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,26 +1,35 @@
|
|||||||
use std::{fs::File, path::Path};
|
use std::{fs::File, path::Path};
|
||||||
use crate::types::{ServerInfo, ServerOnlineResponse};
|
use crate::{handlers, types::ServerInfo};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct ConfigEntry {
|
pub struct ConfigEntry {
|
||||||
handler_type: String,
|
handler_type: String,
|
||||||
address: String,
|
address: Url,
|
||||||
token: String,
|
token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
pub struct Config {
|
||||||
version: String,
|
version: String,
|
||||||
entries: Vec<ConfigEntry>,
|
entries: Vec<ConfigEntry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_configs(path: &Path) -> Result<Config, anyhow::Error> {
|
pub fn parse_configs(path: &Path) -> Result<Config, anyhow::Error> {
|
||||||
let file_handle = File::open(path)?;
|
let file_handle = File::open(path)?;
|
||||||
Ok(serde_json::from_reader::<File, Config>(file_handle)?)
|
Ok(serde_json::from_reader::<File, Config>(file_handle)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_handlers(conf: Config) -> Vec<Box<dyn ServerInfo>> {
|
pub fn build_handlers(conf: Config) -> Vec<Box<dyn ServerInfo>> {
|
||||||
let mut results: Vec<Box<dyn ServerInfo>> = vec!();
|
let mut results: Vec<Box<dyn ServerInfo>> = vec!();
|
||||||
|
for item in conf.entries {
|
||||||
|
match item.handler_type.as_str() {
|
||||||
|
"minecraft" => {
|
||||||
|
results.push(Box::new(handlers::minecraft::Server::new(item.token, item.address)))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
results
|
results
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@ use serde::Deserialize;
|
|||||||
use serde_json;
|
use serde_json;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::types::{PlayerList, ServerInfo, ServerOnlineResponse, ServerResponse};
|
use crate::types::{ServerInfo, ServerOnlineResponse, ServerResponse};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct ApiResponse {
|
struct ApiResponse {
|
||||||
@ -46,12 +46,6 @@ pub struct OnlineResponse {
|
|||||||
players: Vec<String>,
|
players: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlayerList for OnlineResponse {
|
|
||||||
fn players(&self) -> &Vec<String> {
|
|
||||||
&self.players
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
token: String,
|
token: String,
|
||||||
@ -90,12 +84,12 @@ impl ServerInfo for Server {
|
|||||||
players_online: players.online,
|
players_online: players.online,
|
||||||
player_limit: players.max,
|
player_limit: players.max,
|
||||||
version: parsed_data.version.unwrap(),
|
version: parsed_data.version.unwrap(),
|
||||||
players: Some(Box::new(players
|
players: Some(players
|
||||||
.list
|
.list
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|e| e.name.clone())
|
.map(|e| e.name.clone())
|
||||||
.collect())),
|
.collect()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
false => ServerResponse::Offline,
|
false => ServerResponse::Offline,
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@ -4,16 +4,20 @@ mod handlers;
|
|||||||
mod types;
|
mod types;
|
||||||
mod request;
|
mod request;
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
request::request, types::{ServerInfo, ServerResponse}
|
request::request, types::ServerResponse
|
||||||
};
|
};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
|
||||||
|
let config = config_parser::parse_configs(Path::new("config.json")).unwrap();
|
||||||
|
let handlers = config_parser::build_handlers(config);
|
||||||
|
|
||||||
let http_response = request(mc.api_address()).await.unwrap();
|
let http_response = request(handlers[0].api_address()).await.unwrap();
|
||||||
let results = mc.parse(http_response.text().await.unwrap());
|
let results = handlers[0].parse(http_response.text().await.unwrap());
|
||||||
match results {
|
match results {
|
||||||
ServerResponse::Offline => println!("Offline"),
|
ServerResponse::Offline => println!("Offline"),
|
||||||
ServerResponse::Online(online_info) => {
|
ServerResponse::Online(online_info) => {
|
||||||
|
|||||||
@ -15,11 +15,7 @@ pub struct ServerOnlineResponse {
|
|||||||
pub readable_name: String,
|
pub readable_name: String,
|
||||||
pub clean_name: String,
|
pub clean_name: String,
|
||||||
pub reported_name: String,
|
pub reported_name: String,
|
||||||
pub players: Option<Box<Vec<String>>>,
|
pub players: Option<Vec<String>>,
|
||||||
}
|
|
||||||
|
|
||||||
pub trait PlayerList {
|
|
||||||
fn players(&self) -> &Vec<String>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user