Skip to content

Commit

Permalink
add sortie record migration service
Browse files Browse the repository at this point in the history
  • Loading branch information
myangelkamikaze committed Dec 21, 2023
1 parent 8ce8248 commit 4b1d963
Show file tree
Hide file tree
Showing 10 changed files with 1,347 additions and 66 deletions.
2 changes: 2 additions & 0 deletions ElectronicObserver/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using ElectronicObserver.Common;
using ElectronicObserver.Data;
using ElectronicObserver.Database;
using ElectronicObserver.Database.DataMigration;
using ElectronicObserver.Services;
using ElectronicObserver.Utility;
using ElectronicObserver.Utility.ElectronicObserverApi;
Expand Down Expand Up @@ -312,6 +313,7 @@ private void ConfigureServices()
.AddSingleton<ColorService>()
.AddSingleton<ElectronicObserverApiService>()
.AddSingleton<DataAndTranslationIssueReporter>()
.AddSingleton<SortieRecordMigrationService>()
//.ActivateSingleton<DataAndTranslationIssueReporter>() todo uncomment and test with .net 8
// external
.AddSingleton(JotTracker())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ElectronicObserver.Database.Sortie;
using ElectronicObserver.Services;
using ElectronicObserver.Window.Tools.SortieRecordViewer.SortieDetail;
using ElectronicObserverTypes;
using ElectronicObserverTypes.Mocks;

namespace ElectronicObserver.Database.DataMigration;

