mirror of
https://github.com/kwsch/PKHeX
synced 2025-02-16 21:38:40 +00:00
parent
3f1335e19f
commit
36bf73818e
5 changed files with 205 additions and 7 deletions
151
PKHeX.Core/Editing/Database/TrainerDatabase.cs
Normal file
151
PKHeX.Core/Editing/Database/TrainerDatabase.cs
Normal file
|
@ -0,0 +1,151 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace PKHeX.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains many <see cref="ITrainerInfo"/> instances to match against a <see cref="GameVersion"/>.
|
||||
/// </summary>
|
||||
public sealed class TrainerDatabase
|
||||
{
|
||||
private readonly Dictionary<GameVersion, List<ITrainerInfo>> Database = new();
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an appropriate trainer based on the requested <see cref="version"/>.
|
||||
/// </summary>
|
||||
/// <param name="version">Version the trainer should originate from</param>
|
||||
/// <param name="language">Language to request for</param>
|
||||
/// <returns>Null if no trainer found for this version.</returns>
|
||||
public ITrainerInfo? GetTrainer(int version, LanguageID? language = null) => GetTrainer((GameVersion)version, language);
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an appropriate trainer based on the requested <see cref="ver"/>.
|
||||
/// </summary>
|
||||
/// <param name="ver">Version the trainer should originate from</param>
|
||||
/// <param name="language">Language to request for</param>
|
||||
/// <returns>Null if no trainer found for this version.</returns>
|
||||
public ITrainerInfo? GetTrainer(GameVersion ver, LanguageID? language = null)
|
||||
{
|
||||
if (ver <= 0)
|
||||
return null;
|
||||
|
||||
if (!ver.IsValidSavedVersion())
|
||||
return GetTrainerFromGroup(ver, language);
|
||||
|
||||
if (Database.TryGetValue(ver, out var list))
|
||||
return GetRandomChoice(list);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static T GetRandomChoice<T>(IReadOnlyList<T> list)
|
||||
{
|
||||
if (list.Count == 1)
|
||||
return list[0];
|
||||
return list[Util.Rand.Next(list.Count)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an appropriate trainer based on the requested <see cref="ver"/> group.
|
||||
/// </summary>
|
||||
/// <param name="ver">Version the trainer should originate from</param>
|
||||
/// <param name="lang">Language to request for</param>
|
||||
/// <returns>Null if no trainer found for this version.</returns>
|
||||
private ITrainerInfo? GetTrainerFromGroup(GameVersion ver, LanguageID? lang = null)
|
||||
{
|
||||
var possible = Database.Where(z => ver.Contains(z.Key)).ToList();
|
||||
if (lang != null)
|
||||
{
|
||||
possible = possible.Select(z =>
|
||||
{
|
||||
var filtered = z.Value.Where(x => x.Language == (int)lang).ToList();
|
||||
return new KeyValuePair<GameVersion, List<ITrainerInfo>>(z.Key, filtered);
|
||||
}).Where(z => z.Value.Count != 0).ToList();
|
||||
}
|
||||
return GetRandomTrainer(possible);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an appropriate trainer based on the requested <see cref="generation"/>.
|
||||
/// </summary>
|
||||
/// <param name="generation">Generation the trainer should inhabit</param>
|
||||
/// <param name="lang">Language to request for</param>
|
||||
/// <returns>Null if no trainer found for this version.</returns>
|
||||
public ITrainerInfo? GetTrainerFromGen(int generation, LanguageID? lang = null)
|
||||
{
|
||||
var possible = Database.Where(z => z.Key.GetGeneration() == generation).ToList();
|
||||
if (lang != null)
|
||||
{
|
||||
possible = possible.Select(z =>
|
||||
{
|
||||
var filtered = z.Value.Where(x => x.Language == (int)lang).ToList();
|
||||
return new KeyValuePair<GameVersion, List<ITrainerInfo>>(z.Key, filtered);
|
||||
}).Where(z => z.Value.Count != 0).ToList();
|
||||
}
|
||||
return GetRandomTrainer(possible);
|
||||
}
|
||||
|
||||
private static ITrainerInfo? GetRandomTrainer(IReadOnlyList<KeyValuePair<GameVersion, List<ITrainerInfo>>> possible)
|
||||
{
|
||||
if (possible.Count == 0)
|
||||
return null;
|
||||
var group = GetRandomChoice(possible);
|
||||
return GetRandomChoice(group.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the <see cref="trainer"/> to the <see cref="Database"/>.
|
||||
/// </summary>
|
||||
/// <param name="trainer">Trainer details to add.</param>
|
||||
public void Register(ITrainerInfo trainer)
|
||||
{
|
||||
var ver = (GameVersion)trainer.Game;
|
||||
if (ver <= 0 && trainer is SaveFile s)
|
||||
ver = s.Version;
|
||||
if (!Database.TryGetValue(ver, out var list))
|
||||
{
|
||||
Database.Add(ver, new List<ITrainerInfo> { trainer });
|
||||
return;
|
||||
}
|
||||
|
||||
if (list.Contains(trainer))
|
||||
return;
|
||||
list.Add(trainer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the trainer details of the <see cref="pkm"/> to the <see cref="Database"/>.
|
||||
/// </summary>
|
||||
/// <param name="pkm">Pokémon with Trainer details to add.</param>
|
||||
/// <remarks>A copy of the object will be made to prevent modifications, just in case.</remarks>
|
||||
public void RegisterCopy(PKM pkm) => Register(GetTrainerReference(pkm));
|
||||
|
||||
/// <summary>
|
||||
/// Adds the trainer details of the <see cref="info"/> to the <see cref="Database"/>.
|
||||
/// </summary>
|
||||
/// <param name="info">Pokémon with Trainer details to add.</param>
|
||||
/// <remarks>A copy of the object will be made to prevent modifications, just in case.</remarks>
|
||||
public void RegisterCopy(ITrainerInfo info) => Register(new SimpleTrainerInfo(info));
|
||||
|
||||
private static ITrainerInfo GetTrainerReference(PKM pkm)
|
||||
{
|
||||
var result = new SimpleTrainerInfo((GameVersion)pkm.Version)
|
||||
{
|
||||
TID = pkm.TID, SID = pkm.SID, OT = pkm.OT_Name, Gender = pkm.OT_Gender,
|
||||
Language = pkm.Language,
|
||||
Generation = pkm.Generation,
|
||||
};
|
||||
|
||||
if (pkm is IRegionOrigin r)
|
||||
r.CopyRegionOrigin(result);
|
||||
else
|
||||
result.SetDefaultRegionOrigins();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all trainer details from the <see cref="Database"/>.
|
||||
/// </summary>
|
||||
public void Clear() => Database.Clear();
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
namespace PKHeX.Core
|
||||
{
|
||||
public sealed class SimpleTrainerInfo : ITrainerInfo, IRegionOrigin
|
||||
public sealed record SimpleTrainerInfo : ITrainerInfo, IRegionOrigin
|
||||
{
|
||||
public string OT { get; set; } = "PKHeX";
|
||||
public int TID { get; set; } = 12345;
|
||||
public int SID { get; set; } = 54321;
|
||||
public int Gender { get; set; } = 0;
|
||||
public int Gender { get; set; }
|
||||
public int Language { get; set; } = (int)LanguageID.English;
|
||||
|
||||
// IRegionOrigin for generation 6/7
|
||||
|
@ -19,8 +19,28 @@
|
|||
public SimpleTrainerInfo(GameVersion game = GameVersion.SW)
|
||||
{
|
||||
Game = (int) game;
|
||||
SanityCheckRegionOrigin(game);
|
||||
}
|
||||
|
||||
private void SanityCheckRegionOrigin(GameVersion game)
|
||||
{
|
||||
if (GameVersion.Gen7b.Contains(game) || game.GetGeneration() >= 8)
|
||||
ConsoleRegion = Region = Country = 0;
|
||||
this.ClearRegionOrigin();
|
||||
}
|
||||
|
||||
public SimpleTrainerInfo(ITrainerInfo other) : this((GameVersion)other.Game)
|
||||
{
|
||||
OT = other.OT;
|
||||
TID = other.TID;
|
||||
SID = other.SID;
|
||||
Gender = other.Gender;
|
||||
Language = other.Language;
|
||||
Generation = other.Generation;
|
||||
|
||||
if (other is IRegionOrigin r)
|
||||
r.CopyRegionOrigin(this);
|
||||
|
||||
SanityCheckRegionOrigin((GameVersion)Game);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,5 +19,17 @@
|
|||
o.Region = 7; // California
|
||||
o.Country = 49; // USA
|
||||
}
|
||||
|
||||
public static void CopyRegionOrigin(this IRegionOrigin source, IRegionOrigin dest)
|
||||
{
|
||||
dest.ConsoleRegion = source.ConsoleRegion;
|
||||
dest.Country = source.Country;
|
||||
dest.Region = source.Region;
|
||||
}
|
||||
|
||||
public static void ClearRegionOrigin(this IRegionOrigin o)
|
||||
{
|
||||
o.ConsoleRegion = o.Region = o.Country = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,6 +103,7 @@ namespace PKHeX.WinForms
|
|||
public static readonly string BackupPath = Path.Combine(WorkingDirectory, "bak");
|
||||
public static readonly string CryPath = Path.Combine(WorkingDirectory, "sounds");
|
||||
private static readonly string TemplatePath = Path.Combine(WorkingDirectory, "template");
|
||||
private static readonly string TrainerPath = Path.Combine(WorkingDirectory, "trainers");
|
||||
private static readonly string PluginPath = Path.Combine(WorkingDirectory, "plugins");
|
||||
private const string ThreadPath = "https://projectpokemon.org/pkhex/";
|
||||
|
||||
|
@ -325,8 +326,19 @@ namespace PKHeX.WinForms
|
|||
|
||||
private void Menu_EncDatabase_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!this.OpenWindowExists<SAV_Encounters>())
|
||||
new SAV_Encounters(PKME_Tabs).Show();
|
||||
if (this.OpenWindowExists<SAV_Encounters>())
|
||||
return;
|
||||
|
||||
var db = new TrainerDatabase();
|
||||
var sav = C_SAV.SAV;
|
||||
Task.Run(() =>
|
||||
{
|
||||
var files = Directory.EnumerateFiles(TrainerPath, "*.*", SearchOption.AllDirectories);
|
||||
var pkm = BoxUtil.GetPKMsFromPaths(files, sav.Generation);
|
||||
foreach (var f in pkm)
|
||||
db.RegisterCopy(f);
|
||||
});
|
||||
new SAV_Encounters(PKME_Tabs, db).Show();
|
||||
}
|
||||
|
||||
private void MainMenuMysteryDB(object sender, EventArgs e)
|
||||
|
|
|
@ -19,12 +19,14 @@ namespace PKHeX.WinForms
|
|||
private readonly PKMEditor PKME_Tabs;
|
||||
private SaveFile SAV => PKME_Tabs.RequestSaveFile;
|
||||
private readonly SummaryPreviewer ShowSet = new();
|
||||
private readonly TrainerDatabase Trainers;
|
||||
|
||||
public SAV_Encounters(PKMEditor f1)
|
||||
public SAV_Encounters(PKMEditor f1, TrainerDatabase db)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
PKME_Tabs = f1;
|
||||
Trainers = db;
|
||||
|
||||
var grid = EncounterPokeGrid;
|
||||
var smallWidth = grid.Width;
|
||||
|
@ -132,7 +134,8 @@ namespace PKHeX.WinForms
|
|||
|
||||
var enc = Results[index];
|
||||
var criteria = GetCriteria(enc, Main.Settings.EncounterDb);
|
||||
var pk = enc.ConvertToPKM(SAV, criteria);
|
||||
var trainer = Trainers.GetTrainer(enc.Version, enc.Generation <= 2 ? (LanguageID)SAV.Language : null) ?? SAV;
|
||||
var pk = enc.ConvertToPKM(trainer, criteria);
|
||||
pk.RefreshChecksum();
|
||||
PKME_Tabs.PopulateFields(pk, false);
|
||||
slotSelected = index;
|
||||
|
|
Loading…
Add table
Reference in a new issue