Documentation
use anyhow::Result;
use serde::Deserialize;

/// 游戏配置
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
    /// 游戏基本设置
    pub game: GameBaseSetting,
    /// 数据库配置
    pub master_db: SingleDB,
    /// redis 链接配置
    pub redis: RedisConfig,
    /// clickhouse 配置
    pub clickhouse: ClickHouseConfig,
    /// 回放服务器配置
    pub clickhouse_replay: ClickHouseConfig,
}

impl Config {
    ///加载
    pub fn load_config(content: &str) -> Result<Self> {
        Ok(toml::from_str(content)?)
    }
}

/// 基本配置
#[derive(Deserialize, Debug, Clone)]
pub struct GameBaseSetting {
    /// 跑马灯延迟多少秒发送
    pub marquess_delay_time: u64,
    /// 机器人开始游戏最少玩多久
    pub robot_seating_min_time: u32,
    /// 机器人开始游戏最大玩多久
    pub robot_seating_max_time: u32,
    /// 机器人离开后闲置最小时间
    pub robot_leave_min_time: u32,
    /// 机器人离开后闲置最大时间
    pub robot_leave_max_time: u32,
    /// 机器人多久旋转一次最小时间(加一次彩金)
    pub robot_spin_min_time: u32,
    /// 机器人多久旋转一次最大时间(加一次彩金)
    pub robot_spin_max_time: u32,
    /// 彩金控制模式加载时间(秒)
    #[serde(default = "default_lottery_control_flush_time")]
    pub lottery_control_flush_time: i32,
}

/// 默认彩金控制值
fn default_lottery_control_flush_time() -> i32 {
    30
}

/// 主库
#[derive(Deserialize, Debug, Clone)]
pub struct SingleDB {
    /// 数据库链接串
    pub mysql_url: String,
    /// 最大链接数
    pub mysql_max_connections: u32,
}

/// redis 配置
#[derive(Deserialize, Debug, Clone)]
pub struct RedisConfig {
    /// redis url
    pub redis_url: String,
    /// index
    pub redis_index: Option<i32>,
}

/// clickhouse 配置
#[derive(Deserialize, Debug, Clone)]
pub struct ClickHouseConfig {
    /// url
    pub url: String,
    /// 用户名
    pub username: String,
    /// 密码
    pub password: String,
    /// 数据库
    pub database: String,
}