diff --git a/Stars Assistant/CSV/Planet.cs b/Stars Assistant/CSV/Planet.cs index a387716..3e7ce24 100644 --- a/Stars Assistant/CSV/Planet.cs +++ b/Stars Assistant/CSV/Planet.cs @@ -240,7 +240,7 @@ public class Planet if (dbPlanet.Name != Name) throw new InvalidOperationException($"DB Planet Name {dbPlanet.Name} does not macth the CSV Planet Name {Name}"); - dbPlanet.Owner = Owner; + dbPlanet.OwnerId = Owner != "" ? Owner : null; dbPlanet.StarbaseType = StarbaseType; dbPlanet.ReportAge = ReportAge; dbPlanet.Population = Population; diff --git a/Stars Assistant/Model/Planet.cs b/Stars Assistant/Model/Planet.cs index 34d43ec..3c6aced 100644 --- a/Stars Assistant/Model/Planet.cs +++ b/Stars Assistant/Model/Planet.cs @@ -5,12 +5,14 @@ namespace StarsAssistant.Model; public class Planet { + #region Persistant and derived propeties + public const int MinEffectiveValue = 5; [Key] public required string Name { get; set; } - public string? Owner { get; set; } + public string? OwnerId { get; set; } public string? StarbaseType { get; set; } @@ -77,4 +79,41 @@ public class Planet public int? GateMass { get; set; } public int? PctDamage { get; set; } + + #endregion + + #region Relationships + + public Race? Owner { get; set; } + + #endregion + + #region Derived Properties + + public int EffectiveValue => (Value == null) ? 0 : Math.Max((int)Value, MinEffectiveValue); + + public int MaxPopOnPlanet => Race.Player.MaxPopOnPlanet * EffectiveValue / 100; + + public int PopGrowth(int currentPop, int maxPop, int value) { + // What to do with non player races? + if (OwnerId != Race.Player.Name) return 0; + + double popRatio = currentPop / maxPop; + int growth = 0; + if (value < 0) + growth = currentPop * value / 10; + else if (popRatio <= 0.25) + growth = currentPop * value * (Race.Player.GrowthRatePercent ?? 0) / 100; + else if (popRatio <= 1) + growth = currentPop * value * (Race.Player.GrowthRatePercent ?? 0) /100 * 16 / 9 * ((int) Math.Pow(1 - popRatio, 2)); + else + growth = (int) (currentPop * (popRatio - 1) * 0.04); + + return growth / 100 * 100; + } + + public int PopGrowth() => PopGrowth(Population ?? 0, MaxPopOnPlanet, Value ?? 0); + + #endregion + } \ No newline at end of file diff --git a/Stars Assistant/Model/Race.cs b/Stars Assistant/Model/Race.cs index 4f7fabd..c6fc157 100644 --- a/Stars Assistant/Model/Race.cs +++ b/Stars Assistant/Model/Race.cs @@ -15,14 +15,18 @@ public enum PRT { public class Race { + #region Persistant and derived properties + [Key] public required string Name { get; set; } - public int ColonistsPerResource { get; set; } + public bool PlayerRace { get; set; } = false; - public int GrowthRatePercent { get; set; } + public int? ColonistsPerResource { get; set; } - private PRT _PRT; + public int? GrowthRatePercent { get; set; } + + private PRT _PRT = PRT.Other; public PRT PRT { get => _PRT; set { @@ -31,8 +35,8 @@ public class Race } } - private bool _hasOBRM; - public bool HasOBRM { + private bool? _hasOBRM; + public bool? HasOBRM { get => _hasOBRM; set { _hasOBRM = value; @@ -49,29 +53,50 @@ public class Race [NotMapped] public int MaxEffectivePopOnGreen { get; private set; } - private bool _factoryCost3; - public bool FactoryCost3 { + private bool? _factoryCost3; + public bool? FactoryCost3 { get => _factoryCost3; set { _factoryCost3 = value; - FactoryGerCost = _factoryCost3 ? 3 : 4; + FactoryGerCost = (_factoryCost3 ?? false) ? 3 : 4; } } [NotMapped] public int FactoryGerCost { get; private set; } - public int FactoryNumberPer10k { get; set; } + public int? FactoryNumberPer10k { get; set; } - public int FactoryResCost { get; set; } + public int? FactoryResCost { get; set; } - public int FactoryResPer10 { get; set; } + public int? FactoryResPer10 { get; set; } - public int MineResCost { get; set; } + public int? MineResCost { get; set; } - public int MineNumberPer10k { get; set; } + public int? MineNumberPer10k { get; set; } - public int MineMineralsPer10 { get; set; } + public int? MineMineralsPer10 { get; set; } + + #endregion + + #region Relationships + + public ICollection Planets { get; } = new List(); + + // TODO: Implement Lazy Loader, currently hardcoded in testing startup code + public static Race _player = null!; + public static Race Player { + get => _player; + set => _player = value; + } + + #endregion + + #region Public Helpers + + #endregion + + #region Internal Helpers private void UpdatePopDerivedValues() { const int basePop = 1_000_000; @@ -81,10 +106,11 @@ public class Race PRT.JOAT => 1.2, _ => 1, }; - double obrmMod = HasOBRM ? 1.1 : 1.0; + double obrmMod = (HasOBRM ?? false) ? 1.1 : 1.0; MaxPopOnPlanet = Convert.ToInt32(basePop * prtMod * obrmMod); MaxEffectivePopOnRed = MaxPopOnPlanet * Planet.MinEffectiveValue / 100 * 3; MaxEffectivePopOnGreen = MaxPopOnPlanet * 3; } -} + #endregion +} \ No newline at end of file diff --git a/Stars Assistant/Model/StarsDatabase.cs b/Stars Assistant/Model/StarsDatabase.cs index 1894810..6426bd3 100644 --- a/Stars Assistant/Model/StarsDatabase.cs +++ b/Stars Assistant/Model/StarsDatabase.cs @@ -25,12 +25,12 @@ public class StarsDatabase(string DbPath) : DbContext /// /// List of all Planets read. /// - public DbSet Planets { get; set; } + public DbSet Planet { get; set; } /// /// Lists of all defined races. /// - public DbSet Races { get; set; } + public DbSet Race { get; set; } #endregion } \ No newline at end of file diff --git a/Stars Assistant/Program.cs b/Stars Assistant/Program.cs index 0ca711c..edc97b8 100644 --- a/Stars Assistant/Program.cs +++ b/Stars Assistant/Program.cs @@ -45,6 +45,7 @@ sealed class Program Race r = new() { Name = "Atlantis", + PlayerRace = true, ColonistsPerResource = 1000, GrowthRatePercent = 19, PRT = PRT.Other, @@ -58,6 +59,8 @@ sealed class Program MineNumberPer10k = 10 }; db.Add(r); + db.SaveChanges(); + Race.Player = r; var config = CsvConfiguration.FromAttributes(CultureInfo.InvariantCulture); config.Delimiter = "\t"; @@ -68,15 +71,31 @@ sealed class Program using (var csv = new CsvReader(reader, config)) { List records = csv.GetRecords().ToList(); - foreach (CSV.Planet csvp in records) - { - Planet p = new Planet{ Name = csvp.Name }; - csvp.UpdateDbPlanet(p); - db.Add(p); - } - db.SaveChanges(); - } + var planetByPlayer = from csvp in records + group csvp by csvp.Owner into byOwners + select byOwners; + foreach (var owner in planetByPlayer) + { + if (owner.Key != "Atlantis") + { + r = new() + { + Name = owner.Key + }; + db.Add(r); + db.SaveChanges(); + } + + foreach (CSV.Planet csvp in owner) + { + Planet p = new Planet{ Name = csvp.Name }; + csvp.UpdateDbPlanet(p); + db.Add(p); + } + db.SaveChanges(); + } + } } } diff --git a/Stars Assistant/ViewModels/PlanetViewModel.cs b/Stars Assistant/ViewModels/PlanetViewModel.cs index 2c28fe3..1459cbe 100644 --- a/Stars Assistant/ViewModels/PlanetViewModel.cs +++ b/Stars Assistant/ViewModels/PlanetViewModel.cs @@ -1,4 +1,5 @@ using System.Collections.ObjectModel; +using Microsoft.EntityFrameworkCore; using StarsAssistant.Model; namespace StarsAssistant.ViewModels; @@ -8,7 +9,7 @@ public partial class PlanetViewModel(Planet planet) : ViewModelBase public static ObservableCollection LoadAll() { var context = new StarsDatabase("stars.sqlite"); var result = new ObservableCollection(); - foreach (Planet planet in context.Planets.ToList()) + foreach (Planet planet in context.Planet.Where(p => p.OwnerId == "Atlantis").ToList()) { var vm = new PlanetViewModel(planet); result.Add(vm); @@ -20,7 +21,7 @@ public partial class PlanetViewModel(Planet planet) : ViewModelBase public string Name => planet.Name; - public string Owner => planet.Owner ?? String.Empty; + public string Owner => planet.OwnerId ?? String.Empty; public int Value => planet.Value ?? 0; }