From a03edf898352cb456694ffaa8bd138f0bc82c3eb Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Thu, 26 Sep 2024 22:18:50 +0200 Subject: [PATCH] BuCol Setup --- Stars Assistant/CSV/PlanetLoader.cs | 70 ++++++++--------- Stars Assistant/Services/CSVDataLoader.cs | 3 + Stars Assistant/Services/Game.cs | 9 ++- Stars Assistant/Services/GameEngine.cs | 7 ++ Stars Assistant/Services/PlanetManager.cs | 6 +- Stars Assistant/ViewModels/BuColViewModel.cs | 1 - .../ViewModels/PlayerPlanetViewModel.cs | 75 +++++++++++++++++++ Stars Assistant/Views/BuColView.axaml | 15 ++-- 8 files changed, 137 insertions(+), 49 deletions(-) diff --git a/Stars Assistant/CSV/PlanetLoader.cs b/Stars Assistant/CSV/PlanetLoader.cs index 0665347..0ec7484 100644 --- a/Stars Assistant/CSV/PlanetLoader.cs +++ b/Stars Assistant/CSV/PlanetLoader.cs @@ -4,6 +4,7 @@ using CsvHelper.Configuration; using CsvHelper; using System.Globalization; using Splat; +using StarsAssistant.Services; namespace StarsAssistant.CSV; @@ -45,49 +46,48 @@ public class PlanetLoader /// Import File public void ImportForRace(Model.Race race) { - using (var db = Locator.Current.GetService()!) - using (var reader = new StreamReader(Game.PlanetFileForRace(race), System.Text.Encoding.Latin1)) - using (var csv = new CsvReader(reader, CsvConfig)) - { - List records = csv.GetRecords().ToList(); - var planetByPlayer = from csvp in records - group csvp by csvp.Owner into byOwners - select byOwners; + using var db = Locator.Current.GetService()!; + using var reader = new StreamReader(Game.PlanetFileForRace(race), System.Text.Encoding.Latin1); + using var csv = new CsvReader(reader, CsvConfig); - foreach (var owner in planetByPlayer) + List records = csv.GetRecords().ToList(); + var planetByPlayer = from csvp in records + group csvp by csvp.Owner into byOwners + select byOwners; + + foreach (var owner in planetByPlayer) + { + Model.Race? r; + if (owner.Key == race.Name) { - Model.Race? r; - if (owner.Key == race.Name) + r = race; + } + else + { + r = db.Race.Find(owner.Key); + if (r == null) { - r = race; + r = new() { Name = owner.Key }; + db.Add(r); + } + } + + foreach (Planet csvp in owner) + { + Model.Planet? p = db.Planet.Find(csvp.Name); + if (p == null) + { + p = new() { Name = csvp.Name }; + csvp.UpdateDbPlanet(p); + db.Add(p); } else { - r = db.Race.Find(owner.Key); - if (r == null) - { - r = new() { Name = owner.Key }; - db.Add(r); - } + csvp.UpdateDbPlanet(p); + db.Update(p); } - - foreach (Planet csvp in owner) - { - Model.Planet? p = db.Planet.Find(csvp.Name); - if (p == null) - { - p = new() { Name = csvp.Name }; - csvp.UpdateDbPlanet(p); - db.Add(p); - } - else - { - csvp.UpdateDbPlanet(p); - db.Update(p); - } - } - db.SaveChanges(); } + db.SaveChanges(); } } } diff --git a/Stars Assistant/Services/CSVDataLoader.cs b/Stars Assistant/Services/CSVDataLoader.cs index 408d39f..7fe7f23 100644 --- a/Stars Assistant/Services/CSVDataLoader.cs +++ b/Stars Assistant/Services/CSVDataLoader.cs @@ -85,6 +85,9 @@ public partial class CSVDataLoader : IEnableLogger case "p": var loader = new CSV.PlanetLoader(); loader.ImportForRace(Services.Game.Player); + + PlanetManager manager = Locator.Current.GetService()!; + manager.InitFromDatabase(); break; default: diff --git a/Stars Assistant/Services/Game.cs b/Stars Assistant/Services/Game.cs index 9651926..4bae313 100644 --- a/Stars Assistant/Services/Game.cs +++ b/Stars Assistant/Services/Game.cs @@ -123,11 +123,14 @@ public class Game GameEngine.Game = this; Locator.CurrentMutable.RegisterConstant(new CSVDataLoader(), typeof(CSVDataLoader)); Locator.CurrentMutable.Register(() => new StarsDatabase(DatabaseFileName), typeof(StarsDatabase)); - Locator.CurrentMutable.RegisterConstant(new PlanetManager(), typeof(Services.PlanetManager)); + PlanetManager PlanetManager = new(); + Locator.CurrentMutable.RegisterConstant(PlanetManager, typeof(Services.PlanetManager)); // TESTING HELPER if (__doCreateTestData) __createTestData(); + + PlanetManager.InitFromDatabase(); } /// @@ -163,8 +166,8 @@ public class Game PRT = PRT.Other, HasOBRM = true, FactoryCost3 = false, - FactoryNumberPer10k = 8, - FactoryResCost = 8, + FactoryNumberPer10k = 9, + FactoryResCost = 9, FactoryResPer10 = 15, MineResCost = 3, MineMineralsPer10 = 10, diff --git a/Stars Assistant/Services/GameEngine.cs b/Stars Assistant/Services/GameEngine.cs index 9525035..ba90be6 100644 --- a/Stars Assistant/Services/GameEngine.cs +++ b/Stars Assistant/Services/GameEngine.cs @@ -99,6 +99,13 @@ public class GameEngine return (int) tmp; } + /// + /// Returns the Germanium cost for the given number of factories. + /// + /// Factory count + /// Germanium cost + public static int FactoryGermaniumCostForPlayer(int count) => count * Game.Player.FactoryGerCost; + /// /// Returns the maximum operable mines on a given player planet for a given value. /// diff --git a/Stars Assistant/Services/PlanetManager.cs b/Stars Assistant/Services/PlanetManager.cs index 83cc7de..b7ae6ae 100644 --- a/Stars Assistant/Services/PlanetManager.cs +++ b/Stars Assistant/Services/PlanetManager.cs @@ -25,12 +25,12 @@ public class PlanetManager { } - public void __test__Init() + public void InitFromDatabase() { using var db = Locator.Current.GetService()!; { - var playerPlanets = db.Planet.ToList(); - _planets.AddOrUpdate(playerPlanets); + var allPlanets = db.Planet.ToList(); + _planets.AddOrUpdate(allPlanets); } } } diff --git a/Stars Assistant/ViewModels/BuColViewModel.cs b/Stars Assistant/ViewModels/BuColViewModel.cs index 7e11abe..6fbdf8a 100644 --- a/Stars Assistant/ViewModels/BuColViewModel.cs +++ b/Stars Assistant/ViewModels/BuColViewModel.cs @@ -18,7 +18,6 @@ public partial class BuColViewModel : ViewModelBase public BuColViewModel() { var PlanetManager = Locator.Current.GetService()!; - PlanetManager.__test__Init(); PlanetManager.PlayerPlanetsSource .ObserveOn(RxApp.MainThreadScheduler) diff --git a/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs b/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs index 51f335d..9d0a807 100644 --- a/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs +++ b/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs @@ -30,6 +30,31 @@ public partial class PlayerPlanetViewModel : ViewModelBase .Select(popTgt => this.MaxPopulation * popTgt / 10000 * 100) .ToProperty(this, vm => vm.PopulationTarget); + _populationToShipHelper = this + .WhenAnyValue(vm => vm.PopulationTarget) + .Select(popToShip => this.ComputePopulationToShip()) + .ToProperty(this, vm => vm.PopulationToShip); + + _targetFactoriesHelper = this + .WhenAnyValue(vm => vm.PopulationTarget) + .Select(tgtFact => GameEngine.MaxOperableFactoriesForPlayer(_populationTarget, Value)) + .ToProperty(this, vm => vm.TargetFactories); + + _remainingFactoriesHelper = this + .WhenAnyValue(vm => vm.TargetFactories) + .Select(factRem => Factories < _targetFactories ? _targetFactories - Factories : 0) + .ToProperty(this, vm => vm.RemainingFactories); + + _factoryGermaniumDeltaHelper = this + .WhenAnyValue(vm => vm.RemainingFactories) + .Select(gerCost => ComputeFactoryGermaniumDelta()) + .ToProperty(this, vm => vm.FactoryGermaniumDelta); + + _remainingMinesHelper = this + .WhenAnyValue(vm => vm.PopulationTarget) + .Select(remMines => Math.Max(0, GameEngine.MaxOperableMinesForPlayer(_populationTarget, Value) - Mines)) + .ToProperty(this, vm => vm.RemainingMines); + this.WhenActivated((CompositeDisposable disposables) => { // /* handle activation */ @@ -122,6 +147,8 @@ public partial class PlayerPlanetViewModel : ViewModelBase public int PopulationGrowth => GameEngine.PlanetPopGrowthForPlayer(Population, Value); + public int PopulationT1 => Population + PopulationGrowth /* TODO + Pop En Route */; + public int BuildableFactories => GameEngine.MaxBuildableFactoriesForPlayer(Value); public int BuildableMines => GameEngine.MaxBuildableMinesForPlayer(Value); @@ -134,9 +161,57 @@ public partial class PlayerPlanetViewModel : ViewModelBase public int ResourcesFromFactories => GameEngine.ResourcesFromFactForPlayer(Population, Factories, Value); + public int AvailableIronium => MRIronium + SurfaceIronium; + + public int AvailableBoranium => MRBoranium + SurfaceBoranium; + + public int AvailableGermanium => MRGermanium + SurfaceGermanium; + [ObservableAsProperty] private int _populationTarget; + [ObservableAsProperty] + private int _populationToShip; + + [ObservableAsProperty] + private int _targetFactories; + + [ObservableAsProperty] + private int _remainingFactories; + + [ObservableAsProperty] + private int _factoryGermaniumDelta; + + [ObservableAsProperty] + private int _remainingMines; + + #endregion + + #region Helper functions + + private int ComputePopulationToShip() + { + if (Population >= _populationTarget) + return Population - _populationTarget; + + if (PopulationT1 < _populationTarget) + return PopulationT1 - _populationTarget; + + return 0; + } + + private int ComputeFactoryGermaniumDelta() + { + int gerReq = GameEngine.FactoryGermaniumCostForPlayer(_remainingFactories); + + if (gerReq < AvailableGermanium) + return Math.Max(0, SurfaceGermanium - gerReq); + + return AvailableGermanium + MRGermanium - gerReq; + // TODO: Extrapolate to T1, so that excess Germanium is visible. + // TODO: Take shipping into account. + } + #endregion } \ No newline at end of file diff --git a/Stars Assistant/Views/BuColView.axaml b/Stars Assistant/Views/BuColView.axaml index fca7883..31e7392 100644 --- a/Stars Assistant/Views/BuColView.axaml +++ b/Stars Assistant/Views/BuColView.axaml @@ -19,7 +19,7 @@ - + @@ -31,19 +31,20 @@ - + - + - + - - + + + - +