-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgram.cs
535 lines (448 loc) · 20.7 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Coflnet;
using dev;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using RestSharp;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
namespace Coflnet.Sky.Core
{
public class Program
{
public static string InstanceId { get; }
public static bool displayMode = false;
public static bool FullServerMode { get; private set; }
public static bool LightClient { get; private set; }
public static int usersLoaded = 0;
/// <summary>
/// Is set to the last time the ip was rate limited by Mojang
/// </summary>
/// <returns></returns>
private static DateTime BlockedSince = new DateTime(0);
private static string version = "0.4.0";
public static string Version => version;
public static int RequestsSinceStart { get; private set; }
public static bool Migrated { get; internal set; }
public static event Action onStop;
public static CoreServer server;
static Program()
{
InstanceId = DateTime.Now.Ticks.ToString() + version;
}
static void Main(string[] args)
{
Console.CancelKeyPress += delegate
{
Console.WriteLine("\nAbording");
onStop?.Invoke();
};
if (args.Length > 0)
{
if (args.Length > 1)
{
runSubProgram(args[1][0]);
return;
}
}
displayMode = true;
while (true)
{
var res = Console.ReadKey();
if (runSubProgram(res.KeyChar))
return;
//} catch(Exception e)
//{
// Console.WriteLine("Error Occured: "+ e.Message);
// throw e;
//}
}
}
/// <summary>
/// returns true if application should be closed
/// </summary>
/// <param name="mode"></param>
/// <returns></returns>
static bool runSubProgram(char mode)
{
switch (mode)
{
case 'f':
FullServer();
break;
case 'n':
var af = new SaveAuction();
NBT.FillDetails(af, "H4sIAAAAAAAAAG1VUW8bRRAex0nquIUGKpAgCG2BoFiO04sb++yIF8dxk6hRFMVOESBk7d2tfafs3Vp3axx4461PPIGEEJF4QFj8B578H3hHkfoH+sYLYmbvQl3Uk+zbnZmd+b6Z2bkiwArkgiIA5BZgIfBy5Rw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YYpZT0n2K8hPm4MznH/JlQH8+3jcHydK6M7HMVOoHA+jVQaHranHfJ1BQgSy8W4JvUlDljbXgZtVbwJrlD0AqTHrNQOQll/H3kchoIrF1GJI3E9sZywuMLqoj9OMBjGQKKwF13HI4l11RO03GwOocV/Ydb8C5KXnJs7kOdiMEu6k7EpWbno2HMvZsL18hYNzYo1fZmo0lUMdtuzdq0LLxZjRJCXptNqz0/SFigRcjwfR5hV3nsrz9+uI9qhNJsRW4goqw4UPrfhaFrff3jL2xuwFCOsGTwjkmv2+bYk0jYdJUUXwlJuUJKdbrunYPOyX7r7DPWPux0e6fHrV4nD0uukiqGtbW1Aiye8FDQEKzfAMk6ou2LRI8oc1CEu51LHfOW1nGAtRJJHu6ktetfYMKx0eTfeVj1le6PlOZa9V2a0YijWIBCqLxgECDiWzwNUaBRDm+cdVqnnbP+C2RFuEPDHNmHaIVB7o2lDkKE0KfZ0k9obqHTpTwsa3PPcZPHzdBMiVSz6hjb0X9zCMXLgETHYwz6kd2o7Tg1m1fcuicqO3azXnHsplPx6tVBFecoPvVFWMGwSJ+HI/ysfPf7n8+eAyzAcloA+tb8C7sqykCaBgAA"));
//Console.WriteLine (JsonConvert.SerializeObject (.Instance.Items.Where (item => item.Value.AltNames != null && item.Value.AltNames.Count > 3 && !item.Key.Contains("DRAGON")).Select((item)=>new P(item.Value))));
break;
default:
return true;
}
return false;
}
public static void CreateHost(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).ConfigureAppConfiguration((context, config) =>
{
Console.WriteLine("called configure\n+#+#+#+#+#");
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
config.AddJsonFile("custom.conf.json", optional: true, reloadOnChange: false);
config.AddEnvironmentVariables();
});
public static void FullServer()
{
Console.WriteLine($"\n - Starting FullServer {version} - \n");
FullServerMode = true;
server = new CoreServer();
Task.Run(async () =>
{
while (true)
{
try
{
CreateHost(new string[0]);
}
catch (Exception e) { Logger.Instance.Error(e, "Exited asp.net"); }
await Task.Delay(2000);
}
}).ConfigureAwait(false);
var mode = SimplerConfig.SConfig.Instance["MODE"];
var modes = SimplerConfig.SConfig.Instance["MODES"]?.Split(",");
if (modes == null)
modes = new string[] { "indexer", "updater", "flipper" };
LightClient = modes.Contains("light");
if (LightClient)
{
Console.WriteLine("running on " + System.Net.Dns.GetHostName());
Thread.Sleep(Timeout.Infinite);
}
Task redisInit = null;
// bring the db up to date
if (modes.Contains("indexer"))
{
GetDBToDesiredState();
ItemDetails.Instance.LoadFromDB();
redisInit = MakeSureRedisIsInitialized();
Console.WriteLine("booting db dependend stuff");
//NameUpdater.Run();
Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromMinutes(3));
await ItemPrices.Instance.BackfillPrices();
}).ConfigureAwait(false);
try
{
CleanDB();
}
catch (Exception e)
{
Console.WriteLine($"Cleaning failed {e.Message}");
}
}
onStop += () =>
{
Console.WriteLine("stopped");
};
redisInit?.GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
static CancellationTokenSource fillRedisCacheTokenSource;
public static void FillRedisCache()
{
fillRedisCacheTokenSource?.Cancel();
fillRedisCacheTokenSource = new CancellationTokenSource();
Task.Run(async () =>
{
try
{
await ItemPrices.Instance.FillHours(fillRedisCacheTokenSource.Token);
}
catch (Exception e)
{
Console.WriteLine($"Backfill failed :( \n{e.Message}\n {e.InnerException?.Message} {e.StackTrace}");
}
}, fillRedisCacheTokenSource.Token).ConfigureAwait(false); ;
}
public static async Task MakeSureRedisIsInitialized()
{
var Key = "LastbazaarUpdate";
try
{
var last = await CacheService.Instance.GetFromRedis<DateTime>(Key);
await CacheService.Instance.SaveInRedis(Key, DateTime.Now);
if (last < DateTime.Now - TimeSpan.FromMinutes(2))
FillRedisCache();
}
catch (Exception e)
{
await CacheService.Instance.SaveInRedis(Key, default(DateTime));
Logger.Instance.Error($"Redis init failed {e.Message} \n{e.StackTrace}");
}
}
private static void CleanDB()
{
// try cleaning when the dust settled
Thread.Sleep(TimeSpan.FromHours(1));
using (var context = new HypixelContext())
{
// remove dupplicate itemnames
context.Database.ExecuteSqlRaw(@"
DELETE
FROM AltItemNames
WHERE ID NOT IN
(
SELECT MIN(ID)
FROM AltItemNames
GROUP BY Name,DBItemId
)
");
}
}
private static void GetDBToDesiredState()
{
try
{
bool isNew = false;
using (var context = new HypixelContext())
{
try
{
context.Database.ExecuteSqlRaw("CREATE TABLE `__EFMigrationsHistory` ( `MigrationId` nvarchar(150) NOT NULL, `ProductVersion` nvarchar(32) NOT NULL, PRIMARY KEY (`MigrationId`) );");
//context.Database.ExecuteSqlRaw("INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`) VALUES ('20201212165211_start', '3.1.6');");
isNew = true;
//context.Database.ExecuteSqlRaw("DELETE FROM Enchantment where SaveAuctionId is null");
}
catch (Exception e)
{
if (e.Message != "Table '__EFMigrationsHistory' already exists")
Console.WriteLine($"creating migrations table failed {e.Message} {e.StackTrace}");
}
//context.Database.ExecuteSqlRaw("set net_write_timeout=99999; set net_read_timeout=99999");
context.Database.SetCommandTimeout(99999);
// Creates the database if not exists
context.Database.Migrate();
Console.WriteLine("\nmigrated :)\n");
context.SaveChanges();
if (!context.Items.Any() || context.Players.Count() < 2_000_000)
isNew = true;
}
Migrated = true;
}
catch (Exception e)
{
Logger.Instance.Error(e, "GetDB to desired state failed");
Thread.Sleep(TimeSpan.FromSeconds(20));
GetDBToDesiredState();
}
}
public static void RunIsolatedForever(Func<Task> todo, string message, int backoff = 2000)
{
Task.Run(async () =>
{
while (true)
{
try
{
await todo();
}
catch(TaskCanceledException)
{
return;
}
catch (Exception e)
{
Console.WriteLine();
Console.WriteLine($"{message}: {e.Message} {e.StackTrace}\n {e.InnerException?.Message} {e.InnerException?.StackTrace} {e.InnerException?.InnerException?.Message} {e.InnerException?.InnerException?.StackTrace}");
await Task.Delay(2000);
}
await Task.Delay(backoff);
}
}).ConfigureAwait(false);
}
private static void RunIsolatedForever(Action todo, string message, int backoff = 2000)
{
RunIsolatedForever(() =>
{
todo();
return Task.CompletedTask;
}, message, backoff);
}
private static void WaitForDatabaseCreation()
{
try
{
using (var context = new HypixelContext())
{
try
{
var testAuction = new SaveAuction()
{
Uuid = "00000000000000000000000000000000"
};
context.Auctions.Add(testAuction);
context.SaveChanges();
context.Auctions.Remove(testAuction);
context.SaveChanges();
}
catch (Exception)
{
// looks like db doesn't exist yet
Console.WriteLine("Waiting for db creating in the background");
Thread.Sleep(10000);
}
// TODO: switch to .Migrate()
context.Database.Migrate();
}
}
catch (Exception e)
{
Console.WriteLine($"Waiting for db creating in the background {e.Message} {e.InnerException?.Message}");
Thread.Sleep(10000);
}
}
private static System.Collections.Concurrent.ConcurrentDictionary<string, int> PlayerAddCache = new System.Collections.Concurrent.ConcurrentDictionary<string, int>();
public static int AddPlayer(HypixelContext callingContext, string uuid, ref int highestId, string name = null)
{
try
{
if (PlayerAddCache.TryGetValue(uuid, out int id))
return id;
using var context = new HypixelContext();
var existingPlayer = context.Players.Find(uuid);
if (existingPlayer != null)
return existingPlayer.Id;
if (uuid != null)
{
var p = new Player() { UuId = uuid, ChangedFlag = true };
p.Name = name;
p.Id = Interlocked.Increment(ref highestId);
context.Players.Add(p);
context.SaveChanges();
return p.Id;
}
}
catch (Exception e)
{
Logger.Instance.Error(e, "failed to save user");
var existingPlayer = callingContext.Players.Find(uuid);
if (existingPlayer != null)
return existingPlayer.Id;
}
return 0;
}
/// <summary>
/// Downloads username for a given uuid from mojang.
/// Will return null if rate limit reached.
/// </summary>
/// <param name="uuid"></param>
/// <returns>The name or null if error occurs</returns>
public static async Task<string> GetPlayerNameFromUuid(string uuid)
{
if (IsRatelimited())
{
await Task.Delay(2000);
Console.Write("Blocked");
// blocked
return null;
}
else if (RequestsSinceStart >= 2000)
{
Console.Write("\tFreed 2000 ");
RequestsSinceStart = 0;
}
//Create the request
RestClient client = null;
RestRequest request;
int type = 0;
if (RequestsSinceStart == 2)
{
BlockedSince = DateTime.Now;
}
if (RequestsSinceStart < 600)
{
client = new RestClient("https://api.mojang.com/");
request = new RestRequest($"user/profile/{uuid}", Method.Get);
type = 1;
}
else if (RequestsSinceStart < 1200)
{
client = new RestClient("https://sessionserver.mojang.com");
request = new RestRequest($"/session/minecraft/profile/{uuid}", Method.Get);
type = 1;
}
else if (RequestsSinceStart < 1850)
{
client = new RestClient("https://mc-heads.net/");
request = new RestRequest($"/minecraft/profile/{uuid}", Method.Get);
type = 1;
}
else
{
client = new RestClient("https://minecraft-api.com/");
request = new RestRequest($"/api/uuid/pseudo.php?uuid={uuid}", Method.Get);
type = 2;
}
RequestsSinceStart++;
//Get the response and Deserialize
var response = await client.ExecuteAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
{
// Shift out to another method
RequestsSinceStart += 200;
}
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
Logger.Instance.Info(client.BuildUri(request) + $" returned {response.StatusCode} {response.Content.Truncate(100)}");
await Task.Delay(5000); // backoff
RequestsSinceStart += 50;
return null;
}
if (response.Content == "")
{
Console.WriteLine("no content");
return null;
}
if (type == 2)
{
if (response.Content == null)
Console.WriteLine("content null");
return response.Content;
}
dynamic responseDeserialized = JsonConvert.DeserializeObject(response.Content);
if (responseDeserialized == null || (responseDeserialized?.name == null) || responseDeserialized.name == null)
{
Logger.Instance.Error(client.BuildUri(request) + $" returned {response.StatusCode} {response.Content}");
}
if (responseDeserialized == null)
{
return null;
}
switch (type)
{
case 0:
return responseDeserialized[responseDeserialized.Count - 1]?.name;
case 1:
return responseDeserialized.name;
}
return responseDeserialized.name;
}
public static bool IsRatelimited()
{
return DateTime.Now.Subtract(new TimeSpan(0, 10, 0)) < BlockedSince && RequestsSinceStart >= 2400;
}
}
}