Intermediate commit rewrite player planet cache
This commit is contained in:
parent
4789904284
commit
ef5a8ae4af
@ -30,6 +30,11 @@ public class PlanetManager : IDisposable, IEnableLogger
|
|||||||
.Filter(planet => planet.OwnerId == Game.Player.Name)
|
.Filter(planet => planet.OwnerId == Game.Player.Name)
|
||||||
.Transform(planet => new PlayerPlanetViewModel(planet));
|
.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>
|
/// <summary>
|
||||||
/// Observable subject to trigger when we update the planet cache, allows the
|
/// Observable subject to trigger when we update the planet cache, allows the
|
||||||
/// UI to react to it.
|
/// UI to react to it.
|
||||||
@ -64,6 +69,27 @@ public class PlanetManager : IDisposable, IEnableLogger
|
|||||||
innerCache.AddOrUpdate(allPlanets);
|
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);
|
_planetsReloadedSubject.OnNext(Unit.Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,8 +18,6 @@ public partial class BuColViewModel : ViewModelBase
|
|||||||
[Reactive]
|
[Reactive]
|
||||||
private PlayerPlanetViewModel? _selectedPlanet;
|
private PlayerPlanetViewModel? _selectedPlanet;
|
||||||
|
|
||||||
private string? _lastSelectedPlanetName;
|
|
||||||
|
|
||||||
public BuColViewModel()
|
public BuColViewModel()
|
||||||
{
|
{
|
||||||
var planetManager = Locator.Current.GetService<PlanetManager>()!;
|
var planetManager = Locator.Current.GetService<PlanetManager>()!;
|
||||||
@ -30,23 +28,5 @@ public partial class BuColViewModel : ViewModelBase
|
|||||||
.DisposeMany()
|
.DisposeMany()
|
||||||
.Subscribe();
|
.Subscribe();
|
||||||
|
|
||||||
this.WhenAnyValue(x => x.SelectedPlanet)
|
|
||||||
.Subscribe(planet => _lastSelectedPlanetName = planet?.Name);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,20 @@ namespace StarsAssistant.ViewModels;
|
|||||||
|
|
||||||
public partial class PlayerPlanetViewModel : ViewModelBase
|
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;
|
private readonly Services.Game Game;
|
||||||
|
|
||||||
@ -269,6 +282,57 @@ public partial class PlayerPlanetViewModel : ViewModelBase
|
|||||||
, 0);
|
, 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
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user