public CalculatedChampionStat(ChampionStat stats) { CalculateTotalMinionsKills_byGame(stats); CalculateKda(stats); CalculateGames(stats); CalculateWinRate(stats); }
private void CalculateTotalMinionsKills_byGame(ChampionStat stats) { if (stats == null || stats.TotalSessionsPlayed == 0) { this.TotalMinionKills_byGame = 0; } else { this.TotalMinionKills_byGame = stats.TotalMinionKills / stats.TotalSessionsPlayed; } }
private void CalculateKda(ChampionStat stats) { if (stats == null || stats.TotalSessionsPlayed == 0) { this.KDA = 0; } else { if (stats.TotalDeathsPerSession == 0) { stats.TotalDeathsPerSession = 1; } this.KDA = Decimal.Round((decimal)(stats.TotalChampionKills + stats.TotalAssists) / stats.TotalDeathsPerSession, 2); } }
private static void SampleChampionStats() { Dictionary <string, Dictionary <int, ChampionStat> > stats = new Dictionary <string, Dictionary <int, ChampionStat> >(); Dictionary <string, Dictionary <string, ChampionStat> > rolestats = new Dictionary <string, Dictionary <string, ChampionStat> >(); // add an all region for web presentation of championstats var all = new Dictionary <int, ChampionStat>(); int allcount = 0; stats.Add("all", all); // add an all region for web presentation of rolestats var allrole = new Dictionary <string, ChampionStat>(); rolestats.Add("all", allrole); // each key a region foreach (var key in match_data.Keys) { // increase allcount allcount += match_data[key].Count; var current = new Dictionary <int, ChampionStat>(); stats.Add(key, current); var currentrole = new Dictionary <string, ChampionStat>(); rolestats.Add(key, currentrole); // go through matches foreach (var match in match_data[key].Values) { List <MercTeam> teams = new List <MercTeam>(); teams.Add(match.TeamBlue); teams.Add(match.TeamRed); // work through both teams foreach (var team in teams) { // and all players foreach (var champ in team.Participants) { ChampionStat ctl, cta; // add champion to dictionary if not present if (!current.ContainsKey(champ.ChampionId)) { ctl = new ChampionStat { Id = champ.ChampionId }; current.Add(champ.ChampionId, ctl); } else { ctl = current[champ.ChampionId]; } // add champion to dictionary if not present - for all if (!all.ContainsKey(champ.ChampionId)) { cta = new ChampionStat { Id = champ.ChampionId }; all.Add(champ.ChampionId, cta); } else { cta = all[champ.ChampionId]; } // ctl = current champion // cta = current champion - for all if (team.Winner) { ctl.Wincount++; cta.Wincount++; } ctl.Pickcount++; cta.Pickcount++; // count stats (kills, assists, etc) ctl.KillCount += champ.Kills; ctl.AssistCount += champ.Assists; ctl.DeathCount += champ.Deaths; ctl.MinionCount += champ.MinionsKilled; ctl.JungleCount += champ.NeutralMinionsKilled; ctl.HostileJungleCount += champ.NeutralMinionsKilledEnemyJungle; ctl.AlliedJungleCount += champ.NeutralMinionsKilledTeamJungle; ctl.PentaKillCount += champ.PentaKills; // count stats (kills, assists, etc) - for all again cta.KillCount += champ.Kills; cta.AssistCount += champ.Assists; cta.DeathCount += champ.Deaths; cta.MinionCount += champ.MinionsKilled; cta.JungleCount += champ.NeutralMinionsKilled; cta.HostileJungleCount += champ.NeutralMinionsKilledEnemyJungle; cta.AlliedJungleCount += champ.NeutralMinionsKilledTeamJungle; cta.PentaKillCount += champ.PentaKills; // get default role for champ var champDef = Champions.Values.First(x => x.Id == champ.ChampionId.ToString()); ChampionStat rtl, rta; // add role to dicitonary if not exists if (!currentrole.ContainsKey(champDef.Tags[0])) { rtl = new ChampionStat { Role = champDef.Tags[0] }; currentrole.Add(champDef.Tags[0], rtl); } else { rtl = currentrole[champDef.Tags[0]]; } // add role to dicitonary if not exists - for all if (!allrole.ContainsKey(champDef.Tags[0])) { rta = new ChampionStat { Role = champDef.Tags[0] }; allrole.Add(champDef.Tags[0], rta); } else { rta = allrole[champDef.Tags[0]]; } // count win and pick if (team.Winner) { rtl.Wincount++; rta.Wincount++; } rtl.Pickcount++; rta.Pickcount++; // count role stats like kill/assists/etc rtl.KillCount += champ.Kills; rtl.AssistCount += champ.Assists; rtl.DeathCount += champ.Deaths; rtl.MinionCount += champ.MinionsKilled; rtl.JungleCount += champ.NeutralMinionsKilled; rtl.HostileJungleCount += champ.NeutralMinionsKilledEnemyJungle; rtl.AlliedJungleCount += champ.NeutralMinionsKilledTeamJungle; rtl.PentaKillCount += champ.PentaKills; // count role stats like kill/assists/etc - all rta.KillCount += champ.Kills; rta.AssistCount += champ.Assists; rta.DeathCount += champ.Deaths; rta.MinionCount += champ.MinionsKilled; rta.JungleCount += champ.NeutralMinionsKilled; rta.HostileJungleCount += champ.NeutralMinionsKilledEnemyJungle; rta.AlliedJungleCount += champ.NeutralMinionsKilledTeamJungle; rta.PentaKillCount += champ.PentaKills; } } } } // each key a region foreach (var key in stats.Keys) { // for all champs foreach (var champ in stats[key]) { champ.Value.Winrate = (float)champ.Value.Wincount / champ.Value.Pickcount; if (key != "all") { champ.Value.Pickrate = (float)champ.Value.Pickcount / match_data[key].Count * 20; } else { champ.Value.Pickrate = (float)champ.Value.Pickcount / allcount * 20; } champ.Value.AvgKillCount = (float)champ.Value.KillCount / champ.Value.Pickcount; champ.Value.AvgAssistCount = (float)champ.Value.AssistCount / champ.Value.Pickcount; champ.Value.AvgDeathCount = (float)champ.Value.DeathCount / champ.Value.Pickcount; champ.Value.AvgPentaKillCount = (float)champ.Value.PentaKillCount / champ.Value.Pickcount; champ.Value.AvgMinionCount = (float)champ.Value.MinionCount / champ.Value.Pickcount; champ.Value.AvgJungleCount = (float)champ.Value.JungleCount / champ.Value.Pickcount; champ.Value.AvgAlliedJungleCount = (float)champ.Value.AlliedJungleCount / champ.Value.Pickcount; champ.Value.AvgHostileJungleCount = (float)champ.Value.HostileJungleCount / champ.Value.Pickcount; } } // each key a region foreach (var key in rolestats.Keys) { // for all roles foreach (var champ in rolestats[key]) { champ.Value.Winrate = (float)champ.Value.Wincount / champ.Value.Pickcount; if (key != "all") { champ.Value.Pickrate = (float)champ.Value.Pickcount / match_data[key].Count * 10; } else { champ.Value.Pickrate = (float)champ.Value.Pickcount / allcount * 10; } champ.Value.AvgKillCount = (float)champ.Value.KillCount / champ.Value.Pickcount; champ.Value.AvgAssistCount = (float)champ.Value.AssistCount / champ.Value.Pickcount; champ.Value.AvgDeathCount = (float)champ.Value.DeathCount / champ.Value.Pickcount; champ.Value.AvgPentaKillCount = (float)champ.Value.PentaKillCount / champ.Value.Pickcount; champ.Value.AvgMinionCount = (float)champ.Value.MinionCount / champ.Value.Pickcount; champ.Value.AvgJungleCount = (float)champ.Value.JungleCount / champ.Value.Pickcount; champ.Value.AvgAlliedJungleCount = (float)champ.Value.AlliedJungleCount / champ.Value.Pickcount; champ.Value.AvgHostileJungleCount = (float)champ.Value.HostileJungleCount / champ.Value.Pickcount; } } // safe champion stats to file for each region foreach (var key in stats.Keys) { // like ko\Champion.proto string path = Path.Combine(sample_path, key, "Champion.proto"); if (!Directory.Exists(Path.Combine(sample_path, key))) { Directory.CreateDirectory(Path.Combine(sample_path, key)); } using (var s = new FileStream(path, FileMode.Create)) ProtoBuf.Serializer.Serialize <Dictionary <int, ChampionStat> >(s, stats[key]); } // safe role stats to file for each region foreach (var key in rolestats.Keys) { // like ko\Role.proto string path = Path.Combine(sample_path, key, "Role.proto"); if (!Directory.Exists(Path.Combine(sample_path, key))) { Directory.CreateDirectory(Path.Combine(sample_path, key)); } using (var s = new FileStream(path, FileMode.Create)) ProtoBuf.Serializer.Serialize <Dictionary <string, ChampionStat> >(s, rolestats[key]); } }
public List <Player> GetPlayers(List <Team> teams) { List <Player> players = new List <Player>(); ChampionStats = new Dictionary <string, List <ChampionStat> >(); foreach (var playerLink in PlayerLinks) { try { var responseString = client.GetStringAsync(playerLink.Uri).Result; if (responseString == null) { continue; } HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(responseString); var playerName = doc.DocumentNode.SelectSingleNode("//h1[contains(@class,'panel-title')]")?.InnerText; if (playerName == null) { continue; } Player player = new Player() { SportId = 1, Nickname = playerName, }; foreach (var pl in players) { if (pl.Nickname == player.Nickname) { continue; } } foreach (var team in teams) { if (team.Name == playerLink.AdditionalData) { player.TeamId = team.Id; break; } } if (player.TeamId == null) { continue; } var tasks = new List <Task>(); var tables = doc.DocumentNode.SelectNodes("//table[contains(@class,'table_list')]"); if (tables == null) { continue; } foreach (var table in tables) { try { if (table.InnerText.Contains("General stats")) { var columns = table.SelectNodes(".//td"); tasks.Add(Task.Factory.StartNew(() => { for (int i = 0; i < columns.Count - 1; i += 2) { if (columns[i].InnerText.Contains("Record") && columns[i + 1].InnerText != "-") { var winrateArr = columns[i + 1].InnerText.Replace("W", "").Replace("L", "").Split(" - "); var wins = int.Parse(winrateArr[0].Trim()); var losses = int.Parse(winrateArr[1].Trim()); player.Wins = wins; player.Losses = losses; continue; } if (columns[i].InnerText.Contains("KDA") && columns[i + 1].InnerText != "-") { try { player.KDA = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); } catch { } continue; } if (columns[i].InnerText.Contains("CS per Minute") && columns[i + 1].InnerText != "-") { player.CSPerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("Gold Per Minute") && columns[i + 1].InnerText != "-") { player.GoldPerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("Gold%") && columns[i + 1].InnerText != "-") { player.GoldPercent = float.Parse(columns[i + 1].InnerText.Replace(".", ",").Replace("%", "").Trim()); continue; } if (columns[i].InnerText.Contains("Kill Participation") && columns[i + 1].InnerText != "-") { player.KillParticipation = float.Parse(columns[i + 1].InnerText.Replace(".", ",").Replace("%", "").Trim()); continue; } } })); } if (table.InnerText.Contains("Early game")) { var columns = table.SelectNodes(".//td"); tasks.Add(Task.Factory.StartNew(() => { for (int i = 0; i < columns.Count - 1; i += 2) { if (columns[i].InnerText.Contains("Ahead in CS at 15 min") && columns[i + 1].InnerText != "-") { var percentString = columns[i + 1].InnerText.Replace("%", "").Replace(".", ",").Trim().Replace(" \n", "").Trim(); player.AheadInCSAt15Percent = float.Parse(percentString); continue; } if (columns[i].InnerText.Contains("CS Differential at 15 min") && columns[i + 1].InnerText != "-") { player.CSDifferenceAt15 = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("Gold Differential at 15 min") && columns[i + 1].InnerText != "-") { player.GoldDifferenceAt15 = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("XP Differential at 15 min") && columns[i + 1].InnerText != "-") { player.XPDifferenceAt15 = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("First Blood Participation") && columns[i + 1].InnerText != "-") { player.FirstBloodParticipationPercent = float.Parse(columns[i + 1].InnerText.Replace(".", ",").Replace("%", "").Trim()); continue; } if (columns[i].InnerText.Contains("First Blood Victim") && columns[i + 1].InnerText != "-") { player.FirstBloodVictimPercent = float.Parse(columns[i + 1].InnerText.Replace(".", ",").Replace("%", "").Trim()); continue; } } })); } if (table.InnerText.Contains("Aggression")) { var columns = table.SelectNodes(".//td"); tasks.Add(Task.Factory.StartNew(() => { for (int i = 0; i < columns.Count - 1; i += 2) { if (columns[i].InnerText.Contains("Damage Per Minute") && columns[i + 1].InnerText != "-") { player.DamagePerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("Damage%") && columns[i + 1].InnerText != "-") { player.DamagePercent = float.Parse(columns[i + 1].InnerText.Replace(".", ",").Replace("%", "").Trim()); continue; } if (columns[i].InnerText.Contains("K+A Per Minute") && columns[i + 1].InnerText != "-") { player.KillsAndAssistsPerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("Solo kills")) { if (columns[i + 1].InnerText != "-") { player.SoloKills = int.Parse(columns[i + 1].InnerText); } else { player.SoloKills = 0; } continue; } if (columns[i].InnerText.Contains("Pentakills") && columns[i + 1].InnerText != "-") { player.Pentakills = int.Parse(columns[i + 1].InnerText); continue; } } })); } if (table.InnerText.Contains("Vision")) { var columns = table.SelectNodes(".//td"); tasks.Add(Task.Factory.StartNew(() => { for (int i = 0; i < columns.Count - 1; i += 2) { if (columns[i].InnerText.Contains("Vision score Per Minute") && columns[i + 1].InnerText != "-") { player.VisionScorePerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Equals("Ward Per Minute: ") && columns[i + 1].InnerText != "-") { player.WardPerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("Vision Ward Per Minute") && columns[i + 1].InnerText != "-") { player.VisionWardsPerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } if (columns[i].InnerText.Contains("Ward Cleared Per Minute") && columns[i + 1].InnerText != "-") { player.WardsClearedPerMinute = float.Parse(columns[i + 1].InnerText.Replace(".", ",")); continue; } } })); } if (table.InnerText.Contains("Champion") && !table.InnerText.Contains("Score")) { var columns = table.SelectNodes(".//td"); tasks.Add(Task.Factory.StartNew(() => { for (int i = 0; i < columns.Count - 5; i += 4) { ChampionStat stat = new ChampionStat(); stat.ChampionName = columns[i].InnerText; stat.GamesPlayed = int.Parse(columns[i + 1].InnerText); stat.WinratePercent = float.Parse(columns[i + 2].InnerText.Replace(".", ",").Replace("%", "").Replace(" \n", "").Trim()); stat.WinratePercent = Math.Round(stat.WinratePercent, 2); if (columns[i + 3].InnerText == "-") { continue; } stat.KDA = float.Parse(columns[i + 3].InnerText.Replace(".", ",").Trim()); stat.KDA = Math.Round(stat.KDA, 2); if (ChampionStats.ContainsKey(player.Nickname)) { ChampionStats[player.Nickname].Add(stat); } else { ChampionStats.Add(player.Nickname, new List <ChampionStat>() { stat }); } } })); } } catch (Exception ex) { continue; } } Task.WaitAll(tasks.ToArray()); players.Add(player); } catch (Exception ex) { var dt = DateTime.UtcNow; while (dt.AddSeconds(10) > DateTime.UtcNow) { } } } return(players); }
private void CalculateGames(ChampionStat stats) { this.Games = stats == null ? 0 : stats.TotalSessionsPlayed; }
private void CalculateWinRate(ChampionStat stats) { this.WinRate = stats == null ? 0 : Math.Round(stats.TotalSessionsWon / (decimal)stats.TotalSessionsPlayed, 2); }