@@ -116,6 +116,10 @@ private async void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventAr
116116
117117 if ( MiscVar . PlanetWarsMode != PlanetWarsModes . Running ) return ;
118118
119+ // clean up stale running battles (e.g. if Spring process crashed)
120+ var staleBattleIds = RunningBattles . Keys . Where ( id => ! server . Battles . ContainsKey ( id ) ) . ToList ( ) ;
121+ foreach ( var id in staleBattleIds ) RunningBattles . Remove ( id ) ;
122+
119123 switch ( Phase )
120124 {
121125 case PwPhase . AttackCollect :
@@ -188,8 +192,7 @@ private void RunSquadFormation()
188192 var user = server . ConnectedUsers . Get ( name ) ? . User ;
189193 if ( user == null ) { playerPlanet . Remove ( name ) ; continue ; }
190194
191- var rating = RatingSystems . GetRatingSystem ( RatingCategory . Planetwars ) . GetPlayerRating ( user . AccountID ) ;
192- playerWhr [ name ] = rating . LadderElo ;
195+ playerWhr [ name ] = GetPlayerWhr ( name ) ;
193196
194197 // PW-Rank: faction role DisplayOrder, lower = higher rank. No role = int.MaxValue
195198 var account = db . Accounts . Find ( user . AccountID ) ;
@@ -298,18 +301,16 @@ private void RunDefenderAssignment()
298301 {
299302 if ( defenderWhr . ContainsKey ( name ) ) continue ;
300303 if ( ! server . ConnectedUsers . ContainsKey ( name ) ) continue ;
301- var user = server . ConnectedUsers . Get ( name ) ? . User ;
302- if ( user == null ) continue ;
303- var rating = RatingSystems . GetRatingSystem ( RatingCategory . Planetwars ) . GetPlayerRating ( user . AccountID ) ;
304- defenderWhr [ name ] = rating . LadderElo ;
304+ defenderWhr [ name ] = GetPlayerWhr ( name ) ;
305305 }
306306 }
307307
308308 // per-planet: assign defenders, overflow to pool
309309 var floatingPool = new List < string > ( ) ;
310310 var assignedDefenders = new Dictionary < int , List < string > > ( ) ; // planetID -> assigned defender names
311+ var attackedPlanetIds = FormedSquads . Select ( s => s . PlanetID ) . Distinct ( ) . ToList ( ) ;
311312
312- foreach ( var planetId in FormedSquads . Select ( s => s . PlanetID ) . Distinct ( ) )
313+ foreach ( var planetId in attackedPlanetIds )
313314 {
314315 var totalSlotsNeeded = FormedSquads . Where ( s => s . PlanetID == planetId ) . Sum ( s => s . TeamSize ) ;
315316 var volunteers = ( DefenderVotes . ContainsKey ( planetId ) ? DefenderVotes [ planetId ] : new List < string > ( ) )
@@ -330,7 +331,7 @@ private void RunDefenderAssignment()
330331
331332 // floating pool fills unfilled slots on other planets (WHR order)
332333 floatingPool = floatingPool . OrderByDescending ( x => defenderWhr . Get ( x ) ) . ToList ( ) ;
333- foreach ( var planetId in FormedSquads . Select ( s => s . PlanetID ) . Distinct ( ) )
334+ foreach ( var planetId in attackedPlanetIds )
334335 {
335336 var totalSlotsNeeded = FormedSquads . Where ( s => s . PlanetID == planetId ) . Sum ( s => s . TeamSize ) ;
336337 var assigned = assignedDefenders [ planetId ] ;
@@ -344,7 +345,7 @@ private void RunDefenderAssignment()
344345 }
345346
346347 // slice defenders into squads: sort squads by avg attacker WHR desc, assign best defenders to best attackers
347- foreach ( var planetId in FormedSquads . Select ( s => s . PlanetID ) . Distinct ( ) )
348+ foreach ( var planetId in attackedPlanetIds )
348349 {
349350 var squadsForPlanet = FormedSquads
350351 . Where ( s => s . PlanetID == planetId )
@@ -631,9 +632,15 @@ public PwMatchCommand GenerateLobbyCommand()
631632 } ) ;
632633 }
633634
634- // collect all defending factions across all attacked planets
635- var allDefFactions = FormedSquads
636- . SelectMany ( s => GetDefendingFactions ( s ) . Select ( f => f . Shortcut ) )
635+ // collect all defending factions across attacked planets (one DB call per distinct planet, not per squad)
636+ var defFactionCache = new Dictionary < int , List < Faction > > ( ) ;
637+ foreach ( var pid in options . Select ( o => o . PlanetID ) )
638+ {
639+ if ( ! defFactionCache . ContainsKey ( pid ) )
640+ defFactionCache [ pid ] = GetDefendingFactions ( FormedSquads . First ( s => s . PlanetID == pid ) ) ;
641+ }
642+ var allDefFactions = defFactionCache . Values
643+ . SelectMany ( f => f . Select ( x => x . Shortcut ) )
637644 . Distinct ( )
638645 . ToList ( ) ;
639646
@@ -801,18 +808,20 @@ private void RecordPlanetwarsLoss(AttackOption option)
801808
802809 try
803810 {
804- var db = new ZkDataContext ( ) ;
805- var playerIds = option . Attackers . Union ( option . Defenders ) . ToList ( ) ;
806-
807- PlanetWarsTurnHandler . ProcessBattleResult ( option . Map ,
808- null ,
809- db ,
810- 0 ,
811- db . Accounts . Where ( x => playerIds . Contains ( x . Name ) && ( x . Faction != null ) ) . ToList ( ) ,
812- new StringBuilder ( ) ,
813- null ,
814- db . Accounts . Where ( x => option . Attackers . Contains ( x . Name ) && ( x . Faction != null ) ) . ToList ( ) ,
815- server . PlanetWarsEventCreator , server ) ;
811+ using ( var db = new ZkDataContext ( ) )
812+ {
813+ var playerIds = option . Attackers . Union ( option . Defenders ) . ToList ( ) ;
814+
815+ PlanetWarsTurnHandler . ProcessBattleResult ( option . Map ,
816+ null ,
817+ db ,
818+ 0 ,
819+ db . Accounts . Where ( x => playerIds . Contains ( x . Name ) && ( x . Faction != null ) ) . ToList ( ) ,
820+ new StringBuilder ( ) ,
821+ null ,
822+ db . Accounts . Where ( x => option . Attackers . Contains ( x . Name ) && ( x . Faction != null ) ) . ToList ( ) ,
823+ server . PlanetWarsEventCreator , server ) ;
824+ }
816825 }
817826 catch ( Exception ex )
818827 {
@@ -855,14 +864,14 @@ private Task UpdateLobby(string player)
855864
856865 private void SaveStateToDb ( )
857866 {
858- var db = new ZkDataContext ( ) ;
859- var gal = db . Galaxies . First ( x => x . IsDefault ) ;
860-
861- gal . MatchMakerState = JsonConvert . SerializeObject ( ( PlanetWarsMatchMakerState ) this ) ;
862-
863- gal . AttackerSideCounter = AttackerSideCounter ;
864- gal . AttackerSideChangeTime = AttackerSideChangeTime ;
865- db . SaveChanges ( ) ;
867+ using ( var db = new ZkDataContext ( ) )
868+ {
869+ var gal = db . Galaxies . First ( x => x . IsDefault ) ;
870+ gal . MatchMakerState = JsonConvert . SerializeObject ( ( PlanetWarsMatchMakerState ) this ) ;
871+ gal . AttackerSideCounter = AttackerSideCounter ;
872+ gal . AttackerSideChangeTime = AttackerSideChangeTime ;
873+ db . SaveChanges ( ) ;
874+ }
866875 }
867876
868877 private static PwStatus GeneratePwStatus ( )
0 commit comments