-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
388 lines (344 loc) · 16.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using MongoDB.Driver;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Linq;
namespace CompetitiveRatingsUpdater
{
class Program
{
private static MongoClientSettings settings = new MongoClientSettings();
private const string MongoConnectionString = "databasestringconnect";
private const string DatabaseName = "haha";
private const string CollectionName = "xd";
private const string XmlFileName = "RankData.xml";
private static XmlDocument XmlFile = new XmlDocument();
private static MongoClient client = new MongoClient();
private static IMongoDatabase database;
private static IMongoCollection<BsonDocument> _playerCollection;
private static PhysicalFileProvider provider;
private static IChangeToken changeToken;
static void Main(string[] args)
{
provider = new PhysicalFileProvider(AppDomain.CurrentDomain.BaseDirectory)
{
UsePollingFileWatcher = true,
UseActivePolling = true
};
//Connect with mongo
MongoSetup();
if (File.Exists(Directory.GetCurrentDirectory() + $@"/{XmlFileName}"))
{
//Load the thing dumbass!!!!
XmlFile.Load(Directory.GetCurrentDirectory() + $@"/{XmlFileName}");
//Make sure the XML is synced with the database!
SyncXMLToDatabase();
//Attempt to add new players into the thing or update information as needed
InsertPlayers();
//Watch that file!!!! Watch it!!!
StartWatchingFile();
while (true)
{
System.Threading.Thread.Sleep(100);
}
}
else
{
Console.WriteLine($"{XmlFileName} does not exist!");
Console.WriteLine("Attempting to make a new one...");
XmlNode root = XmlFile.CreateElement("RankData");
XmlNode player = XmlFile.CreateElement("Player");
XmlFile.AppendChild(root);
root.AppendChild(player);
XmlFile.Save(Directory.GetCurrentDirectory() + $@"/{XmlFileName}");
Console.WriteLine("File created!");
Console.WriteLine("Attempting to add Player Rank Data to the XML from the database...");
ExtractPlayers();
Console.WriteLine();
//WATCH FILE NOW!!! NOW!!!!!!!!!
StartWatchingFile();
while (true)
{
System.Threading.Thread.Sleep(100);
}
}
}
private static void MongoSetup()
{
Console.WriteLine("Attempting to connect to MongoDB");
settings = MongoClientSettings.FromConnectionString(MongoConnectionString);
client = new MongoClient(settings);
database = client.GetDatabase(DatabaseName);
_playerCollection = database.GetCollection<BsonDocument>(CollectionName);
Console.WriteLine("Connection established!");
}
public static void InsertPlayers()
{
Console.WriteLine("Generating Player Data....");
List<PlayerObject> playerDocuments = GenerateDocuments();
Console.WriteLine();
if (playerDocuments.Count == 0)
{
Console.WriteLine("There's no player data in here lmao");
//This should only occur when the RankData.xml is created by the Server rather than the database handler
ExtractPlayers();
return;
}
List<BsonDocument> bsonDocuments = new List<BsonDocument>();
Console.WriteLine("Comparing Player Data with Mongo Database");
foreach (PlayerObject player in playerDocuments)
{
FilterDefinitionBuilder<BsonDocument> builder = Builders<BsonDocument>.Filter;
FilterDefinition<BsonDocument> filter = builder.And(builder.Eq("name", player.username), builder.Eq("id", player.id));
List<BsonDocument> result = _playerCollection.Find(filter).ToList();
if (result.Count >= 2)
{
Console.WriteLine($"ALERT: Multiple {player.username} #{player.id} exist in in the database!");
Console.WriteLine("This should never happen! Please check the database because that's really weird!!");
}
else if (result.Count == 1)
{
if (result[0].GetValue("rank").ToString() != player.rank)
{
Console.WriteLine($"Updating Player Information for {player.username} #{player.id}...");
UpdateDefinition<BsonDocument> update = Builders<BsonDocument>.Update.Set("rank", player.rank).Set("rankDeviation", player.rankDeviation).Set("volatility", player.volatility);
_playerCollection.UpdateOne(filter, update);
Console.WriteLine("Updated!");
}
else
{
Console.WriteLine($"{player.username} #{player.id} already exists in the database");
}
}
else
{
Console.WriteLine($"Player {player.username} #{player.id} will be inserted into the database...");
bsonDocuments.Add(new BsonDocument
{
{ "name", player.username },
{ "id", player.id },
{ "rank", player.rank },
{ "rankDeviation", player.rankDeviation },
{ "volatility", player.volatility },
});
}
}
if (bsonDocuments.Count > 0)
{
Console.WriteLine("Attempting to Insert new players into the database...");
_playerCollection.InsertMany(bsonDocuments);
Console.WriteLine("Completed Insert Task!");
}
else
Console.WriteLine("There are no new players to add to the database!");
}
private static void ExtractPlayers()
{
Console.WriteLine("Retrieving Player Data...");
List<BsonDocument> bsonDocuments = _playerCollection.Find(FilterDefinition<BsonDocument>.Empty).ToList();
if (bsonDocuments.Count > 0)
{
foreach (BsonDocument bson in bsonDocuments)
{
XmlNode root = XmlFile.SelectSingleNode("RankData");
XmlNode playerNode = XmlFile.CreateElement("Player");
XmlAttribute name = XmlFile.CreateAttribute("name");
name.Value = bson.GetValue("name").ToString();
XmlAttribute id = XmlFile.CreateAttribute("id");
id.Value = bson.GetValue("id").ToString();
XmlElement rank = XmlFile.CreateElement("Rank");
XmlElement rankDeviation = XmlFile.CreateElement("RankDeviation");
XmlElement volatility = XmlFile.CreateElement("Volatility");
root.AppendChild(playerNode);
playerNode.Attributes.Append(name);
playerNode.Attributes.Append(id);
playerNode.AppendChild(rank);
rank.InnerText = bson.GetValue("rank").ToString();
playerNode.AppendChild(rankDeviation);
rankDeviation.InnerText = bson.GetValue("rankDeviation").ToString();
playerNode.AppendChild(volatility);
volatility.InnerText = bson.GetValue("volatility").ToString();
XmlFile.Save(Directory.GetCurrentDirectory() + $@"/{XmlFileName}");
ClearConsoleLine();
Console.Write($"Player {bson.GetValue("name")} loaded... ");
}
ClearConsoleLine();
Console.WriteLine("All players from database loaded!");
}
else
{
Console.WriteLine("Oh there's nothing in the database.");
Console.WriteLine("lmao");
}
}
private static void SyncXMLToDatabase()
{
Console.WriteLine("Syncing player data...");
List<BsonDocument> bsonDocuments = _playerCollection.Find(FilterDefinition<BsonDocument>.Empty).ToList();
if(bsonDocuments.Count > 0)
{
foreach(BsonDocument bson in bsonDocuments)
{
XmlNode nodeToCheck = XmlFile.SelectSingleNode("RankData/Player[@name='" + bson.GetValue("name") + "'][@id='" + bson.GetValue("id") + "']/Rank");
if(nodeToCheck != null)
{
if (nodeToCheck.InnerText != bson.GetValue("rank").ToString())
{
nodeToCheck.InnerText = bson.GetValue("rank").ToString();
nodeToCheck = XmlFile.SelectSingleNode("RankData/Player[@name='" + bson.GetValue("name") + "'][@id='" + bson.GetValue("id") + "']/RankDeviation");
nodeToCheck.InnerText = bson.GetValue("rankDeviation").ToString();
nodeToCheck = XmlFile.SelectSingleNode("RankData/Player[@name='" + bson.GetValue("name") + "'][@id='" + bson.GetValue("id") + "']/Volatility");
nodeToCheck.InnerText = bson.GetValue("volatility").ToString();
XmlFile.Save(Directory.GetCurrentDirectory() + $@"/{XmlFileName}");
ClearConsoleLine();
Console.WriteLine($"Player {bson.GetValue("name")} synced...");
}
else
{
ClearConsoleLine();
Console.Write($"Player {bson.GetValue("name")} verified...");
}
}
else
{
XmlNode root = XmlFile.SelectSingleNode("RankData");
XmlNode playerNode = XmlFile.CreateElement("Player");
XmlAttribute name = XmlFile.CreateAttribute("name");
name.Value = bson.GetValue("name").ToString();
XmlAttribute id = XmlFile.CreateAttribute("id");
id.Value = bson.GetValue("id").ToString();
XmlElement rank = XmlFile.CreateElement("Rank");
XmlElement rankDeviation = XmlFile.CreateElement("RankDeviation");
XmlElement volatility = XmlFile.CreateElement("Volatility");
root.AppendChild(playerNode);
playerNode.Attributes.Append(name);
playerNode.Attributes.Append(id);
playerNode.AppendChild(rank);
rank.InnerText = bson.GetValue("rank").ToString();
playerNode.AppendChild(rankDeviation);
rankDeviation.InnerText = bson.GetValue("rankDeviation").ToString();
playerNode.AppendChild(volatility);
volatility.InnerText = bson.GetValue("volatility").ToString();
XmlFile.Save(Directory.GetCurrentDirectory() + $@"/{XmlFileName}");
ClearConsoleLine();
Console.WriteLine($"Player {bson.GetValue("name")} added to XML...");
}
}
ClearConsoleLine();
Console.WriteLine("Database synced!");
}
else
{
Console.WriteLine("There's nothing to sync!");
Console.WriteLine("How????");
}
}
private static void StartWatchingFile()
{
/*FileSystemWatcher watcher = new FileSystemWatcher(AppDomain.CurrentDomain.BaseDirectory);
watcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.LastAccess
| NotifyFilters.FileName;
watcher.Filter = XmlFileName;
watcher.Changed += OnChanged;
watcher.Error += OnError;
watcher.EnableRaisingEvents = true;*/
changeToken = provider.Watch(XmlFileName);
changeToken.RegisterChangeCallback(Notify, default);
}
private static void Notify(object state)
{
if(changeToken.HasChanged)
{
Console.WriteLine();
Console.WriteLine("File changed!");
//If the file changed it must have something different in it! Run the function!
InsertPlayers();
}
StartWatchingFile();
}
// DIS ONLY WORKS ON WINDOWS LMAOO!!!
/*private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Console.WriteLine();
Console.WriteLine("File changed!");
//If the file changed it must have something different in it! Run the function!
InsertPlayers();
}
private static void OnError(object sender, ErrorEventArgs e)
{
PrintException(e.GetException());
}
private static void PrintException(Exception ex)
{
if (ex != null)
{
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine("Stacktrace");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}*/
private static void ClearConsoleLine()
{
//Console.SetCursorPosition(0, Console.CursorTop - 1);
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
private static List<PlayerObject> GenerateDocuments()
{
//Generate documents based on XML document
List<PlayerObject> playerDatas = new List<PlayerObject>();
Console.WriteLine("Attempting to read XML...");
//Read XML doc here, this will be used to add to the playerDatas List
using (StreamReader reader = new StreamReader(XmlFileName))
{
string body = reader.ReadToEnd();
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(body);
XmlNodeList nodes = XmlDoc.SelectNodes("//Player");
Console.WriteLine("XML read! Attempting to load players...");
foreach (XmlNode node in nodes)
{
if (node.Attributes.Count > 1)
{
PlayerObject newPlayer = new PlayerObject()
{
username = node.Attributes[0].Value,
id = node.Attributes[1].Value,
rank = node.FirstChild.InnerText,
rankDeviation = node.ChildNodes[1].InnerText,
volatility = node.ChildNodes[2].InnerText,
};
ClearConsoleLine();
Console.Write($"Player {node.Attributes[0].Value} #{node.Attributes[1].Value} loaded...");
playerDatas.Add(newPlayer);
}
}
}
ClearConsoleLine();
Console.WriteLine("All players loaded!");
Console.WriteLine("Generation complete!");
return playerDatas;
}
}
public class PlayerObject
{
public string username { get; set; }
public string id { get; set; }
public string rank { get; set; }
public string rankDeviation { get; set; }
public string volatility { get; set; }
}
}