Skip to content

Commit 1e66a63

Browse files
- added d20 statistic permanence
1 parent 368f4a0 commit 1e66a63

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

Server/Server.Console/Program.cs

+35-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using SimpleSockets;
1111
using SimpleSockets.Messaging.Metadata;
1212
using SimpleSockets.Server;
13+
using Newtonsoft.Json.Linq;
1314

1415
namespace PhexensWuerfelraum.Server.Console
1516
{
@@ -21,7 +22,9 @@ internal class Program
2122
private static SimpleSocketListener _listener;
2223

2324
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;
2528

2629
#region version
2730

@@ -39,12 +42,32 @@ private static void Main(string[] args)
3942
WriteLine($"Starting the server {Version}");
4043

4144
// 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);
4347

4448
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));
4650
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+
}
4871

4972
if (File.Exists(privateKeyPath))
5073
{
@@ -78,17 +101,18 @@ private static void Main(string[] args)
78101
BindEvents();
79102
_listener.StartListening(Port);
80103

81-
for (int i = 1; i <= 20; i++)
82-
{
83-
d20statistic[i] = 0;
84-
}
85-
86104
while (true)
87105
{
88106
System.Console.Read();
89107
}
90108
}
91109

110+
private static void WriteStatisticToDisk()
111+
{
112+
d20statisticJson = JObject.FromObject(d20statistic);
113+
File.WriteAllText(d20statisticPath, d20statisticJson.ToString());
114+
}
115+
92116
private static int ShowClients()
93117
{
94118
var ids = new List<int>();
@@ -206,6 +230,8 @@ private static void ListenerOnObjectReceived(IClientInfo client, object obj, Typ
206230
WriteLine($"{i}: {d20statistic[i]} ({Math.Round((double)d20statistic[i] / (double)sumRolls * 100, 2)} %)");
207231
}
208232
WriteLine($"sum: {sumRolls} d20 rolls");
233+
234+
WriteStatisticToDisk();
209235
}
210236
}
211237

0 commit comments

Comments
 (0)