stich fleet loading together

This commit is contained in:
Torben Nehmer 2024-10-02 20:18:00 +02:00
parent e6c2f969b4
commit b1fb26db5a
No known key found for this signature in database
6 changed files with 128 additions and 12 deletions

View File

@ -74,6 +74,14 @@ public class Fleet
#endregion #endregion
#region
public string TrueDestination { get; set; } = String.Empty;
public string ShipTypeGuess { get; set; } = String.Empty;
#endregion
#region Relationships #region Relationships
public Race? Owner { get; set; } public Race? Owner { get; set; }

View File

@ -45,7 +45,7 @@ public partial class CSVDataLoader : IEnableLogger
{ {
// string[] filters = { "*.p*", "*.f*", "*.map" }; // string[] filters = { "*.p*", "*.f*", "*.map" };
Watcher = FsWatcher Watcher = FsWatcher
.ObserveFileSystem(Game.GamePath, [ Game.PlanetFileSearchPattern ]) .ObserveFileSystem(Game.GamePath, [ Game.PlanetFileSearchPattern, Game.FleetFileSearchPattern ])
.ThrottleAndDistinct(2, RxApp.TaskpoolScheduler) .ThrottleAndDistinct(2, RxApp.TaskpoolScheduler)
.Log(this, $"{DateTime.Now.ToLongTimeString()} FsEvent", fsEvent => $"{fsEvent.FullPath} {fsEvent.ChangeType}") .Log(this, $"{DateTime.Now.ToLongTimeString()} FsEvent", fsEvent => $"{fsEvent.FullPath} {fsEvent.ChangeType}")
.ObserveOn(RxApp.TaskpoolScheduler); .ObserveOn(RxApp.TaskpoolScheduler);
@ -83,11 +83,19 @@ public partial class CSVDataLoader : IEnableLogger
switch (type) switch (type)
{ {
case "p": case "p":
var loader = new CSV.PlanetLoader(); var planetLoader = new CSV.PlanetLoader();
loader.ImportForRace(Services.Game.Player); planetLoader.ImportForRace(Services.Game.Player);
PlanetManager manager = Locator.Current.GetService<PlanetManager>()!; PlanetManager planetManager = Locator.Current.GetService<PlanetManager>()!;
manager.InitFromDatabase(); planetManager.InitFromDatabase();
break;
case "f":
var fleetLoader = new CSV.FleetLoader();
fleetLoader.ImportForRace(Services.Game.Player);
FleetManager.PostProcessImportedData();
FleetManager fleetManager = Locator.Current.GetService<FleetManager>()!;
break; break;
default: default:

View File

@ -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<Services.Game>()!;
/// <summary>
/// SourceCache for DynamicData views
/// </summary>
private SourceCache<Fleet, int> _fleets = new(f => f.Id);
public FleetManager() {}
/// <summary>
/// Load the fleet records from the database and push them into our source cache.
/// If the data has been freshly imported, call PostProcessImportedData first.
/// </summary>
public void InitFromDatabase()
{
using var db = Locator.Current.GetService<StarsDatabase>()!;
var allFleets = db.Fleet.ToList();
_fleets.AddOrUpdate(allFleets);
}
/// <summary>
/// Helper to fill up missing data from the original import. Tries to deduce
/// missing properties using heuristics.
/// </summary>
public static void PostProcessImportedData()
{
using var db = Locator.Current.GetService<StarsDatabase>()!;
// 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)} (?<ShipType>.+) #\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();
}
}

View File

@ -66,6 +66,11 @@ public class Game
/// </summary> /// </summary>
public string PlanetFileSearchPattern => $"{BaseName}.p*"; public string PlanetFileSearchPattern => $"{BaseName}.p*";
/// <summary>
/// Search for Planet files using this pattern, will give you all pxx files.
/// </summary>
public string FleetFileSearchPattern => $"{BaseName}.f*";
/// <summary> /// <summary>
/// Internal helper to lazily load the current player from the DB. /// Internal helper to lazily load the current player from the DB.
/// </summary> /// </summary>
@ -132,6 +137,8 @@ public class Game
Locator.CurrentMutable.Register(() => new StarsDatabase(DatabaseFileName), typeof(StarsDatabase)); Locator.CurrentMutable.Register(() => new StarsDatabase(DatabaseFileName), typeof(StarsDatabase));
PlanetManager PlanetManager = new(); PlanetManager PlanetManager = new();
Locator.CurrentMutable.RegisterConstant(PlanetManager, typeof(Services.PlanetManager)); Locator.CurrentMutable.RegisterConstant(PlanetManager, typeof(Services.PlanetManager));
FleetManager FleetManager = new();
Locator.CurrentMutable.RegisterConstant(FleetManager, typeof(Services.FleetManager));
// TESTING HELPER // TESTING HELPER
if (__doCreateTestData) if (__doCreateTestData)

View File

@ -13,24 +13,29 @@ public class PlanetManager
{ {
protected Services.Game Game = Locator.Current.GetService<Services.Game>()!; protected Services.Game Game = Locator.Current.GetService<Services.Game>()!;
/// <summary>
/// SourceCache for DynamicData views
/// </summary>
private SourceCache<Planet, string> _planets = new(p => p.Name); private SourceCache<Planet, string> _planets = new(p => p.Name);
/// <summary>
/// Observable changeset showing all player planets converted to view models.
/// </summary>
public IObservable<IChangeSet<PlayerPlanetViewModel, string>> PlayerPlanetsSource public IObservable<IChangeSet<PlayerPlanetViewModel, string>> PlayerPlanetsSource
=> _planets => _planets
.Connect() .Connect()
.Filter(planet => planet.OwnerId == Game.Player.Name) .Filter(planet => planet.OwnerId == Game.Player.Name)
.Transform(planet => new PlayerPlanetViewModel(planet)); .Transform(planet => new PlayerPlanetViewModel(planet));
public PlanetManager() public PlanetManager() {}
{
}
/// <summary>
/// Load the planet records from the database and push them into our source cache.
/// </summary>
public void InitFromDatabase() public void InitFromDatabase()
{ {
using var db = Locator.Current.GetService<StarsDatabase>()!; using var db = Locator.Current.GetService<StarsDatabase>()!;
{
var allPlanets = db.Planet.ToList(); var allPlanets = db.Planet.ToList();
_planets.AddOrUpdate(allPlanets); _planets.AddOrUpdate(allPlanets);
} }
}
} }

View File

@ -212,6 +212,19 @@ public partial class PlayerPlanetViewModel : ViewModelBase
// TODO: Take shipping into account. // TODO: Take shipping into account.
} }
/*
private int ComputePopulationEnRoute()
{
using var db = Locator.Current.GetService<Model.StarsDatabase>()!;
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 #endregion
} }