2023-01-22 04:02:33 +00:00
|
|
|
namespace PKHeX.Core;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tracks Geolocation history of a <see cref="PKM"/>
|
|
|
|
/// </summary>
|
|
|
|
public interface IGeoTrack : IRegionOrigin
|
2018-07-14 17:34:34 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
byte Geo1_Region { get; set; }
|
|
|
|
byte Geo2_Region { get; set; }
|
|
|
|
byte Geo3_Region { get; set; }
|
|
|
|
byte Geo4_Region { get; set; }
|
|
|
|
byte Geo5_Region { get; set; }
|
|
|
|
byte Geo1_Country { get; set; }
|
|
|
|
byte Geo2_Country { get; set; }
|
|
|
|
byte Geo3_Country { get; set; }
|
|
|
|
byte Geo4_Country { get; set; }
|
|
|
|
byte Geo5_Country { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static partial class Extensions
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Clears all Geolocation history.
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void ClearGeoLocationData(this IGeoTrack g)
|
2018-07-14 17:34:34 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
g.Geo1_Country = g.Geo2_Country = g.Geo3_Country = g.Geo4_Country = g.Geo5_Country = 0;
|
|
|
|
g.Geo1_Region = g.Geo2_Region = g.Geo3_Region = g.Geo4_Region = g.Geo5_Region = 0;
|
2018-07-14 17:34:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Inserts a new Geolocation tuple to the <see cref="IGeoTrack"/> values.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="g">Object tracking the geolocation history</param>
|
|
|
|
/// <param name="GeoCountry">Newly arrived country</param>
|
|
|
|
/// <param name="GeoRegion">Newly arrived region</param>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void TradeGeoLocation(this IGeoTrack g, byte GeoCountry, byte GeoRegion)
|
2018-07-14 17:34:34 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Trickle existing values up one slot
|
|
|
|
g.Geo5_Country = g.Geo4_Country;
|
|
|
|
g.Geo5_Region = g.Geo4_Region;
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
g.Geo4_Country = g.Geo3_Country;
|
|
|
|
g.Geo4_Region = g.Geo3_Region;
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
g.Geo3_Country = g.Geo2_Country;
|
|
|
|
g.Geo3_Region = g.Geo2_Region;
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
g.Geo2_Country = g.Geo1_Country;
|
|
|
|
g.Geo2_Region = g.Geo1_Region;
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
g.Geo1_Country = GeoCountry;
|
|
|
|
g.Geo1_Region = GeoRegion;
|
|
|
|
}
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void SanitizeGeoLocationData(this IGeoTrack g)
|
|
|
|
{
|
|
|
|
if (g.Geo1_Country == 0) g.Geo1_Region = 0;
|
|
|
|
if (g.Geo2_Country == 0) g.Geo2_Region = 0;
|
|
|
|
if (g.Geo3_Country == 0) g.Geo3_Region = 0;
|
|
|
|
if (g.Geo4_Country == 0) g.Geo4_Region = 0;
|
|
|
|
if (g.Geo5_Country == 0) g.Geo5_Region = 0;
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// trickle down empty slots
|
|
|
|
while (true)
|
2018-07-14 17:34:34 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (g.Geo5_Country != 0 && g.Geo4_Country == 0)
|
|
|
|
{
|
|
|
|
g.Geo4_Country = g.Geo5_Country;
|
|
|
|
g.Geo4_Region = g.Geo5_Region;
|
|
|
|
g.Geo5_Country = g.Geo5_Region = 0;
|
|
|
|
}
|
|
|
|
if (g.Geo4_Country != 0 && g.Geo3_Country == 0)
|
|
|
|
{
|
|
|
|
g.Geo3_Country = g.Geo4_Country;
|
|
|
|
g.Geo3_Region = g.Geo4_Region;
|
|
|
|
g.Geo4_Country = g.Geo4_Region = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (g.Geo3_Country != 0 && g.Geo2_Country == 0)
|
|
|
|
{
|
|
|
|
g.Geo2_Country = g.Geo3_Country;
|
|
|
|
g.Geo2_Region = g.Geo3_Region;
|
|
|
|
g.Geo3_Country = g.Geo3_Region = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (g.Geo2_Country != 0 && g.Geo1_Country == 0)
|
2018-07-14 17:34:34 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
g.Geo1_Country = g.Geo2_Country;
|
|
|
|
g.Geo1_Region = g.Geo2_Region;
|
|
|
|
g.Geo2_Country = g.Geo2_Region = 0;
|
|
|
|
continue;
|
2018-07-14 17:34:34 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
break;
|
2018-07-14 17:34:34 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if all Geolocation tuples are valid.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="g"></param>
|
|
|
|
/// <returns></returns>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool GetIsValid(this IGeoTrack g) => g.GetValidity() == GeoValid.Valid;
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if all Geolocation tuples are valid.
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
internal static GeoValid GetValidity(this IGeoTrack g)
|
|
|
|
{
|
|
|
|
bool end = false;
|
|
|
|
GeoValid result;
|
2023-01-22 04:02:33 +00:00
|
|
|
if ((result = UpdateCheck(g.Geo1_Country, g.Geo1_Region, ref end)) != GeoValid.Valid)
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
2023-01-22 04:02:33 +00:00
|
|
|
if ((result = UpdateCheck(g.Geo2_Country, g.Geo2_Region, ref end)) != GeoValid.Valid)
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
2023-01-22 04:02:33 +00:00
|
|
|
if ((result = UpdateCheck(g.Geo3_Country, g.Geo3_Region, ref end)) != GeoValid.Valid)
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
2023-01-22 04:02:33 +00:00
|
|
|
if ((result = UpdateCheck(g.Geo4_Country, g.Geo4_Region, ref end)) != GeoValid.Valid)
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
2023-01-22 04:02:33 +00:00
|
|
|
if ((result = UpdateCheck(g.Geo5_Country, g.Geo5_Region, ref end)) != GeoValid.Valid)
|
2018-07-14 17:34:34 +00:00
|
|
|
return result;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
static GeoValid UpdateCheck(byte country, byte region, ref bool end)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (country != 0)
|
|
|
|
return end ? GeoValid.CountryAfterPreviousEmpty : GeoValid.Valid;
|
|
|
|
if (region != 0) // c == 0
|
2022-06-18 18:04:24 +00:00
|
|
|
return GeoValid.RegionWithoutCountry;
|
|
|
|
end = true;
|
|
|
|
return GeoValid.Valid;
|
2018-07-14 17:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-14 17:34:34 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Geolocation Country-Region tuple validity tagging.
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
internal enum GeoValid
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary> Tuple is valid. </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
Valid,
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Lower-index country is empty, but higher has data (invalid).
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
CountryAfterPreviousEmpty,
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Zero-value country (None) with a non-zero Region (invalid).
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
RegionWithoutCountry,
|
2018-07-14 17:34:34 +00:00
|
|
|
}
|