94 lines
2.0 KiB
Rust
94 lines
2.0 KiB
Rust
use config::{Config, File};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::path::Path;
|
|
use std::collections::HashMap;
|
|
use serenity::prelude::TypeMapKey;
|
|
use std::sync::Arc;
|
|
use std::path::PathBuf;
|
|
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 occupation: Vec<String>,
|
|
pub adjective: Vec<String>
|
|
}
|
|
|
|
impl BotConfig {
|
|
pub fn new(config_path: &Path) -> Result<Self, config::ConfigError> {
|
|
let cfg = Config::builder()
|
|
.add_source(File::from(config_path))
|
|
.build()?;
|
|
|
|
cfg.try_deserialize()
|
|
}
|
|
}
|
|
|
|
#[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 discord_id: u64,
|
|
pub codename: String,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
|
pub struct GameState {
|
|
pub player_channels: HashMap<u64, PlayerData>,
|
|
pub current_phase: Phase,
|
|
}
|
|
|
|
impl GameState {
|
|
pub fn codename_exists(&self, codename: &str) -> bool {
|
|
self.player_channels.iter().any(|(_, data)| {
|
|
data.codename.to_lowercase() == codename
|
|
})
|
|
}
|
|
|
|
pub fn clear(&mut self) {
|
|
self.player_channels.clear();
|
|
self.current_phase = Phase::Night;
|
|
}
|
|
}
|
|
|
|
#[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<Mutex<GlobalData>>;
|
|
}
|
|
|