private void RStatDbUpdate() { ApiSent = NationManager.GetNationsByStatusName("send").Count; ApiPending = NationManager.GetNationsByStatusName("pending").Count; ApiSkipped = NationManager.GetNationsByStatusName("skipped").Count; ApiFailed = NationManager.GetNationsByStatusName("failed").Count; }
public async Task UpdateRecruitmentStatsAsync() { try { _logger.LogInformation(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, "Updating Recruitment Stats")); var today = DateTime.Today.Date; var sent = NationManager.GetNationsByStatusName("send").Select(n => n.Name).ToList(); var manual = NationManager.GetNationsByStatusName("reserved_manual").Select(n => n.Name).ToList(); var region = await _dumpDataService.GetRegionAsync(BaseApiService.ToID(_config.NationStatesRegionName)); var apiRecruited = region.NATIONS.Where(n => sent.Any(s => n.NAME == s)).Select(n => n.NAME).ToList(); var manualRecruited = region.NATIONS.Where(n => manual.Any(m => n.NAME == m)).Select(n => n.NAME).ToList(); RStatDbUpdate(); ApiRecruited = apiRecruited.Count; ApiRatio = Math.Round((100 * ApiRecruited / (sent.Count + ApiFailed + 0.0)), 2); ManualReserved = manual.Count; ManualRecruited = manualRecruited.Count; ManualRatio = Math.Round((100 * ManualRecruited / (manual.Count + 0.0)), 2); _logger.LogInformation(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, "Recruitment Stats Updated")); } catch (Exception ex) { _logger.LogCritical(_defaulEventId, ex, LogMessageBuilder.Build(_defaulEventId, "A critical error occured.")); } }
public async Task <List <Nation> > GetRecruitableNationsAsync(int number, bool isAPI) { List <Nation> returnNations = new List <Nation>(); var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.GetRecruitableNations); try { _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{number} recruitable nations requested")); List <Nation> pendingNations = new List <Nation>(); if (pendingNations.Count == 0) { pendingNations = NationManager.GetNationsByStatusName("pending"); } while (returnNations.Count < number) { var picked = pendingNations.Take(1); var nation = picked.Count() > 0 ? picked.ToArray()[0] : null; if (nation != null) { if (await IsNationRecruitableAsync(nation, id)) { returnNations.Add(nation); if (IsReceivingRecruitableNations && !isAPI) { currentRNStatus.CurrentCount++; } } pendingNations.Remove(nation); returnNations = returnNations.Distinct().ToList(); } else { if (pendingNations.Count == 0) { _logger.LogCritical(id, "No more nations in pending pool !"); return(returnNations); } else { _logger.LogCritical(id, "Picked nation was null !"); } } } } catch (Exception ex) { _logger.LogError(id, ex, LogMessageBuilder.Build(id, "An error occured.")); } return(returnNations); }
private async Task RecruitAsync() { List <Nation> pendingNations = new List <Nation>(); while (IsRecruiting && _config.EnableRecruitment) { try { if (pendingNations.Count == 0) { if (NationManager.GetNationCountByStatusName("pending") == 0) { _logger.LogWarning("Delaying API recruitment for 15 minutes due to lack of recruitable nations"); RecruitmentStatus = "Throttled: lack of nations"; await Task.Delay(900000); } pendingNations = NationManager.GetNationsByStatusName("reserved_api"); if (pendingNations.Count < 10) { var numberToRequest = 10 - pendingNations.Count; await foreach (var resNation in GetRecruitableNationsAsync(numberToRequest, true, _defaulEventId)) { pendingNations.Add(resNation); } if (pendingNations.Count < numberToRequest) { RecruitmentStatus = "Throttled: lack of of nations"; _logger.LogWarning("Didn't received enough recruitable nations"); } else { RecruitmentStatus = "Fully operational"; } foreach (var pendingNation in pendingNations) { await NationManager.SetNationStatusToAsync(pendingNation, "reserved_api"); } } } var picked = pendingNations.Take(1); var nation = picked.Count() > 0 ? picked.ToArray()[0] : null; if (nation != null) { if (await _apiService.IsNationStatesApiActionReadyAsync(NationStatesApiRequestType.SendRecruitmentTelegram, true)) { if (await IsNationRecruitableAsync(nation, _defaulEventId)) { if (await _apiService.SendRecruitmentTelegramAsync(nation.Name)) { await NationManager.SetNationStatusToAsync(nation, "send"); _logger.LogInformation(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, $"Telegram to {nation.Name} queued successfully.")); } else { _logger.LogWarning(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, $"Sending of a Telegram to {nation.Name} failed.")); await NationManager.SetNationStatusToAsync(nation, "failed"); } } pendingNations.Remove(nation); } } else { _logger.LogCritical(_defaulEventId, LogMessageBuilder.Build(_defaulEventId, "No nation to recruit found.")); } } catch (Exception ex) { _logger.LogError(_defaulEventId, ex, LogMessageBuilder.Build(_defaulEventId, "An error occured.")); } await Task.Delay(60000); } if (!_config.EnableRecruitment) { _logger.LogWarning(_defaulEventId, "Recruitment disabled."); } }
private async void UpdateRecruitmentStatsAsync() { while (IsRecruiting) { try { _logger.LogInformation(defaulEventId, LogMessageBuilder.Build(defaulEventId, "Updating Recruitment Stats")); var today = DateTime.Today.Date; var sent = NationManager.GetNationsByStatusName("send").Select(n => n.Name).ToList(); var manual = NationManager.GetNationsByStatusName("reserved_manual").Select(n => n.Name).ToList(); var region = await _dumpDataService.GetRegionAsync(BaseApiService.ToID(_config.NationStatesRegionName)); var apiRecruited = region.NATIONS.Where(n => sent.Any(s => n.NAME == s)).Select(n => n.NAME).ToList(); var manualRecruited = region.NATIONS.Where(n => manual.Any(m => n.NAME == m)).Select(n => n.NAME).ToList(); RStatDbUpdate(); ApiRecruited = apiRecruited.Count; ApiRatio = Math.Round((100 * ApiRecruited / (sent.Count + ApiFailed + 0.0)), 2); ManualReserved = manual.Count; ManualRecruited = manualRecruited.Count; ManualRatio = Math.Round((100 * ManualRecruited / (manual.Count + 0.0)), 2); await WriteRecruited(today, apiRecruited, manualRecruited.AsQueryable()); var rt = await GetRecruitedOn(today); RecruitedTodayA = rt[0]; RecruitedTodayM = rt[1]; var ry = await GetRecruitedOn(today.AddDays(-1)); RecruitedYesterdayA = ry[0]; RecruitedYesterdayM = ry[1]; var lastMonday = (today - new TimeSpan((int)today.DayOfWeek, 0, 0, 0)).AddDays(1); var rlw = await GetRecruitedBetween(lastMonday - TimeSpan.FromDays(7), lastMonday); RecruitedLastWeekA = rlw[0]; RecruitedLastWeekM = rlw[1]; RecruitedLastWeekAvgDA = Math.Round(RecruitedLastWeekA / 7.0, 2); RecruitedLastWeekAvgDM = Math.Round(RecruitedLastWeekM / 7.0, 2); var firstDayLastMonth = new DateTime(today.Year, today.Month, 1).AddMonths(-1); var daysInLastMonth = DateTime.DaysInMonth(firstDayLastMonth.Year, firstDayLastMonth.Month); var rlm = await GetRecruitedBetween(firstDayLastMonth, firstDayLastMonth.AddMonths(1).AddDays(-1)); RecruitedLastMonthA = rlm[0]; RecruitedLastMonthM = rlm[1]; RecruitedLastMonthAvgDA = Math.Round(RecruitedLastMonthA / (daysInLastMonth + 0.0), 2); RecruitedLastMonthAvgDM = Math.Round(RecruitedLastMonthM / (daysInLastMonth + 0.0), 2); _logger.LogInformation(defaulEventId, LogMessageBuilder.Build(defaulEventId, "Recruitment Stats Updated")); } catch (Exception ex) { _logger.LogCritical(defaulEventId, ex, LogMessageBuilder.Build(defaulEventId, "A critical error occured.")); } await Task.Delay(TimeSpan.FromHours(4)); } }