コード例 #1
0
        public async Task <bool> CreateNewTeamAsync(IEnumerable <Guid> summonerIds)
        {
            var seasonInfo = await _seasonInfoRepository.GetCurrentSeasonAsync();

            summonerIds = summonerIds.ToList();
            var result = false;

            try
            {
                var summonerIdsList = summonerIds.ToList();
                var temp            = new List <Guid>(summonerIds);
                foreach (var summonerId in temp)
                {
                    var playerRecord = await _teamPlayerRepository.GetRosterIdForExistingGroupAsync(
                        new List <Guid>
                    {
                        summonerId
                    },
                        seasonInfo.Id);

                    if (playerRecord != null)
                    {
                        summonerIdsList.Remove(summonerId);
                    }
                }
                var summoners = (await _summonerInfoRepository.GetAllForSummonerIdsAsync(summonerIdsList)).ToList();



                if (summoners.Count < MinTeamCountRequirement)
                {
                    throw new Exception("Cannot make a team with less than 5 players");
                }


                var teamTierScores = new List <int>();
                var teamPlayers    = new List <TeamPlayerEntity>();
                foreach (var summoner in summoners)
                {
                    teamPlayers.Add(new TeamPlayerEntity
                    {
                        SummonerId   = summoner.Id,
                        SeasonInfoId = seasonInfo.Id
                    });
                    var currentTierScore  = int.Parse((await _lookupRepository.GetLookupEntity(summoner.Tier_DivisionId)).Value);
                    var previousTierScore = summoner.PreviousSeasonRankId != null
                        ? int.Parse((await _lookupRepository.GetLookupEntity(summoner.Tier_DivisionId)).Value)
                        : 0;

                    var tierScore = Math.Max(currentTierScore, previousTierScore);
                    teamTierScores.Add(tierScore + summoner.CurrentLp);
                }

                var teamsCount = (await _teamRosterRepository.GetAllTeamsAsync(seasonInfo.Id)).Count();

                var teamTierScore = teamTierScores.OrderByDescending(x => x).Take(MinTeamCountRequirement).Sum();
                var team          = new TeamRosterEntity
                {
                    Id            = Guid.NewGuid(),
                    TeamName      = $"Team{teamsCount+1}",
                    TeamTierScore = teamTierScore / MinTeamCountRequirement,
                    SeasonInfoId  = seasonInfo.Id
                };

                var createTeamResult = await _teamRosterRepository.CreateAsync(team);

                if (!createTeamResult)
                {
                    throw new Exception("Failed to create team entity");
                }

                teamPlayers.ForEach(x => x.TeamRosterId = team.Id);

                result = await _teamPlayerRepository.InsertAsync(teamPlayers);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error for Admin Create Team process.");
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// For Admin usage only
        /// </summary>
        /// <returns></returns>
        public async Task <IEnumerable <RosterView> > GetAllRosters(SeasonInfoEntity seasonInfo = null)
        {
            var list = new List <RosterView>();

            if (seasonInfo == null)
            {
                seasonInfo = await _seasonInfoRepository.GetCurrentSeasonAsync();
            }
            var rostersTask           = _teamRosterRepository.GetAllTeamsAsync(seasonInfo.Id);
            var captainsTask          = _teamCaptainRepository.GetAllTeamCaptainsAsync();
            var alternateAccountsTask = _alternateAccountRepository.ReadAllAsync();
            var allPlayersTask        = _teamPlayerRepository.ReadAllForSeasonAsync(seasonInfo.Id);

            var rosters           = await rostersTask;
            var captains          = (await captainsTask).ToList();
            var alternateAccounts = (await alternateAccountsTask).ToList();
            var allPlayers        = (await allPlayersTask).ToList();

            foreach (var roster in rosters)
            {
                var players = allPlayers.Where(x => x.TeamRosterId == roster.Id).ToList();
                var captain = captains.FirstOrDefault(x => x.TeamRosterId == roster.Id);

                var summoners =
                    (await _summonerInfoRepository.GetAllForSummonerIdsAsync(players.Select(x => x.SummonerId))).ToList();

                var alts          = alternateAccounts.Where(x => summoners.Select(y => y.Id).ToList().Contains(x.SummonerId));
                var summonerViews = _summonerMapper.MapDetailed(summoners, alts, new List <PlayerStatsView>()).ToList();
                foreach (var teamPlayerEntity in players)
                {
                    var player = summoners.First(x => x.Id == teamPlayerEntity.SummonerId);

                    var isSub        = teamPlayerEntity.IsSub ?? false;
                    var summonerView = summonerViews.First(x => x.SummonerName == player.SummonerName);
                    summonerView.IsSub = isSub;
                }

                var rosterView = new RosterView
                {
                    RosterId      = roster.Id,
                    Captain       = summoners.FirstOrDefault(x => x.Id == captain?.SummonerId)?.SummonerName,
                    TeamName      = roster.TeamName,
                    Wins          = roster.Wins ?? 0,
                    Loses         = roster.Loses ?? 0,
                    Points        = roster.Points ?? 0,
                    Players       = summonerViews,
                    TeamTierScore = roster.TeamTierScore.GetValueOrDefault()
                };

                var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/logos", roster.Id.ToString());
                if (File.Exists(path))
                {
                    var byteData = await File.ReadAllBytesAsync(path);

                    var base64 = Convert.ToBase64String(byteData);
                    var type   = GetContentType(path);
                    var imgSrc = String.Format($"data:{type};base64,{base64}");
                    rosterView.FileSource = imgSrc;
                }
                rosterView.Cleanup();
                list.Add(rosterView);
            }

            return(list);
        }