PkmnLib_rs/src/static_data/species_data/learnable_moves.rs

82 lines
2.6 KiB
Rust
Executable File

use hashbrown::hash_map::Entry::{Occupied, Vacant};
use hashbrown::HashMap;
use crate::defines::LevelInt;
use crate::StringKey;
/// This allows for storage of the moves a Pokemon can learn.
#[derive(Default, PartialEq, Eq, Debug)]
pub struct LearnableMoves {
/// A map of the moves a Pokemon can learn per level.
learned_by_level: HashMap<LevelInt, Vec<StringKey>>,
/// A list of the distinct moves a Pokemon can learn through leveling up.
distinct_level_moves: Vec<StringKey>,
}
impl LearnableMoves {
/// Instantiates a new Learnable Moves.
pub fn new() -> LearnableMoves {
LearnableMoves::default()
}
/// Adds a new level move the Pokemon can learn.
pub fn add_level_move(&mut self, level: LevelInt, m: &StringKey) {
match self.learned_by_level.entry(level) {
Occupied(x) => {
x.into_mut().push(m.clone());
}
Vacant(_) => {
self.learned_by_level.insert(level, vec![m.clone()]);
}
}
if !self.distinct_level_moves.contains(m) {
self.distinct_level_moves.push(m.clone());
}
}
/// Gets all moves a Pokemon can learn when leveling up to a specific level.
pub fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<StringKey>> {
self.learned_by_level.get(&level)
}
/// Gets the distinct moves a Pokemon can learn through leveling up.
pub fn get_distinct_level_moves(&self) -> &Vec<StringKey> {
&self.distinct_level_moves
}
}
#[cfg(test)]
mod tests {
use crate::static_data::species_data::learnable_moves::LearnableMoves;
#[test]
fn adds_level_moves() {
let mut moves = LearnableMoves::new();
moves.add_level_move(1, &"foo".into());
moves.add_level_move(1, &"bar".into());
let m = moves.get_learned_by_level(1u8).unwrap();
assert_eq!(m.len(), 2);
assert_eq!(m[0], "foo".into());
assert_eq!(m[1], "bar".into());
}
#[test]
fn adds_two_same_moves_at_different_level() {
let mut moves = LearnableMoves::new();
moves.add_level_move(1, &"foo".into());
moves.add_level_move(5, &"foo".into());
let m = moves.get_learned_by_level(1u8).unwrap();
assert_eq!(m.len(), 1);
assert_eq!(m[0], "foo".into());
let m2 = moves.get_learned_by_level(5u8).unwrap();
assert_eq!(m2.len(), 1);
assert_eq!(m2[0], "foo".into());
let distinct = moves.get_distinct_level_moves();
assert_eq!(distinct.len(), 1);
assert_eq!(distinct[0], "foo".into());
}
}