screeps_rust_api/
config.rs

1/// Screeps 配置
2#[derive(Debug)]
3pub struct ScreepsConfig {
4    /// 游戏 token
5    pub token: Option<String>,
6    /// 游戏绑定的邮箱
7    pub email: Option<String>,
8    /// 游戏密码
9    pub password: Option<String>,
10    /// 游戏主机地址:域名+端口
11    pub host: String,
12    /// 是否使用 https
13    pub secure: bool,
14    /// 请求超时时间
15    pub timeout: u64,
16}
17
18impl ScreepsConfig {
19    pub fn new(
20        token: Option<String>,
21        email: Option<String>,
22        password: Option<String>,
23        host: String,
24        secure: bool,
25        timeout: u64,
26    ) -> Self {
27        Self {
28            token,
29            email,
30            password,
31            host,
32            secure,
33            timeout,
34        }
35    }
36
37    /// 设置 token
38    pub fn with_token(&mut self, token: String) {
39        self.token = Some(token);
40    }
41
42    /// 设置邮箱
43    pub fn with_email(&mut self, email: String) {
44        self.email = Some(email);
45    }
46
47    /// 设置密码
48    pub fn with_password(&mut self, password: String) {
49        self.password = Some(password);
50    }
51
52    /// 设置游戏主机地址
53    pub fn with_host(&mut self, host: String) {
54        self.host = host;
55    }
56
57    /// 设置是否启用 https
58    pub fn with_secure(&mut self, secure: bool) {
59        self.secure = secure;
60    }
61
62    /// 设置请求超时
63    pub fn with_timeout(&mut self, timeout: u64) {
64        self.timeout = timeout;
65    }
66
67    /// 构造游戏服务器请求 url 前缀 url
68    pub fn build_base_url(&self) -> String {
69        let protocol = if self.secure { "https" } else { "http" };
70        format!("{}://{}/api", protocol, self.host)
71    }
72}
73
74impl Default for ScreepsConfig {
75    fn default() -> Self {
76        Self {
77            token: None,
78            email: None,
79            password: None,
80            host: "screeps.com".to_string(),
81            secure: true,
82            timeout: 15,
83        }
84    }
85}