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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use super::{Entry, Player, Tracker};
use arcdps::{Profession, Specialization};
use std::ops;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Player tracker with caching.
#[derive(Debug, Clone)]
pub struct CachedTracker<T> {
    /// Inner tracker.
    tracker: Tracker<T>,

    /// Cache for data.
    cache: Vec<(CachedPlayer, T)>,

    /// How to cache for self.
    pub self_policy: CachePolicy,

    /// How to cache for other players.
    pub others_policy: CachePolicy,
}

impl<T> CachedTracker<T> {
    /// Creates a new cached tracker with the given policies.
    pub const fn new(self_policy: CachePolicy, others_policy: CachePolicy) -> Self {
        Self {
            tracker: Tracker::new(),
            cache: Vec::new(),
            self_policy,
            others_policy,
        }
    }

    /// Creates a new tracker caching data for own characters.
    pub const fn for_self() -> Self {
        Self::new(CachePolicy::PerCharacter, CachePolicy::None)
    }

    /// Creates a new tracker caching data for own characters and other players' account.
    pub const fn for_all() -> Self {
        Self::new(CachePolicy::PerCharacter, CachePolicy::PerAccount)
    }

    /// Returns the applicable cache policy.
    fn cache_policy(&self, is_self: bool) -> CachePolicy {
        if is_self {
            self.self_policy
        } else {
            self.others_policy
        }
    }

    /// Searches the cache for an entry, removing it when found.
    fn take_cache(
        &mut self,
        mut predicate: impl FnMut(&CachedPlayer) -> bool,
    ) -> Option<(CachedPlayer, T)> {
        let index = self
            .cache
            .iter()
            .position(|(player, _)| predicate(player))?;
        Some(self.cache.swap_remove(index))
    }

    /// Adds a new tracked player, returning `true` if cached data was used.
    pub fn add_player(&mut self, player: Player, data: T) -> bool {
        let cached = match self.cache_policy(player.is_self) {
            CachePolicy::None => None,
            CachePolicy::PerAccount => self
                .take_cache(|cached| cached.account == player.account)
                .and_then(|(cached, data)| {
                    if cached.character == player.character {
                        Some(data)
                    } else {
                        None
                    }
                }),
            CachePolicy::PerCharacter => self
                .take_cache(|cached| cached.character == player.character)
                .map(|(_, data)| data),
        };
        let found = cached.is_some();

        self.tracker.add_player(player, cached.unwrap_or(data));

        found
    }

    /// Adds a new tracked player with default data, returning `true` if cached data was used.
    pub fn add_player_default(&mut self, player: Player) -> bool
    where
        T: Default,
    {
        self.add_player(player, T::default())
    }

    /// Removes a tracked player, returning `true` if the player was tracked.
    pub fn remove_player(&mut self, id: usize) -> bool {
        self.tracker
            .remove_player(id)
            .map(|entry| self.maybe_cache(entry))
            .is_some()
    }

    /// Removes a tracked player, returning the removed entry if the player was tracked.
    pub fn take_player(&mut self, id: usize) -> Option<Entry<T>>
    where
        T: Clone,
    {
        self.tracker.remove_player(id).map(|entry| {
            self.maybe_cache(entry.clone());
            entry
        })
    }

    /// Returns a reference to the local player (self).
    pub fn get_self(&self) -> Option<&Entry<T>> {
        self.tracker.get_self()
    }

    /// Returns a mutable reference to the local player (self).
    pub fn get_self_mut(&mut self) -> Option<&mut Entry<T>> {
        self.tracker.get_self_mut()
    }

    /// Returns a reference to a tracked player entry.
    pub fn player(&self, id: usize) -> Option<&Entry<T>> {
        self.tracker.player(id)
    }

    /// Returns a mutable reference to a tracked player entry.
    pub fn player_mut(&mut self, id: usize) -> Option<&mut Entry<T>> {
        self.tracker.player_mut(id)
    }

    /// Caches the entry if necessary.
    fn maybe_cache(&mut self, entry: Entry<T>) {
        if self.cache_policy(entry.player.is_self).can_cache() {
            self.cache_entry(entry)
        }
    }

    /// Adds an entry to the cache.
    ///
    /// Caching happens automatically based on the set [`CachePolicy`], so usually this does not have to be called manually.
    pub fn cache_entry(&mut self, entry: Entry<T>) {
        self.cache.push(entry.into())
    }

    /// Adds multiple entries to the cache.
    ///
    /// Caching happens automatically based on the set [`CachePolicy`], so usually this does not have to be called manually.
    pub fn cache_multiple(&mut self, entries: impl Iterator<Item = (CachedPlayer, T)>) {
        self.cache.extend(entries)
    }

    /// Removes a cache entry based on the character name.
    pub fn remove_cache_entry(&mut self, character: impl AsRef<str>) -> Option<(CachedPlayer, T)> {
        let name = character.as_ref();
        self.cache
            .iter()
            .position(|(player, _)| player.character == name)
            .map(|index| self.cache.remove(index))
    }

