public async Task <bool> UploadPlayerStatsAsync(IEnumerable <IFormFile> files)
        {
            var newStats   = new List <PartialPlayerInfo>();
            var seasonInfo = await _seasonInfoRepository.GetCurrentSeasonAsync();

            var teamsTask             = _teamRosterRepository.GetAllTeamsAsync(seasonInfo.Id);
            var registeredPlayersTask = _summonerInfoRepository.GetAllSummonersAsync();
            var playerStatsTask       = _playerStatsRepository.GetAllStatsAsync(seasonInfo.Id);
            var teams             = (await teamsTask).ToDictionary(x => x.TeamName, x => x);
            var teamPlayers       = (await _teamPlayerRepository.ReadAllForSeasonAsync(seasonInfo.Id)).ToList();
            var registeredPlayers = (await registeredPlayersTask).ToDictionary(x => x.SummonerName.ToLowerInvariant(), x => x);

            foreach (var file in files)
            {
                if (file == null || file.Length <= 0)
                {
                    continue;
                }
                var split      = file.FileName.Split("vs");
                var team1      = split[0].Trim();
                var regexSplit = Regex.Matches(split[1], @"^ [a-zA-z ]*");
                var team2      = regexSplit.First().Value;
                team2 = team2.Remove(team2.Length - 1).Trim();

                using (var stream = file.OpenReadStream())
                {
                    var matchStats = new List <PartialPlayerInfo>();
                    for (var i = 0; i < 10; i++)
                    {
                        matchStats.Add(new PartialPlayerInfo());
                    }

                    var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

                    var result = excelReader.AsDataSet();
                    for (var i = 0; i < result.Tables.Count; i++)
                    {
                        var sheet = result.Tables[i];
                        //Match tab
                        if (i == 0)
                        {
                            foreach (var stat in matchStats)
                            {
                                var strDuration = (string)sheet.Rows[1][4];
                                var duration    = TimeSpan.Parse(strDuration);
                                stat.Duration = duration;
                            }
                        }
                        //Team tab
                        else if (i == 1)
                        {
                            //we skip this for now
                        }

                        //Player tab
                        else if (i == 2)
                        {
                            var totalTeamKillsBlue = 0;
                            var totalTeamKillsRed  = 0;
                            for (var row = 1; row < sheet.Rows.Count; row++)
                            {
                                var player = matchStats[row - 1];
                                player.Name = (string)sheet.Rows[row][4];

                                //This is to check if a player is on the team from the match or an e-sub
                                registeredPlayers.TryGetValue(player.Name.ToLowerInvariant(), out var registeredPlayer);

                                teams.TryGetValue(team1, out var dbTeam1);
                                teams.TryGetValue(team2, out var dbTeam2);
                                if (registeredPlayer != null && (dbTeam1 != null || dbTeam2 != null))
                                {
                                    var teamPlayer = teamPlayers.FirstOrDefault(x =>
                                                                                x.SummonerId == registeredPlayer.Id && (x.TeamRosterId == dbTeam1?.Id || x.TeamRosterId == dbTeam2?.Id));
                                    if (teamPlayer == null)
                                    {
                                        player.SoftDelete = true;
                                    }
                                }

                                player.Assists = Convert.ToInt32((double)sheet.Rows[row][7]);
                                player.Deaths  = Convert.ToInt32((double)sheet.Rows[row][9]);
                                player.Gold    = Convert.ToInt32((double)sheet.Rows[row][16]);
                                player.Kills   = Convert.ToInt32((double)sheet.Rows[row][20]);
                                if (row < 6)
                                {
                                    totalTeamKillsBlue += player.Kills;
                                }
                                else
                                {
                                    totalTeamKillsRed += player.Kills;
                                }
                                player.CS          = Convert.ToInt32((double)sheet.Rows[row][22]);
                                player.VisionScore = Convert.ToInt32((double)sheet.Rows[row][23]);
                            }
                            for (var j = 0; j < matchStats.Count; j++)
                            {
                                var player = matchStats[j];
                                if (j < 5)
                                {
                                    player.TotalTeamKills = totalTeamKillsBlue;
                                }
                                else
                                {
                                    player.TotalTeamKills += totalTeamKillsRed;
                                }
                            }
                        }
                    }

                    newStats.AddRange(matchStats);
                    stream.Close();
                }
            }

            var newList = new List <PartialPlayerInfo>();

            foreach (var newStat in newStats)
            {
                if (newStat.SoftDelete)
                {
                    continue;
                }

                var existing = newList.FirstOrDefault(x => x.Name.ToLowerInvariant() == newStat.Name.ToLowerInvariant());
                if (existing != null)
                {
                    existing.AddOtherGame(newStat);
                }
                else
                {
                    newList.Add(newStat);
                }
            }
            var playerStats = (await playerStatsTask).ToDictionary(x => x.SummonerId, x => x);

            var updateList = new List <PlayerStatsEntity>();
            var insertList = new List <PlayerStatsEntity>();

            foreach (var playersStat in newList)
            {
                if (registeredPlayers.TryGetValue(playersStat.Name.ToLowerInvariant(), out var registeredPlayer))
                {
                    if (playerStats.TryGetValue(registeredPlayer.Id, out var oldStats))
                    {
                        oldStats.UpdateValues(playersStat);
                        updateList.Add(oldStats);
                    }
                    else
                    {
                        var newStat = new PlayerStatsEntity(playersStat, registeredPlayer.Id, seasonInfo.Id);
                        insertList.Add(newStat);
                    }
                }
            }

            var insertResult = _playerStatsRepository.InsertAsync(insertList);
            var updateResult = _playerStatsRepository.UpdateAsync(updateList);

            return(await insertResult && await updateResult);
        }
