private async Task <bool> isWin(long game, Summoner tocheck) { await CommandHandlingService.Logger(new LogMessage(LogSeverity.Debug, "isWin Check", $"GameID:{game}, PlayerACC:{tocheck.AccountId}, Name:{tocheck.Name}")); Match match = await _rapi.RAPI.MatchV4.GetMatchAsync(Region.NA, game); int playerId = -1; int playerTeam = -1; int playerTeamId = -1; foreach (var part in match.ParticipantIdentities) { if (part.Player.CurrentAccountId == tocheck.AccountId) { playerId = part.ParticipantId - 1; playerTeamId = match.Participants[playerId].TeamId; playerTeam = (playerTeamId == 100) ? 0 : 1; break; } } if (match.Teams[playerTeam].TeamId != playerTeamId) { throw new IndexOutOfRangeException($"TeamId found:{playerTeamId}, Team index made:{playerTeam}, TeamId in index reached:{match.Teams[playerTeam].TeamId}"); } //await CommandHandlingService.Logger(new LogMessage(LogSeverity.Debug, "isWinCheck", $"{match.Teams[playerTeam].Win == "Win"}")); return(match.Teams[playerTeam].Win == "Win"); }
public async Task MainAsync() { using (StreamReader tfile = File.OpenText("config.json")){ dynamic config = JsonConvert.DeserializeObject(tfile.ReadToEnd()); char tempPrefix = config.prefix ?? '`'; int tempLevel = config.logLevel ?? 3; CLIENT_ID = config.clientId ?? "NONE"; CLIENT_SECRET = config.clientSecret ?? "NONE"; RKEY = config.riotKey ?? throw (new ArgumentNullException("No Riot API Key (riotKey in config file) given!")); BOT_TOKEN = config.botToken ?? throw (new ArgumentNullException("No Discord Bot token (botKey in config file) given!")); CommandHandlingService.setLog(tempLevel); CommandHandlingService.setPrefix(tempPrefix); } await CommandHandlingService.Logger(new LogMessage(LogSeverity.Info, "Config", $"Prefix set to:'{CommandHandlingService.Prefix}'")); using (var services = ConfigServices()){ main_client = services.GetRequiredService <DiscordSocketClient>(); services.GetRequiredService <CommandService>().Log += Logger; rapi = services.GetRequiredService <RapiInfo>(); main_client.Log += Logger; await main_client.LoginAsync(TokenType.Bot, BOT_TOKEN); await main_client.StartAsync(); await services.GetRequiredService <Services.CommandHandlingService>().InitializeAsync(); await Task.Delay(-1); } }
public async Task wr(params string[] names) { if (names.Length > _rapi.maxSearchWinrateNames) { await Context.User.SendMessageAsync($"Too many names entered! Limit: {_rapi.maxSearchWinrateNames}"); } else { await Context.Channel.TriggerTypingAsync(); List <EmbedFieldBuilder> content = new List <EmbedFieldBuilder>(); Summoner topSumm = null; int[] qs = { 420, 440 }; foreach (string target in names) { EmbedFieldBuilder field = new EmbedFieldBuilder(); field.Name = target; Summoner tofind = null; Matchlist mlist = null; double wins = 0, losses = 0, wr; try{ tofind = await _rapi.RAPI.SummonerV4.GetBySummonerNameAsync(Region.NA, target) ?? throw new InvalidDataException(); mlist = await _rapi.RAPI.MatchV4.GetMatchlistAsync(Region.NA, tofind.AccountId, queue : qs, endIndex : 20); if (mlist.TotalGames == 0) { throw new InvalidOperationException(); } topSumm = topSumm ?? tofind; foreach (MatchReference mref in mlist.Matches) { await CommandHandlingService.Logger(new LogMessage(LogSeverity.Debug, "winrate", $"mref: {(mref != null)}")); if (await isWin(mref.GameId, tofind)) { wins++; } else { losses++; } } wr = wins / (wins + losses); field.Value = string.Format("WR: {0:P}\nWins: {1}\nLosses:{2}\n", wr, wins, losses); } catch (InvalidDataException) { field.Value = "Does not exist"; } catch (InvalidOperationException) { field.Value = "No ranked games on record"; } catch (NullReferenceException nre) { await CommandHandlingService.Logger(new LogMessage(LogSeverity.Debug, "winrate", "Poorly Handled Exception", nre)); field.Value = "Error"; topSumm = tofind ?? tofind ?? topSumm; } content.Add(field); } await CommandHandlingService.Logger(new LogMessage(LogSeverity.Debug, "winrate command", "Building embedded message...")); EmbedBuilder embeddedMessage = new EmbedBuilder(); embeddedMessage.WithThumbnailUrl($"http://ddragon.leagueoflegends.com/cdn/{_rapi.patchNum}/img/profileicon/{(topSumm != null ? topSumm.ProfileIconId : 501)}.png"); embeddedMessage.WithColor(0xff69b4); embeddedMessage.WithTitle("Last Twenty All Ranked Queues"); embeddedMessage.WithColor(0xff69b4); embeddedMessage.WithFields(content); embeddedMessage.WithCurrentTimestamp(); embeddedMessage.WithFooter(new EmbedFooterBuilder().WithText("As of")); await ReplyAsync("", embed : embeddedMessage.Build()); } }