Finish Owner Relationship, update races and add first helpers for derived values. Update naming

This commit is contained in:
Torben Nehmer 2024-08-18 19:21:18 +02:00
parent 15b252248a
commit aff9d23841
No known key found for this signature in database
6 changed files with 115 additions and 30 deletions

View File

@ -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;

View File

@ -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
}

View File

@ -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<Planet> Planets { get; } = new List<Planet>();
// 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
}

View File

@ -25,12 +25,12 @@ public class StarsDatabase(string DbPath) : DbContext
/// <summary>
/// List of all Planets read.
/// </summary>
public DbSet<Planet> Planets { get; set; }
public DbSet<Planet> Planet { get; set; }
/// <summary>
/// Lists of all defined races.
/// </summary>
public DbSet<Race> Races { get; set; }
public DbSet<Race> Race { get; set; }
#endregion
}

View File

@ -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<Planet>(CultureInfo.InvariantCulture);
config.Delimiter = "\t";
@ -68,15 +71,31 @@ sealed class Program
using (var csv = new CsvReader(reader, config))
{
List<CSV.Planet> records = csv.GetRecords<CSV.Planet>().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();
}
}
}
}

View File

@ -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<PlanetViewModel> LoadAll() {
var context = new StarsDatabase("stars.sqlite");
var result = new ObservableCollection<PlanetViewModel>();
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;
}