-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
322 lines (267 loc) · 10.1 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using Dapper;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using FW.Core;
using FW.Core.Models;
using Stoic.Chain;
using Stoic.Log;
using Stoic.Utilities;
namespace FW
{
class Program
{
public static bool ClosedBySignal = false;
public static bool ShouldRun = true;
static void Main(string[] args)
{
#region Initialization
Console.CancelKeyPress += new ConsoleCancelEventHandler(SigIntHandler);
var state = new State();
var ch = new ConsoleHelper(args);
var logger = new Logger(LogLevels.DEBUG);
state.Config = new Config {
DbDsn = string.Empty,
LogFile = $"fw-{DateTime.UtcNow.ToString("yyyy-MM-dd")}.log",
Port = 6055
};
if (File.Exists("config.json")) {
try {
var oldConfig = (Config)state.Config.Clone();
state.Config = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));
if (string.IsNullOrWhiteSpace(state.Config.LogFile)) {
state.Config.LogFile = oldConfig.LogFile;
}
if (state.Config.Port < 1024) {
state.Config.Port = oldConfig.Port;
}
} catch (Exception e) {
logger.Log(LogLevels.WARNING, $"Failed to read config file, but it exists, check file: {e.Message}");
}
} else {
logger.Log(LogLevels.NOTICE, "No config file found, continuing with defaults");
}
var serv = new SocketServer(10, Convert.ToInt32(ch.GetParameter("p", "port", state.Config.Port.ToString())), ref logger);
logger.AddAppender(new ConsoleAppender());
logger.AddAppender(new FileAppender(ch.GetParameter("lf", "log-file", state.Config.LogFile), FileAppenderOutputTypes.PLAIN));
logger.Log(LogLevels.DEBUG, "Initialized game console subsystem");
logger.Log(LogLevels.DEBUG, "Initialized game logging subsystem");
logger.Log(LogLevels.DEBUG, "Initialized game socket subsystem");
logger.Log(LogLevels.DEBUG, "Initialized game state subsystem");
logger.Output();
using (var conn = new MySqlConnection(state.Config.DbDsn)) {
logger.Log(LogLevels.INFO, "Beginning database migration..");
Type migrationBaseType = typeof(MigrationBase);
var migrations = new List<MigrationBase>();
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
foreach (var t in asm.GetTypes()) {
if (migrationBaseType.IsAssignableFrom(t) && !t.IsAbstract) {
migrations.Add((MigrationBase)Activator.CreateInstance(t));
}
}
}
migrations.Sort((a, b) => a.Name.CompareTo(b.Name));
if (migrations.Count > 0) {
List<string> existingMigrations = new List<string>();
try {
existingMigrations = conn.Query<string>("SELECT `FileName` FROM `Migration` ORDER BY `ID` ASC").AsList();
} catch { }
foreach (var mig in migrations) {
string logLine = $" -> Executing migration '{mig.Name}'.. ";
if (existingMigrations.Contains(mig.Name)) {
logLine += "SKIPPED (Already Executed)";
logger.Log(LogLevels.INFO, logLine);
continue;
}
try {
conn.Execute(mig.Sql);
conn.Execute($"INSERT INTO `Migration` (`FileName`) VALUES (@FileName)", new { FileName = mig.Name });
logLine += "SUCCESS";
} catch (MySqlException mex) {
logLine += $"ERROR ({mex.ErrorCode} - {mex.Message})";
}
logger.Log(LogLevels.INFO, logLine);
}
} else {
logger.Log(LogLevels.DEBUG, "No database migrations to perform, migration complete");
}
}
logger.Log(LogLevels.INFO, "Finished initializing game subsystems");
logger.Output();
var game = new ChainHelper<TickDispatch, Command, List<Command>>();
game.LinkNode(new Game.ActionNode(ref logger));
game.LinkNode(new Game.Objects.ObjectsNode(ref logger));
game.LinkNode(new Game.World.WorldNode(ref logger));
game.LinkNode(new Game.Players.PlayersNode(ref logger));
logger.Output();
state.Branch = (File.Exists("branch.txt")) ? File.ReadAllText("branch.txt").Trim() : "master";
state.Commit = (File.Exists("commit.txt")) ? File.ReadAllText("commit.txt").Trim() : "000000";
state.Version = (File.Exists("version.txt")) ? File.ReadAllText("version.txt").Trim() : "0.0.0.0";
#region Rooms (Temp)
//////////////////////////////
// TEMP ROOM INITIALIZATION //
//////////////////////////////
state.AddRoom(new Room(
1,
"Common Room",
new Vector3(0.0f, 0.0f, 0.0f),
new Exit[2] {
new Exit(1, 2, Directions.East, false, true),
new Exit(1, 3, Directions.Up, false, true)
},
true,
true,
Biomes.Interior,
Terrains.Interior,
@"The atmosphere of this common room is jovial and bustling. A duo of minstrels are belting songs of bravery near a stone fireplace, with a half circle of benches splayed out in front. A wooden bar along the western wall features a half dozen tapped kegs and shelving full of steins. Around the room are tables full of adventures sharing stories, dicing, eating, drinking, and playing cards.",
null,
20));
state.AddRoom(new Room(
2,
"Private Booth",
new Vector3(-1.0f, 0.0f, 0.0f),
new Exit[1] {
new Exit(2, 1, Directions.West, false, true)
},
true,
true,
Biomes.Interior,
Terrains.Interior,
@"Tucked in the back corner of the tavern, this secluded booth provides ample opportunity for private conversation between those sitting around the table. Several pieces of empty glassware are stacked in the center of the table, showing that this booth is used regularly and not often approached by the waitresses",
null,
20));
state.AddRoom(new Room(
3,
"Upstairs Hallway",
new Vector3(0.0f, 1.0f, 0.0f),
new Exit[4] {
new Exit(3, 1, Directions.Down, false, true),
new Exit(3, 4, Directions.East, false, true),
new Exit(3, 5, Directions.North, false, true),
new Exit(3, 6, Directions.West, false, true)
},
true,
true,
Biomes.Interior,
Terrains.Interior,
@"A wide hallway runs the length of the building, with sturdy wooden doors leading to individual rooms in each direction. Small candles are nestled into wall sconces lining the walls, providing ample light in this area no matter the time of day. Small placards have been hand engraved and nailed to each door.",
null,
20));
state.AddRoom(new Room(
4,
"Xitan's Roomo",
new Vector3(-1.0f, 1.0f, 0.0f),
new Exit[1] {
new Exit(4, 3, Directions.West, false, true)
},
true,
true,
Biomes.Interior,
Terrains.Interior,
//@"There isn't much left of this room's floor as far as you can see, it's mostly covered with all manner of paraphernalia. Among the many things strewn about are wood working tools, books, weapons, strange armor, and a half-finished painting in a back corner. One of the notebooks appears to say ""Xitan's Bridge Designs."" You notice there are obvious paths through the clutter you can walk through, one of which leads to a pillow and rolled up blanket on the floor.",
"Everything slows down, as you turn into a turtle upon entering the room..",
null,
20));
state.AddRoom(new Room(
5,
"Kyssandra's Room",
new Vector3(1.0f, 1.0f, 0.0f),
new Exit[1] {
new Exit(5, 3, Directions.South, false, true)
},
true,
true,
Biomes.Interior,
Terrains.Interior,
@"This room is devoid of a majority of the furniture you'd expect in a bedroom, except for the bed itself. Hundreds of ivory wax candles are scattered around the mostly empty floor, making it nearly impossible to walk anywhere but to and from the bed itself. The canopied bed is draped in a luxurious ivory linen with edgings of gold that glint as the glow of the candles dance across the metallic fibers. Carved into the soft wood of one of the bedposts is the name 'Kyssandra' flanked by two small stars.",
null,
20));
state.AddRoom(new Room(
6,
"Neryndil's Room",
new Vector3(0.0f, 1.0f, 1.0f),
new Exit[1] {
new Exit(6, 3, Directions.East, false, true)
},
true,
true,
Biomes.Interior,
Terrains.Interior,
@"",
null,
20));
//////////////////////////////
// TEMP ROOM INITIALIZATION //
//////////////////////////////
#endregion
#endregion
int errCount = 0;
while (ShouldRun) {
bool gotSockErr = false;
var disp = new TickDispatch();
try {
disp.Initialize(state, serv.Poll());
} catch (Exception ex) {
logger.Log(LogLevels.ERROR, ex.Message);
logger.Log(LogLevels.DEBUG, ex.StackTrace);
var cmds = new List<Command>();
foreach (var s in serv.Sockets) {
if (!s.Value.Socket.Connected) {
cmds.Add(new Command {
Contents = "DISCONNECT",
ID = s.Key,
Type = CommandTypes.DISCONNECTED
});
}
}
gotSockErr = true;
disp.Initialize(state, cmds);
}
if (gotSockErr) {
if (++errCount > 25) {
ShouldRun = false;
}
} else {
errCount = 0;
}
game.Traverse(ref disp);
if (disp.Results.Count > 0) {
foreach (var c in disp.Results) {
if (c.Type == CommandTypes.SEND) {
serv.Send(c.ID, c.Contents);
} else if (c.Type == CommandTypes.DISCONNECTED) {
serv.Close(c.ID);
}
}
}
logger.Output();
}
logger.Log(LogLevels.INFO, "Shutting down game subsystems");
serv.Shutdown();
logger.Output();
if (!ClosedBySignal) {
FinalPause();
}
return;
}
public static void FinalPause()
{
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadLine();
return;
}
public static void SigIntHandler(object sender, ConsoleCancelEventArgs args)
{
args.Cancel = true;
Console.WriteLine();
Console.WriteLine("Captured SIGINT, shutting down...");
ShouldRun = false;
ClosedBySignal = true;
return;
}
}
}