10
10
using SimpleSockets ;
11
11
using SimpleSockets . Messaging . Metadata ;
12
12
using SimpleSockets . Server ;
13
+ using Newtonsoft . Json . Linq ;
13
14
14
15
namespace PhexensWuerfelraum . Server . Console
15
16
{
@@ -21,7 +22,9 @@ internal class Program
21
22
private static SimpleSocketListener _listener ;
22
23
23
24
private static readonly List < HeartbeatPacket > heartbeats = new ( ) ;
24
- private static Dictionary < int , int > d20statistic = new ( ) ;
25
+ private static Dictionary < int , int > d20statistic ;
26
+ private static JObject d20statisticJson ;
27
+ private static string d20statisticPath ;
25
28
26
29
#region version
27
30
@@ -39,12 +42,32 @@ private static void Main(string[] args)
39
42
WriteLine ( $ "Starting the server { Version } ") ;
40
43
41
44
// make sure the config folder exists
42
- Directory . CreateDirectory ( Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "config" ) ) ;
45
+ string configFolder = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "config" ) ;
46
+ Directory . CreateDirectory ( configFolder ) ;
43
47
44
48
var privKeyFileName = "PrivateKey.pfx" ;
45
- var privateKeyPath = Path . Combine ( Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "config" , privKeyFileName ) ) ;
49
+ var privateKeyPath = Path . Combine ( Path . Combine ( configFolder , privKeyFileName ) ) ;
46
50
var publicKeyFileName = "PublicKey.pem" ;
47
- var publicKeyPath = Path . Combine ( Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "config" , publicKeyFileName ) ) ;
51
+ var publicKeyPath = Path . Combine ( Path . Combine ( configFolder , publicKeyFileName ) ) ;
52
+
53
+ d20statisticPath = Path . Combine ( configFolder , "d20statistic.json" ) ;
54
+
55
+ if ( File . Exists ( d20statisticPath ) )
56
+ {
57
+ d20statisticJson = JObject . Parse ( File . ReadAllText ( d20statisticPath ) ) ;
58
+ d20statistic = d20statisticJson . ToObject < Dictionary < int , int > > ( ) ;
59
+ }
60
+ else
61
+ {
62
+ d20statistic = new ( ) ;
63
+
64
+ for ( int i = 1 ; i <= 20 ; i ++ )
65
+ {
66
+ d20statistic [ i ] = 0 ;
67
+ }
68
+
69
+ WriteStatisticToDisk ( ) ;
70
+ }
48
71
49
72
if ( File . Exists ( privateKeyPath ) )
50
73
{
@@ -78,17 +101,18 @@ private static void Main(string[] args)
78
101
BindEvents ( ) ;
79
102
_listener . StartListening ( Port ) ;
80
103
81
- for ( int i = 1 ; i <= 20 ; i ++ )
82
- {
83
- d20statistic [ i ] = 0 ;
84
- }
85
-
86
104
while ( true )
87
105
{
88
106
System . Console . Read ( ) ;
89
107
}
90
108
}
91
109
110
+ private static void WriteStatisticToDisk ( )
111
+ {
112
+ d20statisticJson = JObject . FromObject ( d20statistic ) ;
113
+ File . WriteAllText ( d20statisticPath , d20statisticJson . ToString ( ) ) ;
114
+ }
115
+
92
116
private static int ShowClients ( )
93
117
{
94
118
var ids = new List < int > ( ) ;
@@ -206,6 +230,8 @@ private static void ListenerOnObjectReceived(IClientInfo client, object obj, Typ
206
230
WriteLine ( $ "{ i } : { d20statistic [ i ] } ({ Math . Round ( ( double ) d20statistic [ i ] / ( double ) sumRolls * 100 , 2 ) } %)") ;
207
231
}
208
232
WriteLine ( $ "sum: { sumRolls } d20 rolls") ;
233
+
234
+ WriteStatisticToDisk ( ) ;
209
235
}
210
236
}
211
237
0 commit comments