    /// Returns whether there are any cached entries.
    pub fn cached(&self) -> bool {
        !self.cache.is_empty()
    }

    /// Returns the amount of cached entries.
    pub fn cache_len(&self) -> usize {
        self.cache.len()
    }

    /// Returns an iterator over the current cache contents.
    pub fn cache_iter(&self) -> impl Iterator<Item = &(CachedPlayer, T)> {
        self.cache.iter()
    }

    /// Returns a mutable iterator over the current cache contents.
    pub fn cache_iter_mut(&mut self) -> impl Iterator<Item = &mut (CachedPlayer, T)> {
        self.cache.iter_mut()
    }

    /// Clears the cache.
    pub fn clear_cache(&mut self) {
        self.cache.clear()
    }
}

impl<T> Default for CachedTracker<T> {
    fn default() -> Self {
        Self::for_self()
    }
}

impl<T> ops::Deref for CachedTracker<T> {
    type Target = [Entry<T>];

    fn deref(&self) -> &Self::Target {
        self.tracker.entries.as_slice()
    }
}

impl<T> ops::DerefMut for CachedTracker<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.tracker.entries.as_mut_slice()
    }
}

/// How data should be cached.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CachePolicy {
    /// Do not cache anything.
    None,

    /// Cache one entry per account.
    PerAccount,

    /// Cache one entry per character.
    PerCharacter,
}

impl CachePolicy {
    /// Whether caching is allowed.
    pub const fn can_cache(&self) -> bool {
        matches!(self, Self::PerAccount | Self::PerCharacter)
    }
}

/// An player entry in the tracker cache.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CachedPlayer {
    /// Account name of cached player.
    pub account: String,

    /// Character name of cached player.
    pub character: String,

    /// Profession of cached player.
    #[cfg_attr(feature = "serde", serde(default))]
    pub profession: Profession,

    /// Elite specialization of cached player.
    #[cfg_attr(feature = "serde", serde(default))]
    pub elite: Specialization,
}

impl CachedPlayer {
    /// Creates a new player cache entry.
    pub fn new(
        account: impl Into<String>,
        character: impl Into<String>,
        profession: Profession,
        specialization: Specialization,
    ) -> Self {
        Self {
            account: account.into(),
            character: character.into(),
            profession,
            elite: specialization,
        }
    }
}

impl From<Player> for CachedPlayer {
    fn from(player: Player) -> Self {
        Self::new(
            player.account,
            player.character,
            player.profession,
            player.elite,
        )
    }
}

impl<T> From<Entry<T>> for (CachedPlayer, T) {
    fn from(entry: Entry<T>) -> Self {
        (entry.player.into(), entry.data)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arcdps::{Profession, Specialization};

    #[test]
    fn caching() {
        let mut tracker = CachedTracker::for_self();

        let player = Player::new(
            123,
            456,
            "Char",
            "Acc",
            true,
            Profession::Unknown,
            Specialization::Unknown,
            1,
        );

        tracker.add_player_default(player.clone());
        assert_eq!(1, tracker.len());

        tracker.player_mut(123).unwrap().data = 42;
        assert_eq!(42, tracker.player(123).unwrap().data);

        tracker.remove_player(123);
        assert!(tracker.is_empty());

        tracker.add_player_default(player);
        assert_eq!(1, tracker.len());
        assert_eq!(42, tracker.player(123).unwrap().data);
    }

    #[test]
    fn cache_policy() {
        let mut tracker = CachedTracker::for_all();

        let char1 = Player::new(
            123,
            1,
            "Char1",
            "Account1",
            false,
            Profession::Unknown,
            Specialization::Unknown,
            1,
        );

        let char2 = Player::new(
            456,
            2,
            "Char2",
            "Account1",
            false,
            Profession::Unknown,
            Specialization::Unknown,
            1,
        );

        let char3 = Player::new(
            789,
            3,
            "Char3",
            "Account2",
            false,
            Profession::Unknown,
            Specialization::Unknown,
            1,
        );

        tracker.add_player(char1, 42);

        tracker.remove_player(123);
        assert_eq!(1, tracker.cache.len());

        let cached = tracker.add_player(char2.clone(), 100);
        assert!(!cached);
        assert_eq!(100, tracker.player(456).unwrap().data);
        assert!(tracker.cache.is_empty());

        tracker.remove_player(456);
        assert!(tracker.is_empty());
        assert_eq!(1, tracker.cache.len());

        let cached = tracker.add_player_default(char2);
        assert!(cached);
        assert_eq!(100, tracker.player(456).unwrap().data);
        assert!(tracker.cache.is_empty());

        tracker.remove_player(456);
        let cached = tracker.add_player_default(char3);
        assert!(!cached);

        tracker.remove_player(789);
        assert!(tracker.is_empty());
        assert_eq!(2, tracker.cache.len());
    }
}