battler 0.9.1

Pokémon battle engine for Rust.
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use alloc::{
    format,
    string::String,
    vec::Vec,
};
use core::{
    fmt,
    fmt::Display,
};

use hashbrown::{
    HashMap,
    hash_map::Entry,
};
use itertools::Itertools;

/// Trait for objects that can be added directly to the battle log.
pub trait BattleLoggable {
    /// Logs the object into the entry.
    fn log(&self, entry: &mut UncommittedBattleLogEntry);
}

impl BattleLoggable for &str {
    fn log(&self, entry: &mut UncommittedBattleLogEntry) {
        entry.add_flag(*self)
    }
}

impl<T> BattleLoggable for (&str, T)
where
    T: Display,
{
    fn log(&self, entry: &mut UncommittedBattleLogEntry) {
        entry.set(self.0, &self.1)
    }
}

/// An uncommitted, mutable entry that is added to the [`BattleLog`] and can be modified after the
/// fact.
///
/// This object should not be constructed directly. Instead, use the
/// [`battle_log_entry`][`crate::battle_log_entry`] macro.
#[derive(Debug, Clone)]
pub struct UncommittedBattleLogEntry {
    title: String,
    values: HashMap<String, String>,
    insertion_order: Vec<String>,
}

impl UncommittedBattleLogEntry {
    /// Creates a new entry with the given title.
    pub fn new<T>(title: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            title: title.into(),
            values: HashMap::default(),
            insertion_order: Vec::new(),
        }
    }

    /// Adds the given value to the entry.
    pub fn extend<T>(&mut self, value: &T)
    where
        T: BattleLoggable,
    {
        value.log(self)
    }

    fn add_entry<K, V>(&mut self, key: K, value: V)
    where
        K: Into<String>,
        V: Display,
    {
        match self.values.entry(key.into()) {
            Entry::Occupied(mut entry) => *entry.get_mut() = format!("{value}"),
            Entry::Vacant(entry) => {
                let entry = entry.insert_entry(format!("{value}"));
                self.insertion_order.push(entry.key().clone());
            }
        }
    }

    /// Adds a new flag (a property with no value) to the entry.
    pub fn add_flag<K>(&mut self, key: K)
    where
        K: Into<String>,
    {
        self.add_entry(key, "")
    }

    /// Sets the value of a property on the entry.
    ///
    /// If the property did not exist before, the pair is added. If the property did exist, the
    /// value is updated.
    pub fn set<K, V>(&mut self, key: K, value: V)
    where
        K: Into<String>,
        V: Display,
    {
        self.add_entry(key, value)
    }

    /// Removes the property.
    pub fn remove(&mut self, key: &str) {
        self.values.remove(key);
    }

    fn commit(&self) -> String {
        let values = self
            .insertion_order
            .iter()
            .filter_map(|key| self.values.get_key_value(key))
            .map(|(key, value)| {
                if value.is_empty() {
                    key.clone()
                } else {
                    format!("{key}:{value}")
                }
            })
            .join("|");

        if values.is_empty() {
            self.title.clone()
        } else {
            format!("{}|{}", self.title, values)
        }
    }
}

impl Display for UncommittedBattleLogEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.commit())
    }
}

/// Constructs a [`UncommittedBattleLogEntry`] to be added to the [`BattleLog`].
///
/// This macro enforces a common format for all messages in the battle log.
#[macro_export]
macro_rules! battle_log_entry {
    ($title:expr) => {{
        $crate::log::UncommittedBattleLogEntry::new($title)
    }};
    ($title:expr $(, $entries:expr)+ $(,)?) => {{
        let mut entry = $crate::log::UncommittedBattleLogEntry::new($title);
        $($crate::log::BattleLoggable::log(&$entries, &mut entry);)*
        entry
    }};
}

/// Battle log entry.
#[derive(Debug)]
pub enum BattleLogEntry<'e> {
    Committed(&'e str),
    Uncommitted(&'e UncommittedBattleLogEntry),
}

impl BattleLogEntry<'_> {
    /// Is the log entry committed and published for clients?
    pub fn committed(&self) -> bool {
        match self {
            Self::Committed(_) => true,
            Self::Uncommitted(_) => false,
        }
    }
}

impl Display for BattleLogEntry<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Committed(entry) => write!(f, "{entry}"),
            Self::Uncommitted(entry) => write!(f, "{entry}"),
        }
    }
}

