-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
102 lines (92 loc) · 3.5 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
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace game
{
internal static class Program
{
const int EXIT_CODE_RESTART = 64;
static void Test()
{
var a = new int[294 * 6];
var rnd = new Random();
var sw = new Stopwatch();
sw.Restart();
for (var tt = 0; tt < 1000; ++tt)
{
for (var i = 0; i < a.Length; i++)
{
a[i] = rnd.Next() % 4;
}
var gameLogic = new GameLogic(0, a);
gameLogic.GetResponseData(0);
}
Console.WriteLine($"{sw.Elapsed}");
}
public static async Task Main(string[] args)
{
var calcScoreMode = true;
var apiMode = true;
var batchMode = false;
if (args.Length >= 1)
{
switch (args[0])
{
case "calcScore":
apiMode = false;
break;
case "api":
calcScoreMode = false;
break;
case "batch":
apiMode = false;
calcScoreMode = false;
batchMode = true;
break;
case "test":
Test();
return;
case "-":
break;
default:
throw new Exception("invalid args[0] (mode)");
}
}
var gameStartAt = Environment.GetEnvironmentVariable("GAME_START_AT") ?? "0";
var gamePeriod = Environment.GetEnvironmentVariable("GAME_PERIOD") ?? "9223372036854775807";
var startAt = long.Parse(gameStartAt);
var endAt = startAt + long.Parse(gamePeriod);
var gameDbHost = Environment.GetEnvironmentVariable("GAMEDB_HOST") ?? "localhost";
var gameDbPort = Environment.GetEnvironmentVariable("GAMEDB_PORT") ?? "6379";
var redisConfig = ConfigurationOptions.Parse($"{gameDbHost}:{gameDbPort}");
var redis = ConnectionMultiplexer.Connect(redisConfig);
var cts = new CancellationTokenSource();
if (apiMode)
{
var apiPort = Environment.GetEnvironmentVariable("API_PORT") ?? "8080";
new Api(redis, redisConfig, apiPort, startAt, endAt).Start(cts.Token);
}
Console.WriteLine($"StartAt: {startAt} Period: {gamePeriod} EndAt: {endAt}");
Console.WriteLine("started.");
if (calcScoreMode)
{
new Thread(() => new CalcScore(redis, redisConfig, startAt, endAt).Start(cts.Token)).Start();
}
if (!batchMode)
{
redis.GetSubscriber().Subscribe("restart", (_, message) => {
var msg = (string)message;
if (msg == "all" || (apiMode && msg == "api") || (calcScoreMode && msg == "calcScore")) {
System.Environment.ExitCode = EXIT_CODE_RESTART;
cts.Cancel();
}
});
try {
await Task.Delay(Timeout.Infinite, cts.Token);
} catch (TaskCanceledException) { /* ignored */ }
}
}
}
}