twgame_core/
config.rs

1use std::collections::HashMap;
2
3/// config which can be set with explicit values
4/// or if not specified returns the default value of the given version
5/// can return the certainty of a value
6pub struct Config {
7    /// time in seconds `kill` NetMsg's (and ddrace_commands) are ignored after issuing a successful kill
8    sv_kill_delay: i32,
9
10    /// time in minutes in race until the kill NetMsg and Joining Spectators is ignored and typing
11    /// `/kill` into the chat is required
12    sv_kill_protection: i32,
13}
14
15impl Config {
16    pub fn sv_kill_delay(&self) -> i32 {
17        self.sv_kill_delay
18    }
19    pub fn sv_kill_protection(&self) -> i32 {
20        self.sv_kill_protection
21    }
22
23    fn set_sv_kill_delay(&mut self, value: &str) {
24        if let Ok(value) = value.parse() {
25            if (0..=9999).contains(&value) {
26                self.sv_kill_delay = value;
27            }
28        }
29    }
30    fn set_sv_kill_protection(&mut self, value: &str) {
31        if let Ok(value) = value.parse() {
32            if (0..=9999).contains(&value) {
33                self.sv_kill_protection = value;
34            }
35        }
36    }
37}
38
39impl Config {
40    pub fn new(_version: &str) -> Config {
41        Config {
42            sv_kill_delay: 1,
43            sv_kill_protection: 20,
44        }
45    }
46
47    pub fn apply_config(&mut self, config: &HashMap<String, String>) {
48        for (name, value) in config {
49            match &name[..] {
50                "sv_kill_delay" => self.set_sv_kill_delay(value),
51                "sv_kill_protection" => self.set_sv_kill_protection(value),
52                _ => todo!(),
53            }
54        }
55    }
56}