diff --git a/Stars Assistant/Model/Planet.cs b/Stars Assistant/Model/Planet.cs index 68e7437..75c2c57 100644 --- a/Stars Assistant/Model/Planet.cs +++ b/Stars Assistant/Model/Planet.cs @@ -99,23 +99,23 @@ public class Planet public int EffectiveValue() => EffectiveValue(Value); public static int MaxPopOnPlanet (int? value) { - return Race.Player.MaxPopOnPlanet * EffectiveValue(value) / 100; + return Services.Game.Player.MaxPopOnPlanet * EffectiveValue(value) / 100; } public int MaxPopOnPlanet() => MaxPopOnPlanet(Value); public int PopGrowth(int currentPop, int maxPop, int value) { // What to do with non player races? - if (OwnerId != Race.Player.Name) return 0; + if (OwnerId != Services.Game.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; + growth = currentPop * value * (Services.Game.Player.GrowthRatePercent ?? 0) / 100; else if (popRatio <= 1) - growth = currentPop * value * (Race.Player.GrowthRatePercent ?? 0) /100 * 16 / 9 * ((int) Math.Pow(1 - popRatio, 2)); + growth = currentPop * value * (Services.Game.Player.GrowthRatePercent ?? 0) /100 * 16 / 9 * ((int) Math.Pow(1 - popRatio, 2)); else growth = (int) (currentPop * (popRatio - 1) * 0.04); @@ -126,7 +126,7 @@ public class Planet public int MaxFact(int currentPop) { int effectivePop = Math.Min(currentPop, MaxPopOnPlanet()); - double tmp = (double) effectivePop / 10000 * Race.Player.FactoryNumberPer10k ?? 0; + double tmp = (double) effectivePop / 10000 * Services.Game.Player.FactoryNumberPer10k ?? 0; return (int) tmp; } @@ -134,15 +134,15 @@ public class Planet public int MaxMines(int currentPop) { int effectivePop = Math.Min(currentPop, MaxPopOnPlanet()); - double tmp = (double) effectivePop / 10000 * Race.Player.MineNumberPer10k ?? 0; + double tmp = (double) effectivePop / 10000 * Services.Game.Player.MineNumberPer10k ?? 0; return (int) tmp; } public int MaxMines() => MaxMines(Population ?? 0); public static int ResourcesFromPop(int currentPop, int value) { - if (Race.Player.ColonistsPerResource == null - || Race.Player.ColonistsPerResource == 0) + if (Services.Game.Player.ColonistsPerResource == null + || Services.Game.Player.ColonistsPerResource == 0) { return 0; } @@ -162,7 +162,7 @@ public class Planet popHalfEfficiency = 2 * maxPop; } int popEffective = popFullEfficiency + (popHalfEfficiency / 2); - return (popEffective / Race.Player.ColonistsPerResource) ?? 0; + return (popEffective / Services.Game.Player.ColonistsPerResource) ?? 0; } public int ResourcesFromPop() => ResourcesFromPop(Population ?? 0, Value ?? 0); @@ -170,10 +170,10 @@ public class Planet public static int ResourcesFromFact(int currentPop, int factories, int value) { int maxPop = MaxPopOnPlanet(value); int effectivePop = Math.Min(currentPop, maxPop); - double tmp = (double) effectivePop / 10000 * Race.Player.FactoryNumberPer10k ?? 0; + double tmp = (double) effectivePop / 10000 * Services.Game.Player.FactoryNumberPer10k ?? 0; int maxOperableFactories = (int) tmp; int operableFactories = Math.Min(factories, maxOperableFactories); - tmp = (double) operableFactories / 10 * Race.Player.FactoryResPer10 ?? 0; + tmp = (double) operableFactories / 10 * Services.Game.Player.FactoryResPer10 ?? 0; return (int) tmp; } diff --git a/Stars Assistant/Model/Race.cs b/Stars Assistant/Model/Race.cs index c954fec..691ce4b 100644 --- a/Stars Assistant/Model/Race.cs +++ b/Stars Assistant/Model/Race.cs @@ -85,13 +85,6 @@ public class Race 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 diff --git a/Stars Assistant/Program.cs b/Stars Assistant/Program.cs index 4458e27..27c4a9e 100644 --- a/Stars Assistant/Program.cs +++ b/Stars Assistant/Program.cs @@ -95,10 +95,9 @@ sealed class Program }; db.Add(r); db.SaveChanges(); - Race.Player = r; var loader = new CSV.PlanetLoader(); - loader.ImportForRace(Race.Player); + loader.ImportForRace(Services.Game.Player); } } diff --git a/Stars Assistant/Services/CSVDataLoader.cs b/Stars Assistant/Services/CSVDataLoader.cs index c2a70b3..92687da 100644 --- a/Stars Assistant/Services/CSVDataLoader.cs +++ b/Stars Assistant/Services/CSVDataLoader.cs @@ -53,6 +53,6 @@ public partial class CSVDataLoader : IEnableLogger this.Log().Debug($"Got file type {type} for player {player}"); var loader = new CSV.PlanetLoader(); - loader.ImportForRace(Model.Race.Player); + loader.ImportForRace(Services.Game.Player); } } diff --git a/Stars Assistant/Services/Game.cs b/Stars Assistant/Services/Game.cs index e7d49f1..1585cc0 100644 --- a/Stars Assistant/Services/Game.cs +++ b/Stars Assistant/Services/Game.cs @@ -1,3 +1,4 @@ +using System.Reflection.PortableExecutable; using Splat; namespace StarsAssistant.Services; @@ -64,6 +65,22 @@ public class Game /// public string PlanetFileSearchPattern => $"{BaseName}.p*"; + /// + /// Internal helper to lazily load the current player from the DB. + /// + private readonly static LazyLazyPlayer = new ( () => { + using var db = Locator.Current.GetService()!; + Model.Race result = db.Race + .Where(r => r.PlayerRace == true) + .First(); + return result; + }); + + /// + /// Get the Race object for the current player, lazy initialized. + /// + public static Model.Race Player => LazyPlayer.Value; + /// /// Get the name of a planet file for a given race. ///