Example #1
0
        public async Task <IActionResult> BanUser()
        {
            var players = await _summonerInfoRepository.GetAllSummonersAsync();

            return(View(players));
        }
        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);
        }
        public async Task <bool> UpdateSummonerRequestsAsync(UserEntity user, SummonerRequestView view)
        {
            var seasonInfo = await _seasonInfoRepository.GetCurrentSeasonAsync();

            SetIsSubToElements(view);
            var summoners      = (await _summonerInfoRepository.GetAllSummonersAsync()).ToDictionary(x => x.Id, x => x);
            var summonerEntity = summoners.First(x => x.Value.UserId == user.Id);

            summoners.Remove(summonerEntity.Key);

            var requestedSummonerEntities = (await _requestedSummonerRepository.ReadAllForSummonerAsync(summonerEntity.Key, seasonInfo.Id)).ToList();

            //pulled from db
            var kvp = new Dictionary <string, SummonerRequestEntity>();

            foreach (var requestEntity in requestedSummonerEntities)
            {
                if (summoners.TryGetValue(requestEntity.SummonerRequestedId, out var summoner))
                {
                    kvp.TryAdd(summoner.SummonerName, requestEntity);
                }
            }

            var createList = new List <SummonerRequestEntity>();
            var updateList = new List <SummonerRequestEntity>();
            var deleteList = new List <SummonerRequestEntity>();

            //new list
            foreach (var requestedSummoner in view.RequestedSummoners)
            {
                //skip these
                if (requestedSummoner.SummonerName == null || requestedSummoner.SummonerName == DefaultText)
                {
                    continue;
                }
                //insert
                if (!kvp.TryGetValue(requestedSummoner.SummonerName, out var oldSummonerRequest))
                {
                    var newEntity = new SummonerRequestEntity
                    {
                        Id                  = Guid.NewGuid(),
                        SummonerId          = summonerEntity.Key,
                        SummonerRequestedId = summoners.First(x => x.Value.SummonerName == requestedSummoner.SummonerName).Key,
                        IsSub               = requestedSummoner.IsSub,
                        SeasonInfoId        = seasonInfo.Id
                    };
                    createList.Add(newEntity);
                }
                //update
                else
                {
                    oldSummonerRequest.SummonerRequestedId =
                        summoners.First(x => x.Value.SummonerName == requestedSummoner.SummonerName).Key;
                    oldSummonerRequest.IsSub = requestedSummoner.IsSub;
                    updateList.Add(oldSummonerRequest);
                }
            }

            //delete
            foreach (var oldEntity in kvp)
            {
                if (!view.RequestedSummoners.Select(x => x.SummonerName).Contains(oldEntity.Key))
                {
                    deleteList.Add(oldEntity.Value);
                }
            }

            var createTask = _requestedSummonerRepository.CreateAsync(createList);
            var updateTask = _requestedSummonerRepository.UpdateAsync(updateList);
            var deleteTask = _requestedSummonerRepository.DeleteAsync(deleteList);

            return(await createTask && await updateTask && await deleteTask);
        }