diff --git a/Stars Assistant/Model/Fleet.cs b/Stars Assistant/Model/Fleet.cs index f2dcf85..5a09109 100644 --- a/Stars Assistant/Model/Fleet.cs +++ b/Stars Assistant/Model/Fleet.cs @@ -74,6 +74,14 @@ public class Fleet #endregion + #region + + public string TrueDestination { get; set; } = String.Empty; + + public string ShipTypeGuess { get; set; } = String.Empty; + + #endregion + #region Relationships public Race? Owner { get; set; } diff --git a/Stars Assistant/Services/CSVDataLoader.cs b/Stars Assistant/Services/CSVDataLoader.cs index 7fe7f23..e7c41a0 100644 --- a/Stars Assistant/Services/CSVDataLoader.cs +++ b/Stars Assistant/Services/CSVDataLoader.cs @@ -45,7 +45,7 @@ public partial class CSVDataLoader : IEnableLogger { // string[] filters = { "*.p*", "*.f*", "*.map" }; Watcher = FsWatcher - .ObserveFileSystem(Game.GamePath, [ Game.PlanetFileSearchPattern ]) + .ObserveFileSystem(Game.GamePath, [ Game.PlanetFileSearchPattern, Game.FleetFileSearchPattern ]) .ThrottleAndDistinct(2, RxApp.TaskpoolScheduler) .Log(this, $"{DateTime.Now.ToLongTimeString()} FsEvent", fsEvent => $"{fsEvent.FullPath} {fsEvent.ChangeType}") .ObserveOn(RxApp.TaskpoolScheduler); @@ -83,11 +83,19 @@ public partial class CSVDataLoader : IEnableLogger switch (type) { case "p": - var loader = new CSV.PlanetLoader(); - loader.ImportForRace(Services.Game.Player); + var planetLoader = new CSV.PlanetLoader(); + planetLoader.ImportForRace(Services.Game.Player); - PlanetManager manager = Locator.Current.GetService()!; - manager.InitFromDatabase(); + PlanetManager planetManager = Locator.Current.GetService()!; + planetManager.InitFromDatabase(); + break; + + case "f": + var fleetLoader = new CSV.FleetLoader(); + fleetLoader.ImportForRace(Services.Game.Player); + + FleetManager.PostProcessImportedData(); + FleetManager fleetManager = Locator.Current.GetService()!; break; default: diff --git a/Stars Assistant/Services/FleetManager.cs b/Stars Assistant/Services/FleetManager.cs new file mode 100644 index 0000000..2d364dc --- /dev/null +++ b/Stars Assistant/Services/FleetManager.cs @@ -0,0 +1,75 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Text.RegularExpressions; +using DynamicData; +using DynamicData.Binding; +using Splat; +using StarsAssistant.Model; +using StarsAssistant.ViewModels; + +namespace StarsAssistant.Services; + +public class FleetManager +{ + protected Services.Game Game = Locator.Current.GetService()!; + + /// + /// SourceCache for DynamicData views + /// + private SourceCache _fleets = new(f => f.Id); + + public FleetManager() {} + + /// + /// Load the fleet records from the database and push them into our source cache. + /// If the data has been freshly imported, call PostProcessImportedData first. + /// + public void InitFromDatabase() + { + using var db = Locator.Current.GetService()!; + var allFleets = db.Fleet.ToList(); + _fleets.AddOrUpdate(allFleets); + } + + /// + /// Helper to fill up missing data from the original import. Tries to deduce + /// missing properties using heuristics. + /// + public static void PostProcessImportedData() + { + using var db = Locator.Current.GetService()!; + + // Check for all cases where we're targeting another fleet instead of + // a planet. Update the DB accordingly. + var playerFleets = from flt in db.Fleet + where flt.OwnerFileId == Game.Player.PlayerFileId + select flt; + + Regex shipPattern = new ($@"^{Regex.Escape(Game.Player.Name)} (?.+) #\d+$"); + + foreach (Fleet flt in playerFleets) + { + if (flt.Destination != "-- ") + { + var trueDest = db.Fleet + .FirstOrDefault(f => f.FleetName == $"{Game.Player.Name} {flt.Destination}" + && f.Planet != String.Empty); + flt.TrueDestination = trueDest?.Planet ?? flt.Destination; + } + else + { + flt.TrueDestination = "-- "; + } + + Match m = shipPattern.Match(flt.FleetName); + if (m.Success) + flt.ShipTypeGuess = m.Groups[1].Value; + + db.Update(flt); + } + + db.SaveChanges(); + } + +} diff --git a/Stars Assistant/Services/Game.cs b/Stars Assistant/Services/Game.cs index ca70861..e7ecd95 100644 --- a/Stars Assistant/Services/Game.cs +++ b/Stars Assistant/Services/Game.cs @@ -66,6 +66,11 @@ public class Game /// public string PlanetFileSearchPattern => $"{BaseName}.p*"; + /// + /// Search for Planet files using this pattern, will give you all pxx files. + /// + public string FleetFileSearchPattern => $"{BaseName}.f*"; + /// /// Internal helper to lazily load the current player from the DB. /// @@ -132,6 +137,8 @@ public class Game Locator.CurrentMutable.Register(() => new StarsDatabase(DatabaseFileName), typeof(StarsDatabase)); PlanetManager PlanetManager = new(); Locator.CurrentMutable.RegisterConstant(PlanetManager, typeof(Services.PlanetManager)); + FleetManager FleetManager = new(); + Locator.CurrentMutable.RegisterConstant(FleetManager, typeof(Services.FleetManager)); // TESTING HELPER if (__doCreateTestData) diff --git a/Stars Assistant/Services/PlanetManager.cs b/Stars Assistant/Services/PlanetManager.cs index b7ae6ae..1ded2da 100644 --- a/Stars Assistant/Services/PlanetManager.cs +++ b/Stars Assistant/Services/PlanetManager.cs @@ -13,24 +13,29 @@ public class PlanetManager { protected Services.Game Game = Locator.Current.GetService()!; + /// + /// SourceCache for DynamicData views + /// private SourceCache _planets = new(p => p.Name); + /// + /// Observable changeset showing all player planets converted to view models. + /// public IObservable> PlayerPlanetsSource => _planets .Connect() .Filter(planet => planet.OwnerId == Game.Player.Name) .Transform(planet => new PlayerPlanetViewModel(planet)); - public PlanetManager() - { - } + public PlanetManager() {} + /// + /// Load the planet records from the database and push them into our source cache. + /// public void InitFromDatabase() { using var db = Locator.Current.GetService()!; - { - var allPlanets = db.Planet.ToList(); - _planets.AddOrUpdate(allPlanets); - } + var allPlanets = db.Planet.ToList(); + _planets.AddOrUpdate(allPlanets); } } diff --git a/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs b/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs index 9d0a807..ff75c90 100644 --- a/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs +++ b/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs @@ -212,6 +212,19 @@ public partial class PlayerPlanetViewModel : ViewModelBase // TODO: Take shipping into account. } + /* + private int ComputePopulationEnRoute() + { + using var db = Locator.Current.GetService()!; + + var enRoute = from flt in db.Fleet + where flt.OwnerFileId == Game.Player.PlayerFileId && flt.TrueDestination == Planet.Name + select new { Colonists = flt.Sum(f => f.Colonists) }; + + return enRoute.Colonists ?? 0; + } + */ + #endregion } \ No newline at end of file