/// <summary> /// Performs work on data as add new bot, remove old bot, update bot. /// </summary> /// <param name="downloadedData"></param> private static void UpdateBotsData(DownloadedData downloadedData) { for (int i = 0; i < downloadedData.Bots.Count; i++) { //If the bot doesn't exist in the database - Add the bot if (!DoesBotAlreadyExist(downloadedData.Bots[i][0])) { if (debug) { Core.WriteLine("Adding a new bot...", Core.LogType.Worker); } bots.AddBot(downloadedData.Bots[i][0], Int32.Parse(downloadedData.Bots[i][1]), long.Parse(downloadedData.Bots[i][2])); } else // If the bot does exist { //Update data to new data. if (debug) { Core.WriteLine("Updating bot data...", Core.LogType.Worker); } bots.bot[i].timesSeen = Int32.Parse(downloadedData.Bots[i][1]); bots.bot[i].lastSeen = long.Parse(downloadedData.Bots[i][2]); } } }
/// <summary> /// Loop and show the main menu /// </summary> private static void MainMenu() { Thread.Sleep(100); Console.WriteLine("? or help will show commands"); Console.WriteLine("Enter a command:"); while (runMainMenu) // Lock the user in the menu loop { switch (Console.ReadLine().ToLower()) // Get user input as lower case { //Nice case devided switch case #region Case help case "?": case "help": Core.WriteLine("Commands:" + "\n\rclear, cls -- This will clear the console." + "\n\rdebug, dev -- This will activate debug console output." + "\n\rstart -- This will start the worker." + "\n\rstop -- This will stop the worker after work has finished." + "\n\rupload -- This will upload current bot data to the remote server." + "\n\rdownload -- This will download new data to the software." + "\n\rreset, r -- WARNING! Instantly resets bots data and overwrite data on next pass." + "\n\rstatus, info -- Shows software information and session data." + "\n\rlist, bots -- Lists all bots and their data in the console." + "\n\rexit, quit -- Instantly closes the software.", Core.LogType.Requested); break; #endregion Case help #region Case clear case "clear": case "cls": Console.Clear(); break; #endregion Case clear #region Case debug case "dev": case "debug": debug = !debug; Core.debug = !Core.debug; Core.WriteLine("Debug mode is now set " + debug.ToString(), Core.LogType.Requested); break; #endregion Case debug #region Worker Controller #region Case start case "start": if (botScraperThread.IsAlive) { Core.WriteLine("Worker is already ONLINE.", Core.LogType.Requested); break; } runWorker = true; try { botScraperThread = new Thread(() => Do_Work()); botScraperThread.Start(); Core.WriteLine("Worker is starting...", Core.LogType.Requested); } catch (ThreadStateException x) { Core.WriteLine(x.Message, Core.LogType.Console); } catch (OutOfMemoryException x) { Core.WriteLine(x.Message, Core.LogType.Console); } break; #endregion Case start #region Case stop case "stop": if (!botScraperThread.IsAlive) { Core.WriteLine("Worker is already OFFLINE...", Core.LogType.Requested); break; } runWorker = false; Core.WriteLine("Worker is scheduled to stop. Please allow up to " + Core.updateTimerSeconds.ToString() + " second(s) to finish current work.", Core.LogType.Requested); break; #endregion Case stop #region Case upload case "upload": string json = SaveDataToLocalJsonFile(); // Save file (to get string) UploadJsonToDatabase(json); // Upload file Core.WriteLine("Upload complete.", Core.LogType.Requested); break; #endregion Case upload #region Case download case "download": //Download new bot-data downloadedData = Core.Download <DownloadedData>(Core.scrapeURL); // Download new data //Create update bots data UpdateBotsData(downloadedData); // Perform work on the new data Core.WriteLine("Download Complete.", Core.LogType.Requested); break; #endregion Case download #endregion Worker Controller #region Bot Controller #region Case Reset case "r": case "reset": bots = new BotsData(); Core.WriteLine("Bots have been reset. Save and upload will begin in next cycle.", Core.LogType.Requested); break; #endregion Case Reset #region Case status/info case "status": case "info": SendStatusInformation(); Core.WriteLine("This software was created by " + Core.authorName + "/" + Core.authorRealName + " @ 2020", Core.LogType.Requested); break; #endregion Case status/info #region Case list bots case "list": case "bots": try { if (bots.bot.Count > 0) { // List bots if there are any in the list foreach (Bot bot in bots.bot) { Core.WriteLine(bot.name + " has been sighted " + bot.timesSeen.ToString() + " times and was last seen: " + Core.TranslateLongToDateTime(bot.lastSeen).ToString(), Core.LogType.Requested); } } else { Core.WriteLine("There are no bots in the list.", Core.LogType.Requested); } } catch (InvalidOperationException x) { Core.WriteLine(x.Message); } break; #endregion Case list bots #endregion Bot Controller #region Case exit case "exit": case "quit": Environment.Exit(0); break; #endregion Case exit default: Core.WriteLine("That is not an accepted command. Please try again.\n\rFor help & commands, write \"help\" or \"?\".\n\r"); break; } } }
/// <summary> /// Clean previous DownloadedData /// </summary> private static void CleanData() { GC.Collect(); // Is this needed? downloadedData = new DownloadedData(); // New collector. Begin anew! }
/// <summary> /// Worker start method /// </summary> private static void Do_Work() { Core.WriteLine("Worker started...", Core.LogType.Worker); if (startTimer == null) { startTimer = DateTime.Now; } started++; while (runWorker) { if (debug) { Core.WriteLine("New Loop...", Core.LogType.Worker); } //Keep track of how many software-loops have been started. loops++; //Clean if (debug) { Core.WriteLine("Cleaning data...", Core.LogType.Worker); } CleanData(); //Download new bot-data if (debug) { Core.WriteLine("Download new data...", Core.LogType.Worker); } downloadedData = Core.Download <DownloadedData>(Core.scrapeURL); if (downloadedData == null) { Core.WriteLine("Downloaded data was not valid. Aborting...", Core.LogType.Worker); return; } //Create update bots data if (debug) { Core.WriteLine("Building database data...", Core.LogType.Worker); } UpdateBotsData(downloadedData); //Clean out old bots if (debug) { Core.WriteLine("Checking for old bots...", Core.LogType.Worker); } CleanOutOldBots(); //Save local json file if (debug) { Core.WriteLine("Saving bot data to local file...", Core.LogType.Worker); } string json = SaveDataToLocalJsonFile(); //Upload Data if (debug) { Core.WriteLine("Upload checking...", Core.LogType.Worker); } UploadJsonToDatabase(json); //We wait before running the bot again. if (debug) { Core.WriteLine("Waiting for another pass to start...", Core.LogType.Worker); } Thread.Sleep(Core.ResolveSecondsToMilliseconds(Core.updateTimerSeconds)); } stopped++; Core.WriteLine("Worker stopped...", Core.LogType.Worker); }