First draft of VeekunImport project. (Database insert commands are nops.) (Also includes some Pokedex types.)

This commit is contained in:
Greg Edwards 2014-12-08 02:31:47 -05:00
parent 76de928a13
commit 30b820b87f
18 changed files with 849 additions and 5 deletions

View File

@ -2,6 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PkmnFoundations.Data;
using System.Data.SQLite;
using System.Data;
using PkmnFoundations.Structures;
using PkmnFoundations.Pokedex;
using PkmnFoundations.Support;
using System.Threading;
using System.Globalization;
using System.IO;
namespace VeekunImport
{
@ -9,6 +19,379 @@ namespace VeekunImport
{
static void Main(string[] args)
{
String veekunFilename;
ConnectionStringSettings css;
if (args.Length < 1) veekunFilename = "pokedex.sqlite";
else veekunFilename = args[0];
if (veekunFilename.Contains(';')) throw new NotSupportedException("The character ; in filenames is not supported.");
if (veekunFilename.Contains('?'))
{
Console.WriteLine("Usage: VeekunImport [filename [connectionString providerName]]");
Console.WriteLine("filename: Filename of Veekun sqlite database. Default: pokedex.sqlite");
Console.WriteLine("connectionString: pkmnFoundations connection string. Default: from app.config");
Console.WriteLine("providerName: .NET classname of connection provider. Default: from app.config");
return;
}
if (args.Length < 3)
css = ConfigurationManager.ConnectionStrings["pkmnFoundationsConnectionString"];
else
css = new ConnectionStringSettings("", args[1], args[2]);
Database db = Database.CreateInstance(css);
using (SQLiteConnection connVeekun = new SQLiteConnection("Data Source=" + veekunFilename + "; Version=3"))
{
connVeekun.Open();
// General logic:
// 1. run a query against veekun, populate a DataTable.
// 2. foreach DataRow, instance a class from the PkmnFoundations.Pokedex namespace.
// 3. call Database.AddToPokedex on that object.
// pkmncf_pokedex_pokemon
SQLiteDataReader rdPokemon = (SQLiteDataReader)connVeekun.ExecuteReader("SELECT " +
"pokemon_species.id, " +
"(SELECT pokemon_species_names.name FROM pokemon_species_names WHERE pokemon_species_names.pokemon_species_id = pokemon_species.id AND local_language_id = 1) AS name_ja, " +
"(SELECT pokemon_species_names.name FROM pokemon_species_names WHERE pokemon_species_names.pokemon_species_id = pokemon_species.id AND local_language_id = 9) AS name_en, " +
"(SELECT pokemon_species_names.name FROM pokemon_species_names WHERE pokemon_species_names.pokemon_species_id = pokemon_species.id AND local_language_id = 5) AS name_fr, " +
"(SELECT pokemon_species_names.name FROM pokemon_species_names WHERE pokemon_species_names.pokemon_species_id = pokemon_species.id AND local_language_id = 8) AS name_it, " +
"(SELECT pokemon_species_names.name FROM pokemon_species_names WHERE pokemon_species_names.pokemon_species_id = pokemon_species.id AND local_language_id = 6) AS name_de, " +
"(SELECT pokemon_species_names.name FROM pokemon_species_names WHERE pokemon_species_names.pokemon_species_id = pokemon_species.id AND local_language_id = 7) AS name_es, " +
"(SELECT pokemon_species_names.name FROM pokemon_species_names WHERE pokemon_species_names.pokemon_species_id = pokemon_species.id AND local_language_id = 3) AS name_ko, " +
"growth_rate_id, gender_rate, " +
"(SELECT egg_group_id FROM pokemon_egg_groups WHERE species_id = pokemon_species.id ORDER BY egg_group_id LIMIT 1) AS egg_group_1, " +
"(SELECT egg_group_id FROM pokemon_egg_groups WHERE species_id = pokemon_species.id ORDER BY egg_group_id LIMIT 1, 1) AS egg_group_2, " +
"hatch_counter, has_gender_differences " +
"FROM pokemon_species " +
"ORDER BY pokemon_species.id");
while (rdPokemon.Read())
{
int id = Convert.ToInt32(rdPokemon["id"]);
byte growth_rate_id = Convert.ToByte(rdPokemon["growth_rate_id"]);
int gender_rate = Convert.ToInt32(rdPokemon["gender_rate"]);
byte egg_group_1 = Convert.ToByte(rdPokemon["egg_group_1"]);
byte egg_group_2 = 0;
if (!(rdPokemon["egg_group_2"] is DBNull)) egg_group_2 = Convert.ToByte(rdPokemon["egg_group_2"]);
int hatch_counter = Convert.ToInt32(rdPokemon["hatch_counter"]);
byte has_gender_differences = Convert.ToByte(rdPokemon["has_gender_differences"]);
// todo: Family ID
Species s = new Species(null, id,
GetLocalizedString(rdPokemon, "name_"),
(GrowthRates)growth_rate_id,
(byte)gender_rate,
(EggGroups)egg_group_1,
(EggGroups)egg_group_2,
(byte)hatch_counter,
has_gender_differences != 0
);
db.PokedexInsertSpecies(s);
Console.WriteLine("Inserted {0} {1} {2} {3} {4} {5}",
s.NationalDex, s.Name.ToString(), s.GrowthRate, s.GenderRatio, s.EggSteps, s.GenderVariations);
}
rdPokemon.Close();
// pkmncf_pokedex_pokemon_forms
SQLiteDataReader rdForms = (SQLiteDataReader)connVeekun.ExecuteReader("SELECT id, " +
"(SELECT species_id FROM pokemon WHERE id = pokemon_id) AS NationalDex, " +
"form_order - 1 AS FormValue, " +
"(SELECT pokemon_form_names.form_name FROM pokemon_form_names WHERE pokemon_form_names.pokemon_form_id = pokemon_forms.id AND local_language_id = 1) AS name_ja, " +
"(SELECT pokemon_form_names.form_name FROM pokemon_form_names WHERE pokemon_form_names.pokemon_form_id = pokemon_forms.id AND local_language_id = 9) AS name_en, " +
"(SELECT pokemon_form_names.form_name FROM pokemon_form_names WHERE pokemon_form_names.pokemon_form_id = pokemon_forms.id AND local_language_id = 5) AS name_fr, " +
"(SELECT pokemon_form_names.form_name FROM pokemon_form_names WHERE pokemon_form_names.pokemon_form_id = pokemon_forms.id AND local_language_id = 8) AS name_it, " +
"(SELECT pokemon_form_names.form_name FROM pokemon_form_names WHERE pokemon_form_names.pokemon_form_id = pokemon_forms.id AND local_language_id = 6) AS name_de, " +
"(SELECT pokemon_form_names.form_name FROM pokemon_form_names WHERE pokemon_form_names.pokemon_form_id = pokemon_forms.id AND local_language_id = 7) AS name_es, " +
"(SELECT pokemon_form_names.form_name FROM pokemon_form_names WHERE pokemon_form_names.pokemon_form_id = pokemon_forms.id AND local_language_id = 3) AS name_ko, " +
"form_identifier, " +
"(SELECT height FROM pokemon WHERE id = pokemon_id) AS height, " +
"(SELECT weight FROM pokemon WHERE id = pokemon_id) AS weight, " +
"(SELECT base_experience FROM pokemon WHERE id = pokemon_id) AS experience " +
"FROM pokemon_forms");
while (rdForms.Read())
{
int id = Convert.ToInt32(rdForms["id"]);
int NationalDex = Convert.ToInt32(rdForms["NationalDex"]);
byte FormValue = Convert.ToByte(rdForms["FormValue"]);
String form_identifier = rdForms["form_identifier"].ToString();
int height = Convert.ToInt32(rdForms["height"]);
int weight = Convert.ToInt32(rdForms["weight"]);
int experience = Convert.ToInt32(rdForms["experience"]);
Form f = new Form(null, id,
NationalDex, FormValue,
GetLocalizedString(rdForms, "name_"),
form_identifier,
height,
weight,
experience
);
db.PokedexInsertForm(f);
Console.WriteLine("Inserted {0} {1} {2} {3} {4} {5:0.0}m {6:0.0}kg {7}",
f.ID, f.NationalDex, f.Value, f.Name.ToString(),
f.Suffix, f.Height * 0.1f, f.Weight * 0.1f, f.Experience);
}
rdForms.Close();
// pkmncf_pokedex_pokemon_families
// pkmncf_pokedex_pokemon_evolutions
// pkmncf_pokedex_types
SQLiteDataReader rdTypes = (SQLiteDataReader)connVeekun.ExecuteReader("SELECT id, damage_class_id, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 1) AS name_ja, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 9) AS name_en, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 5) AS name_fr, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 8) AS name_it, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 6) AS name_de, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 7) AS name_es, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 3) AS name_ko " +
"FROM types ORDER BY id");
// http://stackoverflow.com/questions/27156585/failed-to-enable-constraints-without-dataadapter
while (rdTypes.Read())
{
int id = Convert.ToInt32(rdTypes["id"]);
PkmnFoundations.Pokedex.Type t = new PkmnFoundations.Pokedex.Type(null,
id,
GetLocalizedString(rdTypes, "name_"),
GetDamageClass(rdTypes));
db.PokedexInsertType(t);
Console.WriteLine("Inserted {0} {1} {2}", t.ID, t.Name.ToString(), t.DamageClass);
}
rdTypes.Close();
SQLiteDataReader rdMoves = (SQLiteDataReader)connVeekun.ExecuteReader("SELECT id, type_id, " +
"(SELECT name FROM move_names WHERE move_names.move_id = moves.id AND local_language_id = 1) AS name_ja, " +
"(SELECT name FROM move_names WHERE move_names.move_id = moves.id AND local_language_id = 9) AS name_en, " +
"(SELECT name FROM move_names WHERE move_names.move_id = moves.id AND local_language_id = 5) AS name_fr, " +
"(SELECT name FROM move_names WHERE move_names.move_id = moves.id AND local_language_id = 8) AS name_it, " +
"(SELECT name FROM move_names WHERE move_names.move_id = moves.id AND local_language_id = 6) AS name_de, " +
"(SELECT name FROM move_names WHERE move_names.move_id = moves.id AND local_language_id = 7) AS name_es, " +
"(SELECT name FROM move_names WHERE move_names.move_id = moves.id AND local_language_id = 3) AS name_ko, " +
"damage_class_id, power, pp, accuracy, priority, target_id FROM moves");
while (rdMoves.Read())
{
int id = Convert.ToInt32(rdMoves["id"]);
int type_id = Convert.ToInt32(rdMoves["type_id"]);
int damage_class_id = Convert.ToInt32(rdMoves["damage_class_id"]);
short power = DatabaseExtender.Coalesce(rdMoves["power"], (short)0);
short pp = DatabaseExtender.Coalesce(rdMoves["pp"], (short)0);
short accuracy = DatabaseExtender.Coalesce(rdMoves["accuracy"], (short)0);
short priority = Convert.ToInt16(rdMoves["priority"]);
int target_id = Convert.ToInt32(rdMoves["target_id"]);
Move m = new Move(null,
id,
type_id,
GetLocalizedString(rdMoves, "name_"),
(DamageClass)damage_class_id,
power,
pp,
accuracy,
priority,
(BattleTargets)target_id
);
db.PokedexInsertMove(m);
Console.WriteLine("Inserted {0} {1}", m.ID, m.Name.ToString());
}
SQLiteDataReader rdAbilities = (SQLiteDataReader)connVeekun.ExecuteReader("SELECT id, " +
"(SELECT name FROM ability_names WHERE ability_names.ability_id = abilities.id AND local_language_id = 1) AS name_ja, " +
"(SELECT name FROM ability_names WHERE ability_names.ability_id = abilities.id AND local_language_id = 9) AS name_en, " +
"(SELECT name FROM ability_names WHERE ability_names.ability_id = abilities.id AND local_language_id = 5) AS name_fr, " +
"(SELECT name FROM ability_names WHERE ability_names.ability_id = abilities.id AND local_language_id = 8) AS name_it, " +
"(SELECT name FROM ability_names WHERE ability_names.ability_id = abilities.id AND local_language_id = 6) AS name_de, " +
"(SELECT name FROM ability_names WHERE ability_names.ability_id = abilities.id AND local_language_id = 7) AS name_es, " +
"(SELECT name FROM ability_names WHERE ability_names.ability_id = abilities.id AND local_language_id = 3) AS name_ko " +
"FROM abilities WHERE is_main_series = 1");
while (rdAbilities.Read())
{
int id = Convert.ToInt32(rdAbilities["id"]);
Ability a = new Ability(null,
id, GetLocalizedString(rdAbilities, "name_")
);
db.PokedexInsertAbility(a);
Console.WriteLine("Inserted {0} {1}", a.Value, a.Name.ToString());
}
rdAbilities.Close();
Dictionary<int, ItemLoading> items = new Dictionary<int, ItemLoading>();
for (int generation = 3; generation < 7; generation++)
{
String filename = String.Format("items{0}.txt", generation);
if (!File.Exists(filename))
{
Console.WriteLine("File {0} not found, skipped.", filename);
continue;
}
using (FileStream fs = File.Open(filename, FileMode.Open))
{
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
int lineNumber = -1;
String line;
while ((line = sr.ReadLine()) != null)
{
lineNumber++;
String[] fields = line.Split('\t');
if (fields.Length != 3)
{
Console.WriteLine("{0} line {1} has bad format, skipped.", filename, lineNumber);
continue;
}
int id, thisGenId;
String name = fields[1];
if (!Int32.TryParse(fields[0], out thisGenId) ||
!Int32.TryParse(fields[2], out id))
{
Console.WriteLine("{0} line {1} has bad format, skipped.", filename, lineNumber);
continue;
}
ItemLoading theItem = null;
if (!items.ContainsKey(id))
{
theItem = new ItemLoading(id, name);
items.Add(id, theItem);
}
theItem = theItem ?? items[id];
theItem.Name = name; // prefer newer names where available
switch (generation)
{
// todo: maybe use an array or dictionary for these fields.
case 3:
theItem.Value3 = thisGenId;
break;
case 4:
theItem.Value4 = thisGenId;
break;
case 5:
theItem.Value5 = thisGenId;
break;
case 6:
theItem.Value6 = thisGenId;
break;
}
}
}
}
// lookup Veekun ID number against some generation or another.
Dictionary<int, ItemLoading> items3 = items
.Where(i => i.Value.Value3 != null && i.Value.NameLocalized == null)
.ToDictionary(i => (int)i.Value.Value3, i => i.Value);
Dictionary<int, ItemLoading> items4 = items
.Where(i => i.Value.Value4 != null && i.Value.NameLocalized == null)
.ToDictionary(i => (int)i.Value.Value4, i => i.Value);
Dictionary<int, ItemLoading> items5 = items
.Where(i => i.Value.Value5 != null && i.Value.NameLocalized == null)
.ToDictionary(i => (int)i.Value.Value5, i => i.Value);
Dictionary<int, ItemLoading> items6 = items
.Where(i => i.Value.Value6 != null && i.Value.NameLocalized == null)
.ToDictionary(i => (int)i.Value.Value6, i => i.Value);
SQLiteDataReader rdItems = (SQLiteDataReader)connVeekun.ExecuteReader("SELECT id, cost, " +
"(SELECT game_index FROM item_game_indices WHERE item_id = items.id AND generation_id = 3) AS value3, " +
"(SELECT game_index FROM item_game_indices WHERE item_id = items.id AND generation_id = 4) AS value4, " +
"(SELECT game_index FROM item_game_indices WHERE item_id = items.id AND generation_id = 5) AS value5, " +
"(SELECT game_index FROM item_game_indices WHERE item_id = items.id AND generation_id = 6) AS value6, " +
"(SELECT name FROM item_names WHERE item_names.item_id = items.id AND local_language_id = 1) AS name_ja, " +
"(SELECT name FROM item_names WHERE item_names.item_id = items.id AND local_language_id = 9) AS name_en, " +
"(SELECT name FROM item_names WHERE item_names.item_id = items.id AND local_language_id = 5) AS name_fr, " +
"(SELECT name FROM item_names WHERE item_names.item_id = items.id AND local_language_id = 8) AS name_it, " +
"(SELECT name FROM item_names WHERE item_names.item_id = items.id AND local_language_id = 6) AS name_de, " +
"(SELECT name FROM item_names WHERE item_names.item_id = items.id AND local_language_id = 7) AS name_es, " +
"(SELECT name FROM item_names WHERE item_names.item_id = items.id AND local_language_id = 3) AS name_ko " +
"FROM items");
Dictionary<int, ItemLoading>[] itemsGeneration = new Dictionary<int, ItemLoading>[] { null, null, items3, items4, items5, items6 };
while (rdItems.Read())
{
List<ItemLoading> toProcess = new List<ItemLoading>(4);
for (int generation = 3; generation < 7; generation++)
{
String col = String.Format("value{0}", generation);
if (!(rdItems[col] is DBNull))
{
Dictionary<int, ItemLoading> dict = itemsGeneration[generation - 1];
int value = Convert.ToInt32(rdItems[col]);
if (dict.ContainsKey(value))
{
ItemLoading il = dict[value];
il.NameLocalized = GetLocalizedString(rdItems, "name_");
il.Price = Convert.ToInt32(rdItems["cost"]);
}
}
}
}
rdItems.Close();
foreach (ItemLoading il in items.Values)
{
LocalizedString name = il.NameLocalized;
if (name == null)
{
name = new LocalizedString() { { "EN", il.Name } };
Console.WriteLine("Veekun database missing item {0} {1}. Non-English translations will be missing.", il.ID, name);
}
Item i = new Item(null, il.ID, il.Value3, il.Value4, il.Value5, il.Value6, il.Price, name);
db.PokedexInsertItem(i);
Console.WriteLine("Inserted {0} {1}", i.ID, i.Name.ToString());
}
connVeekun.Close();
}
Console.ReadKey();
}
private static String[] LANGS = { "JA", "EN", "FR", "IT", "DE", "ES", "KO" };
private static LocalizedString GetLocalizedString(IDataReader reader, String column)
{
LocalizedString result = new LocalizedString();
foreach (String lang in LANGS)
{
String col = column + lang.ToLowerInvariant();
if (reader[col] is DBNull) continue;
result.Add(lang, (String)reader[col]);
}
return result;
}
private static DamageClass GetDamageClass(IDataReader reader)
{
if (reader["damage_class_id"] is DBNull) return DamageClass.None;
return (DamageClass)(Convert.ToInt32(reader["damage_class_id"]) - 1);
}
}
internal class ItemLoading
{
public ItemLoading(int id, String name)
{
ID = id;
Name = name;
Value3 = Value4 = Value5 = Value6 = null;
NameLocalized = null;
}
public int ID;
public String Name;
public LocalizedString NameLocalized;
public int? Value3, Value4, Value5, Value6;
public int Price;
}
}

