stich fleet loading together
This commit is contained in:
parent
e6c2f969b4
commit
b1fb26db5a
@ -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; }
|
||||
|
@ -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<PlanetManager>()!;
|
||||
manager.InitFromDatabase();
|
||||
PlanetManager planetManager = Locator.Current.GetService<PlanetManager>()!;
|
||||
planetManager.InitFromDatabase();
|
||||
break;
|
||||
|
||||
case "f":
|
||||
var fleetLoader = new CSV.FleetLoader();
|
||||
fleetLoader.ImportForRace(Services.Game.Player);
|
||||
|
||||
FleetManager.PostProcessImportedData();
|
||||
FleetManager fleetManager = Locator.Current.GetService<FleetManager>()!;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
75
Stars Assistant/Services/FleetManager.cs
Normal file
75
Stars Assistant/Services/FleetManager.cs
Normal 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();
|
||||
}
|
||||
|
||||
}
|
@ -66,6 +66,11 @@ public class Game
|
||||
/// </summary>
|
||||
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>
|
||||
/// Internal helper to lazily load the current player from the DB.
|
||||
/// </summary>
|
||||
@ -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)
|
||||
|
@ -13,24 +13,29 @@ public class PlanetManager
|
||||
{
|
||||
protected Services.Game Game = Locator.Current.GetService<Services.Game>()!;
|
||||
|
||||
/// <summary>
|
||||
/// SourceCache for DynamicData views
|
||||
/// </summary>
|
||||
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
|
||||
=> _planets
|
||||
.Connect()
|
||||
.Filter(planet => planet.OwnerId == Game.Player.Name)
|
||||
.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()
|
||||
{
|
||||
using var db = Locator.Current.GetService<StarsDatabase>()!;
|
||||
{
|
||||
var allPlanets = db.Planet.ToList();
|
||||
_planets.AddOrUpdate(allPlanets);
|
||||
}
|
||||
var allPlanets = db.Planet.ToList();
|
||||
_planets.AddOrUpdate(allPlanets);
|
||||
}
|
||||
}
|
||||
|
@ -212,6 +212,19 @@ public partial class PlayerPlanetViewModel : ViewModelBase
|
||||
// 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
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user