supports config loading
This commit is contained in:
parent
ec981ccb41
commit
f1880424ab
@ -3,5 +3,5 @@ use poise;
|
||||
|
||||
struct BotRunner {
|
||||
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 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<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)?;
|
||||
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!();
|
||||
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
|
||||
}
|
||||
@ -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<String>,
|
||||
}
|
||||
|
||||
impl PlayerList for OnlineResponse {
|
||||
fn players(&self) -> &Vec<String> {
|
||||
&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,
|
||||
|
||||
10
src/main.rs
10
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) => {
|
||||
|
||||
@ -15,11 +15,7 @@ pub struct ServerOnlineResponse {
|
||||
pub readable_name: String,
|
||||
pub clean_name: String,
|
||||
pub reported_name: String,
|
||||
pub players: Option<Box<Vec<String>>>,
|
||||
}
|
||||
|
||||
pub trait PlayerList {
|
||||
fn players(&self) -> &Vec<String>;
|
||||
pub players: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user