55using System . Threading . Tasks ;
66using ZkData ;
77using System . Data . Entity ;
8+ using System . Threading ;
89
910namespace Ratings
1011{
1112 public class RatingSystems
1213 {
13- public static Dictionary < RatingCategory , WholeHistoryRating > whr = new Dictionary < RatingCategory , WholeHistoryRating > ( ) ;
14-
1514 public static readonly IEnumerable < RatingCategory > ratingCategories = Enum . GetValues ( typeof ( RatingCategory ) ) . Cast < RatingCategory > ( ) ;
1615
17- private static HashSet < int > processedBattles = new HashSet < int > ( ) ;
18-
19- public static bool Initialized { get ; private set ; }
16+ private static RatingSystems handler ;
2017
21- private static object processingLock = new object ( ) ;
2218
2319 public static void Init ( )
2420 {
25- Initialized = false ;
26- ratingCategories . ForEach ( category => whr [ category ] = new WholeHistoryRating ( category ) ) ;
21+ handler = new RatingSystems ( false ) ;
22+ }
2723
24+ public static void ReinitializeRatingSystems ( )
25+ {
26+ Trace . TraceInformation ( "Reinitializing rating systems..." ) ;
2827 Task . Factory . StartNew ( ( ) => {
28+ handler = new RatingSystems ( true ) ;
29+ Trace . TraceInformation ( "Ratings have been recalculated!" ) ;
30+ } ) ;
31+ }
32+
33+ public static IEnumerable < IRatingSystem > GetRatingSystems ( )
34+ {
35+ return handler . whr . Values ;
36+ }
37+
38+ public static IRatingSystem GetRatingSystem ( RatingCategory category )
39+ {
40+ if ( handler == null ) return null ;
41+ return handler . GetRatingSystemInternal ( category ) ;
42+ }
43+
44+
45+ public static void ProcessResult ( SpringBattle battle )
46+ {
47+ if ( ! handler . Initialized ) return ;
48+ handler . ProcessBattle ( battle ) ;
49+ }
50+
51+ public static Tuple < int , int > GetPlanetwarsFactionStats ( int factionID )
52+ {
53+ return handler . GetPlanetwarsFactionStatsInternal ( factionID ) ;
54+ }
55+
56+ public static int ConvertDateToDays ( DateTime date )
57+ {
58+ return ( int ) ( date . ToUniversalTime ( ) . Subtract ( new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ) . TotalDays / 1 ) ;
59+ }
60+ public static DateTime ConvertDaysToDate ( int days )
61+ {
62+ return new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) . AddDays ( days ) ;
63+ }
64+
65+
66+ private Dictionary < RatingCategory , WholeHistoryRating > whr = new Dictionary < RatingCategory , WholeHistoryRating > ( ) ;
67+
68+ private HashSet < int > processedBattles = new HashSet < int > ( ) ;
69+
70+ private object processingLock = new object ( ) ;
71+
72+ public bool Initialized { get ; private set ; } = false ;
73+
74+ private RatingSystems ( bool waitForCompleteInitialization )
75+ {
76+ ratingCategories . ForEach ( category => whr [ category ] = new WholeHistoryRating ( category , this ) ) ;
77+
78+ Action initBattles = ( ) =>
79+ {
2980 lock ( processingLock )
3081 {
3182 try
@@ -48,24 +99,30 @@ public static void Init()
4899 }
49100 }
50101 Initialized = true ;
51- whr . Values . ForEach ( w => w . UpdateRatings ( ) ) ;
102+ if ( waitForCompleteInitialization )
103+ {
104+ SemaphoreSlim completedUpdates = new SemaphoreSlim ( 0 ) ;
105+ whr . Values . ForEach ( w => w . UpdateRatings ( ( ) => completedUpdates . Release ( ) ) ) ;
106+ whr . Values . ForEach ( w => completedUpdates . Wait ( ) ) ;
107+ }
108+ else
109+ {
110+ whr . Values . ForEach ( w => w . UpdateRatings ( ) ) ;
111+ }
52112 }
53113 }
54114 catch ( Exception ex )
55115 {
56116 Trace . TraceError ( "WHR: Error reading battles from DB" + ex ) ;
57117 }
58118 }
59- } ) ;
60- }
61-
62-
63- public static IEnumerable < IRatingSystem > GetRatingSystems ( )
64- {
65- return whr . Values ;
119+ } ;
120+ if ( waitForCompleteInitialization ) initBattles . Invoke ( ) ;
121+ else Task . Factory . StartNew ( initBattles ) ;
66122 }
123+
67124
68- public static IRatingSystem GetRatingSystem ( RatingCategory category )
125+ private IRatingSystem GetRatingSystemInternal ( RatingCategory category )
69126 {
70127 if ( ! whr . ContainsKey ( category ) )
71128 {
@@ -75,15 +132,9 @@ public static IRatingSystem GetRatingSystem(RatingCategory category)
75132 return whr [ category ] ;
76133 }
77134
78- private static int latestBattle ;
135+ private int latestBattle ;
79136
80- public static void ProcessResult ( SpringBattle battle )
81- {
82- if ( ! Initialized ) return ;
83- ProcessBattle ( battle ) ;
84- }
85-
86- private static void ProcessBattle ( SpringBattle battle )
137+ private void ProcessBattle ( SpringBattle battle )
87138 {
88139 lock ( processingLock )
89140 {
@@ -103,9 +154,9 @@ private static void ProcessBattle(SpringBattle battle)
103154 }
104155 }
105156
106- private static Dictionary < int , Tuple < int , int , int > > factionCache = new Dictionary < int , Tuple < int , int , int > > ( ) ;
157+ private Dictionary < int , Tuple < int , int , int > > factionCache = new Dictionary < int , Tuple < int , int , int > > ( ) ;
107158
108- public static Tuple < int , int > GetPlanetwarsFactionStats ( int factionID )
159+ private Tuple < int , int > GetPlanetwarsFactionStatsInternal ( int factionID )
109160 {
110161 try
111162 {
@@ -131,23 +182,15 @@ public static Tuple<int, int> GetPlanetwarsFactionStats(int factionID)
131182 count = factionCache [ factionID ] . Item2 ;
132183 skill = factionCache [ factionID ] . Item3 ;
133184 return new Tuple < int , int > ( count , skill ) ;
134- } catch ( Exception ex )
185+ }
186+ catch ( Exception ex )
135187 {
136188 Trace . TraceError ( "WHR failed to calculate faction stats " + ex ) ;
137189 return new Tuple < int , int > ( - 1 , - 1 ) ;
138190 }
139191 }
140192
141- public static int ConvertDateToDays ( DateTime date )
142- {
143- return ( int ) ( date . ToUniversalTime ( ) . Subtract ( new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ) . TotalDays / 1 ) ;
144- }
145- public static DateTime ConvertDaysToDate ( int days )
146- {
147- return new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) . AddDays ( days ) ;
148- }
149-
150- private static bool IsCategory ( SpringBattle battle , RatingCategory category )
193+ private bool IsCategory ( SpringBattle battle , RatingCategory category )
151194 {
152195 int battleID = - 1 ;
153196 try
0 commit comments