457 lines
17 KiB
C#
457 lines
17 KiB
C#
using CsvHelper;
|
|
using CsvHelper.Configuration;
|
|
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace HRServer.Models
|
|
{
|
|
public static class HorseFactory
|
|
{
|
|
public static string HorseDataPath = "horses";
|
|
// Thread-safe Dictionary
|
|
private static Dictionary<ulong, Horse> Horses = new();
|
|
public static Horse? GetHorse(ulong id)
|
|
{
|
|
return Horses.TryGetValue(id, out var horse) ? horse : null;
|
|
}
|
|
public static void AddOrUpdateHorse(Horse horse)
|
|
{
|
|
if (horse == null || horse.Id == null)
|
|
throw new ArgumentNullException(nameof(horse), "Horse or its ID cannot be null.");
|
|
|
|
Horses[horse.Id.Value] = horse;
|
|
}
|
|
public static void DeleteHorse(ulong id)
|
|
{
|
|
Horses.Remove(id);
|
|
}
|
|
public static IReadOnlyDictionary<ulong, Horse> GetAllHorses()
|
|
{
|
|
return Horses;
|
|
}
|
|
public static void SaveHorsesToFile()
|
|
{
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true // Aktiviert die formatierte Ausgabe
|
|
};
|
|
// Create an example genetics set for color
|
|
HorseGenetics example = new HorseGenetics();
|
|
// Check any horse whether all colors are loaded
|
|
if (Horses.First().Value.Genetics.Colors.Count < example.Colors.Count)
|
|
{
|
|
// Add missing colors to all horses
|
|
foreach (var horse in Horses)
|
|
{
|
|
foreach (var color in example.Colors)
|
|
{
|
|
if (!horse.Value.Genetics.Colors.ContainsKey(color.Key))
|
|
horse.Value.Genetics.Colors[color.Key] = color.Value;
|
|
}
|
|
}
|
|
}
|
|
// JSON-Datei speichern
|
|
var json = JsonSerializer.Serialize(Horses, options);
|
|
File.WriteAllText($"{HorseDataPath}.json", json);
|
|
|
|
// CSV-Datei speichern
|
|
if (Program.GoogleDriveFolder != string.Empty)
|
|
SaveToCsv();
|
|
}
|
|
|
|
private static void SaveToCsv()
|
|
{
|
|
using var writer = new StreamWriter($@"{Program.GoogleDriveFolder}\HorseData.csv");
|
|
using var csv = new CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture)
|
|
{
|
|
Delimiter = ",",
|
|
Escape = '"'
|
|
});
|
|
|
|
var flattenedHorses = GetAllHorses().Select(horse => new
|
|
{
|
|
// Grundlegende Informationen
|
|
Id = horse.Value.Id,
|
|
Name = horse.Value.HorseName,
|
|
Age = horse.Value.Age,
|
|
Gender = horse.Value.Gender,
|
|
Breed = horse.Value.Breed,
|
|
Notes = horse.Value.Notes,
|
|
Link = horse.Value.Link,
|
|
Owner = horse.Value.Owner,
|
|
LastDrawnDate = horse.Value.LastDrawnDate,
|
|
|
|
// HorseSummary
|
|
FatherName = horse.Value.Summary.FatherName,
|
|
FatherLink = horse.Value.Summary.FatherLink,
|
|
Conception = horse.Value.Summary.Conception,
|
|
UltrasoundGender = horse.Value.Summary.UltrasoundGender,
|
|
RelatedId1 = horse.Value.Summary.RelatedIds.ElementAtOrDefault(0),
|
|
RelatedId2 = horse.Value.Summary.RelatedIds.ElementAtOrDefault(1),
|
|
|
|
// HorseTraining
|
|
Training = horse.Value.Training.Training,
|
|
|
|
// HorseGenetics - Genetic Potential
|
|
GP = horse.Value.Genetics.GP,
|
|
Acceleration = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Acceleration"),
|
|
Agility = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Agility"),
|
|
Balance = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Balance"),
|
|
Bascule = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Bascule"),
|
|
PullingPower = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Pulling power"),
|
|
Speed = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Speed"),
|
|
Sprint = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Sprint"),
|
|
Stamina = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Stamina"),
|
|
Strength = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Strength"),
|
|
Surefootedness = horse.Value.Genetics.GeneticPotential.GetValueOrDefault("Surefootedness"),
|
|
|
|
// HorseGenetics - Disciplines
|
|
Dressage = horse.Value.Genetics.Disciplines.GetValueOrDefault("Dressage"),
|
|
Driving = horse.Value.Genetics.Disciplines.GetValueOrDefault("Driving"),
|
|
Endurance = horse.Value.Genetics.Disciplines.GetValueOrDefault("Endurance"),
|
|
Eventing = horse.Value.Genetics.Disciplines.GetValueOrDefault("Eventing"),
|
|
FlatRacing = horse.Value.Genetics.Disciplines.GetValueOrDefault("Flat Racing"),
|
|
ShowJumping = horse.Value.Genetics.Disciplines.GetValueOrDefault("Show Jumping"),
|
|
WesternReining = horse.Value.Genetics.Disciplines.GetValueOrDefault("Western Reining"),
|
|
|
|
// HorseGenetics - Colors (Farbgenetik vollständig)
|
|
Extension = horse.Value.Genetics.Colors.GetValueOrDefault("Extension"),
|
|
Agouti = horse.Value.Genetics.Colors.GetValueOrDefault("Agouti"),
|
|
Grey = horse.Value.Genetics.Colors.GetValueOrDefault("Grey"),
|
|
Creampearl = horse.Value.Genetics.Colors.GetValueOrDefault("Creampearl"),
|
|
Dun = horse.Value.Genetics.Colors.GetValueOrDefault("Dun"),
|
|
Champagne = horse.Value.Genetics.Colors.GetValueOrDefault("Champagne"),
|
|
Silver = horse.Value.Genetics.Colors.GetValueOrDefault("Silver"),
|
|
Mushroom = horse.Value.Genetics.Colors.GetValueOrDefault("Mushroom"),
|
|
Frame = horse.Value.Genetics.Colors.GetValueOrDefault("Frame"),
|
|
Appaloosa = horse.Value.Genetics.Colors.GetValueOrDefault("Appaloosa"),
|
|
PATN1 = horse.Value.Genetics.Colors.GetValueOrDefault("PATN1"),
|
|
MITF = horse.Value.Genetics.Colors.GetValueOrDefault("MITF"),
|
|
SW2 = horse.Value.Genetics.Colors.GetValueOrDefault("SW2"),
|
|
KIT = horse.Value.Genetics.Colors.GetValueOrDefault("KIT"),
|
|
RAB = horse.Value.Genetics.Colors.GetValueOrDefault("RAB"),
|
|
Seal = horse.Value.Genetics.Colors.GetValueOrDefault("Seal"),
|
|
Flaxen = horse.Value.Genetics.Colors.GetValueOrDefault("Flaxen"),
|
|
Sooty = horse.Value.Genetics.Colors.GetValueOrDefault("Sooty"),
|
|
Pangare = horse.Value.Genetics.Colors.GetValueOrDefault("Pangare"),
|
|
Sabino = horse.Value.Genetics.Colors.GetValueOrDefault("Sabino"),
|
|
CustomColor = horse.Value.Genetics.Colors.GetValueOrDefault("Custom"),
|
|
|
|
// HorseAchievements
|
|
MaxShowResult = horse.Value.Achievements.MaxShowResult,
|
|
MinShowResult = horse.Value.Achievements.MinShowResult,
|
|
MaxCompetitionResult = horse.Value.Achievements.MaxCompetitionResult,
|
|
MinCompetitionResult = horse.Value.Achievements.MinCompetitionResult,
|
|
ShortConformation = horse.Value.Achievements.ShortConformation,
|
|
Walk = horse.Value.Achievements.Conformation.GetValueOrDefault("Walk"),
|
|
Trot = horse.Value.Achievements.Conformation.GetValueOrDefault("Trot"),
|
|
Canter = horse.Value.Achievements.Conformation.GetValueOrDefault("Canter"),
|
|
Gallop = horse.Value.Achievements.Conformation.GetValueOrDefault("Gallop"),
|
|
Posture = horse.Value.Achievements.Conformation.GetValueOrDefault("Posture"),
|
|
|
|
// HorseHealth
|
|
Fertility = horse.Value.Health.Health.GetValueOrDefault("Fertility"),
|
|
ColicResistance = horse.Value.Health.Health.GetValueOrDefault("Colic resistance"),
|
|
HoofQuality = horse.Value.Health.Health.GetValueOrDefault("Hoof quality"),
|
|
BackProblems = horse.Value.Health.Health.GetValueOrDefault("Back problems"),
|
|
RespiratoryDisease = horse.Value.Health.Health.GetValueOrDefault("Respiratory disease"),
|
|
ResistanceToLameness = horse.Value.Health.Health.GetValueOrDefault("Resistance to lameness")
|
|
});
|
|
|
|
csv.WriteRecords(flattenedHorses);
|
|
}
|
|
|
|
public static void LoadHorsesFromFile()
|
|
{
|
|
if (!File.Exists(HorseDataPath + ".json"))
|
|
{
|
|
File.Create(HorseDataPath + ".json").Close();
|
|
return;
|
|
}
|
|
|
|
var json = File.ReadAllText(HorseDataPath + ".json");
|
|
Horses = JsonSerializer.Deserialize<Dictionary<ulong, Horse>>(json);
|
|
}
|
|
}
|
|
|
|
public class Horse
|
|
{
|
|
// Basic Information
|
|
private ulong? _id;
|
|
private int? _age;
|
|
private string _horseName = string.Empty;
|
|
private string _gender = string.Empty;
|
|
private string _breed = string.Empty;
|
|
private string _link = string.Empty;
|
|
private string _notes = string.Empty;
|
|
private DateTime _lastDrawnDate = DateTime.Now;
|
|
|
|
[JsonPropertyName("id")]
|
|
public ulong? Id
|
|
{
|
|
get => _id;
|
|
set
|
|
{
|
|
_id = value;
|
|
LoadState.BasicInfoLoaded = true;
|
|
}
|
|
}
|
|
[JsonPropertyName("age")]
|
|
public int? Age
|
|
{
|
|
get => _age;
|
|
set
|
|
{
|
|
_age = value;
|
|
}
|
|
}
|
|
[JsonPropertyName("notes")]
|
|
public string Notes
|
|
{
|
|
get => _notes;
|
|
set
|
|
{
|
|
_notes = value;
|
|
}
|
|
}
|
|
[JsonPropertyName("name")]
|
|
public string HorseName
|
|
{
|
|
get => _horseName;
|
|
set
|
|
{
|
|
_horseName = value;
|
|
}
|
|
}
|
|
[JsonPropertyName("gender")]
|
|
public string Gender
|
|
{
|
|
get => _gender;
|
|
set
|
|
{
|
|
_gender = value;
|
|
}
|
|
}
|
|
[JsonPropertyName("breed")]
|
|
public string Breed
|
|
{
|
|
get => _breed;
|
|
set
|
|
{
|
|
_breed = value;
|
|
}
|
|
}
|
|
[JsonPropertyName("link")]
|
|
public string Link
|
|
{
|
|
get => _link;
|
|
set
|
|
{
|
|
_link = value;
|
|
}
|
|
}
|
|
private string _owner = string.Empty;
|
|
[JsonPropertyName("owner")]
|
|
public string Owner
|
|
{
|
|
get => _owner;
|
|
set
|
|
{
|
|
_owner = value;
|
|
}
|
|
}
|
|
[JsonPropertyName("lastDrawnDate")]
|
|
public DateTime LastDrawnDate
|
|
{
|
|
get => _lastDrawnDate;
|
|
set
|
|
{
|
|
_lastDrawnDate = value;
|
|
}
|
|
}
|
|
private HorseSummary _summary = new();
|
|
public HorseSummary Summary
|
|
{
|
|
get => _summary;
|
|
set
|
|
{
|
|
_summary = value;
|
|
LoadState.SummaryLoaded = true;
|
|
}
|
|
}
|
|
|
|
private HorseTraining _training = new();
|
|
public HorseTraining Training
|
|
{
|
|
get => _training;
|
|
set
|
|
{
|
|
_training = value;
|
|
LoadState.TrainingLoaded = true;
|
|
}
|
|
}
|
|
|
|
private HorseGenetics _genetics = new();
|
|
public HorseGenetics Genetics
|
|
{
|
|
get => _genetics;
|
|
set
|
|
{
|
|
_genetics = value;
|
|
LoadState.GeneticsLoaded = true;
|
|
}
|
|
}
|
|
|
|
private HorseAchievements _achievements = new();
|
|
public HorseAchievements Achievements
|
|
{
|
|
get => _achievements;
|
|
set
|
|
{
|
|
_achievements = value;
|
|
LoadState.AchievementsLoaded = true;
|
|
}
|
|
}
|
|
|
|
private HorseHealth _health = new();
|
|
public HorseHealth Health
|
|
{
|
|
get => _health;
|
|
set
|
|
{
|
|
_health = value;
|
|
LoadState.HealthLoaded = true;
|
|
}
|
|
}
|
|
|
|
public DataLoadState LoadState { get; set; } = new();
|
|
|
|
public bool IsAllDataLoaded()
|
|
{
|
|
return LoadState.IsAllDataLoaded();
|
|
}
|
|
|
|
public void PrintLoadState()
|
|
{
|
|
Console.WriteLine("Load State of Horse Data:");
|
|
Console.WriteLine($"- Basic Info Loaded: {LoadState.BasicInfoLoaded}");
|
|
Console.WriteLine($"- Summary Loaded: {LoadState.SummaryLoaded}");
|
|
Console.WriteLine($"- Training Loaded: {LoadState.TrainingLoaded}");
|
|
Console.WriteLine($"- Genetics Loaded: {LoadState.GeneticsLoaded}");
|
|
Console.WriteLine($"- Achievements Loaded: {LoadState.AchievementsLoaded}");
|
|
Console.WriteLine($"- Health Loaded: {LoadState.HealthLoaded}");
|
|
}
|
|
}
|
|
|
|
public class DataLoadState
|
|
{
|
|
public bool BasicInfoLoaded { get; set; } = false;
|
|
public bool BasicInfoNeedsRefresh { get; set; } = false;
|
|
public bool SummaryLoaded { get; set; } = false;
|
|
public bool SummaryNeedsRefresh { get; set; } = false;
|
|
public bool TrainingLoaded { get; set; } = false;
|
|
public bool TrainingNeedsRefresh { get; set; } = false;
|
|
public bool GeneticsLoaded { get; set; } = false;
|
|
public bool GeneticsNeedsRefresh { get; set; } = false;
|
|
public bool AchievementsLoaded { get; set; } = false;
|
|
public bool AchievementsNeedsRefresh { get; set; } = false;
|
|
public bool HealthLoaded { get; set; } = false;
|
|
public bool HealthNeedsRefresh { get; set; } = false;
|
|
|
|
public bool IsAllDataLoaded()
|
|
{
|
|
return BasicInfoLoaded && SummaryLoaded && TrainingLoaded && GeneticsLoaded && AchievementsLoaded && HealthLoaded;
|
|
}
|
|
}
|
|
|
|
public class HorseSummary
|
|
{
|
|
[JsonPropertyName("RelatedIds")]
|
|
public List<string> RelatedIds { get; set; } = new();
|
|
[JsonPropertyName("Conception")]
|
|
public string Conception { get; set; } = string.Empty;
|
|
[JsonPropertyName("FatherLink")]
|
|
public string FatherLink { get; set; } = string.Empty;
|
|
[JsonPropertyName("FatherName")]
|
|
public string FatherName { get; set; } = string.Empty;
|
|
[JsonPropertyName("UltrasoundGender")]
|
|
public string UltrasoundGender { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class HorseTraining
|
|
{
|
|
[JsonPropertyName("Training")]
|
|
public string Training { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class HorseGenetics
|
|
{
|
|
[JsonPropertyName("GP")]
|
|
public int GP { get; set; }
|
|
|
|
[JsonPropertyName("GeneticPotential")]
|
|
public Dictionary<string, float> GeneticPotential { get; set; } = new();
|
|
|
|
[JsonPropertyName("Disciplines")]
|
|
public Dictionary<string, float> Disciplines { get; set; } = new() {
|
|
{ "Dressage", 0 },
|
|
{ "Driving", 0 },
|
|
{ "Endurance", 0 },
|
|
{ "Eventing", 0 },
|
|
{ "Flat Racing", 0 },
|
|
{ "Show Jumping", 0 },
|
|
{"Western Reining", 0 }
|
|
};
|
|
|
|
[JsonPropertyName("Colors")]
|
|
public Dictionary<string, string> Colors { get; set; } = new() {
|
|
{ "Extension", "n/n" },
|
|
{ "Agouti", "n/n" },
|
|
{ "Grey", "n/n" },
|
|
{ "Creampearl", "n/n" },
|
|
{ "Dun", "n/n" },
|
|
{ "Champagne", "n/n" },
|
|
{ "Silver", "n/n" },
|
|
{ "Mushroom", "n/n" },
|
|
{ "Frame", "n/n" },
|
|
{ "Appaloosa", "n/n" },
|
|
{ "PATN1", "n/n" },
|
|
{ "MITF", "n/n" },
|
|
{ "SW2", "n/n" },
|
|
{ "KIT", "n/n" },
|
|
{ "RAB", "n/n" }, // RAB/n
|
|
{ "Seal", "n/n" }, // AT/n
|
|
{ "Flaxen", "n/n" }, // f/n
|
|
{ "Sooty", "n/n" }, // Sty/n
|
|
{ "Pangare", "n/n" }, // P/n
|
|
{ "Sabino", "n/n" }, // Sab/n
|
|
{ "WildBay", "n/n" }, // A+/n
|
|
{ "Custom", "" },
|
|
};
|
|
}
|
|
|
|
public class HorseAchievements
|
|
{
|
|
[JsonPropertyName("ShowResults")]
|
|
public List<double> ShowResults { get; set; } = new();
|
|
[JsonPropertyName("Conformation")]
|
|
public Dictionary<string, string> Conformation { get; set; } = new();
|
|
[JsonPropertyName("ShortConformation")]
|
|
public string ShortConformation { get; set; } = string.Empty;
|
|
[JsonPropertyName("MaxShowResult")]
|
|
public double MaxShowResult { get; set; } = -1;
|
|
[JsonPropertyName("MinShowResult")]
|
|
public double MinShowResult { get; set; } = -1;
|
|
[JsonPropertyName("MaxCompetitionResult")]
|
|
public double MaxCompetitionResult { get; set; } = -1;
|
|
[JsonPropertyName("MinCompetitionResult")]
|
|
public double MinCompetitionResult { get; set; } = -1;
|
|
}
|
|
|
|
public class HorseHealth
|
|
{
|
|
[JsonPropertyName("Health")]
|
|
public Dictionary<string, string> Health { get; set; } = new();
|
|
}
|
|
}
|