public string LastRank(string accountId, string server) { MatchList matchList = _client.GetMatchListByAccountIdAsync(accountId, null, null, null, null, null, null, null, server).Result; if (matchList == null) { return(null); } Match match = _client.GetMatchAsync(matchList.Matches[0].GameId, server).Result; if (match == null) { return(null); } int partId = -1; foreach (MatchParticipantIdentity identity in match.ParticipantIdentities) { if (identity.Player.AccountId == accountId) { partId = identity.ParticipantId; } } foreach (MatchParticipant participant in match.Participants) { if (participant.ParticipantId == partId) { return(participant.HighestAchievedSeasonTier); } } return(null); }
private async void StoreMatches() { _client = new RiotClient(RiotNet.Models.Region.EUW); string filePath = Path.GetFullPath(@"=****;"); List<long> MatchIDs; using (StreamReader sr = new StreamReader(filePath)) { MatchIDs = JsonConvert.DeserializeObject<List<long>>(sr.ReadToEnd()); } MySqlConnection conn = null; string cs = @"server=****;userid==****;password==****;database==****;"; conn = new MySqlConnection(cs); try { conn.Open(); foreach (long id in MatchIDs) { Console.WriteLine(id); RiotNet.Models.MatchDetail Match = await _client.GetMatchAsync(id); if (Match != null) { foreach (RiotNet.Models.MatchTeam team in Match.Teams) { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = String.Format(@"INSERT INTO `Team`( `Winner`, `inhibitorKills`, `towerKills`, `firstTower`, `firstBlood`, `firstBaron`, `firstInhibitor`, `firstDragon`, `baronKills`, `dragonKills`) VALUES ( {0},{1},{2},{3},{4}, {5},{6},{7},{8},{9})", team.Winner, team.InhibitorKills, team.TowerKills, team.FirstTower, team.FirstBlood, team.FirstBaron, team.FirstInhibitor, team.FirstDragon, team.BaronKills, team.DragonKills); cmd.Prepare(); cmd.ExecuteNonQuery(); } } } } catch (MySqlException ex) { Console.WriteLine("Error: {0}", ex.ToString()); } finally { if (conn != null) { conn.Close(); } } }
async Task <SummonerData> GetSummoner(string name, string server) { SummonerData summonerData = new SummonerData(); int elo; string info = ""; DateTime start = DateTime.Now; var summoner = await client.GetSummonerBySummonerNameAsync(name, server); if (summoner == null) { return(null); } var leagueEntries = await client.GetLeagueEntriesBySummonerIdAsync(summoner.Id, server); var leagueEntry = leagueEntries.Where(l => l.QueueType == "RANKED_SOLO_5x5").FirstOrDefault(); if (leagueEntry == null) { var matchList = await client.GetMatchListByAccountIdAsync(summoner.AccountId, platformId : server); if (matchList == null) { elo = StaticValues.DefaultElo; info = "No match list. Default value."; } else { var match = await client.GetMatchAsync(matchList.Matches[0].GameId, server); if (match == null) { elo = StaticValues.DefaultElo; info = "Can't get last match. Default value."; } else { info = "Based on last match."; var participant = match.Participants.Where(p => p.ParticipantId == match.ParticipantIdentities.Where(i => i.Player.SummonerName == summoner.Name).FirstOrDefault().ParticipantId).FirstOrDefault(); var tier = participant.HighestAchievedSeasonTier; if (tier == null || tier == "UNRANKED") { elo = StaticValues.DefaultElo; info += " (Unranked)"; } else { elo = RankToPoints($"{tier} 2"); info += $" ({tier})"; } } } } else { elo = RankToPoints($"{leagueEntry.Tier} {RomanToArabic(leagueEntry.Rank)}") + leagueEntry.LeaguePoints; info = $"{leagueEntry.Tier} {leagueEntry.Rank}"; } var timeout = 3000 - (DateTime.Now - start).TotalMilliseconds; if (timeout > 0) { Thread.Sleep((int)timeout); } summonerData.Name = summoner.Name; summonerData.Info = info; summonerData.Elo = elo; return(summonerData); }
public async Task <TracerPlayer> UpdateStatsAsync(long tpID) { if (scl == null) { scl = (await client.GetStaticChampionsAsync()); scl.Keys = new Dictionary <string, string>(); foreach (var k in scl.Data.Keys) { scl.Keys.Add(scl.Data[k].Id.ToString(), k); } } TracerPlayer tp = db.TracerPlayers.Include(t => t.Summoner) .Include(t => t.PlayerStats) .ThenInclude(t => t.championStats) .ThenInclude(x => x.Stats) .Where(t => t.Summoner.Id == (long)tpID).FirstOrDefault(); bool doneSome = false; if (tp.PlayerStats != null && tp.PlayerStats.championStats != null && tp.PlayerStats.championStats.Count > 0) { //Delete db trash.. foreach (ChampionStats cs in tp.PlayerStats.championStats) { db.Remove(cs); doneSome = true; } if (tp.PlayerStats.stats != null) { db.Remove(tp.PlayerStats.stats); doneSome = true; } if (tp.PlayerStats != null) { db.Remove(tp.PlayerStats); doneSome = true; } if (doneSome) { db.SaveChanges(); } } //check the db TracerDemo.Model.Stats generalStats = CleanStats(); DateTime lastTwoMonths = DateTime.Today.AddMonths(-2); var endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTimeKind.Utc); var beginTime = endTime.AddMonths(-2); IEnumerable <QueueType> rankedQueues = new[] { QueueType.TEAM_BUILDER_RANKED_SOLO }; MatchList matchList = await client.GetMatchListByAccountIdAsync (tp.Summoner.AccountId, rankedQueues : rankedQueues); if (matchList == null) { return(tp); } List <MatchReference> matchReferneces = matchList.Matches; if (matchReferneces == null) { return(tp); } PlayerStats tracerStats = new PlayerStats(); tracerStats.TracerPlayerId = tp.Id; Dictionary <long, ChampionStats> champDict = new Dictionary <long, ChampionStats>(); int i = 0; foreach (MatchReference mr in matchReferneces) { if (mr.Timestamp > beginTime && mr.Timestamp < endTime) { i++; } else { Console.WriteLine("All rpocessed"); break; } Console.WriteLine("Processing match " + i + " of " + matchReferneces.Count); int champId = mr.Champion; //bool firstGame = false; if (!champDict.ContainsKey(champId)) { ChampionStats cs = new ChampionStats { TracerPlayerId = tp.Id, ChampionId = champId, ChampionName = scl.Keys[champId.ToString()], Stats = CleanStats() }; champDict.Add(champId, cs); //firstGame = true; } Match m = await client.GetMatchAsync(mr.GameId); if (m == null) { continue; } int blueDeaths = 0; int blueAsists = 0; int blueKills = 0; int redDeaths = 0; int redAsists = 0; int redKills = 0; int playerKills = 0; int playerAssists = 0; int playerDeaths = 0; TeamSide playerSide = TeamSide.Team1; foreach (MatchParticipant mp in m.Participants) { switch (mp.TeamId) { case TeamSide.Team1: blueDeaths += mp.Stats.Deaths; blueAsists += mp.Stats.Assists; blueKills += mp.Stats.Kills; break; case TeamSide.Team2: redDeaths += mp.Stats.Deaths; redAsists += mp.Stats.Assists; redKills += mp.Stats.Kills; break; } if (mp.ChampionId == champId) { playerSide = mp.TeamId; MatchParticipantStats matchPlayerStats = mp.Stats; champDict[champId].Stats.Games += 1; generalStats.Games += 1; champDict[champId].Stats.Minutes += (float)m.GameDuration.TotalMinutes; generalStats.Minutes += (float)m.GameDuration.TotalMinutes; champDict[champId].Stats.MinutesXMatch = champDict[champId].Stats.Minutes / champDict[champId].Stats.Games; generalStats.MinutesXMatch = generalStats.Minutes / generalStats.Games; if (matchPlayerStats.Win) { champDict[champId].Stats.Wins += 1; generalStats.Wins += 1; } champDict[champId].Stats.WinRate = (champDict[champId].Stats.Wins * 1f) / ((champDict[champId].Stats.Games * 1f)); generalStats.WinRate = (generalStats.Wins * 1f) / (generalStats.Games * 1f); //Champion Wards champDict[champId].Stats.Wards += mp.Stats.WardsPlaced; champDict[champId].Stats.WardsKilled += mp.Stats.WardsKilled; champDict[champId].Stats.WardXmin = champDict[champId].Stats.Wards / champDict[champId].Stats.Minutes; champDict[champId].Stats.WardKillXmin = champDict[champId].Stats.WardsKilled / champDict[champId].Stats.Minutes; //General Wards generalStats.Wards += mp.Stats.WardsPlaced; generalStats.WardsKilled += mp.Stats.WardsKilled; generalStats.WardXmin = generalStats.Wards / generalStats.Minutes; generalStats.WardKillXmin = generalStats.WardsKilled / generalStats.Minutes; generalStats.Kills += mp.Stats.Kills; generalStats.Daths += mp.Stats.Deaths; generalStats.Asists += mp.Stats.Assists; generalStats.Minions += mp.Stats.TotalMinionsKilled; generalStats.MinionsMinute = generalStats.Minions / generalStats.Minutes; champDict[champId].Stats.Kills += mp.Stats.Kills; champDict[champId].Stats.Daths += mp.Stats.Deaths; champDict[champId].Stats.Asists += mp.Stats.Assists; champDict[champId].Stats.Minions += mp.Stats.TotalMinionsKilled; champDict[champId].Stats.MinionsMinute = champDict[champId].Stats.Minions / champDict[champId].Stats.Minutes; playerKills += mp.Stats.Kills; playerDeaths += mp.Stats.Deaths; playerAssists += mp.Stats.Assists; if (mp.Stats.FirstBloodKill || mp.Stats.FirstBloodAssist) { champDict[champId].Stats.FirstBlood += 1; generalStats.FirstBlood += 1; } } } switch (playerSide) { case TeamSide.Team1: generalStats.KillParticipation += redKills; generalStats.KillShare += redKills; generalStats.DeathShare += redDeaths; champDict[champId].Stats.KillParticipation += redKills; champDict[champId].Stats.KillShare += redKills; champDict[champId].Stats.DeathShare += redDeaths; break; case TeamSide.Team2: generalStats.KillParticipation += redKills; generalStats.KillShare += redKills; generalStats.DeathShare += redDeaths; champDict[champId].Stats.KillParticipation += redKills; champDict[champId].Stats.KillShare += redKills; champDict[champId].Stats.DeathShare += redDeaths; break; } } generalStats.KillParticipation = (generalStats.Kills + generalStats.Asists) / Math.Max(1f, generalStats.KillParticipation); generalStats.KillShare = (generalStats.Kills) / Math.Max(1f, generalStats.KillShare); generalStats.DeathShare = (generalStats.Daths) / Math.Max(1f, generalStats.DeathShare); generalStats.FirstBlood = (generalStats.FirstBlood / generalStats.Games); List <ChampionStats> championStats = champDict.Values.ToList(); tp.PlayerStatsId = tracerStats.Id; tp.IsProcesing = false; tp.LastUpdate = endTime.ToFileTimeUtc(); db.TracerPlayers.Update(tp); db.SaveChanges(); foreach (ChampionStats cs in championStats) { champDict[cs.ChampionId].Stats.KillParticipation = (champDict[cs.ChampionId].Stats.Kills + champDict[cs.ChampionId].Stats.Asists) / Math.Max(1f, champDict[cs.ChampionId].Stats.KillParticipation); champDict[cs.ChampionId].Stats.KillShare = (champDict[cs.ChampionId].Stats.Kills) / Math.Max(1f, champDict[cs.ChampionId].Stats.KillShare); champDict[cs.ChampionId].Stats.DeathShare = (champDict[cs.ChampionId].Stats.Daths) / Math.Max(1f, champDict[cs.ChampionId].Stats.DeathShare); champDict[cs.ChampionId].Stats.FirstBlood = champDict[cs.ChampionId].Stats.FirstBlood / champDict[cs.ChampionId].Stats.Games; db.Add(cs); } db.SaveChanges(); tracerStats.stats = generalStats; tracerStats.championStats = championStats; tracerStats.TracerPlayerId = tp.Id; tracerStats.player = tp; tracerStats.StatsId = generalStats.Id; db.Add(tracerStats); db.SaveChanges(); processingSummoners.Remove(tp.Summoner.Name); return(tp); }