using System.Diagnostics.CodeAnalysis;
using PkmnLib.Static;
using PkmnLib.Static.Species;
namespace PkmnLib.Dynamic.Models.Serialized;
///
/// A serialized Pokémon is a representation of a Pokémon that can be easily serialized and deserialized.
///
public record SerializedPokemon
{
///
public SerializedPokemon()
{
}
///
[SetsRequiredMembers]
public SerializedPokemon(IPokemon pokemon)
{
Species = pokemon.Species.Name;
Form = pokemon.Form.Name;
Level = pokemon.Level;
Experience = pokemon.Experience;
PersonalityValue = pokemon.PersonalityValue;
Gender = pokemon.Gender;
Coloring = pokemon.Coloring;
HeldItem = pokemon.HeldItem?.Name;
CurrentHealth = pokemon.CurrentHealth;
Happiness = pokemon.Happiness;
IndividualValues = new SerializedStats(pokemon.IndividualValues);
EffortValues = new SerializedStats(pokemon.EffortValues);
Nature = pokemon.Nature.Name;
Nickname = pokemon.Nickname;
Ability = pokemon.Form.GetAbility(pokemon.AbilityIndex);
Moves = pokemon.Moves.Select(move =>
{
if (move == null)
return null;
return new SerializedLearnedMove
{
MoveName = move.MoveData.Name,
LearnMethod = move.LearnMethod,
CurrentPp = move.CurrentPp,
};
}).ToArray();
AllowedExperience = pokemon.AllowedExperience;
IsEgg = pokemon.IsEgg;
Status = pokemon.StatusScript.Script?.Name;
}
///
public required string Species { get; set; }
///
public required string Form { get; set; }
///
public LevelInt Level { get; set; }
///
public uint Experience { get; set; }
///
public uint PersonalityValue { get; set; }
///
public Gender Gender { get; set; }
///
public byte Coloring { get; set; }
///
public string? HeldItem { get; set; }
///
public uint CurrentHealth { get; set; }
///
public byte Happiness { get; set; }
///
public required SerializedStats IndividualValues { get; set; }
///
public required SerializedStats EffortValues { get; set; }
///
public required string Nature { get; set; }
///
public string? Nickname { get; set; }
///
/// The ability of the Pokémon. Note that this is the ability name, not the ability index, as the ability index might
/// change if the order of abilities changes in data.
///
public required string Ability { get; set; }
///
public required SerializedLearnedMove?[] Moves { get; set; }
///
public bool AllowedExperience { get; set; }
///
public bool IsEgg { get; set; }
///
/// The status of the Pokémon. This is the name of the status script, if any exists.
///
public string? Status { get; set; }
}
///
/// A serialized learned move is a representation of a learned move that can be easily serialized and deserialized.
///
public record SerializedLearnedMove
{
///
/// The name of the move.
///
public required string MoveName { get; set; }
///
public required MoveLearnMethod LearnMethod { get; set; }
///
public required byte CurrentPp { get; set; }
}
public record SerializedStats
{
public SerializedStats()
{
}
public SerializedStats(ImmutableStatisticSet stats)
{
Hp = stats.Hp;
Attack = stats.Attack;
Defense = stats.Defense;
SpecialAttack = stats.SpecialAttack;
SpecialDefense = stats.SpecialDefense;
Speed = stats.Speed;
}
public SerializedStats(long hp, long attack, long defense, long specialAttack, long specialDefense, long speed)
{
Hp = hp;
Attack = attack;
Defense = defense;
SpecialAttack = specialAttack;
SpecialDefense = specialDefense;
Speed = speed;
}
public long Hp { get; set; }
public long Attack { get; set; }
public long Defense { get; set; }
public long SpecialAttack { get; set; }
public long SpecialDefense { get; set; }
public long Speed { get; set; }
public IndividualValueStatisticSet ToIndividualValueStatisticSet()
{
if (Hp < 0 || Attack < 0 || Defense < 0 || SpecialAttack < 0 || SpecialDefense < 0 || Speed < 0)
throw new InvalidOperationException("Stats cannot be negative.");
if (Hp > byte.MaxValue || Attack > byte.MaxValue || Defense > byte.MaxValue || SpecialAttack > byte.MaxValue ||
SpecialDefense > byte.MaxValue || Speed > byte.MaxValue)
throw new InvalidOperationException("Stats cannot be higher than 255.");
return new IndividualValueStatisticSet((byte)Hp, (byte)Attack, (byte)Defense, (byte)SpecialAttack,
(byte)SpecialDefense, (byte)Speed);
}
public EffortValueStatisticSet ToEffortValueStatisticSet()
{
if (Hp < 0 || Attack < 0 || Defense < 0 || SpecialAttack < 0 || SpecialDefense < 0 || Speed < 0)
throw new InvalidOperationException("Stats cannot be negative.");
if (Hp > byte.MaxValue || Attack > byte.MaxValue || Defense > byte.MaxValue || SpecialAttack > byte.MaxValue ||
SpecialDefense > byte.MaxValue || Speed > byte.MaxValue)
throw new InvalidOperationException("Stats cannot be higher than 255.");
return new EffortValueStatisticSet((byte)Hp, (byte)Attack, (byte)Defense, (byte)SpecialAttack,
(byte)SpecialDefense, (byte)Speed);
}
}