@@ -467,15 +467,19 @@ public string DebugPlayer(Account player)
467467 //Runs in O(N log(N)) for all players
468468 private void UpdateRankings ( IEnumerable < Player > players )
469469 {
470+ Dictionary < int , float > oldRatings = new Dictionary < int , float > ( ) ;
471+ List < SpringBattlePlayer > lastBattlePlayers ;
472+ var debriefings = new Dictionary < int , PendingDebriefing > ( pendingDebriefings ) ;
473+ int matched = 0 ;
474+
470475 try
471476 {
472- Dictionary < int , float > oldRatings = new Dictionary < int , float > ( ) ;
473-
477+
474478 //check for ladder elo updates
475479 using ( var db = new ZkDataContext ( ) )
476480 {
477- var battleIDs = pendingDebriefings . Keys . ToList ( ) ;
478- var lastBattlePlayers = db . SpringBattlePlayers . Where ( p => battleIDs . Contains ( p . SpringBattleID ) && ! p . IsSpectator ) . Include ( x => x . Account ) . DistinctBy ( x => x . AccountID ) . ToList ( ) ;
481+ var battleIDs = debriefings . Keys . ToList ( ) ;
482+ lastBattlePlayers = db . SpringBattlePlayers . Where ( p => battleIDs . Contains ( p . SpringBattleID ) && ! p . IsSpectator ) . Include ( x => x . Account ) . DistinctBy ( x => x . AccountID ) . ToList ( ) ;
479483 oldRatings = lastBattlePlayers . ToDictionary ( p => ( p . AccountID ) , p => GetPlayerRating ( p . AccountID ) . LadderElo ) ;
480484 lastBattlePlayers . Where ( p => ! playerRatings . ContainsKey ( ( p . AccountID ) ) ) . ForEach ( p => playerRatings [ ( p . AccountID ) ] = new PlayerRating ( DefaultRating ) ) ;
481485 lastBattlePlayers . ForEach ( p => playerRatings [ ( p . AccountID ) ] . LadderElo = Ranks . UpdateLadderRating ( p . Account , category , getPlayerById ( ( p . AccountID ) ) . avgElo + RatingOffset , p . IsInVictoryTeam , ! p . IsInVictoryTeam , db ) ) ;
@@ -512,7 +516,6 @@ private void UpdateRankings(IEnumerable<Player> players)
512516 this . activePlayers = playerCount ;
513517 int rank = 0 ;
514518 List < int > newTopPlayers = new List < int > ( ) ;
515- int matched = 0 ;
516519 List < float > newPercentileBrackets = new List < float > ( ) ;
517520 newPercentileBrackets . Add ( playerRatings [ sortedPlayers . First ( ) . Value ] . LadderElo ) ;
518521 float percentile ;
@@ -552,37 +555,46 @@ private void UpdateRankings(IEnumerable<Player> players)
552555
553556 //check for rank updates
554557
555- if ( pendingDebriefings . Any ( ) )
558+
559+ }
560+ catch ( Exception ex )
561+ {
562+ Trace . TraceError ( "WHR " + category + ": Failed to update rankings: " + ex ) ;
563+ return ;
564+ }
565+ try
566+ {
567+ if ( debriefings . Any ( ) )
556568 {
557569 List < int > playersWithRatingChange = new List < int > ( ) ;
558570 Dictionary < int , int > oldRanks = new Dictionary < int , int > ( ) ;
559571 Dictionary < int , Account > updatedRanks = new Dictionary < int , Account > ( ) ;
560572 Dictionary < int , Account > involvedAccounts = new Dictionary < int , Account > ( ) ;
561- Trace . TraceInformation ( "WHR Filling in Debriefings for Battles: " + pendingDebriefings . Keys . Select ( x => "B" + x ) . StringJoin ( ) ) ;
573+ Trace . TraceInformation ( "WHR Filling in Debriefings for Battles: " + debriefings . Keys . Select ( x => "B" + x ) . StringJoin ( ) ) ;
562574 using ( var db = new ZkDataContext ( ) )
563575 {
564- var battleIDs = pendingDebriefings . Keys . ToList ( ) ;
565- var lastBattlePlayers = db . SpringBattlePlayers . Where ( p => battleIDs . Contains ( p . SpringBattleID ) && ! p . IsSpectator ) . Include ( x => x . Account ) . ToList ( ) ;
566576 involvedAccounts = lastBattlePlayers . ToDictionary ( p => p . AccountID , p => p . Account ) ;
567577 Trace . TraceInformation ( "WHR Debriefing players: " + involvedAccounts . Values . Select ( x => x . Name ) . StringJoin ( ) ) ;
568578 oldRanks = lastBattlePlayers . ToDictionary ( p => p . AccountID , p => p . Account . Rank ) ;
569579 updatedRanks = lastBattlePlayers . Where ( p => Ranks . UpdateRank ( p . Account , p . IsInVictoryTeam , ! p . IsInVictoryTeam , db ) ) . Select ( x => x . Account ) . ToDictionary ( p => p . AccountID , p => p ) ;
570580 updatedRanks . Values . ForEach ( p => db . Entry ( p ) . State = EntityState . Modified ) ;
571581 playersWithRatingChange = lastBattlePlayers . Select ( x => x . AccountID ) . ToList ( ) ;
572582
573- lastBattlePlayers . Where ( p => playerOldRatings . ContainsKey ( ( p . AccountID ) ) && ! p . EloChange . HasValue ) . ForEach ( p =>
583+ lastBattlePlayers . Where ( p => /* playerOldRatings.ContainsKey((p.AccountID)) && */ ! p . EloChange . HasValue ) . ForEach ( p =>
574584 {
575585 //p.EloChange = playerRatings[(p.AccountID)].RealElo - playerOldRatings[(p.AccountID)].RealElo;
576586 p . EloChange = playerRatings [ ( p . AccountID ) ] . LadderElo - oldRatings [ ( p . AccountID ) ] ;
587+ db . Entry ( p ) . State = EntityState . Modified ;
577588 } ) ;
578589
579- db . SpringBattlePlayers . Where ( p => battleIDs . Contains ( p . SpringBattleID ) && ! p . IsSpectator ) . ToList ( ) . ForEach ( x => playerOldRatings [ ( x . AccountID ) ] = playerRatings [ ( x . AccountID ) ] ) ;
590+ lastBattlePlayers . ForEach ( x => playerOldRatings [ x . AccountID ] = playerRatings [ x . AccountID ] ) ;
580591 db . SaveChanges ( ) ;
581592 }
582593 //Publish new results only after saving new stats to db.
583- pendingDebriefings . ForEach ( pair =>
594+ debriefings . ForEach ( pair =>
584595 {
585- pair . Value . partialDebriefing . DebriefingUsers . Values . ForEach ( user => {
596+ pair . Value . partialDebriefing . DebriefingUsers . Values . ForEach ( user =>
597+ {
586598 try
587599 {
588600 user . EloChange = playerRatings [ ( user . AccountID ) ] . LadderElo - oldRatings [ ( user . AccountID ) ] ;
@@ -594,7 +606,7 @@ private void UpdateRankings(IEnumerable<Player> players)
594606 user . PrevRankElo = prog . RankFloorElo ;
595607 user . NewElo = prog . CurrentElo ;
596608 }
597- catch ( Exception ex )
609+ catch ( Exception ex )
598610 {
599611 Trace . TraceError ( "Unable to complete debriefing for user " + user . AccountID + ": " + ex ) ;
600612 }
@@ -603,9 +615,9 @@ private void UpdateRankings(IEnumerable<Player> players)
603615 pair . Value . debriefingConsumer . Invoke ( pair . Value . partialDebriefing ) ;
604616 } ) ;
605617 RatingsUpdated ( this , new RatingUpdate ( ) { affectedPlayers = playersWithRatingChange } ) ;
606- pendingDebriefings . Clear ( ) ;
618+ debriefings . ForEach ( x => pendingDebriefings . TryRemove ( x . Key , out _ ) ) ;
619+
607620 }
608-
609621
610622 //check for topX updates
611623 GetTopPlayers ( GlobalConst . LadderSize ) ;
@@ -620,12 +632,7 @@ private void UpdateRankings(IEnumerable<Player> players)
620632 }
621633 catch ( Exception ex )
622634 {
623- string dbg = "WHR " + category + ": Failed to update rankings " + ex + "\n Players: " ;
624- foreach ( var p in players )
625- {
626- dbg += p . id + " (" + p . days . Count + " days), " ;
627- }
628- Trace . TraceError ( dbg ) ;
635+ Trace . TraceError ( "WHR " + category + ": Failed to process battles for rankings: " + ex ) ;
629636 }
630637 }
631638
0 commit comments