Beispiel #2
0
        private async Task <bool> UpdateStatsAsync(MatchSubmissionView view, SummonerInfoEntity userPlayer)
        {
            var divisionTask = _scheduleService.GetDivisionIdByScheduleAsync(view.ScheduleId);
            var matchesTask  = _matchDetailRepository.ReadForScheduleId(view.ScheduleId);
            var seasonInfo   = await _seasonInfoRepository.GetCurrentSeasonAsync();

            var registeredPlayersTask  = _summonerInfoRepository.GetAllValidSummonersAsync();
            var previousListedMvpsTask = _matchMvpRepository.ReadAllForTeamScheduleId(view.ScheduleId);
            var registeredPlayers      = (await registeredPlayersTask).ToDictionary(x => x.SummonerName.ToLowerInvariant(), x => x);
            var matchDictionary        = (await matchesTask);
            var mvpDetails             = (await previousListedMvpsTask).OrderBy(x => x.Game).ToDictionary(x => x.Game, x => x);

            var divisionId       = await divisionTask;
            var insertMvpDetails = new List <MatchMvpEntity>();
            var updateMvpDetails = new List <MatchMvpEntity>();
            //Will always insert new records and never update, we will delete any old records first
            var insertDetailsList      = new List <MatchDetailEntity>();
            var insertStatsList        = new List <PlayerStatsEntity>();
            var championDetails        = new List <ChampionStatsEntity>();
            var insertAchievementsList = new List <AchievementEntity>();

            //List of Ids to hard delete

            foreach (var gameInfo in view.GameInfos)
            {
                if (gameInfo.HomeTeamForfeit || gameInfo.AwayTeamForfeit)
                {
                    continue;
                }

                //matchhistory.na.leagueoflegends.com/en/#match-details/NA1/{match id}/{dont care}?tab=overview
                var split = gameInfo.MatchHistoryLink.Split("/");
                //If an invalid match was submitted, will fail the entire process
                if (!uint.TryParse(split[6], out var matchId))
                {
                    return(false);
                }

                var version       = (await GlobalVariables.ChampsApi.Versions.GetAllAsync()).First();
                var championsTask = GlobalVariables.ChampsApi.Champions.GetAllAsync(version);
                var riotMatchTask = GlobalVariables.Api.Match.GetMatchAsync(RiotSharp.Misc.Region.Na, matchId);

                var riotMatch = await riotMatchTask;
                var champions = await championsTask;

                CollectBans(view, riotMatch, champions, seasonInfo, divisionId, championDetails);

                var gameDuration = riotMatch.GameDuration;

                var matchList = new List <MatchDetailContract>();

                await CollectPlayerMatchDetailsAsync(view, riotMatch, champions, gameInfo, registeredPlayers, gameDuration, seasonInfo, matchDictionary,
                                                     matchList, divisionId, championDetails);

                CollectMatchMvpData(view, matchList, registeredPlayers, gameInfo, mvpDetails, updateMvpDetails, insertMvpDetails, userPlayer);

                insertDetailsList.AddRange(matchList.Select(x => x.MatchDetail));
                insertStatsList.AddRange(matchList.Select(x => x.PlayerStats));
                insertAchievementsList.AddRange(matchList.SelectMany(x => x.Achievements));
            }

            if (!await DeleteOldRecords(view))
            {
                return(false);
            }

            var insertAchievementsResult = await _achievementRepository.InsertAsync(insertAchievementsList);

            var insertMvpResult = await _matchMvpRepository.CreateAsync(insertMvpDetails);

            var updateMvpResult = await _matchMvpRepository.UpdateAsync(updateMvpDetails);

            var insertStatsResult = await _playerStatsRepository.InsertAsync(insertStatsList);

            var insertDetailsResult = await _matchDetailRepository.InsertAsync(insertDetailsList);

            var insertChampionStatsResult = await _championStatsRepository.CreateAsync(championDetails);

            return(insertStatsResult && insertDetailsResult && insertMvpResult && updateMvpResult && insertChampionStatsResult && insertAchievementsResult);
        }