use config::{Config, File}; use serde::{Deserialize, Serialize}; use serenity::prelude::TypeMapKey; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; use structopt::StructOpt; use tokio::sync::Mutex; #[derive(Debug, StructOpt)] #[structopt(name = "WOXlf", about = "WOXlf discord bot")] pub struct Args { pub cfg_path: PathBuf, } #[derive(Debug, Deserialize, Serialize, Clone)] pub struct BotConfig { pub token: String, pub app_id: u64, pub host_channel: u64, pub category: u64, pub game_state_dir: PathBuf, pub occupation: Vec, pub adjective: Vec, } impl BotConfig { pub fn new(config_path: &Path) -> Result { let cfg = Config::builder() .add_source(File::from(config_path)) .build()?; cfg.try_deserialize() } pub fn get_game_state_path(&self) -> PathBuf { self.game_state_dir.join("wOxlf_data.toml") } } #[derive(Debug, Deserialize, Serialize, Clone)] pub enum Phase { Day, Night, } impl Default for Phase { fn default() -> Self { Self::Night } } #[derive(Debug, Deserialize, Serialize, Clone, Default, Hash)] pub struct PlayerData { pub channel: u64, pub discord_id: u64, pub codename: String, } #[derive(Debug, Deserialize, Serialize, Clone, Default)] pub struct GameState { pub current_phase: Phase, pub player_data: Vec, } impl GameState { pub fn codename_exists(&self, codename: &str) -> bool { self.player_data .iter() .any(|data| data.codename.to_lowercase() == codename) } pub fn clear(&mut self) { self.player_data.clear(); self.current_phase = Phase::Night; } pub fn get_player_from_channel(&self, channel_id: u64) -> Option<&PlayerData> { self.player_data.iter().find(|p| p.channel == channel_id) } } #[derive(Debug, Deserialize, Serialize, Clone)] pub struct GlobalData { pub cfg: BotConfig, pub game_state: GameState, } impl GlobalData { pub fn new(cfg: BotConfig) -> Self { Self { cfg, game_state: GameState::default(), } } } impl TypeMapKey for GlobalData { type Value = Arc>; } #[derive(Debug, Deserialize, Serialize, Clone)] pub enum MessageSource { Player(u64), Host, Automated, }