/// Mutable battle log entry.
///
/// Only uncommitted logs are mutable.
#[derive(Debug)]
pub enum BattleLogEntryMut<'e> {
    Committed(&'e str),
    Uncommitted(&'e mut UncommittedBattleLogEntry),
}

impl BattleLogEntryMut<'_> {
    pub fn committed(&self) -> bool {
        match self {
            Self::Committed(_) => true,
            Self::Uncommitted(_) => false,
        }
    }
}

impl Display for BattleLogEntryMut<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Committed(entry) => write!(f, "{entry}"),
            Self::Uncommitted(entry) => write!(f, "{entry}"),
        }
    }
}

/// A log of battle events that can be exported.
///
/// When a new [`BattleLogEntry`] is added, it is considered uncommitted and is mutable (see
/// [`BattleLogEntryMut`]). Logs must be manually committed using the [`Self::commit`] method in
/// order to appear for clients. Once a log is committed, it is considered immutable.
///
/// The log also keeps track of reads. Once a log is read out using [`Self::read_out`], it will not
/// be read out again.
pub struct BattleLog {
    last_read: usize,
    committed_logs: Vec<String>,
    new_log_entries: Vec<UncommittedBattleLogEntry>,
}

impl BattleLog {
    /// Creates a new battle log.
    pub fn new() -> Self {
        Self {
            last_read: 0,
            committed_logs: Vec::new(),
            new_log_entries: Vec::new(),
        }
    }

    /// Does the log contain new messages since the last call to [`Self::read_out`]?
    pub fn has_new_messages(&self) -> bool {
        !self.new_log_entries.is_empty()
    }

    /// Pushes a new entry to the log.
    pub fn push(&mut self, entry: UncommittedBattleLogEntry) {
        self.new_log_entries.push(entry)
    }

    /// Pushes multiple entries to the log.
    pub fn push_extend<I>(&mut self, iterable: I)
    where
        I: IntoIterator<Item = UncommittedBattleLogEntry>,
    {
        self.new_log_entries.extend(iterable.into_iter());
    }

    /// Commits all uncommitted logs.
    pub fn commit(&mut self) {
        let new_log_entries = core::mem::replace(&mut self.new_log_entries, Vec::new());
        self.committed_logs
            .extend(new_log_entries.into_iter().map(|entry| entry.commit()))
    }

    /// Returns an iterator over all committed logs.
    pub fn logs(&self) -> impl Iterator<Item = &str> {
        self.committed_logs.iter().map(|s| s.as_ref())
    }

    /// Reads out any new, committed logs that have been added since the last call to
    /// [`Self::read_out`].
    pub fn read_out(&mut self) -> impl Iterator<Item = &str> {
        let i = self.last_read;
        self.last_read = self.committed_logs.len();
        self.committed_logs[i..].iter().map(|s| s.as_ref())
    }

    /// Returns the total number of log entries.
    pub fn len(&self) -> usize {
        self.committed_logs.len() + self.new_log_entries.len()
    }

    /// Returns a reference to the log entry at the given index.
    pub fn get(&self, index: usize) -> Option<BattleLogEntry<'_>> {
        self.committed_logs
            .get(index)
            .map(|s| BattleLogEntry::Committed(s.as_ref()))
            .or_else(|| {
                index
                    .checked_sub(self.committed_logs.len())
                    .and_then(|index| {
                        self.new_log_entries
                            .get(index)
                            .map(|entry| BattleLogEntry::Uncommitted(entry))
                    })
            })
    }

    /// Returns a mutable reference to the log entry at the given index.
    pub fn get_mut(&mut self, index: usize) -> Option<BattleLogEntryMut<'_>> {
        self.committed_logs
            .get(index)
            .map(|s| BattleLogEntryMut::Committed(s.as_ref()))
            .or_else(|| {
                index
                    .checked_sub(self.committed_logs.len())
                    .and_then(|index| {
                        self.new_log_entries
                            .get_mut(index)
                            .map(|entry| BattleLogEntryMut::Uncommitted(entry))
                    })
            })
    }
}

#[cfg(test)]
mod battle_log_test {
    use alloc::{
        borrow::ToOwned,
        format,
        string::{
            String,
            ToString,
        },
        vec,
        vec::Vec,
    };
    use core::{
        fmt,
        fmt::Display,
    };

    use crate::log::{
        BattleLog,
        BattleLogEntryMut,
        BattleLoggable,
        UncommittedBattleLogEntry,
    };

    fn last_log(log: &mut BattleLog) -> String {
        log.get(log.len() - 1).unwrap().to_string()
    }

    struct CustomData {
        a: u32,
        b: String,
    }

