PkmnLib.NET/PkmnLib.Dataloader/NatureDataLoader.cs

60 lines
2.0 KiB
C#

using System;
using System.IO;
using System.Linq;
using PkmnLib.Static;
using PkmnLib.Static.Libraries;
namespace PkmnLib.Dataloader;
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;
}
}