View File

@ -9,11 +9,12 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>VeekunImport</RootNamespace>
<AssemblyName>VeekunImport</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
@ -33,7 +34,11 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SQLite">
<HintPath>..\library\lib\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -45,9 +50,24 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="items3.txt" />
<Content Include="items5.txt" />
<Content Include="items4.txt" />
<Content Include="items3.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="items5.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="items4.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\library\Library.csproj">
<Project>{408efc7e-c6b0-4160-8628-2679e34385ce}</Project>
<Name>Library</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

10
VeekunImport/app.config Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="pkmnFoundationsConnectionString"
connectionString="Server=10.211.55.2;Database=gts;User ID=gts;Password=gts;Pooling=true;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>

View File

@ -7,6 +7,7 @@ using MySql.Data.MySqlClient;
using PkmnFoundations.Structures;
using PkmnFoundations.Support;
using System.Security.Cryptography;
using PkmnFoundations.Pokedex;
namespace PkmnFoundations.Data
{
@ -2510,5 +2511,49 @@ namespace PkmnFoundations.Data
}
}
#endregion
#region Pokedex
public override void PokedexInsertSpecies(Species s)
{
//throw new NotImplementedException();
}
public override void PokedexInsertForm(Form f)
{
//throw new NotImplementedException();
}
public override void PokedexInsertFamily(Family f)
{
//throw new NotImplementedException();
}
public override void PokedexInsertEvolution(Evolution f)
{
//throw new NotImplementedException();
}
public override void PokedexInsertType(Pokedex.Type t)
{
//throw new NotImplementedException();
}
public override void PokedexInsertItem(Item i)
{
//throw new NotImplementedException();
}
public override void PokedexInsertMove(Move m)
{
//throw new NotImplementedException();
}
public override void PokedexInsertAbility(Ability a)
{
//throw new NotImplementedException();
}
#endregion
}
}

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Data;
using PkmnFoundations.Structures;
using System.Configuration;
using PkmnFoundations.Pokedex;
namespace PkmnFoundations.Data
{
@ -150,5 +151,18 @@ namespace PkmnFoundations.Data
public abstract ulong BattleVideoCount5();
#endregion
#region Pokedex
public abstract void PokedexInsertSpecies(Species s);
public abstract void PokedexInsertForm(Form f);
public abstract void PokedexInsertFamily(Family f);
public abstract void PokedexInsertEvolution(Evolution f);
public abstract void PokedexInsertType(PkmnFoundations.Pokedex.Type t);
public abstract void PokedexInsertItem(Item i);
public abstract void PokedexInsertMove(Move m);
public abstract void PokedexInsertAbility(Ability a);
#endregion
}
}

