public void DeleteCalculatedMatchupInfo(long gameId) { Db_LccCachedCalculatedMatchupInfo matchup = _lccDatabaseContext.CalculatedMatchupInformation .Include(x => x.EnemyTeam) .Include(x => x.EnemyTeam.Players) .Include(x => x.FriendlyTeam) .Include(x => x.FriendlyTeam.Players) .ThenInclude(y => y.Trinket) .FirstOrDefault(x => x.GameId == gameId); if (matchup != null) { _lccDatabaseContext.CalculatedMatchupInformation.Remove(matchup); } }
//Method triggered when a match is requested that we don't currently have in the cache public async Task <Db_LccCachedCalculatedMatchupInfo> CreateDatabaseModelForCalculatedMatchupInfo(RiotSharp.Endpoints.MatchEndpoint.Match match, Timeline timeline, long usersChampionId) { try { int usersTeamId = match.Participants.FirstOrDefault(x => x.ChampionId == usersChampionId).TeamId; IEnumerable <Participant> friendlyTeamParticipants = match.Participants.Where(x => x.TeamId == usersTeamId); IEnumerable <Participant> enemyTeamParticipants = match.Participants.Where(x => x.TeamId != usersTeamId); Db_LccCachedCalculatedMatchupInfo cachedMatchupInformation = new Db_LccCachedCalculatedMatchupInfo() { GameId = match.GameId, MatchDate = match.GameCreation, MatchPatch = match.GameVersion, MatchDuration = match.GameDuration, FriendlyTeamWin = match.Teams.FirstOrDefault(x => x.TeamId == usersTeamId).Win == MatchOutcome.Win }; cachedMatchupInformation.FriendlyTeam = await CreateCachedTeamInformationFromRiotMatch ( match, match.Participants.Where(x => x.TeamId == usersTeamId), timeline, match.Participants.FirstOrDefault(x => x.ChampionId == usersChampionId).TeamId ); cachedMatchupInformation.EnemyTeam = await CreateCachedTeamInformationFromRiotMatch ( match, match.Participants.Where(x => x.TeamId != usersTeamId), timeline, match.Participants.FirstOrDefault(x => x.ChampionId != usersChampionId).TeamId ); return(cachedMatchupInformation); } catch (Exception e) { Console.WriteLine("Error when creating dbmodel for cached matchup info."); } return(new Db_LccCachedCalculatedMatchupInfo()); }
public void InsertCalculatedMatchupInfo(Db_LccCachedCalculatedMatchupInfo match) { _lccDatabaseContext.CalculatedMatchupInformation.Add(match); }
public void UpdateCalculatedMatchupInfo(Db_LccCachedCalculatedMatchupInfo matchup) { _lccDatabaseContext.Entry(matchup).State = EntityState.Modified; }
public LccCalculatedMatchupInformation CreateLccCalculatedMatchupInformationFromCache(Db_LccCachedCalculatedMatchupInfo match) { // General info LccCalculatedMatchupInformation matchupInformation = new LccCalculatedMatchupInformation() { MatchDate = match.MatchDate, MatchPatch = match.MatchPatch, MatchDuration = match.MatchDuration, GameId = match.GameId, FriendlyTeamWin = match.FriendlyTeamWin, }; matchupInformation.FriendlyTeam = CreateLccTeamInformationFromCache(match.FriendlyTeam); matchupInformation.EnemyTeam = CreateLccTeamInformationFromCache(match.EnemyTeam); return(matchupInformation); }
public async Task <JsonResult> GetMatchup(long usersChampionId, string usersLane, long[] friendlyTeamChampions, long[] enemyTeamChampions, int maxMatchLimit = 5, string summonerName = "BOLULU") { IEnumerable <Db_LccBasicMatchInfo> allMatchesInDatabase = _matchupInformationRepository.GetAllMatchups(); IList <long> friendlyTeamChampionIds = new List <long>(friendlyTeamChampions) { usersChampionId }; IList <long> enemyTeamChampionIds = enemyTeamChampions; IEnumerable <Db_LccBasicMatchInfo> allMatchesContainingUsersChampion = allMatchesInDatabase.Where (x => x.LosingTeamChampions.Any(p => p.ChampionId == usersChampionId || x.WinningTeamChampions.Any(u => u.ChampionId == usersChampionId))); IEnumerable <Db_LccBasicMatchInfo> allMatchesWithRequestedTeams = new List <Db_LccBasicMatchInfo>(); //if a summoner name parameter is passed filter based on this too Console.WriteLine(DateTime.Now + ": Finding matches for requested matchup"); if (summonerName != "") { allMatchesWithRequestedTeams = allMatchesContainingUsersChampion.Where //Check to see if the Enemys team Ids are the losing team, and the friendly team are the winning team (q => (enemyTeamChampionIds.All(e => q.LosingTeamChampions.Any(l => l.ChampionId == e && l.SummonerName == summonerName)) && friendlyTeamChampionIds.All(f => q.WinningTeamChampions.Any(l => l.ChampionId == f && l.SummonerName == summonerName))) //Check to see if the winning team Ids are the losing team, and the enemy team are the winning team || (enemyTeamChampionIds.All(e => q.WinningTeamChampions.Any(l => l.ChampionId == e && l.SummonerName == summonerName)) && friendlyTeamChampionIds.All(f => q.LosingTeamChampions.Any(l => l.ChampionId == f && l.SummonerName == summonerName)))); } else { allMatchesContainingUsersChampion.Where //Check to see if the Enemys team Ids are the losing team, and the friendly team are the winning team (q => (enemyTeamChampionIds.All(e => q.LosingTeamChampions.Any(l => l.ChampionId == e)) && friendlyTeamChampionIds.All(f => q.WinningTeamChampions.Any(l => l.ChampionId == f))) //Check to see if the winning team Ids are the losing team, and the enemy team are the winning team || (enemyTeamChampionIds.All(e => q.WinningTeamChampions.Any(l => l.ChampionId == e)) && friendlyTeamChampionIds.All(f => q.LosingTeamChampions.Any(l => l.ChampionId == f)))); } Console.WriteLine(DateTime.Now + ": Found - " + allMatchesWithRequestedTeams.Count() + " matches"); List <LccCalculatedMatchupInformation> matchesToReturnToUser = new List <LccCalculatedMatchupInformation>(); if (allMatchesWithRequestedTeams.Any()) { int matchReturnCount = 0; allMatchesWithRequestedTeams = allMatchesWithRequestedTeams.OrderByDescending(x => x.MatchDate); foreach (var match in allMatchesWithRequestedTeams) { if (matchReturnCount == maxMatchLimit) { break; } try { Console.WriteLine(DateTime.Now + ": Looking in database cache for match"); Db_LccCachedCalculatedMatchupInfo cachedMatchInfo = _cachedCalculatedMatchupInformaton.GetCalculatedMatchupInfoByGameId(match.GameId); Console.WriteLine(DateTime.Now + ": Finished Looking in database cache for match"); if (cachedMatchInfo == null) { Console.WriteLine(DateTime.Now + ": Didn't find it"); Console.WriteLine(DateTime.Now + ": retrieving match from riot"); Match riotMatchInformation = await _riotApi.Match.GetMatchAsync(Region.euw, match.GameId); Console.WriteLine(DateTime.Now + ": retrieved match from riot"); Console.WriteLine(DateTime.Now + ": retrieving match timeline from riot"); Timeline timeline = await _riotApi.Match.GetMatchTimelineAsync(Region.euw, match.GameId); Console.WriteLine(DateTime.Now + ": retrieved match timeline from riot"); Console.WriteLine(DateTime.Now + ": Creating database cache version from this match"); Db_LccCachedCalculatedMatchupInfo newCachedMatchInfo = await _matchControllerUtils.CreateDatabaseModelForCalculatedMatchupInfo(riotMatchInformation, timeline, usersChampionId); Console.WriteLine(DateTime.Now + ": Finished creating database cache version from this match"); //Add to persistant storage Console.WriteLine(DateTime.Now + ": Saving this match to the database"); _cachedCalculatedMatchupInformaton.InsertCalculatedMatchupInfo(newCachedMatchInfo); _cachedCalculatedMatchupInformaton.Save(); Console.WriteLine(DateTime.Now + ": Saved this match to the database"); cachedMatchInfo = newCachedMatchInfo; } else { Console.WriteLine(DateTime.Now + " Match was in the cache"); } Console.WriteLine(DateTime.Now + ": Creating Lcc version of cached match"); matchesToReturnToUser.Add(_matchControllerUtils.CreateLccCalculatedMatchupInformationFromCache(cachedMatchInfo)); Console.WriteLine(DateTime.Now + ": Finished creating Lcc version of cached match"); matchReturnCount++; } catch (Exception e) { Console.WriteLine("Exception encountered when creating match :" + e.Message); } } } return(new JsonResult(matchesToReturnToUser)); }