    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

    use csflow::{memflow::Address, enums::PlayerType};

    #[derive(Clone, Copy)]
    pub enum CachedEntity {
        Bomb {ptr: Address},
        Player {ptr: Address, player_type: PlayerType},
    }

    pub struct Cache {
        timestamp: std::time::Instant,
        entity_cache: Vec<CachedEntity>,
        map_name: String,
        entity_list: Address,
    }

    impl Cache {
        pub fn is_valid(&self) -> bool {
            if self.timestamp.elapsed() > std::time::Duration::from_millis(250) {
                return false;
            }

            true
        }

        pub fn new_invalid() -> Cache {
            Cache {
                timestamp: std::time::Instant::now().checked_sub(std::time::Duration::from_millis(500)).unwrap(),
                entity_cache: Vec::new(),
                map_name: String::new(),
                entity_list: Address::null(),
            }
        }

        pub fn entity_cache(&mut self) -> Vec<CachedEntity> {
            self.entity_cache.clone()
        }

        pub fn map_name(&self) -> String {
            self.map_name.clone()
        }

        pub fn entity_list(&self) -> Address {
            self.entity_list
        }
    }

    pub struct CacheBuilder {
        entity_cache: Option<Vec<CachedEntity>>,
        map_name: Option<String>,
        entity_list: Option<Address>
    }

    impl CacheBuilder {
        pub fn new() -> CacheBuilder {
            CacheBuilder {
                entity_cache: None,
                map_name: None,
                entity_list: None,
            }
        }

        pub fn entity_cache(mut self, entity_cache: Vec<CachedEntity>) -> CacheBuilder {
            self.entity_cache = Some(entity_cache);
            self
        }

        pub fn map_name(mut self, map_name: String) -> CacheBuilder {
            self.map_name = Some(map_name);
            self
        }

        pub fn entity_list(mut self, entity_list: Address) -> CacheBuilder {
            self.entity_list = Some(entity_list);
            self
        }

        pub fn build(self) -> anyhow::Result<Cache> {
            Ok(Cache {
                timestamp: std::time::Instant::now(),
                entity_cache: self.entity_cache.ok_or(anyhow::anyhow!("entity_cache not set on builder"))?,
                map_name: self.map_name.ok_or(anyhow::anyhow!("map_name not set on builder"))?,
                entity_list: self.entity_list.ok_or(anyhow::anyhow!("entity_list not set on builder"))?,
            })
        }
    }