sa/Stars Assistant/Services/FleetManager.cs

116 lines
3.7 KiB
C#
Raw Normal View History

2024-10-02 18:18:00 +00:00
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>
2024-10-05 16:47:53 +00:00
private SourceList<Fleet> _fleets = new();
2024-10-02 18:18:00 +00:00
public FleetManager() {}
2024-10-04 17:05:23 +00:00
public class FleetSummary
{
public string Destination { get; set; } = string.Empty;
public int TotalIronium { get; set; }
public int TotalBoranium { get; set; }
public int TotalGermanium { get; set; }
public int TotalColonists { get; set; }
}
private IObservable<IChangeSet<FleetSummary, string>>? _fleetSummaries;
public void test()
{
2024-10-05 16:47:53 +00:00
var xxxxx = _fleets.Connect()
.GroupOn(fleet => fleet.TrueDestination)
.Transform(group =>
group.List.Connect()
.ToCollection()
.Select(query =>
{
var iro = query.Sum(f => f.Ironium);
var bor = query.Sum(f => f.Boranium);
var ger = query.Sum(f => f.Germanium);
var col = query.Sum(f => f.Colonists);
return new FleetSummary
{
Destination = group.GroupKey,
TotalIronium = iro,
TotalBoranium = bor,
TotalGermanium = ger,
TotalColonists = col
};
})
);
2024-10-04 17:05:23 +00:00
}
2024-10-02 18:18:00 +00:00
/// <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();
2024-10-04 17:05:23 +00:00
_fleets.Edit(innerCache =>
{
innerCache.Clear();
2024-10-05 16:47:53 +00:00
innerCache.Add(allFleets);
2024-10-04 17:05:23 +00:00
}
);
2024-10-02 18:18:00 +00:00
}
/// <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();
}
}