1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use std::path::PathBuf;
use std::collections::{HashMap, HashSet};
use std::time::Duration;

use fehler::throws;

use crate::Error;

/// which pieces of information to subscribe to
///
/// [source](https://developer.valvesoftware.com/wiki/Counter-Strike:_Global_Offensive_Game_State_Integration#List_of_Gamestate_Integrations)
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Subscription {
    /// history of round wins
    MapRoundWins,
    /// mode, map, phase, team scores
    Map,
    /// steamid
    PlayerID,
    /// scoreboard info
    PlayerMatchStats,
    /// armor, flashed, equip_value, health, etc.
    PlayerState,
    /// list of player weapons and weapon state
    PlayerWeapons,
    /// info about the game providing info
    Provider,
    /// round phase and the winning team
    Round,
    /// grenade effecttime, lifetime, owner, position, type, velocity
    AllGrenades,
    /// the steam id of each player
    AllPlayersID,
    /// the scoreboard info for each player
    AllPlayersMatchStats,
    /// player_position but for each player
    AllPlayersPosition,
    /// the player_state for each player
    AllPlayersState,
    /// the player_weapons for each player
    AllPlayersWeapons,
    /// location of the bomb, who's carrying it, dropped or not
    Bomb,
    /// time remaining in tenths of a second, which phase
    PhaseCountdowns,
    /// forward direction, position for currently spectated player
    PlayerPosition,
}

impl Subscription {
    /// The subscriptions available in every context
    pub const UNRESTRICTED: &'static [Subscription] = &[
        Subscription::MapRoundWins,
        Subscription::Map,
        Subscription::PlayerID,
        Subscription::PlayerMatchStats,
        Subscription::PlayerState,
        Subscription::PlayerWeapons,
        Subscription::Provider,
        Subscription::Round,
    ];

    /// The subscriptions only available to spectators (**UNTESTED**)
    pub const SPECTATOR_ONLY: &'static [Subscription] = &[
        Subscription::AllGrenades,
        Subscription::AllPlayersID,
        Subscription::AllPlayersMatchStats,
        Subscription::AllPlayersPosition,
        Subscription::AllPlayersState,
        Subscription::AllPlayersWeapons,
        Subscription::Bomb,
        Subscription::PhaseCountdowns,
        Subscription::PlayerPosition,
    ];
}

impl From<&Subscription> for Subscription {
    fn from(x: &Subscription) -> Self {
        *x
    }
}

/// Builder struct for GSIConfig
#[derive(Clone)]
pub struct GSIConfigBuilder {
    name: String,
    timeout: Option<Duration>,
    buffer: Option<Duration>,
    throttle: Option<Duration>,
    heartbeat: Option<Duration>,
    auth: HashMap<String, String>,
    precision_time: Option<u8>,
    precision_position: Option<u8>,
    precision_vector: Option<u8>,
    subscriptions: HashSet<Subscription>,
}

impl GSIConfigBuilder {
    /// Initialize the builder, with the given service name
    pub fn new<S: Into<String>>(name: S) -> GSIConfigBuilder {
        GSIConfigBuilder {
            name: name.into(),
            timeout: None,
            buffer: None,
            throttle: None,
            heartbeat: None,
            auth: HashMap::new(),
            precision_time: None,
            precision_position: None,
            precision_vector: None,
            subscriptions: HashSet::new()
        }
    }

    /// CS:GO's client timeout for requests (default is 1.1 seconds)
    pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
        self.timeout = Some(timeout);
        self
    }

    /// minimum wait between sending updates (default is 0.1 seconds)
    pub fn buffer(&mut self, buffer: Duration) -> &mut Self {
        self.buffer = Some(buffer);
        self
    }

    /// minimum wait between response to one update and sending the next (default is 1.0 seconds)
    pub fn throttle(&mut self, throttle: Duration) -> &mut Self {
        self.throttle = Some(throttle);
        self
    }

    /// maximum time between updates (default is 60 seconds)
    pub fn heartbeat(&mut self, heartbeat: Duration) -> &mut Self {
        self.heartbeat = Some(heartbeat);
        self
    }

    /// adds an authorization key/value pair (**not currently verified**)
    pub fn auth<S1: Into<String>, S2: Into<String>>(&mut self, key: S1, value: S2) -> &mut Self {
        self.auth.insert(key.into(), value.into());
        self
    }

    /// digits after the decimal point in time values (default is 2)
    pub fn precision_time(&mut self, precision: u8) -> &mut Self {
        self.precision_time = Some(precision);
        self
    }

    /// digits after the decimal point in position values (default is 2)
    pub fn precision_position(&mut self, precision: u8) -> &mut Self {
        self.precision_position = Some(precision);
        self
    }

    /// digits after the decimal point in vector values (default is 2)
    pub fn precision_vector(&mut self, precision: u8) -> &mut Self {
        self.precision_vector = Some(precision);
        self
    }

    /// subscribe to a certain set of update info
    pub fn subscribe(&mut self, subscription: Subscription) -> &mut Self {
        self.subscriptions.insert(subscription);
        self
    }

    /// subscribe to several sets of update info
    pub fn subscribe_multiple<I: IntoIterator<Item=S>, S: Into<Subscription>>(&mut self, subscriptions: I) -> &mut Self {
        self.subscriptions.extend(subscriptions.into_iter().map(|x| x.into()));
        self
    }

    /// create the config object
    pub fn build(&self) -> GSIConfig {
        GSIConfig::from(self)
    }
}

