1313
1414namespace ZeroKWeb
1515{
16- public class LadderCalculator : ITopPlayerProvider
16+ public class AwardCalculator
1717 {
1818 private const int LadderRefreshMinutes = 30 ;
1919
20- private LadderModel ladderModel = new LadderModel ( ) ;
20+ private AwardModel awardModel = new AwardModel ( ) ;
2121
2222 private Timer timer ;
2323
24- public LadderCalculator ( )
24+ public AwardCalculator ( )
2525 {
26- timer = new Timer ( ( t ) => { ladderModel = ComputeLadder ( ) ; } , this , LadderRefreshMinutes * 60 * 1000 , LadderRefreshMinutes * 60 * 1000 ) ;
26+ timer = new Timer ( ( t ) => { awardModel = ComputeLadder ( ) ; } , this , LadderRefreshMinutes * 60 * 1000 , LadderRefreshMinutes * 60 * 1000 ) ;
2727 }
2828
2929 public void RecomputeNow ( )
3030 {
31- ladderModel = ComputeLadder ( ) ;
32- TopPlayersUpdated ? . Invoke ( this , this ) ;
31+ awardModel = ComputeLadder ( ) ;
3332 }
3433
35- public LadderModel GetLadder ( )
34+ public AwardModel GetAwards ( )
3635 {
37- return ladderModel ;
36+ return awardModel ;
3837 }
39-
40- public List < Account > GetTop ( ) => ladderModel ? . TopAccounts ;
41- public List < Account > GetTopCasual ( ) => ladderModel ? . TopCasual ;
42- public event EventHandler < ITopPlayerProvider > TopPlayersUpdated ;
38+
4339
4440
4541 private static List < AwardItem > CalculateAwards ( ZkDataContext db )
@@ -108,138 +104,23 @@ private static List<AwardItem> CalculateAwards(ZkDataContext db)
108104 }
109105
110106 object computeLadderLock = new object ( ) ;
111- private LadderModel ComputeLadder ( )
107+ private AwardModel ComputeLadder ( )
112108 {
113109 lock ( computeLadderLock )
114110 {
115111 try
116112 {
117- if ( ! RatingSystems . DisableRatingSystems )
118- {
119- RatingSystems . BackupToDB ( ) ;
120- }
121113
122114 var db = new ZkDataContext ( ) ;
123115 db . Database . CommandTimeout = 600 ;
124116
125117 var awardItems = CalculateAwards ( db ) ;
126-
127- var ladderTimeout = DateTime . UtcNow . AddDays ( - GlobalConst . LadderActivityDays ) ;
128-
129- // set unused accounts weight to 1
130- db . Accounts . Where ( x => ! x . SpringBattlePlayers . Any (
131- y => ( y . SpringBattle . StartTime > ladderTimeout ) && ! y . SpringBattle . IsMatchMaker && ! y . IsSpectator ) )
132- . Update ( acc => new Account ( ) { EloWeight = 1 } ) ;
133-
134- db . Accounts . Where ( x => ! x . SpringBattlePlayers . Any ( y => ( y . SpringBattle . StartTime > ladderTimeout ) && y . SpringBattle . IsMatchMaker && ! y . IsSpectator ) ) . Update ( acc => new Account ( ) { EloMmWeight = 1 } ) ;
135-
136- db . SaveChanges ( ) ;
137-
138-
139- foreach (
140- var entry in
141- db . Accounts . Where ( x => x . EloWeight > 1 )
142- . Select (
143- acc =>
144- new
145- {
146- Account = acc ,
147- LastGame =
148- acc . SpringBattlePlayers . Where ( x => ! x . IsSpectator && ! x . SpringBattle . IsMatchMaker )
149- . OrderByDescending ( x => x . SpringBattleID )
150- . Select ( x => x . SpringBattle . StartTime ) . FirstOrDefault ( )
151- } ) )
152- {
153- var days = DateTime . UtcNow . Subtract ( entry . LastGame ) . TotalDays ;
154- var decayRatio = ( ( days - 7 ) / ( GlobalConst . LadderActivityDays - 7 ) ) . Clamp ( 0 , 1 ) ;
155- entry . Account . EloWeight = Math . Min ( entry . Account . EloWeight ,
156- Math . Max ( 1 , GlobalConst . EloWeightMax - ( GlobalConst . EloWeightMax - 1 ) * decayRatio ) ) ;
157- }
158-
159- db . SaveChanges ( ) ;
160-
161-
162- foreach (
163- var entry in
164- db . Accounts . Where ( x => x . EloMmWeight > 1 )
165- . Select (
166- acc =>
167- new
168- {
169- Account = acc ,
170- LastGame =
171- acc . SpringBattlePlayers . Where ( x => ! x . IsSpectator && x . SpringBattle . IsMatchMaker )
172- . OrderByDescending ( x => x . SpringBattleID )
173- . Select ( x => x . SpringBattle . StartTime ) . FirstOrDefault ( )
174- } ) )
175- {
176- var days = DateTime . UtcNow . Subtract ( entry . LastGame ) . TotalDays ;
177- var decayRatio = ( ( days - 7 ) / ( GlobalConst . LadderActivityDays - 7 ) ) . Clamp ( 0 , 1 ) ;
178- entry . Account . EloMmWeight = Math . Min ( entry . Account . EloMmWeight ,
179- Math . Max ( 1 , GlobalConst . EloWeightMax - ( GlobalConst . EloWeightMax - 1 ) * decayRatio ) ) ;
180- }
181- db . SaveChanges ( ) ;
182-
183-
184- // recalc competitive ranking
185- var cnt = 0 ;
186- foreach ( var a in
187- db . Accounts . Where (
188- x =>
189- x . SpringBattlePlayers . Any (
190- y => ( y . SpringBattle . StartTime > ladderTimeout ) && y . SpringBattle . IsMatchMaker && ! y . IsSpectator ) )
191- . OrderByDescending ( x => x . EffectiveMmElo )
192- . WithTranslations ( ) )
193- {
194- cnt ++ ;
195- a . CompetitiveRank = cnt ;
196- }
197- db . SaveChanges ( ) ;
198-
199- cnt = 0 ;
200- foreach ( var a in
201- db . Accounts . Where (
202- x =>
203- x . SpringBattlePlayers . Any (
204- y => ( y . SpringBattle . StartTime > ladderTimeout ) && ! y . SpringBattle . IsMatchMaker && ! y . IsSpectator ) )
205- . OrderByDescending ( x => x . EffectiveElo )
206- . WithTranslations ( ) )
207- {
208- cnt ++ ;
209- a . CasualRank = cnt ;
210- }
211- db . SaveChanges ( ) ;
212-
213- var topAccounts =
214- db . Accounts . Where (
215- x =>
216- x . SpringBattlePlayers . Any (
217- y => ( y . SpringBattle . StartTime > ladderTimeout ) && y . SpringBattle . IsMatchMaker && ! y . IsSpectator ) )
218- . Include ( x => x . Clan )
219- . Include ( x => x . Faction )
220- . OrderByDescending ( x => x . EffectiveMmElo )
221- . WithTranslations ( )
222- . Take ( GlobalConst . LadderSize )
223- . ToList ( ) ;
224-
225- var topCasual =
226- db . Accounts . Where (
227- x =>
228- x . SpringBattlePlayers . Any (
229- y => ( y . SpringBattle . StartTime > ladderTimeout ) && ! y . SpringBattle . IsMatchMaker && ! y . IsSpectator ) )
230- . Include ( x => x . Clan )
231- . Include ( x => x . Faction )
232- . OrderByDescending ( x => x . EffectiveElo )
233- . WithTranslations ( )
234- . Take ( GlobalConst . LadderSize )
235- . ToList ( ) ;
236-
237- return new LadderModel { AwardItems = awardItems , TopAccounts = topAccounts , TopCasual = topCasual } ;
118+ return new AwardModel { AwardItems = awardItems } ;
238119 }
239120 catch ( Exception ex )
240121 {
241122 Trace . TraceError ( "Error computing ladder: {0}" , ex ) ;
242- return new LadderModel ( ) ;
123+ return new AwardModel ( ) ;
243124 }
244125 }
245126 }
@@ -256,11 +137,9 @@ public class AwardItem
256137 public Account TopScoreHolderM ;
257138 }
258139
259- public class LadderModel
140+ public class AwardModel
260141 {
261142 public List < AwardItem > AwardItems = new List < AwardItem > ( ) ;
262- public List < Account > TopAccounts = new List < Account > ( ) ;
263- public List < Account > TopCasual = new List < Account > ( ) ;
264143 }
265144
266145 }
0 commit comments