- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
388 lines (344 loc) · 16.4 KB
/
Copy pathProgram.cs
File metadata and controls
388 lines (344 loc) · 16.4 KB
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
usingMicrosoft.Extensions.FileProviders;
usingMicrosoft.Extensions.Primitives;
usingMongoDB.Driver;
usingMongoDB.Bson;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Xml;
usingSystem.Linq;
namespaceCompetitiveRatingsUpdater
{
classProgram
{
privatestaticMongoClientSettingssettings=newMongoClientSettings();
privateconststringMongoConnectionString="databasestringconnect";
privateconststringDatabaseName="haha";
privateconststringCollectionName="xd";
privateconststringXmlFileName="RankData.xml";
privatestaticXmlDocumentXmlFile=newXmlDocument();
privatestaticMongoClientclient=newMongoClient();
privatestaticIMongoDatabasedatabase;
privatestaticIMongoCollection<BsonDocument>_playerCollection;
privatestaticPhysicalFileProviderprovider;
privatestaticIChangeTokenchangeToken;
staticvoidMain(string[]args)
{
provider=newPhysicalFileProvider(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...");
XmlNoderoot=XmlFile.CreateElement("RankData");
XmlNodeplayer=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);
}
}
}
privatestaticvoidMongoSetup()
{
Console.WriteLine("Attempting to connect to MongoDB");
settings=MongoClientSettings.FromConnectionString(MongoConnectionString);
client=newMongoClient(settings);
database=client.GetDatabase(DatabaseName);
_playerCollection=database.GetCollection<BsonDocument>(CollectionName);
Console.WriteLine("Connection established!");
}
publicstaticvoidInsertPlayers()
{
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=newList<BsonDocument>();
Console.WriteLine("Comparing Player Data with Mongo Database");
foreach(PlayerObjectplayerinplayerDocuments)
{
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!!");
}
elseif(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(newBsonDocument
{
{"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!");
}
privatestaticvoidExtractPlayers()
{
Console.WriteLine("Retrieving Player Data...");
List<BsonDocument>bsonDocuments=_playerCollection.Find(FilterDefinition<BsonDocument>.Empty).ToList();
if(bsonDocuments.Count>0)
{
foreach(BsonDocumentbsoninbsonDocuments)
{
XmlNoderoot=XmlFile.SelectSingleNode("RankData");
XmlNodeplayerNode=XmlFile.CreateElement("Player");
XmlAttributename=XmlFile.CreateAttribute("name");
name.Value=bson.GetValue("name").ToString();
XmlAttributeid=XmlFile.CreateAttribute("id");
id.Value=bson.GetValue("id").ToString();
XmlElementrank=XmlFile.CreateElement("Rank");
XmlElementrankDeviation=XmlFile.CreateElement("RankDeviation");
XmlElementvolatility=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");
}
}
privatestaticvoidSyncXMLToDatabase()
{
Console.WriteLine("Syncing player data...");
List<BsonDocument>bsonDocuments=_playerCollection.Find(FilterDefinition<BsonDocument>.Empty).ToList();
if(bsonDocuments.Count>0)
{
foreach(BsonDocumentbsoninbsonDocuments)
{
XmlNodenodeToCheck=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
{
XmlNoderoot=XmlFile.SelectSingleNode("RankData");
XmlNodeplayerNode=XmlFile.CreateElement("Player");
XmlAttributename=XmlFile.CreateAttribute("name");
name.Value=bson.GetValue("name").ToString();
XmlAttributeid=XmlFile.CreateAttribute("id");
id.Value=bson.GetValue("id").ToString();
XmlElementrank=XmlFile.CreateElement("Rank");
XmlElementrankDeviation=XmlFile.CreateElement("RankDeviation");
XmlElementvolatility=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????");
}
}
privatestaticvoidStartWatchingFile()
{
/*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);
}
privatestaticvoidNotify(objectstate)
{
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);
}
}*/
privatestaticvoidClearConsoleLine()
{
//Console.SetCursorPosition(0, Console.CursorTop - 1);
intcurrentLineCursor=Console.CursorTop;
Console.SetCursorPosition(0,Console.CursorTop);
Console.Write(newstring(' ',Console.WindowWidth));
Console.SetCursorPosition(0,currentLineCursor);
}
privatestaticList<PlayerObject>GenerateDocuments()
{
//Generate documents based on XML document
List<PlayerObject>playerDatas=newList<PlayerObject>();
Console.WriteLine("Attempting to read XML...");
//Read XML doc here, this will be used to add to the playerDatas List
using(StreamReaderreader=newStreamReader(XmlFileName))
{
stringbody=reader.ReadToEnd();
XmlDocumentXmlDoc=newXmlDocument();
XmlDoc.LoadXml(body);
XmlNodeListnodes=XmlDoc.SelectNodes("//Player");
Console.WriteLine("XML read! Attempting to load players...");
foreach(XmlNodenodeinnodes)
{
if(node.Attributes.Count>1)
{
PlayerObjectnewPlayer=newPlayerObject()
{
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!");
returnplayerDatas;
}
}
publicclassPlayerObject
{
publicstringusername{get;set;}
publicstringid{get;set;}
publicstringrank{get;set;}
publicstringrankDeviation{get;set;}
publicstringvolatility{get;set;}
}
}