/// Game State Integration configuration
pub struct GSIConfig {
    service_name: String,
    timeout: Duration,
    buffer: Duration,
    throttle: Duration,
    heartbeat: Duration,
    auth: HashMap<String, String>,
    precision_time: u8,
    precision_position: u8,
    precision_vector: u8,
    subscriptions: HashSet<Subscription>,
}

impl From<GSIConfigBuilder> for GSIConfig {
    fn from(builder: GSIConfigBuilder) -> Self {
        GSIConfig {
            service_name: builder.name,
            timeout: builder.timeout.unwrap_or_else(|| Duration::from_secs_f64(1.1)),
            buffer: builder.buffer.unwrap_or_else(|| Duration::from_secs_f64(0.1)),
            throttle: builder.throttle.unwrap_or_else(|| Duration::from_secs_f64(1.0)),
            heartbeat: builder.throttle.unwrap_or_else(|| Duration::from_secs(60)),
            auth: builder.auth,
            precision_time: builder.precision_time.unwrap_or(2),
            precision_position: builder.precision_position.unwrap_or(2),
            precision_vector: builder.precision_vector.unwrap_or(2),
            subscriptions: builder.subscriptions,
        }
    }
}

impl From<&GSIConfigBuilder> for GSIConfig {
    fn from(builder: &GSIConfigBuilder) -> Self {
        Self::from(builder.clone())
    }
}

impl GSIConfig {
    #[throws]
    pub(crate) fn install_into<P: Into<PathBuf>>(&self, cfg_folder: P, port: u16) {
        let mut cfg_path = cfg_folder.into();
        cfg_path.push(&format!("gamestate_integration_{}.cfg", &self.service_name));
        let config = config_file::ConfigFile::new(&self, port);
        let config = vdf_serde::to_string(&config)
            .map_err(|err| Error::ConfigInstallError { description: "failed to serialize config for installation", cause: Some(Box::new(err)) })?;
        ::std::fs::write(cfg_path, config.as_bytes())
            .map_err(|err| Error::ConfigInstallError { description: "failed to write config file", cause: Some(Box::new(err)) })?;
    }
}

mod config_file {
    use std::collections::HashMap;

    use serde::Serialize;
    use crate::config::GSIConfig;

    #[derive(Serialize)]
    struct Precision {
        precision_time: u8,
        precision_position: u8,
        precision_vector: u8,
    }

    #[derive(Serialize)]
    struct Data {
        map_round_wins: bool,
        map: bool,
        player_id: bool,
        player_match_stats: bool,
        player_state: bool,
        player_weapons: bool,
        provider: bool,
        round: bool,

        // Below this line must be spectating or observing
        allgrenades: bool,
        allplayers_id: bool,
        allplayers_match_stats: bool,
        allplayers_position: bool,
        allplayers_state: bool,
        allplayers_weapons: bool,
        bomb: bool,
        phase_countdowns: bool,
        player_position: bool,
    }

    #[derive(Serialize)]
    #[serde(rename = "Main gamestate integration")]
    pub struct ConfigFile {
        uri: String,
        timeout: f64,
        buffer: f64,
        throttle: f64,
        heartbeat: f64,
        auth: HashMap<String, String>,
        output: Precision,
        data: Data,
    }

    impl ConfigFile {
        pub fn new(config: &GSIConfig, port: u16) -> Self {
            use super::Subscription;
            ConfigFile {
                uri: format!("http://127.0.0.1:{}", port),
                timeout: config.timeout.as_secs_f64(),
                buffer: config.buffer.as_secs_f64(),
                throttle: config.throttle.as_secs_f64(),
                heartbeat: config.heartbeat.as_secs_f64(),
                auth: config.auth.clone(),
                output: Precision {
                    precision_time: config.precision_time,
                    precision_position: config.precision_position,
                    precision_vector: config.precision_vector,
                },
                data: Data {
                    map_round_wins: config.subscriptions.contains(&Subscription::MapRoundWins),
                    map: config.subscriptions.contains(&Subscription::Map),
                    player_id: config.subscriptions.contains(&Subscription::PlayerID),
                    player_match_stats: config.subscriptions.contains(&Subscription::PlayerMatchStats),
                    player_state: config.subscriptions.contains(&Subscription::PlayerState),
                    player_weapons: config.subscriptions.contains(&Subscription::PlayerWeapons),
                    provider: config.subscriptions.contains(&Subscription::Provider),
                    round: config.subscriptions.contains(&Subscription::Round),
                    allgrenades: config.subscriptions.contains(&Subscription::AllGrenades),
                    allplayers_id: config.subscriptions.contains(&Subscription::AllPlayersID),
                    allplayers_match_stats: config.subscriptions.contains(&Subscription::AllPlayersMatchStats),
                    allplayers_position: config.subscriptions.contains(&Subscription::AllPlayersPosition),
                    allplayers_state: config.subscriptions.contains(&Subscription::AllPlayersState),
                    allplayers_weapons: config.subscriptions.contains(&Subscription::AllPlayersWeapons),
                    bomb: config.subscriptions.contains(&Subscription::Bomb),
                    phase_countdowns: config.subscriptions.contains(&Subscription::PhaseCountdowns),
                    player_position: config.subscriptions.contains(&Subscription::PlayerPosition),
                },
            }
        }
    }
}