    impl Display for CustomData {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{} => {}", self.a, self.b)
        }
    }

    #[test]
    fn formats_entries() {
        let mut log = BattleLog::new();

        log.push(battle_log_entry!("a", ("b", "c")));
        assert_eq!(last_log(&mut log), "a|b:c");

        log.push(battle_log_entry!("time", ("time", 100000i32), "real"));
        assert_eq!(last_log(&mut log), "time|time:100000|real");

        log.push(battle_log_entry!(
            "customdata",
            ("pi", 3.1415926535f64),
            (
                "data",
                CustomData {
                    a: 234,
                    b: "bulbasaur".to_owned(),
                },
            ),
            ("a", 0i32),
            ("b", 1i32),
            ("c", 0i32),
        ));
        assert_eq!(
            last_log(&mut log),
            "customdata|pi:3.1415926535|data:234 => bulbasaur|a:0|b:1|c:0"
        );
    }

    struct CustomDataWithLogImplementation {
        a: u32,
        b: String,
    }

    impl BattleLoggable for CustomDataWithLogImplementation {
        fn log(&self, log: &mut UncommittedBattleLogEntry) {
            log.set("a", format!("{}", self.a));
            log.set("b", &self.b);
        }
    }

    #[test]
    fn allows_custom_implementation() {
        let mut log = BattleLog::new();

        log.push(battle_log_entry!(
            "customdata",
            "abc",
            CustomDataWithLogImplementation {
                a: 234,
                b: "bulbasaur".to_owned(),
            },
            ("val", 0i32),
        ));
        assert_eq!(last_log(&mut log), "customdata|abc|a:234|b:bulbasaur|val:0");
    }

    #[test]
    fn records_length() {
        let mut log = BattleLog::new();
        assert_eq!(log.len(), 0);
        log.push(battle_log_entry!("one"));
        assert_eq!(log.len(), 1);
        log.push(battle_log_entry!("two", "three", "four"));
        assert_eq!(log.len(), 2);
    }

    #[test]
    fn returns_entry_by_index() {
        let mut log = BattleLog::new();
        assert!(log.get(0).is_none());
        log.push(battle_log_entry!("one"));
        assert_eq!(log.get(0).unwrap().to_string(), "one");
        assert!(log.get(1).is_none());
        log.push(battle_log_entry!("two", "three", "four"));
        assert_eq!(log.get(1).unwrap().to_string(), "two|three|four");
    }

    #[test]
    fn reads_out_new_committed_logs() {
        let mut log = BattleLog::new();
        assert!(log.read_out().collect::<Vec<_>>().is_empty());
        log.push(battle_log_entry!("one"));
        log.push(battle_log_entry!("two", "three", "four"));
        assert!(log.read_out().collect::<Vec<_>>().is_empty());
        log.commit();
        assert_eq!(
            log.read_out().collect::<Vec<_>>(),
            vec!["one", "two|three|four"]
        );
        assert!(log.read_out().collect::<Vec<_>>().is_empty());
        log.push(battle_log_entry!("five", "six"));
        log.push(battle_log_entry!("seven", "eight"));
        assert!(log.read_out().collect::<Vec<_>>().is_empty());
        log.commit();
        assert_eq!(
            log.read_out().collect::<Vec<_>>(),
            vec!["five|six", "seven|eight"]
        );
        assert!(log.read_out().collect::<Vec<_>>().is_empty());
    }

    #[test]
    fn returns_entry_type() {
        let mut log = BattleLog::new();
        log.push(battle_log_entry!("move", "tackle"));
        assert!(log.get(0).is_some_and(|entry| !entry.committed()));
        log.commit();
        assert!(log.get(0).is_some_and(|entry| entry.committed()));

        log.push(battle_log_entry!("move", "name:tackle", "bad"));
        if let Some(BattleLogEntryMut::Uncommitted(entry)) = log.get_mut(1) {
            entry.add_flag("noanim");
            entry.remove("bad");
            entry.set("damage", 12);
            entry.extend(&CustomDataWithLogImplementation {
                a: 1,
                b: "2".to_owned(),
            });
        }
        assert!(log.get(1).is_some_and(|entry| {
            entry
                .to_string()
                .eq("move|name:tackle|noanim|damage:12|a:1|b:2")
        }));

        log.commit();
        assert_eq!(
            log.read_out().collect::<Vec<_>>(),
            vec!["move|tackle", "move|name:tackle|noanim|damage:12|a:1|b:2"]
        );
    }
}