Intermediate commit rewrite player planet cache

This commit is contained in:
Torben Nehmer 2025-01-15 21:48:24 +01:00
parent 4789904284
commit ef5a8ae4af
No known key found for this signature in database
3 changed files with 92 additions and 22 deletions

View File

@ -30,6 +30,11 @@ public class PlanetManager : IDisposable, IEnableLogger
.Filter(planet => planet.OwnerId == Game.Player.Name)
.Transform(planet => new PlayerPlanetViewModel(planet));
/// <summary>
/// Cache with all player planets, indexed by planet name.
/// </summary>
private SourceCache<PlayerPlanetViewModel, string> _playerPlanets = new(p => p.Planet.Name);
/// <summary>
/// Observable subject to trigger when we update the planet cache, allows the
/// UI to react to it.
@ -64,6 +69,27 @@ public class PlanetManager : IDisposable, IEnableLogger
innerCache.AddOrUpdate(allPlanets);
}
);
var allPlayerPlanets = allPlanets.Where(planet => planet.OwnerId == Game.Player.Name);
var allPlayerCacheKeys = _playerPlanets.Keys.ToList();
var playerPlanetNames = from p in allPlayerPlanets select p.Name;
var playerPlanetsToDelete = allPlayerCacheKeys.Except(playerPlanetNames);
_playerPlanets.Edit(innerCache =>
{
foreach (var name in playerPlanetsToDelete)
innerCache.RemoveKey(name);
foreach (var planet in allPlayerPlanets)
{
var playerPlanet = innerCache.Lookup(planet.Name);
if (playerPlanet.HasValue)
playerPlanet.Value.Planet = planet;
else
innerCache.Add(new PlayerPlanetViewModel(planet));
}
}
);
_planetsReloadedSubject.OnNext(Unit.Default);
}

View File

@ -18,8 +18,6 @@ public partial class BuColViewModel : ViewModelBase
[Reactive]
private PlayerPlanetViewModel? _selectedPlanet;
private string? _lastSelectedPlanetName;
public BuColViewModel()
{
var planetManager = Locator.Current.GetService<PlanetManager>()!;
@ -28,25 +26,7 @@ public partial class BuColViewModel : ViewModelBase
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out _playerPlanetsView)
.DisposeMany()
.Subscribe();
this.WhenAnyValue(x => x.SelectedPlanet)
.Subscribe(planet => _lastSelectedPlanetName = planet?.Name);
.Subscribe();
this.WhenActivated((CompositeDisposable disposables) =>
{
planetManager.PlanetsReloaded
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ => RestoreSelection())
.DisposeWith(disposables);
});
}
private void RestoreSelection()
{
if (_lastSelectedPlanetName != null)
{
SelectedPlanet = PlayerPlanets.FirstOrDefault(p => p.Name == _lastSelectedPlanetName);
}
}
}

View File

@ -13,7 +13,20 @@ namespace StarsAssistant.ViewModels;
public partial class PlayerPlanetViewModel : ViewModelBase
{
private readonly Model.Planet Planet;
[Reactive]
private Model.Planet _planet;
/* TODO
public Model.Planet Planet
{
get => _planet;
set
{
this.RaiseAndSetIfChanged(ref _planet, value);
RaisePropertyChangedForAll();
}
}
*/
private readonly Services.Game Game;
@ -269,6 +282,57 @@ public partial class PlayerPlanetViewModel : ViewModelBase
, 0);
}
private void RaisePropertyChangedForAll()
{
OnPropertyChanged(nameof(Name));
OnPropertyChanged(nameof(Owner));
OnPropertyChanged(nameof(StarbaseType));
OnPropertyChanged(nameof(Population));
OnPropertyChanged(nameof(Value));
OnPropertyChanged(nameof(Mines));
OnPropertyChanged(nameof(Factories));
OnPropertyChanged(nameof(DefPercent));
OnPropertyChanged(nameof(SurfaceIronium));
OnPropertyChanged(nameof(SurfaceBoranium));
OnPropertyChanged(nameof(SurfaceGermanium));
OnPropertyChanged(nameof(MRIronium));
OnPropertyChanged(nameof(MRBoranium));
OnPropertyChanged(nameof(MRGermanium));
OnPropertyChanged(nameof(MCIronium));
OnPropertyChanged(nameof(MCBoranium));
OnPropertyChanged(nameof(MCGermanium));
OnPropertyChanged(nameof(Resources));
OnPropertyChanged(nameof(Gravity));
OnPropertyChanged(nameof(Temperature));
OnPropertyChanged(nameof(Radiation));
OnPropertyChanged(nameof(GravityOrig));
OnPropertyChanged(nameof(TemperatureOrig));
OnPropertyChanged(nameof(RadiationOrig));
OnPropertyChanged(nameof(MaxTerraforming));
OnPropertyChanged(nameof(CapacityPercent));
OnPropertyChanged(nameof(ScanRange));
OnPropertyChanged(nameof(PenScanRange));
OnPropertyChanged(nameof(Driver));
OnPropertyChanged(nameof(DriverWarp));
OnPropertyChanged(nameof(RouteTarget));
OnPropertyChanged(nameof(GateRange));
OnPropertyChanged(nameof(GateMass));
OnPropertyChanged(nameof(PctDamage));
OnPropertyChanged(nameof(EffectiveValue));
OnPropertyChanged(nameof(MaxPopulation));
OnPropertyChanged(nameof(PopulationGrowth));
OnPropertyChanged(nameof(PopulationT1));
OnPropertyChanged(nameof(BuildableFactories));
OnPropertyChanged(nameof(BuildableMines));
OnPropertyChanged(nameof(OperableFactories));
OnPropertyChanged(nameof(OperableMines));
OnPropertyChanged(nameof(ResourcesFromPopulation));
OnPropertyChanged(nameof(ResourcesFromFactories));
OnPropertyChanged(nameof(AvailableIronium));
OnPropertyChanged(nameof(AvailableBoranium));
OnPropertyChanged(nameof(AvailableGermanium));
}
#endregion
}