public class SortieRecordMigrationService(ToolService toolService)
{
private ToolService ToolService { get; } = toolService;

public async Task Migrate(ElectronicObserverContext db, SortieRecord record)
{
if (record.Version is 0)
{
await MigrateToVersion1(db, record);
}
}

private async Task MigrateToVersion1(ElectronicObserverContext db, SortieRecord record)
{
SortieDetailViewModel? sortieDetails = ToolService.GenerateSortieDetailViewModel(db, record);

if (sortieDetails?.FleetsBeforeSortie.Fleets is null) return;
if (sortieDetails.Fleets.Fleets is null) return;
if (record.FleetAfterSortieData is null) return;

FixHp(record.FleetData, sortieDetails.FleetsBeforeSortie.Fleets);
FixHp(record.FleetAfterSortieData, sortieDetails.Fleets.Fleets);

record.Version = 1;

// need to manually call update because change tracker is off in sortie records
// and because the hp change can't be detected by ef (would need to create a new SortieFleetData instance)
db.Sorties.Update(record);
await db.SaveChangesAsync();
}

private static void FixHp(SortieFleetData fleetsToFix, IEnumerable<IFleetData?> computedFleets)
{
foreach ((SortieFleet? dbFleet, IFleetData? computedFleet) in fleetsToFix.Fleets.Zip(computedFleets))
{
if (dbFleet is null) continue;
if (computedFleet is null) continue;

foreach ((SortieShip dbShip, IShipData? computedShip) in dbFleet.Ships.Zip(computedFleet.MembersInstance))
{
if (computedShip is null) continue;

dbShip.Hp = ((ShipDataMock)computedShip).HPCurrent;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class ApiFileService : ObservableObject
private static int CurrentApiFileVersion => 1;

// before version 1:
// fleet after sortie can be null
// hp value is set to hp max instead of hp current
private static int CurrentSortieRecordVersion => 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.Json;
using ElectronicObserver.Database;
using ElectronicObserver.Database.DataMigration;
using ElectronicObserver.Database.KancolleApi;
using ElectronicObserver.KancolleApi.Types;
using ElectronicObserver.KancolleApi.Types.ApiGetMember.Mapinfo;
using ElectronicObserver.KancolleApi.Types.Models;
using ElectronicObserver.Window.Tools.SortieRecordViewer.Sortie.Battle;
using ElectronicObserver.Window.Tools.SortieRecordViewer.Sortie.Node;
using ElectronicObserver.Window.Tools.SortieRecordViewer.SortieDetail;
using ElectronicObserverTypes;
using ElectronicObserverTypes.Mocks;

namespace ElectronicObserver.Window.Tools.SortieRecordViewer.SortieCostViewer;

Expand Down Expand Up @@ -42,7 +40,8 @@ public class SortieCostViewModel
public SortieCostModel TotalAirBaseSupplyCost { get; }
public SortieCostModel TotalCost { get; }

public SortieCostViewModel(ElectronicObserverContext db, SortieRecordViewModel sortie, SortieDetailViewModel? sortieDetails)
public SortieCostViewModel(ElectronicObserverContext db, SortieRecordMigrationService sortieRecordMigrationService,
SortieRecordViewModel sortie)
{
Db = db;

Expand All @@ -55,36 +54,16 @@ public SortieCostViewModel(ElectronicObserverContext db, SortieRecordViewModel s
NodeSupportFleetId = sortie.Model.FleetData.NodeSupportFleetId;
BossSupportFleetId = sortie.Model.FleetData.BossSupportFleetId;

sortieRecordMigrationService.Migrate(db, sortie.Model).Wait();

FleetsBeforeSortie = sortie.Model.FleetData.MakeFleets();
FleetsAfterSortie = sortie.Model.FleetAfterSortieData.MakeFleets();
AirBases = sortie.Model.FleetData.AirBases
.Select(a => GameDataExtensions.MakeAirBase(a))
.Select(a => a.MakeAirBase())
.ToList();

if (FleetsAfterSortie is not null)
{
BattleFleets? initialState = sortieDetails?.Nodes
.OfType<BattleNode>()
.FirstOrDefault()
?.FirstBattle
.FleetsBeforeBattle;

if (initialState?.Fleets is not null)
{
FixHp(FleetsBeforeSortie, initialState.Fleets);
}

BattleFleets? finalState = sortieDetails?.Nodes
.OfType<BattleNode>()
.LastOrDefault()
?.LastBattle
.FleetsAfterBattle;

if (finalState?.Fleets is not null)
{
FixHp(FleetsAfterSortie, finalState.Fleets);
}

SortieFleetSupplyCost = SupplyCost(FleetsBeforeSortie[SortieFleetId - 1], FleetsAfterSortie[SortieFleetId - 1]);
SortieFleetRepairCost = RepairCost(FleetsBeforeSortie[SortieFleetId - 1], FleetsAfterSortie[SortieFleetId - 1]);

Expand All @@ -108,9 +87,7 @@ public SortieCostViewModel(ElectronicObserverContext db, SortieRecordViewModel s

TotalSupplyCost = SortieFleetSupplyCost + NodeSupportSupplyCost + BossSupportSupplyCost;

TotalRepairCost = FleetsBeforeSortie
.Zip(FleetsAfterSortie, RepairCost)
.Aggregate(new SortieCostModel(), (a, b) => a + b);
TotalRepairCost = SortieFleetRepairCost;

TotalAirBaseSortieCost = AirBases
.Where(a => a.ActionKind is AirBaseActionKind.Mission)
Expand All @@ -133,23 +110,6 @@ public SortieCostViewModel(ElectronicObserverContext db, SortieRecordViewModel s
TotalCost ??= new();
}

private static void FixHp(List<IFleetData?> fleetsToFix, List<IFleetData?> computedFleets)
{
foreach ((IFleetData? dbFleet, IFleetData? computedFleet) in fleetsToFix.Zip(computedFleets))
{
if (dbFleet is null) continue;
if (computedFleet is null) continue;

foreach ((IShipData? dbShip, IShipData? computedShip) in dbFleet.MembersInstance.Zip(computedFleet.MembersInstance))
{
if (dbShip is null) continue;
if (computedShip is null) continue;

((ShipDataMock)dbShip).HPCurrent = ((ShipDataMock)computedShip).HPCurrent;
}
}
}

private static SortieCostModel SupplyCost(IFleetData? before, IFleetData? after) => (before, after) switch
{
(not null, not null) => before.MembersInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using ElectronicObserver.Common;
using ElectronicObserver.Database;
using ElectronicObserver.Services;
using ElectronicObserver.Window.Tools.SortieRecordViewer.SortieDetail;
using ElectronicObserver.Database.DataMigration;

namespace ElectronicObserver.Window.Tools.SortieRecordViewer.SortieCostViewer;

public class SortieCostViewerViewModel : WindowViewModelBase
{
public SortieCostViewerTranslationViewModel Translation { get; }
public ToolService ToolService { get; }

public ElectronicObserverContext Db { get; }
public ObservableCollection<SortieRecordViewModel> Sorties { get; }
public ObservableCollection<SortieCostViewModel> SortieCosts { get; } = new();
private ElectronicObserverContext Db { get; }
private SortieRecordMigrationService SortieRecordMigrationService { get; }
private ObservableCollection<SortieRecordViewModel> Sorties { get; }
public ObservableCollection<SortieCostViewModel> SortieCosts { get; } = [];

public SortieCostModel? SortieCostSummary { get; private set; }

public SortieCostViewerViewModel(ElectronicObserverContext db,
public SortieCostViewerViewModel(ElectronicObserverContext db, SortieRecordMigrationService sortieRecordMigrationService,
ObservableCollection<SortieRecordViewModel> sorties)
{
Translation = Ioc.Default.GetRequiredService<SortieCostViewerTranslationViewModel>();
ToolService = Ioc.Default.GetRequiredService<ToolService>();

Db = db;
SortieRecordMigrationService = sortieRecordMigrationService;
Sorties = sorties;
}

Expand All @@ -40,9 +39,7 @@ private void CalculateCost()
{
foreach (SortieRecordViewModel sortie in Sorties)
{
sortie.Model.EnsureApiFilesLoaded(Db).Wait();
SortieDetailViewModel? details = ToolService.GenerateSortieDetailViewModel(Db, sortie.Model);
SortieCosts.Add(new(Db, sortie, details));
SortieCosts.Add(new(Db, SortieRecordMigrationService, sortie));
}

SortieCostSummary = SortieCosts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public partial class SortieDetailViewModel : WindowViewModelBase
public DateTime? StartTime { get; set; }
public int World { get; }
public int Map { get; }
private BattleFleets Fleets { get; set; }
private BattleFleets? FleetsAfterSortie { get; set; }
public BattleFleets Fleets { get; set; }
public BattleFleets FleetsBeforeSortie { get; private set; }
private BattleFleets? FleetsAfterSortie { get; }
public List<List<int>?> StrikePoints { get; } = new();

public ObservableCollection<SortieNode> Nodes { get; } = new();
Expand All @@ -72,6 +73,7 @@ public SortieDetailViewModel(ElectronicObserverContext db, SortieRecord sortie,
World = sortie.World;
Map = sortie.Map;
Fleets = fleets;
FleetsBeforeSortie = fleets.Clone();
FleetsAfterSortie = fleetsAfterSortie;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using ElectronicObserver.Common.Datagrid;
using ElectronicObserver.Data;
using ElectronicObserver.Database;
using ElectronicObserver.Database.DataMigration;
using ElectronicObserver.Services;
using ElectronicObserver.Utility;
using ElectronicObserver.Window.Tools.SortieRecordViewer.DataExport;
Expand All @@ -31,6 +32,7 @@ public partial class SortieRecordViewerViewModel : WindowViewModelBase
{
private ToolService ToolService { get; }
private FileService FileService { get; }
private SortieRecordMigrationService SortieRecordMigrationService { get; }
private ElectronicObserverContext Db { get; } = new();
private DataExportHelper DataExportHelper { get; }

Expand Down Expand Up @@ -74,6 +76,7 @@ public SortieRecordViewerViewModel()
{
ToolService = Ioc.Default.GetRequiredService<ToolService>();
FileService = Ioc.Default.GetRequiredService<FileService>();
SortieRecordMigrationService = Ioc.Default.GetRequiredService<SortieRecordMigrationService>();
DataExportHelper = new(Db, ToolService);
SortieRecordViewer = Ioc.Default.GetRequiredService<SortieRecordViewerTranslationViewModel>();

Expand Down Expand Up @@ -420,7 +423,7 @@ private void OpenSortieCost()
{
if (SelectedSorties.Count <= 0) return;

SortieCostViewerViewModel sortieCost = new(Db, SelectedSorties);
SortieCostViewerViewModel sortieCost = new(Db, SortieRecordMigrationService, SelectedSorties);

new SortieCostViewerWindow(sortieCost).Show();
}
Expand Down
Loading

0 comments on commit 4b1d963

Please sign in to comment.