44using dsstats . db8 ;
55using dsstats . shared ;
66using dsstats . shared . Extensions ;
7+ using dsstats . shared . Interfaces ;
78using Microsoft . EntityFrameworkCore ;
89using Microsoft . Extensions . Logging ;
910using Microsoft . Extensions . Options ;
10- using System ;
1111using System . Diagnostics ;
1212
1313namespace dsstats . db8services ;
@@ -18,20 +18,24 @@ public class ReplayRepository : IReplayRepository
1818 private readonly ReplayContext context ;
1919 private readonly IOptions < DbImportOptions > dbImportOptions ;
2020 private readonly IMapper mapper ;
21+ private readonly IImportService importService ;
2122
2223 public ReplayRepository ( ILogger < ReplayRepository > logger ,
2324 ReplayContext context ,
2425 IOptions < DbImportOptions > dbImportOptions ,
25- IMapper mapper )
26+ IMapper mapper ,
27+ IImportService importService )
2628 {
2729 this . logger = logger ;
2830 this . context = context ;
2931 this . dbImportOptions = dbImportOptions ;
3032 this . mapper = mapper ;
33+ this . importService = importService ;
3134 }
3235
33- public async Task SaveReplay ( ReplayDto replayDto , HashSet < Unit > units , HashSet < Upgrade > upgrades )
36+ public async Task SaveReplay ( ReplayDto replayDto )
3437 {
38+ await importService . Init ( ) ;
3539 replayDto . SetDefaultFilter ( ) ;
3640
3741 var dbReplay = mapper . Map < Replay > ( replayDto ) ;
@@ -80,10 +84,10 @@ public async Task SaveReplay(ReplayDto replayDto, HashSet<Unit> units, HashSet<U
8084
8185 foreach ( var spawn in replayPlayer . Spawns )
8286 {
83- spawn . Units = await GetMapedSpawnUnits ( spawn , replayPlayer . Race , units ) ;
87+ spawn . Units = GetMapedSpawnUnits ( spawn , replayPlayer . Race ) ;
8488 }
8589
86- replayPlayer . Upgrades = await GetMapedPlayerUpgrades ( replayPlayer , upgrades ) ;
90+ replayPlayer . Upgrades = GetMapedPlayerUpgrades ( replayPlayer ) ;
8791
8892 }
8993
@@ -106,55 +110,31 @@ public async Task SaveReplay(ReplayDto replayDto, HashSet<Unit> units, HashSet<U
106110 }
107111 }
108112
109- private async Task < ICollection < SpawnUnit > > GetMapedSpawnUnits ( Spawn spawn , Commander commander , HashSet < Unit > units )
113+ private ICollection < SpawnUnit > GetMapedSpawnUnits ( Spawn spawn , Commander commander )
110114 {
111115 List < SpawnUnit > spawnUnits = new ( ) ;
112116 foreach ( var spawnUnit in spawn . Units )
113117 {
114- var listUnit = units . FirstOrDefault ( f => f . Name . Equals ( spawnUnit . Unit . Name ) ) ;
115- if ( listUnit == null )
116- {
117- listUnit = new ( )
118- {
119- Name = spawnUnit . Unit . Name
120- } ;
121- context . Units . Add ( listUnit ) ;
122- await context . SaveChangesAsync ( ) ;
123- units . Add ( listUnit ) ;
124- }
125-
126118 spawnUnits . Add ( new ( )
127119 {
128120 Count = spawnUnit . Count ,
129121 Poss = spawnUnit . Poss ,
130- UnitId = listUnit . UnitId ,
122+ UnitId = importService . GetUnitId ( spawnUnit . Unit . Name ) ,
131123 SpawnId = spawn . SpawnId
132124 } ) ;
133125 }
134126 return spawnUnits ;
135127 }
136128
137- private async Task < ICollection < PlayerUpgrade > > GetMapedPlayerUpgrades ( ReplayPlayer player , HashSet < Upgrade > upgrades )
129+ private ICollection < PlayerUpgrade > GetMapedPlayerUpgrades ( ReplayPlayer player )
138130 {
139131 List < PlayerUpgrade > playerUpgrades = new ( ) ;
140132 foreach ( var playerUpgrade in player . Upgrades )
141133 {
142- var listUpgrade = upgrades . FirstOrDefault ( f => f . Name . Equals ( playerUpgrade . Upgrade . Name ) ) ;
143- if ( listUpgrade == null )
144- {
145- listUpgrade = new ( )
146- {
147- Name = playerUpgrade . Upgrade . Name
148- } ;
149- context . Upgrades . Add ( listUpgrade ) ;
150- await context . SaveChangesAsync ( ) ;
151- upgrades . Add ( listUpgrade ) ;
152- }
153-
154134 playerUpgrades . Add ( new ( )
155135 {
156136 Gameloop = playerUpgrade . Gameloop ,
157- UpgradeId = listUpgrade . UpgradeId ,
137+ UpgradeId = importService . GetUpgradeId ( playerUpgrade . Upgrade . Name ) ,
158138 ReplayPlayerId = player . ReplayPlayerId
159139 } ) ;
160140 }
0 commit comments