@@ -107,13 +107,9 @@ public void ProcessBattle(SpringBattle battle)
107107
108108 public List < Account > GetTopPlayers ( int count )
109109 {
110- return GetTopPlayers ( count , x => true ) ;
111- //TODO use db query once accountrating is implemented
112- /*
113110 ZkDataContext db = new ZkDataContext ( ) ;
114111 List < int > retIDs = topPlayers . Take ( count ) . ToList ( ) ;
115- return db.Accounts.Where(a => retIDs.Contains(a.AccountID)).ToList();
116- */
112+ return db . Accounts . Where ( a => retIDs . Contains ( a . AccountID ) ) . OrderByDescending ( x => x . AccountRatings . Where ( r => r . RatingCategory == category ) . Select ( r => r . Elo ) . DefaultIfEmpty ( - 1 ) . FirstOrDefault ( ) ) . ToList ( ) ;
117113 }
118114
119115 public List < Account > GetTopPlayers ( int count , Func < Account , bool > selector )
@@ -196,6 +192,7 @@ public void UpdateRatings()
196192 players . ForEach ( p => p . runOneNewtonIteration ( ) ) ;
197193 players . ForEach ( p => p . updateUncertainty ( ) ) ;
198194 UpdateRankings ( players ) ;
195+ SaveToDB ( players . Select ( x => x . id ) ) ;
199196 } ) ;
200197 }
201198 else
@@ -226,6 +223,30 @@ public void UpdateRatings()
226223
227224 }
228225
226+ public void SaveToDB ( IEnumerable < int > players )
227+ {
228+ lock ( dbLock )
229+ {
230+
231+ var db = new ZkDataContext ( ) ;
232+ foreach ( int player in players )
233+ {
234+ var accountRating = db . AccountRatings . Where ( x => x . RatingCategory == category && x . AccountID == player ) . FirstOrDefault ( ) ;
235+ if ( accountRating == null )
236+ {
237+ accountRating = new AccountRating ( player , category ) ;
238+ accountRating . UpdateFromRatingSystem ( playerRatings [ player ] ) ;
239+ db . AccountRatings . InsertOnSubmit ( accountRating ) ;
240+ }
241+ else
242+ {
243+ accountRating . UpdateFromRatingSystem ( playerRatings [ player ] ) ;
244+ }
245+ }
246+ db . SaveChanges ( ) ;
247+ }
248+ }
249+
229250 public void SaveToDB ( )
230251 {
231252 lock ( dbLock )
@@ -244,7 +265,7 @@ public void SaveToDB()
244265 continue ;
245266 }
246267 processedPlayers . Add ( accountRating . AccountID ) ;
247- if ( Math . Abs ( playerRatings [ accountRating . AccountID ] . Elo - accountRating . Elo ) > 10 )
268+ if ( Math . Abs ( playerRatings [ accountRating . AccountID ] . Elo - accountRating . Elo ) > 1 )
248269 {
249270 accountRating . UpdateFromRatingSystem ( playerRatings [ accountRating . AccountID ] ) ;
250271 }
0 commit comments