continue fleet summary, factor out helper class.
This commit is contained in:
		
							
								
								
									
										14
									
								
								Stars Assistant/Model/FleetSummary.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								Stars Assistant/Model/FleetSummary.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace StarsAssistant.Model;
 | 
			
		||||
 | 
			
		||||
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; }
 | 
			
		||||
 | 
			
		||||
        public override string ToString() => $"To {Destination}: {TotalIronium}I/{TotalBoranium}B/{TotalGermanium}G/{TotalColonists}C";
 | 
			
		||||
}
 | 
			
		||||
@@ -2,6 +2,7 @@ using System;
 | 
			
		||||
using System.Collections.Immutable;
 | 
			
		||||
using System.Collections.ObjectModel;
 | 
			
		||||
using System.ComponentModel;
 | 
			
		||||
using System.Diagnostics.CodeAnalysis;
 | 
			
		||||
using System.Reactive;
 | 
			
		||||
using System.Reactive.Linq;
 | 
			
		||||
using System.Text.RegularExpressions;
 | 
			
		||||
@@ -15,40 +16,39 @@ using StarsAssistant.ViewModels;
 | 
			
		||||
 | 
			
		||||
namespace StarsAssistant.Services;
 | 
			
		||||
 | 
			
		||||
public class FleetManager : IEnableLogger
 | 
			
		||||
public class FleetManager : IEnableLogger, IDisposable
 | 
			
		||||
{
 | 
			
		||||
    protected Services.Game Game = Locator.Current.GetService<Services.Game>()!;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// SourceCache for DynamicData views
 | 
			
		||||
    /// SourceList for fleets read from the game files
 | 
			
		||||
    /// </summary> 
 | 
			
		||||
    private SourceList<Fleet> _fleets = new();
 | 
			
		||||
 | 
			
		||||
    public FleetManager() {}
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Fleet data summarized by destination, will be chaned to _fleets
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    private SourceCache<FleetSummary, string> _fleetSummaries = new(fs => fs.Destination);
 | 
			
		||||
 | 
			
		||||
    public class FleetSummary
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Public accessor to the continously updated fleet summaries.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public IObservableCache<FleetSummary, string> FleetSummaries => _fleetSummaries.AsObservableCache();
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Disposal tracking
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    private IDisposable _fleetSummariesSubscription;
 | 
			
		||||
 | 
			
		||||
    public FleetManager() 
 | 
			
		||||
    {
 | 
			
		||||
        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; }
 | 
			
		||||
 | 
			
		||||
        public override string ToString() => $"To {Destination}: {TotalIronium}I/{TotalBoranium}B/{TotalGermanium}G/{TotalColonists}C";
 | 
			
		||||
        CreateFleetSummariesLink();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private IObservable<IChangeSet<FleetSummary>>? _fleetSummaries;
 | 
			
		||||
 | 
			
		||||
    private ReadOnlyObservableCollection<FleetSummary>? summaries;
 | 
			
		||||
 | 
			
		||||
    private IDisposable? d1;
 | 
			
		||||
 | 
			
		||||
    private IDisposable? d2;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public void test() 
 | 
			
		||||
    [MemberNotNull(nameof(_fleetSummariesSubscription))]
 | 
			
		||||
    protected void CreateFleetSummariesLink()
 | 
			
		||||
    {
 | 
			
		||||
        _fleetSummaries = _fleets.Connect()
 | 
			
		||||
        _fleetSummariesSubscription = _fleets.Connect()
 | 
			
		||||
            .Filter(fleet => fleet.TrueDestination != "-- ")
 | 
			
		||||
            .GroupOn(fleet => fleet.TrueDestination)
 | 
			
		||||
            .Log(this, $"{DateTime.Now.ToLongTimeString()} fleetWatcher", grp => $"{grp.TotalChanges} detected")
 | 
			
		||||
@@ -60,15 +60,20 @@ public class FleetManager : IEnableLogger
 | 
			
		||||
                    TotalGermanium = group.List.Items.Sum(f => f.Germanium),
 | 
			
		||||
                    TotalColonists = group.List.Items.Sum(f => f.Colonists)
 | 
			
		||||
                })
 | 
			
		||||
            .AddKey(fs => fs.Destination)
 | 
			
		||||
            .Log(this, "FleetManager _fleetSummaries update", changes => 
 | 
			
		||||
                    $"{changes.Adds} adds, {changes.Updates} updates, {changes.Removes} removes"
 | 
			
		||||
                )
 | 
			
		||||
            .PopulateInto(_fleetSummaries)
 | 
			
		||||
        ;
 | 
			
		||||
 | 
			
		||||
        /*
 | 
			
		||||
        // Demo only
 | 
			
		||||
        var sourceCache = new SourceCache<FleetSummary, string>(fs => fs.Destination);
 | 
			
		||||
        var tmp = _fleetSummaries
 | 
			
		||||
        var tmp = fleetSummaries
 | 
			
		||||
            .AddKey(fs => fs.Destination)
 | 
			
		||||
            .PopulateInto(sourceCache);
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        _fleetSummaries
 | 
			
		||||
            .ObserveOn(RxApp.MainThreadScheduler)
 | 
			
		||||
            .Bind(out summaries)
 | 
			
		||||
@@ -93,6 +98,7 @@ public class FleetManager : IEnableLogger
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            );
 | 
			
		||||
        */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
@@ -151,4 +157,25 @@ public class FleetManager : IEnableLogger
 | 
			
		||||
        db.SaveChanges();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Handle disposal of all subscriptions and dependencies.
 | 
			
		||||
    /// </summary>/
 | 
			
		||||
    protected virtual void Dispose(bool disposing)
 | 
			
		||||
    {
 | 
			
		||||
        if (disposing)
 | 
			
		||||
        {
 | 
			
		||||
            _fleetSummariesSubscription.Dispose();
 | 
			
		||||
            _fleetSummaries.Dispose();
 | 
			
		||||
            _fleets.Dispose();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Boilerplate disposal
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public void Dispose()
 | 
			
		||||
    {
 | 
			
		||||
        Dispose(true);
 | 
			
		||||
        GC.SuppressFinalize(this);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user