public static void DoUpkeepForAll() { AlliancePlugin.LoadAllAlliancesForUpkeep(); List <Alliance> saveThese = new List <Alliance>(); List <Alliance> deleteThese = new List <Alliance>(); try { using (var db = new LiteDatabase(connectionString)) { var collection = db.GetCollection <BankData>("BankData"); foreach (Alliance alliance in AlliancePlugin.AllAlliances.Values) { FileUtils jsonStuff = new FileUtils(); jsonStuff.WriteToJsonFile <Alliance>(AlliancePlugin.path + "//UpkeepBackups//" + alliance.AllianceId + ".json", alliance); var bank = collection.FindById(alliance.AllianceId); if (bank == null) { bank = new BankData { Id = alliance.AllianceId, balance = 1 }; collection.Insert(bank); alliance.failedUpkeep++; if (alliance.failedUpkeep >= AlliancePlugin.config.UpkeepFailBeforeDelete) { deleteThese.Add(alliance); AlliancePlugin.Log.Info("Deleting " + alliance.name); } else { AlliancePlugin.Log.Info("failed upkeep " + String.Format("{0:n0}", alliance.GetUpkeep()) + " SC. for " + alliance.name); AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, Upgrades disabled.", true, 0); } } else { if (bank.balance >= alliance.GetUpkeep()) { bank.balance -= alliance.GetUpkeep(); collection.Update(bank); AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Paying upkeep of " + String.Format("{0:n0}", alliance.GetUpkeep()) + " SC.", true, 0); alliance.Upkeep(alliance.GetUpkeep(), 1); alliance.bankBalance -= alliance.GetUpkeep(); alliance.failedUpkeep = 0; AlliancePlugin.Log.Info("Paying upkeep " + String.Format("{0:n0}", alliance.GetUpkeep()) + " SC. for " + alliance.name); saveThese.Add(alliance); } else { alliance.failedUpkeep++; if (alliance.failedUpkeep >= AlliancePlugin.config.UpkeepFailBeforeDelete) { deleteThese.Add(alliance); AlliancePlugin.Log.Info("Deleting " + alliance.name); } else { AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, Upgrades disabled.", true, 0); AlliancePlugin.Log.Info("failed upkeep " + String.Format("{0:n0}", alliance.GetUpkeep()) + " SC. for " + alliance.name); } } } } } foreach (Alliance alliance in deleteThese) { AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, met the threshold for delete. Deleting Alliance.", true, 0); AlliancePlugin.AllAlliances.Remove(alliance.name); File.Delete(AlliancePlugin.path + "//AllianceData//" + alliance.AllianceId + ".json"); foreach (long id in alliance.AllianceMembers) { AlliancePlugin.FactionsInAlliances.Remove(id); } } foreach (Alliance alliance in saveThese) { AlliancePlugin.SaveAllianceData(alliance); } } catch (Exception ex) { AlliancePlugin.Log.Error("Error for all upkeep"); AlliancePlugin.Log.Error(ex); return; } }