Skip to content

Commit e799149

Browse files
committed
Update Program.cs
1 parent fe6257f commit e799149

1 file changed

Lines changed: 67 additions & 7 deletions

File tree

  • app/Project Tracker Server/Project Tracker Server

app/Project Tracker Server/Project Tracker Server/Program.cs

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class Program {
2121
private static readonly string DATA_FILE =
2222
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
2323
+ "/Project Tracker Data/data.json";
24+
private static readonly string CONNECTION_DEATILS_FILE =
25+
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
26+
+ "/Project Tracker Data/connection-details.json";
2427
private readonly static int ID_LENGTH = 30;
2528

2629
private static bool viewConnectionDetails = false;
@@ -29,6 +32,8 @@ class Program {
2932
private static bool speedUpStatistics = false;
3033
private static string lastMonth = "";
3134

35+
private static List<string> onlineUsers = new List<string>();
36+
3237
private static string[] resetDaysOpened = {
3338
"Monday: 0",
3439
"Tuesday: 0",
@@ -88,6 +93,11 @@ private static void CopyData(string origin) {
8893
ClientDataManifest.Rootobject clientDataValues =
8994
JsonConvert.DeserializeObject<ClientDataManifest.Rootobject>(clientJson);
9095

96+
if (onlineUsers.Contains(clientDataValues.UserID)) {
97+
return;
98+
}
99+
onlineUsers.Add(clientDataValues.UserID);
100+
91101
string serverJson = File.ReadAllText(DATA_FILE);
92102
ServerDataManifest.Rootobject serverDataValues =
93103
JsonConvert.DeserializeObject<ServerDataManifest.Rootobject>(serverJson);
@@ -177,12 +187,32 @@ private static void BackgroundLisener() {
177187
server.Bind(localEndPoint);
178188
server.Listen(100);
179189

190+
if (File.Exists(CONNECTION_DEATILS_FILE)) {
191+
File.Delete(CONNECTION_DEATILS_FILE);
192+
}
193+
DateTime date;
180194
while (true) {
195+
196+
if (!File.Exists(CONNECTION_DEATILS_FILE)) {
197+
File.Create(CONNECTION_DEATILS_FILE);
198+
}
199+
200+
string connectionDetailsList = "";
201+
202+
203+
string timestamp;
181204
try {
205+
connectionDetailsList = File.ReadAllText(CONNECTION_DEATILS_FILE);
206+
207+
date = DateTime.Now;
208+
timestamp = "[" + date.Hour + ":" + date.Minute + ":" + date.Second + "]";
182209
if (viewConnectionDetails) {
183-
Console.WriteLine("Waiting for a connection...");
210+
Console.WriteLine(timestamp + " Waiting for a connection...");
184211
}
185-
212+
connectionDetailsList += timestamp + " Waiting for a connection...\n";
213+
File.WriteAllText(CONNECTION_DEATILS_FILE, connectionDetailsList);
214+
215+
186216
Socket client = server.Accept();
187217

188218
// Take up to 5mb of file. Should only be a few kb though
@@ -198,9 +228,14 @@ private static void BackgroundLisener() {
198228
string fileData = new string(chars);
199229

200230
string username = GetUserID(fileData);
231+
232+
date = DateTime.Now;
233+
timestamp = "[" + date.Hour + ":" + date.Minute + ":" + date.Second + "]";
201234
if (viewConnectionDetails) {
202-
Console.WriteLine("Received info from " + username);
235+
Console.WriteLine(timestamp + " Received info from " + username);
203236
}
237+
connectionDetailsList += timestamp + " Received info from " + username + "\n";
238+
File.WriteAllText(CONNECTION_DEATILS_FILE, connectionDetailsList);
204239

205240
if (username.Length < ID_LENGTH) {
206241
username = CreateNewID();
@@ -212,13 +247,29 @@ private static void BackgroundLisener() {
212247
CopyData(savePath);
213248
File.Delete(savePath);
214249

250+
date = DateTime.Now;
251+
timestamp = "[" + date.Hour + ":" + date.Minute + ":" + date.Second + "]";
215252
if (viewConnectionDetails) {
216-
Console.WriteLine("Closed the server");
253+
Console.WriteLine(timestamp + " Closed the server");
217254
}
255+
connectionDetailsList += timestamp + " Closed the server\n";
256+
File.WriteAllText(CONNECTION_DEATILS_FILE, connectionDetailsList);
257+
}
258+
catch (IOException) {
259+
218260
}
219261
catch (Exception e) {
262+
date = DateTime.Now;
263+
timestamp = "[" + date.Hour + ":" + date.Minute + ":" + date.Second + "]";
220264
if (viewConnectionDetails) {
221-
Console.WriteLine("We had the following error: " + e);
265+
Console.WriteLine(timestamp + " We had the following error: " + e);
266+
}
267+
connectionDetailsList += timestamp + " We had the following error: " + e + "\n";
268+
try {
269+
File.WriteAllText(CONNECTION_DEATILS_FILE, connectionDetailsList);
270+
}
271+
catch (IOException) {
272+
222273
}
223274
}
224275
}
@@ -278,6 +329,13 @@ private static void BackgroundConsoleHelper() {
278329
Console.WriteLine("Press E to go back to the main console menu.\n");
279330
viewConnectionDetails = true;
280331

332+
try {
333+
Console.WriteLine(File.ReadAllText(CONNECTION_DEATILS_FILE));
334+
}
335+
catch (IOException) {
336+
337+
}
338+
281339
while (viewConnectionDetails) {
282340
ConsoleKeyInfo key = Console.ReadKey();
283341
if (key.Key == ConsoleKey.E) {
@@ -290,7 +348,8 @@ private static void BackgroundConsoleHelper() {
290348
Console.Clear();
291349
Console.ForegroundColor = blue;
292350
Console.WriteLine("You're currently viewing statistics.");
293-
Console.WriteLine("Press E to go back to the main console menu.\n");
351+
Console.WriteLine("Press E to go back to the main console menu.");
352+
Console.WriteLine("Press S to speed up/slow down statistics.\n");
294353
viewStatistics = true;
295354

296355
while (viewStatistics) {
@@ -354,6 +413,7 @@ private static void BackgroundStatistics() {
354413
if (resetTimer == 0) {
355414
lastMinuteSessions = serverDataValues.CurrentlyOpenSessions;
356415
serverDataValues.CurrentlyOpenSessions = 0;
416+
onlineUsers.Clear();
357417
}
358418

359419
if (serverDataValues.CurrentlyOpenSessions < 0) {
@@ -364,7 +424,7 @@ private static void BackgroundStatistics() {
364424
Console.Clear();
365425
Console.WriteLine("You're currently viewing statistics.");
366426
Console.WriteLine("Press E to go back to the main console menu.");
367-
Console.WriteLine("Press S to speed up/slow down statistics.");
427+
Console.WriteLine("Press S to speed up/slow down statistics.\n");
368428

369429
int secondsUntilReset = resetTimer;
370430
int minutesUntilReset = 0;

0 commit comments

Comments
 (0)