diff --git a/src/bot_runner.rs b/src/bot_runner.rs index fe1205f..2043819 100644 --- a/src/bot_runner.rs +++ b/src/bot_runner.rs @@ -3,5 +3,5 @@ use poise; struct BotRunner { client: poise::serenity_prelude::Client, - server_data: dyn ServerInfo, + server_data: dyn ServerInfo, } diff --git a/src/config_parser.rs b/src/config_parser.rs index f569e69..3d2d89d 100644 --- a/src/config_parser.rs +++ b/src/config_parser.rs @@ -1,26 +1,35 @@ use std::{fs::File, path::Path}; -use crate::types::{ServerInfo, ServerOnlineResponse}; +use crate::{handlers, types::ServerInfo}; use serde::Deserialize; +use url::Url; #[derive(Deserialize)] -struct ConfigEntry { +pub struct ConfigEntry { handler_type: String, - address: String, + address: Url, token: String, } #[derive(Deserialize)] -struct Config { +pub struct Config { version: String, entries: Vec, } -fn parse_configs(path: &Path) -> Result { +pub fn parse_configs(path: &Path) -> Result { let file_handle = File::open(path)?; Ok(serde_json::from_reader::(file_handle)?) } -fn build_handlers(conf: Config) -> Vec> { +pub fn build_handlers(conf: Config) -> Vec> { let mut results: Vec> = 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 } \ No newline at end of file diff --git a/src/handlers/minecraft.rs b/src/handlers/minecraft.rs index 9f239d3..c91e9cb 100644 --- a/src/handlers/minecraft.rs +++ b/src/handlers/minecraft.rs @@ -2,7 +2,7 @@ use serde::Deserialize; use serde_json; use url::Url; -use crate::types::{PlayerList, ServerInfo, ServerOnlineResponse, ServerResponse}; +use crate::types::{ServerInfo, ServerOnlineResponse, ServerResponse}; #[derive(Deserialize, Debug)] struct ApiResponse { @@ -46,12 +46,6 @@ pub struct OnlineResponse { players: Vec, } -impl PlayerList for OnlineResponse { - fn players(&self) -> &Vec { - &self.players - } -} - pub struct Server { #[allow(dead_code)] token: String, @@ -90,12 +84,12 @@ impl ServerInfo for Server { players_online: players.online, player_limit: players.max, version: parsed_data.version.unwrap(), - players: Some(Box::new(players + players: Some(players .list .unwrap_or_default() .iter() .map(|e| e.name.clone()) - .collect())), + .collect()), }) } false => ServerResponse::Offline, diff --git a/src/main.rs b/src/main.rs index 5f78888..9a467c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,16 +4,20 @@ mod handlers; mod types; mod request; +use std::path::Path; + use crate::{ - request::request, types::{ServerInfo, ServerResponse} + request::request, types::ServerResponse }; #[tokio::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 results = mc.parse(http_response.text().await.unwrap()); + let http_response = request(handlers[0].api_address()).await.unwrap(); + let results = handlers[0].parse(http_response.text().await.unwrap()); match results { ServerResponse::Offline => println!("Offline"), ServerResponse::Online(online_info) => { diff --git a/src/types.rs b/src/types.rs index 568ec08..77d960c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -15,11 +15,7 @@ pub struct ServerOnlineResponse { pub readable_name: String, pub clean_name: String, pub reported_name: String, - pub players: Option>>, -} - -pub trait PlayerList { - fn players(&self) -> &Vec; + pub players: Option>, } #[allow(dead_code)]