Move data and data loading to plugin libraries.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
57
PkmnLib.Dynamic/Libraries/DataLoaders/NatureDataLoader.cs
Normal file
57
PkmnLib.Dynamic/Libraries/DataLoaders/NatureDataLoader.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Libraries;
|
||||
|
||||
namespace PkmnLib.Dynamic.Libraries.DataLoaders;
|
||||
|
||||
public static class NatureDataLoader
|
||||
{
|
||||
private static readonly char[] CommonCsvDelimiters = ['|', ','];
|
||||
|
||||
public static NatureLibrary LoadNatureLibrary(Stream stream)
|
||||
{
|
||||
var library = new NatureLibrary();
|
||||
|
||||
using var reader = new StreamReader(stream);
|
||||
var header = reader.ReadLine();
|
||||
if (header == null)
|
||||
throw new InvalidDataException("Type data is empty.");
|
||||
var delimiter = CommonCsvDelimiters.FirstOrDefault(header.Contains);
|
||||
if (delimiter == default)
|
||||
throw new InvalidDataException("No valid delimiter found in type data.");
|
||||
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
if (line == null)
|
||||
break;
|
||||
var values = line.Split(delimiter)!;
|
||||
var nature = values[0];
|
||||
var increasedStat = values[1];
|
||||
var decreasedStat = values[2];
|
||||
|
||||
var increasedModifier = 1.1f;
|
||||
var decreasedModifier = 0.9f;
|
||||
|
||||
if (increasedStat == string.Empty)
|
||||
{
|
||||
increasedStat = "Hp";
|
||||
increasedModifier = 1.0f;
|
||||
}
|
||||
|
||||
if (decreasedStat == string.Empty)
|
||||
{
|
||||
decreasedStat = "Hp";
|
||||
decreasedModifier = 1.0f;
|
||||
}
|
||||
|
||||
if (!Enum.TryParse<Statistic>(increasedStat, out var increasedStatEnum))
|
||||
throw new InvalidDataException($"Increased stat {increasedStat} is not a valid stat.");
|
||||
if (!Enum.TryParse<Statistic>(decreasedStat, out var decreasedStatEnum))
|
||||
throw new InvalidDataException($"Decreased stat {decreasedStat} is not a valid stat.");
|
||||
|
||||
library.Add(new Nature(nature, increasedStatEnum, decreasedStatEnum, increasedModifier, decreasedModifier));
|
||||
}
|
||||
|
||||
return library;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user