View File

@ -486,5 +486,20 @@ namespace PkmnFoundations.Data
return GetByteArray(reader, reader.GetOrdinal(column), length);
}
#endregion
/// <summary>
/// Coalesces nulls and DBNulls down to a default value
/// </summary>
/// <typeparam name="T">Return type</typeparam>
/// <param name="value"></param>
/// <param name="_default"></param>
/// <returns></returns>
/// <exception cref="System.InvalidCastException">value is neither null, DBNull, nor of type T</exception>
public static T Coalesce<T>(object value, T _default)
{
if (value == null) return _default;
if (value is DBNull) return _default;
return (T)value; // allow InvalidCastException to escape
}
}
}

View File

@ -52,6 +52,15 @@
<Compile Include="Data\DatabaseExtender.cs" />
<Compile Include="Data\DataMysql.cs" />
<Compile Include="Data\DataSqlite.cs" />
<Compile Include="Pokedex\Ability.cs" />
<Compile Include="Pokedex\Evolution.cs" />
<Compile Include="Pokedex\Item.cs" />
<Compile Include="Pokedex\Move.cs" />
<Compile Include="Pokedex\Pokedex.cs" />
<Compile Include="Pokedex\Species.cs" />
<Compile Include="Pokedex\Family.cs" />
<Compile Include="Pokedex\Form.cs" />
<Compile Include="Pokedex\Type.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Structures\BattleSubwayPokemon5.cs" />
<Compile Include="Structures\BattleSubwayProfile5.cs" />
@ -75,6 +84,7 @@
<Compile Include="Support\AssertHelper.cs" />
<Compile Include="Support\EncodedString4.cs" />
<Compile Include="Support\EncodedString5.cs" />
<Compile Include="Support\LocalizedString.cs" />
<Compile Include="Support\LogHelper.cs" />
<Compile Include="Support\StreamExtender.cs" />
<Compile Include="Support\StringHelper.cs" />

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PkmnFoundations.Support;
namespace PkmnFoundations.Pokedex
{
public class Ability
{
public Ability(Pokedex pokedex, int value, LocalizedString name)
{
m_pokedex = pokedex;
Value = value;
Name = name;
}
private Pokedex m_pokedex;
public int Value { get; private set; }
public LocalizedString Name { get; private set; }
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PkmnFoundations.Pokedex
{
public class Evolution
{
}
}

11
library/Pokedex/Family.cs Normal file
View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PkmnFoundations.Pokedex
{
public class Family
{
}
}

36
library/Pokedex/Form.cs Normal file
View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PkmnFoundations.Support;
namespace PkmnFoundations.Pokedex
{
public class Form
{
public Form(Pokedex pokedex, int id, int national_dex, byte value,
LocalizedString name, String suffix, int height, int weight, int experience)
{
m_pokedex = pokedex;
ID = id;
NationalDex = national_dex;
Value = value;
Name = name;
Suffix = suffix;
Height = height;
Weight = weight;
Experience = experience;
}
private Pokedex m_pokedex;
public int ID { get; private set; }
public int NationalDex { get; private set; }
public byte Value { get; private set; }
public LocalizedString Name { 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; }
}
}

34
library/Pokedex/Item.cs Normal file
View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PkmnFoundations.Support;
namespace PkmnFoundations.Pokedex
{
public class Item
{
public Item(Pokedex pokedex, int id, int ? value3, int ? value4, int ? value5, int ? value6, int price, LocalizedString name)
{
m_pokedex = pokedex;
ID = id;
Value3 = value3;
Value4 = value4;
Value5 = value5;
Value6 = value6;
Price = price;
Name = name;
}
private Pokedex m_pokedex;
public int ID { get; private set; }
public int ? Value3 { get; private set; }
public int ? Value4 { get; private set; }
public int ? Value5 { get; private set; }
public int ? Value6 { get; private set; }
public int Price { get; private set; }
public LocalizedString Name { get; private set; }
}
}

42
library/Pokedex/Move.cs Normal file
View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PkmnFoundations.Structures;
using PkmnFoundations.Support;
namespace PkmnFoundations.Pokedex
{
public class Move
{
public Move(Pokedex pokedex, int id, int type_id, LocalizedString name,
DamageClass damage_class, int damage, int pp, int accuracy, int priority,
BattleTargets target)
{
m_pokedex = pokedex;
ID = id;
TypeID = type_id;
Name = name;
DamageClass = damage_class;
Damage = damage;
PP = pp;
Accuracy = accuracy;
Priority = priority;
Target = target;
}
private Pokedex m_pokedex;
public int ID { get; private set; }
// todo: this field should be private and only dealt with during construction.
// Instead, an actual Type object should be exposed and its ID number can be retrieved if necessary.
public int TypeID { get; private set; }
public LocalizedString Name { get; private set; }
public DamageClass DamageClass { get; private set; }
public int Damage { get; private set; }
public int PP { get; private set; }
public int Accuracy { get; private set; }
public int Priority { get; private set; }
public BattleTargets Target { get; private set; }
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PkmnFoundations.Pokedex
{
public class Pokedex
{
}
}

View File

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PkmnFoundations.Structures;
using PkmnFoundations.Support;
namespace PkmnFoundations.Pokedex
{
public class Species
{
public Species(Pokedex pokedex, int national_dex, LocalizedString name,
GrowthRates growth_rate, byte gender_ratio, EggGroups egg_group_1,
EggGroups egg_group_2, int egg_steps, bool gender_variations)
{
m_pokedex = pokedex;
NationalDex = national_dex;
Name = name;
GrowthRate = growth_rate;
GenderRatio = gender_ratio;
EggGroup1 = egg_group_1;
EggGroup2 = egg_group_2;
EggSteps = egg_steps;
GenderVariations = gender_variations;
//if (pokedex != null && pokedex.Eager)
{
// Retrieve foreign information like Family and Formes.
// Otherwise, this information will be lazily evaluated
// from the Pokedex instance.
// If pokedex is null, this information is unavailable.
// todo: database ownership/lazy stuff can go in a base class.
}
}
/*
* Species s = new Species(null, Convert.ToInt32(row["id"]),
new LocalizedString(){{"JA", row["name_ja"].ToString()},
{"EN", row["name_en"].ToString()},
{"FR", row["name_fr"].ToString()},
{"IT", row["name_it"].ToString()},
{"DE", row["name_de"].ToString()},
{"ES", row["name_es"].ToString()},
{"KO", row["name_ko"].ToString()}
},
(GrowthRates)(Convert.ToByte(row["growth_rate_id"])),
(byte)Convert.ToInt32(row["gender_rate"]),
(byte)Convert.ToInt32(row["hatch_counter"]),
Convert.ToByte(row["has_gender_differences"]) != 0
);
* */
// todo: Implement IEquitable and compare against NationalDex
// Same goes for all these pokedex classes.
private Pokedex m_pokedex;
public int NationalDex { get; private set; }
public LocalizedString Name { get; private set; }
public GrowthRates GrowthRate { get; private set; }
public byte GenderRatio { get; private set; }
public EggGroups EggGroup1 { get; private set; }
public EggGroups EggGroup2 { get; private set; }
public int EggSteps { get; private set; }
public bool GenderVariations { get; private set; }
}
}

26
library/Pokedex/Type.cs Normal file
View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PkmnFoundations.Structures;
using PkmnFoundations.Support;
namespace PkmnFoundations.Pokedex
{
public class Type
{
public Type(Pokedex pokedex, int id, LocalizedString name, DamageClass damage_class)
{
m_pokedex = pokedex;
ID = id;
Name = name;
DamageClass = damage_class;
}
private Pokedex m_pokedex;
public int ID { get; private set; }
public LocalizedString Name { get; private set; }
public DamageClass DamageClass { get; private set; }
}
}

View File

@ -93,6 +93,7 @@ namespace PkmnFoundations.Structures
public enum DamageClass
{
None = 0,
Physical = 1,
Special = 2,
Support = 3
@ -132,4 +133,44 @@ namespace PkmnFoundations.Structures
Star = 0x10,
Diamond = 0x20
}
public enum EggGroups : byte
{
None = 0,
Monster = 1,
Water1 = 2,
Bug = 3,
Flying = 4,
Ground = 5,
Fairy = 6,
Plant = 7,
HumanShape = 8,
Water3 = 9,
Mineral = 10,
Indeterminate = 11,
Water2 = 12,
Ditto = 13,
Dragon = 14,
NoEggs = 15
}
public enum BattleTargets : byte
{
// Pasted straight from veekun.
// todo: Review and maybe clarify these names
SpecificMove = 1,
SelectedPokemonMeFirst = 2,
Ally = 3,
UsersField = 4,
UserOrAlly = 5,
OpponentsField = 6,
User = 7,
RandomOpponent = 8,
AllOtherPokemon = 9,
SelectedPokemon = 10,
AllOpponents = 11,
EntireField = 12,
UserAndAllies = 13,
AllPokemon = 14
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace PkmnFoundations.Support
{
public class LocalizedString : Dictionary<String, String>
{
public LocalizedString()
{
}
public override string ToString()
{
if (Count == 0) return null;
String lang = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpperInvariant();
// fixme: this is not O(1) but should be
try
{
return this[lang];
}
catch (KeyNotFoundException)
{
}
try
{
return this["EN"];
}
catch (KeyNotFoundException)
{
}
return Values.First();
}
}
}