/// <summary> /// Reads the application settings file /// </summary> /// <returns>Returns null if failed</returns> public static AppSettings.ApplicationSettings GetSettings() { if (File.Exists(EndPoints.Application.SETTINGS_FILE_PATH)) { string jsonStr = File.ReadAllText(EndPoints.Application.SETTINGS_FILE_PATH); return(JsonConvert.DeserializeObject <AppSettings.ApplicationSettings>(jsonStr)); } else { var settings = new AppSettings.ApplicationSettings(); for (int i = 0; i < 3; i++) { settings.bots.Add(new AppSettings.BotSettings()); } string jsonStr = JsonConvert.SerializeObject(settings, Formatting.Indented); File.WriteAllText(EndPoints.Application.SETTINGS_FILE_PATH, jsonStr); return(null); } }
/// <summary> /// Class constructor /// </summary> /// <param name="settings">Application settings class</param> public Session(AppSettings.ApplicationSettings settings) { mSettings = settings; mLog = new Log("Session", "Logs\\Session.txt", 3); mDatabase = new Database("Database\\Items.sqlite"); if (!mDatabase.mConnected) { return; } if (AddBots(settings.bots)) { /*Find first host bot in our list and assign it as host*/ mBotHost = mBotList.FirstOrDefault(o => o.mBotType == Bot.BotType.Main); if (mBotHost == null) { mLog.Write(Log.LogLevel.Error, "Could not find a main bot from the list. We cannot continue."); return; } /*Connect to Discord*/ mDiscord = new Discord(this, settings.discord); /*Start trade worker thread, this will send items to users*/ mTradeWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; mTradeWorker.DoWork += TradeWorkerOnDoWork; mTradeWorker.RunWorkerCompleted += TradeWorkerOnRunWorkerCompleted; mTradeWorker.RunWorkerAsync(); /*Starts queuue worker thread, this will go through send offers and check their state*/ mQueueWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; mQueueWorker.DoWork += MQueueWorker_DoWork; mQueueWorker.RunWorkerCompleted += MQueueWorkerOnRunWorkerCompleted; mQueueWorker.RunWorkerAsync(); /*Start the daily scheduled restart timer*/ //mScheduledRestartTimer = new System.Timers.Timer(); //mScheduledRestartTimer.Interval = (new TimeSpan(08, 00, 00) - DateTime.Now.TimeOfDay).TotalMilliseconds; //mScheduledRestartTimer.Elapsed += new ElapsedEventHandler(ScheduledDailyRestart); //mScheduledRestartTimer.Start(); /*We'll pause here until either thread breaks*/ /*It will go be on a loop until either of the threads die*/ Console.Clear(); while (mTradeWorker.IsBusy && mQueueWorker.IsBusy) { Thread.Sleep(1000); } /*Log information about which worker that isn't running*/ mLog.Write(Log.LogLevel.Info, $"A worker has exited." + $"mTradeWorker running status: {mTradeWorker.IsBusy}" + $"mQueueWorker running status: {mQueueWorker.IsBusy}"); /*Send cancel request to our threads*/ mTradeWorker.CancelAsync(); mQueueWorker.CancelAsync(); } else { mLog.Write(Log.LogLevel.Error, "Bots were not added"); } }