Added pokemon ability information to Pokedex.

This commit is contained in:
Greg Edwards 2022-08-22 01:28:53 -04:00
parent 238f373102
commit b7eeecb609
9 changed files with 197 additions and 27 deletions

View File

@ -3847,6 +3847,21 @@ namespace PkmnFoundations.Data
}
}
public override List<FormAbilities> PokedexGetAllFormAbilities(Pokedex.Pokedex pokedex)
{
using (MySqlConnection db = CreateConnection())
{
db.Open();
using (MySqlDataReader reader = (MySqlDataReader)db.ExecuteReader("SELECT " +
"form_id, MinGeneration, Ability1, Ability2, HiddenAbility1 " +
"FROM pkmncf_pokedex_pokemon_form_abilities"))
{
return ReaderToList(reader, pokedex, () => new FormAbilities(pokedex, reader));
}
}
}
public override List<Family> PokedexGetAllFamilies(Pokedex.Pokedex pokedex)
{
using (MySqlConnection db = CreateConnection())

View File

@ -311,6 +311,7 @@ namespace PkmnFoundations.Data
public abstract List<Species> PokedexGetAllSpecies(Pokedex.Pokedex pokedex);
public abstract List<Form> PokedexGetAllForms(Pokedex.Pokedex pokedex);
public abstract List<FormStats> PokedexGetAllFormStats(Pokedex.Pokedex pokedex);
public abstract List<FormAbilities> PokedexGetAllFormAbilities(Pokedex.Pokedex pokedex);
public abstract List<Family> PokedexGetAllFamilies(Pokedex.Pokedex pokedex);
public abstract List<Evolution> PokedexGetAllEvolutions(Pokedex.Pokedex pokedex);

View File

@ -61,6 +61,7 @@
<Compile Include="Data\SqlDatabaseExtender.cs" />
<Compile Include="Pokedex\Ability.cs" />
<Compile Include="Pokedex\Evolution.cs" />
<Compile Include="Pokedex\FormAbilities.cs" />
<Compile Include="Pokedex\FormStats.cs" />
<Compile Include="Pokedex\Item.cs" />
<Compile Include="Pokedex\Location.cs" />

View File

@ -11,7 +11,7 @@ namespace PkmnFoundations.Pokedex
public class Form : PokedexRecordBase
{
public Form(Pokedex pokedex, int id, int species_id, byte value,
LocalizedString name, String suffix, int height, int weight, int experience)
LocalizedString name, string suffix, int height, int weight, int experience)
: base(pokedex)
{
m_species_pair = Species.CreatePair(m_pokedex);
@ -46,12 +46,13 @@ namespace PkmnFoundations.Pokedex
{
base.PrefetchRelations();
m_form_stats = m_pokedex.FormStats(ID);
m_form_abilities = m_pokedex.FormAbilities(ID);
}
public int ID { get; private set; }
public byte Value { get; private set; }
public LocalizedString Name { get; private set; }
public String Suffix { get; private set; }
public string Suffix { get; private set; }
public int Height { get; private set; }
public int Weight { get; private set; }
public int Experience { get; private set; }
@ -77,6 +78,14 @@ namespace PkmnFoundations.Pokedex
return m_form_stats.Last(pair => (int)(pair.Key) <= (int)generation).Value;
}
private SortedList<Generations, FormAbilities> m_form_abilities;
public FormAbilities Abilities(Generations generation)
{
if (m_form_abilities == null) m_form_abilities = m_pokedex.FormAbilities(ID);
// xxx: above
return m_form_abilities.Last(pair => (int)(pair.Key) <= (int)generation).Value;
}
public static LazyKeyValuePair<int, Form> CreatePair(Pokedex pokedex)
{
return new LazyKeyValuePair<int, Form>(

View File

@ -0,0 +1,103 @@
using PkmnFoundations.Structures;
using PkmnFoundations.Support;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace PkmnFoundations.Pokedex
{
public class FormAbilities : PokedexRecordBase
{
public FormAbilities(Pokedex pokedex, int form_id, Generations min_generation,
int ability1, int ability2, int hiddenAbility1)
: base(pokedex)
{
m_form_pair = Form.CreatePair(m_pokedex);
m_ability1_pair = Ability.CreatePair(m_pokedex);
m_ability2_pair = Ability.CreatePair(m_pokedex);
m_hidden_ability1_pair = Ability.CreatePair(m_pokedex);
m_lazy_pairs.Add(m_form_pair);
m_lazy_pairs.Add(m_ability1_pair);
m_lazy_pairs.Add(m_hidden_ability1_pair);
m_form_pair.Key = form_id;
MinGeneration = min_generation;
m_ability1_pair.Key = ability1;
m_ability2_pair.Key = ability2;
m_hidden_ability1_pair.Key = hiddenAbility1;
}
public FormAbilities(Pokedex pokedex, IDataReader reader)
: this(
pokedex,
Convert.ToInt32(reader["form_id"]),
(Generations)Convert.ToInt32(reader["MinGeneration"]),
Convert.ToInt32(reader["Ability1"]),
Convert.ToInt32(reader["Ability2"]),
Convert.ToInt32(reader["HiddenAbility1"])
)
{
}
public Generations MinGeneration { get; private set; }
private LazyKeyValuePair<int, Form> m_form_pair;
private LazyKeyValuePair<int, Ability> m_ability1_pair;
private LazyKeyValuePair<int, Ability> m_ability2_pair;
private LazyKeyValuePair<int, Ability> m_hidden_ability1_pair;
public int FormID
{
get { return m_form_pair.Key; }
}
public Form Form
{
get { return m_form_pair.Value; }
}
public int Ability1ID
{
get { return m_ability1_pair.Key; }
}
public Ability Ability1
{
get { return m_ability1_pair.Value; }
}
public int Ability2ID
{
get { return m_ability2_pair.Key; }
}
public Ability Ability2
{
get { return m_ability2_pair.Value; }
}
public int HiddenAbility1ID
{
get { return m_hidden_ability1_pair.Key; }
}
public Ability HiddenAbility1
{
get { return m_hidden_ability1_pair.Value; }
}
public Ability[] Abilities
{
get
{
// xxx: We probably want the actual data to be stored in a collection to avoid this silly if-else.
if (Ability1 != null && Ability2 != null)
return new Ability[] { Ability1, Ability2 };
else if (Ability1 != null)
return new Ability[] { Ability1 };
else if (Ability2 != null)
return new Ability[] { Ability2 };
else
return new Ability[] { };
}
}
}
}

View File

@ -15,6 +15,7 @@ namespace PkmnFoundations.Pokedex
: base(pokedex)
{
m_form_pair = Form.CreatePair(m_pokedex);
// xxx: Do we maybe want to expose types as some sort of collection maybe?
m_type1_pair = Type.CreatePair(m_pokedex);
m_type2_pair = Type.CreatePair(m_pokedex);
m_lazy_pairs.Add(m_form_pair);

View File

@ -33,28 +33,10 @@ namespace PkmnFoundations.Pokedex
m_locations = db.PokedexGetAllLocations(this).ToDictionary(l => l.ID, l => l);
List<FormStats> form_stats = db.PokedexGetAllFormStats(this);
form_stats.Sort(delegate(FormStats f, FormStats other)
{
if (f.FormID != other.FormID) return f.FormID.CompareTo(other.FormID);
return f.MinGeneration.CompareTo(other.MinGeneration);
});
m_form_stats = ProcessGenerationalChangeset(form_stats, fs => fs.FormID, fs => fs.MinGeneration);
Dictionary<int, SortedList<Generations, FormStats>> resultFormStats = new Dictionary<int, SortedList<Generations, FormStats>>();
SortedList<Generations, FormStats> currFormStats = null;
int currFormId = 0;
foreach (FormStats f in form_stats)
{
if (currFormStats == null || currFormId != f.FormID)
{
if (currFormStats != null) resultFormStats.Add(currFormId, currFormStats);
currFormStats = new SortedList<Generations, FormStats>();
}
currFormStats.Add(f.MinGeneration, f);
currFormId = f.FormID;
}
if (currFormStats != null) resultFormStats.Add(currFormId, currFormStats);
m_form_stats = resultFormStats;
List<FormAbilities> form_abilities = db.PokedexGetAllFormAbilities(this);
m_form_abilities = ProcessGenerationalChangeset(form_abilities, fa => fa.FormID, fa => fa.MinGeneration);
}
private void BuildAdditionalIndexes()
@ -111,6 +93,42 @@ namespace PkmnFoundations.Pokedex
AddGeneration(m_location_values_generations, m_locations, LocationNumbering.Generation6, l => l.Value6);
}
private Dictionary<int, SortedList<Generations, T>> ProcessGenerationalChangeset<T>(List<T> data, Func<T, int> idGetter, Func<T, Generations> minGenerationGetter)
{
// xxx: Instead of passing in these two lamdbas, we want an IGenerationalChangesetItem interface with corresponding properties.
var sorted = data.ToList();
sorted.Sort(delegate (T f, T other)
{
int idF = idGetter(f);
int idOther = idGetter(other);
if (idF != idOther) return idF.CompareTo(idOther);
Generations genF = minGenerationGetter(f);
Generations genOther = minGenerationGetter(other);
return genF.CompareTo(genOther);
});
Dictionary<int, SortedList<Generations, T>> resultFormStats = new Dictionary<int, SortedList<Generations, T>>();
SortedList<Generations, T> currFormStats = null;
int currFormId = 0;
foreach (T f in sorted)
{
int idF = idGetter(f);
if (currFormStats == null || currFormId != idF)
{
if (currFormStats != null) resultFormStats.Add(currFormId, currFormStats);
currFormStats = new SortedList<Generations, T>();
}
currFormStats.Add(minGenerationGetter(f), f);
currFormId = idF;
}
if (currFormStats != null) resultFormStats.Add(currFormId, currFormStats);
return resultFormStats;
}
private void AddGeneration<TGen, TKey, TValue>(Dictionary<TGen, Dictionary<TKey, TValue>> dest, Dictionary<TKey, TValue> src, TGen generation, Func<TValue, TKey?> keyGetter)
where TKey : struct
{
@ -150,6 +168,11 @@ namespace PkmnFoundations.Pokedex
foreach (var j in k.Value)
j.Value.PrefetchRelations();
}
foreach (var k in m_form_abilities)
{
foreach (var j in k.Value)
j.Value.PrefetchRelations();
}
}
private Dictionary<int, Species> m_species;
@ -157,6 +180,7 @@ namespace PkmnFoundations.Pokedex
private Dictionary<int, Form> m_forms;
private Dictionary<int, Dictionary<byte, Form>> m_forms_by_value;
private Dictionary<int, SortedList<Generations, FormStats>> m_form_stats;
private Dictionary<int, SortedList<Generations, FormAbilities>> m_form_abilities;
//private Dictionary<int, Evolution> m_evolutions;
private Dictionary<int, Item> m_items;
@ -210,6 +234,11 @@ namespace PkmnFoundations.Pokedex
return m_form_stats[form_id];
}
internal SortedList<Generations, FormAbilities> FormAbilities(int form_id)
{
return m_form_abilities[form_id];
}
public IDictionary<int, Item> Items
{
get

View File

@ -24,12 +24,12 @@ namespace PkmnFoundations.Pokedex
p.Evaluate();
}
public static LocalizedString LocalizedStringFromReader(IDataReader reader, String prefix)
public static LocalizedString LocalizedStringFromReader(IDataReader reader, string prefix)
{
// fixme: share this field with CreateLocalizedStringQueryPieces
String[] langs = new String[] { "JA", "EN", "FR", "IT", "DE", "ES", "KO" };
string[] langs = new string[] { "JA", "EN", "FR", "IT", "DE", "ES", "KO" };
LocalizedString result = new LocalizedString();
foreach (String lang in langs)
foreach (string lang in langs)
{
try
{

View File

@ -715,10 +715,21 @@ CREATE TABLE IF NOT EXISTS `pkmncf_pokedex_pokemon_forms` (
-- Data exporting was unselected.
-- Dumping structure for table gts.pkmncf_pokedex_pokemon_form_abilities
CREATE TABLE IF NOT EXISTS `pkmncf_pokedex_pokemon_form_abilities` (
`form_id` int(10) unsigned NOT NULL,
`MinGeneration` int(10) unsigned NOT NULL,
`Ability1` int(10) unsigned DEFAULT NULL,
`Ability2` int(10) unsigned DEFAULT NULL,
`HiddenAbility1` int(10) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Data exporting was unselected.
-- Dumping structure for table gts.pkmncf_pokedex_pokemon_form_stats
CREATE TABLE IF NOT EXISTS `pkmncf_pokedex_pokemon_form_stats` (
`form_id` int(10) unsigned NOT NULL,
`MinGeneration` int(11) unsigned NOT NULL,
`MinGeneration` int(10) unsigned NOT NULL,
`Type1` int(10) unsigned DEFAULT NULL,
`Type2` int(10) unsigned DEFAULT NULL,
`BaseHP` int(11) DEFAULT NULL,