PkmnLib_rs/src/dynamic_data/models/serialization/serialized_pokemon.rs

139 lines
5.3 KiB
Rust

use crate::defines::LevelInt;
use crate::dynamic_data::{MoveLearnMethod, Pokemon, VolatileScriptsOwner};
use crate::static_data::{AbilityIndex, ClampedStatisticSet, Gender, TypeIdentifier};
use crate::StringKey;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
/// The serializable version of a [Pokemon](Pokemon).
#[derive(Serialize, Deserialize)]
pub struct SerializedPokemon {
/// The name of the species of the Pokemon.
pub species: StringKey,
/// The name of the form of the Pokemon.
pub form: StringKey,
/// The name of the species of the Pokemon that is displayed, if overridden.
pub display_species: Option<StringKey>,
/// The name of the form of the Pokemon that is displayed, if overridden.
pub display_form: Option<StringKey>,
/// The level of the Pokemon.
pub level: LevelInt,
/// The experience of the Pokemon.
pub experience: u32,
/// The personality value of the Pokemon.
pub personality_value: u32,
/// The gender of the Pokemon.
pub gender: Gender,
/// The coloring of the Pokemon.
pub coloring: u8,
/// The held item of the Pokemon.
pub held_item: Option<StringKey>,
/// The current health of the Pokemon.
pub current_health: u32,
/// The weight of the Pokemon.
pub weight: f32,
/// The height of the Pokemon.
pub height: f32,
/// The happiness of the Pokemon.
pub happiness: u8,
/// The stat boosts of the Pokemon.
pub stat_boosts: ClampedStatisticSet<i8, -6, 6>,
/// The individual values of the Pokemon.
pub individual_values: ClampedStatisticSet<u8, 0, 31>,
/// The effort values of the Pokemon.
pub effort_values: ClampedStatisticSet<u8, 0, 252>,
/// The nature of the Pokemon.
pub nature: StringKey,
/// The nickname of the Pokemon.
pub nickname: Option<String>,
/// The ability index of the Pokemon.
pub ability_index: AbilityIndex,
/// The ability that is currently active on the Pokemon, if overridden.
pub override_ability: Option<StringKey>,
/// The moves of the Pokemon.
pub moves: Vec<SerializedLearnedMove>,
/// Whether the Pokemon is allowed to gain experience.
pub allowed_experience: bool,
/// The types of the Pokemon.
pub types: Vec<TypeIdentifier>,
/// Whether the Pokemon is an egg.
pub is_egg: bool,
/// The volatile scripts of the Pokemon.
pub volatile_scripts: Vec<StringKey>,
}
/// The serializable version of a [LearnedMove](LearnedMove).
#[derive(Serialize, Deserialize)]
pub struct SerializedLearnedMove {
/// The name of the move.
pub move_data: StringKey,
/// A potential offset from the normal maximal power points.
pub max_pp_modification: u8,
/// The amount of remaining power points. If this is 0, we can not use the move anymore.
pub remaining_pp: u8,
/// The way the move was learned.
pub learn_method: MoveLearnMethod,
}
#[allow(clippy::from_over_into)] // From doesn't allow for Result.
impl Into<anyhow_ext::Result<SerializedPokemon>> for &Pokemon {
fn into(self) -> anyhow::Result<SerializedPokemon> {
Ok(SerializedPokemon {
species: self.species().name().clone(),
form: self.form().name().clone(),
display_species: if self.has_different_display_species() {
Some(self.display_species().name().clone())
} else {
None
},
display_form: if self.has_different_display_form() {
Some(self.display_form().name().clone())
} else {
None
},
level: self.level(),
experience: self.experience(),
personality_value: self.personality_value(),
gender: self.gender(),
coloring: self.coloring(),
held_item: {
let held_item = self.held_item().read();
held_item.as_ref().map(|held_item| held_item.name().clone())
},
current_health: self.current_health(),
weight: self.weight(),
height: self.height(),
happiness: self.happiness(),
stat_boosts: self.stat_boosts().deref().clone(),
individual_values: self.individual_values().deref().clone(),
effort_values: self.effort_values().deref().clone(),
nature: self.library().static_data().natures().get_nature_name(self.nature())?,
nickname: self.nickname().clone(),
ability_index: *self.real_ability(),
override_ability: {
if self.is_ability_overriden() {
Some(self.active_ability()?.name().clone())
} else {
None
}
},
moves: self
.learned_moves()
.read()
.iter()
.flatten()
.map(|lm| SerializedLearnedMove {
move_data: lm.move_data().name().clone(),
max_pp_modification: lm.max_pp_modification(),
remaining_pp: lm.remaining_pp(),
learn_method: lm.learn_method(),
})
.collect(),
allowed_experience: self.allowed_experience_gain(),
types: self.types().to_vec(),
is_egg: self.is_egg(),
volatile_scripts: self.volatile_scripts().get_script_names(),
})
}
}