private async Task <bool> IsNationRecruitableAsync(Nation nation, EventId id) { if (nation != null) { var criteriaFit = await DoesNationFitCriteriaAsync(nation.Name); if (criteriaFit) { var apiResponse = await WouldReceiveTelegramAsync(nation, id); if (apiResponse == 0) { _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{nation.Name} - No receive.")); } else if (apiResponse == 1) { return(true); } else { _logger.LogWarning(id, LogMessageBuilder.Build(id, $"Recruitable nation check: {nation.Name} failed.")); await NationManager.SetNationStatusToAsync(nation, "failed"); return(false); } } else { _logger.LogDebug(id, LogMessageBuilder.Build(id, $"{nation.Name} does not fit criteria and is therefore skipped.")); } } await NationManager.SetNationStatusToAsync(nation, "skipped"); return(false); }
private async Task EnsurePoolFilledAsync() { var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.EnsurePoolFilled); List <REGION> regionsToRecruitFrom = await GetRegionToRecruitFromAsync(id); while (IsRecruiting) { bool fillingUp = false; int counter = 0; int pendingCount = NationManager.GetNationCountByStatusName("pending"); while (pendingCount < _config.MinimumRecruitmentPoolSize) { if (!fillingUp) { fillingUp = true; _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Filling up pending pool now from {pendingCount} to {_config.MinimumRecruitmentPoolSize}")); } PoolStatus = "Filling up with random nations"; var regionId = _rnd.Next(regionsToRecruitFrom.Count); var region = regionsToRecruitFrom.ElementAt(regionId); string nationName; do { var nationId = _rnd.Next(region.NATIONNAMES.Count); nationName = region.NATIONNAMES.ElementAt(nationId); }while (await NationManager.IsNationPendingSkippedSendOrFailedAsync(nationName) || await IsNationRecruitableAsync(new Nation() { Name = nationName, StatusTime = DateTime.UtcNow }, id)); var nation = await NationManager.GetNationAsync(nationName); if (nation != null) { await NationManager.SetNationStatusToAsync(nation, "pending"); } else { await NationManager.AddUnknownNationsAsPendingAsync(new List <string>() { nationName }, true); } counter++; pendingCount = NationManager.GetNationCountByStatusName("pending"); _logger.LogDebug(id, LogMessageBuilder.Build(id, $"Added nation '{nationName}' to pending. Now at {pendingCount} from minimum {_config.MinimumRecruitmentPoolSize}.")); } if (fillingUp) { _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Filled up pending pool to minimum. (Added {counter} nations to pending.)")); PoolStatus = "Waiting for new nations"; } await Task.Delay(1800000); //30 min } }
private async Task <bool> IsNationRecruitableAsync(Nation nation, EventId id) { if (nation != null) { var result = await IsNationRecruitableAsync(nation.Name, id); if (!result) { await NationManager.SetNationStatusToAsync(nation, "skipped"); } return(result); } return(false); }
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."); } }
public async Task DoGetRecruitableNationsAsync([Remainder, Summary("Number of nations to be returned")] int number) { //await ReplyAsync("The /rn command needed to be disabled because of major issues with the manual recruitment system. Drehtisch will fix those issue asap. Sorry :("); Console.ResetColor(); var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.RNCommand); List <Nation> returnNations = new List <Nation>(); try { if (await _permManager.IsAllowedAsync(PermissionType.ManageRecruitment, Context.User)) { if (!_recruitmentService.IsReceivingRecruitableNations) { try { if (number <= 120) { var currentRN = new RNStatus { IssuedBy = Context.User.Username, FinalCount = number, StartedAt = DateTimeOffset.UtcNow, AvgTimePerFoundNation = TimeSpan.FromSeconds(2) }; var channel = await Context.User.GetOrCreateDMChannelAsync(); _recruitmentService.StartReceiveRecruitableNations(currentRN); await ReplyAsync($"{_actionQueued}{Environment.NewLine}{Environment.NewLine}You can request the status of this command using /rns. Finish expected in approx. (mm:ss): {currentRN.ExpectedIn():mm\\:ss}"); StringBuilder builder = new StringBuilder(); var counter = 0; await foreach (var nation in _recruitmentService.GetRecruitableNationsAsync(number, false, id)) { counter++; builder.Append($"{nation.Name}, "); if (counter % 8 == 0) { await channel.SendMessageAsync(builder.ToString()); builder.Clear(); _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Dispatched {counter}/{number} nations to {Context.User.Username}")); } await NationManager.SetNationStatusToAsync(nation, "reserved_manual"); } if (builder.Length > 0) { await channel.SendMessageAsync(builder.ToString()); _logger.LogInformation(id, LogMessageBuilder.Build(id, $"Dispatched {counter}/{number} nations to {Context.User.Username}")); } if (counter < number) { await ReplyAsync($"Something went wrong didn't received as much nations as requested."); } } else { await ReplyAsync($"{number} exceeds the maximum of 120 Nations (15 Telegrams a 8 recipients) to be returned."); } } finally { _recruitmentService.StopReceiveRecruitableNations(); } } else { await ReplyAsync($"There is already a /rn command running. Try again later."); } } else { await ReplyAsync(AppSettings._permissionDeniedResponse); } } catch (Exception ex) { _logger.LogCritical(id, ex, LogMessageBuilder.Build(id, "A critical error occured")); await ReplyAsync($"Something went wrong. Sorry :( "); } }
public async Task DoGetRecruitableNations([Remainder, Summary("Number of nations to be returned")] int number) { var id = LogEventIdProvider.GetEventIdByType(LoggingEvent.RNCommand); List <Nation> returnNations = new List <Nation>(); try { if (await _permManager.IsAllowedAsync(PermissionType.ManageRecruitment, Context.User)) { if (!_recruitmentService.IsReceivingRecruitableNations) { if (number <= 120) { var currentRN = new RNStatus { IssuedBy = Context.User.Username, FinalCount = number, StartedAt = DateTimeOffset.UtcNow, AvgTimePerFoundNation = TimeSpan.FromSeconds(2) }; _recruitmentService.StartReceiveRecruitableNations(currentRN); await ReplyAsync($"{actionQueued}{Environment.NewLine}{Environment.NewLine}You can request the status of this command using /rns. Finish expected in approx. (mm:ss): {currentRN.ExpectedIn().ToString(@"mm\:ss")}"); _logger.LogInformation(id, LogMessageBuilder.Build(id, $"{number} recruitable nations requested.")); returnNations = await _recruitmentService.GetRecruitableNationsAsync(number, false); foreach (var nation in returnNations) { await NationManager.SetNationStatusToAsync(nation, "reserved_manual"); } StringBuilder builder = new StringBuilder(); builder.AppendLine("-----"); var firstReplyStart = $"<@{Context.User.Id}> Your action just finished.{Environment.NewLine}Changed status of {returnNations.Count} nations from 'pending' to 'reserved_manual'.{Environment.NewLine}Recruitable Nations are (each segment for 1 telegram):{Environment.NewLine}"; int replyCount = (number / 40) + (number % 40 != 0 ? 1 : 0); int currentReply = 1; for (int i = 1; i <= returnNations.Count; i++) { var nation = returnNations[i - 1]; if (i % 8 == 0) { builder.AppendLine($"{nation.Name}"); builder.AppendLine("-----"); } else { builder.Append($"{nation.Name}, "); } if (i % 40 == 0) { if (i / 40 == 1) { await ReplyAsync($"{firstReplyStart} Reply {currentReply}/{replyCount}{Environment.NewLine}{builder.ToString()}"); } else { await ReplyAsync($"Reply {currentReply}/{replyCount}{Environment.NewLine}{builder.ToString()}"); } builder.Clear(); currentReply++; } } if (returnNations.Count < 40) { await ReplyAsync($"{firstReplyStart}{builder.ToString()}"); } else { if (number % 40 != 0) { await ReplyAsync($"Reply {currentReply}/{replyCount}{Environment.NewLine}{builder.ToString()}"); } } if (returnNations.Count < number) { await ReplyAsync($"{Environment.NewLine}- - - - -{Environment.NewLine}WARNING: No more nations in pending nations pool."); } } else { await ReplyAsync($"{number} exceeds the maximum of 120 Nations (15 Telegrams a 8 recipients) to be returned."); } } else { await ReplyAsync($"There is already a /rn command running. Try again later."); } } else { await ReplyAsync(AppSettings.PERMISSION_DENIED_RESPONSE); } } catch (Exception ex) { _logger.LogCritical(id, ex, LogMessageBuilder.Build(id, "An critical error occured")); await ReplyAsync($"Something went wrong :( "); foreach (var nation in returnNations) { await NationManager.SetNationStatusToAsync(nation, "pending"); } } finally { _recruitmentService.StopReceiveRecruitableNations(); } }