Skip to main content

source2_demo/event/
definition.rs

1use crate::HashMap;
2use std::rc::Rc;
3
4/// Definition for a game event type declared by the replay.
5pub struct GameEventDefinition {
6    pub(crate) name: String,
7    pub(crate) keys: Vec<Rc<GameEventKey>>,
8    pub(crate) name_to_key: HashMap<String, Rc<GameEventKey>>,
9}
10
11impl GameEventDefinition {
12    /// Returns the event definition name.
13    pub fn name(&self) -> &str {
14        &self.name
15    }
16
17    /// Returns the ordered key definitions for this event.
18    pub fn keys(&self) -> impl Iterator<Item = &GameEventKey> {
19        self.keys.iter().map(|key| key.as_ref())
20    }
21}
22
23#[derive(Debug)]
24/// Definition for a single field in a game event.
25pub struct GameEventKey {
26    pub(crate) id: i32,
27    pub(crate) name: String,
28    pub(crate) type_id: i32,
29}
30
31impl GameEventKey {
32    /// Returns the zero-based key index.
33    pub fn id(&self) -> i32 {
34        self.id
35    }
36
37    /// Returns the key name.
38    pub fn name(&self) -> &str {
39        &self.name
40    }
41
42    /// Returns the raw game event key type ID from the replay descriptor.
43    pub fn type_id(&self) -> i32 {
44        self.type_id
45    }
46
47    /// Returns a human-readable game event key type name.
48    pub fn type_name(&self) -> &'static str {
49        match self.type_id {
50            1 => "string",
51            2 => "float",
52            3 => "long",
53            4 => "short",
54            5 => "byte",
55            6 => "bool",
56            7 => "uint64",
57            8 => "long",
58            9 => "short",
59            _ => "unknown",
60        }
61    }
62}