public virtual IActionResult Profile()
        {
            int  userId      = User.GetId();
            User currentUser = _leagueContext.Users.Find(userId);
            ProfileAndMatches profileAndMatches = new ProfileAndMatches
            {
                Profile = _leagueContext.Profiles.Find(currentUser.ProfileId)
            };
            var currentUserMatches = _leagueContext.Matches
                                     .Include(m => m.FirstPlayer)
                                     .ThenInclude(fp => fp.Profile)
                                     .Include(m => m.SecondPlayer)
                                     .ThenInclude(sp => sp.Profile)
                                     .Include(m => m.Round)
                                     .Where(m => m.FirstPlayerId == userId || m.SecondPlayerId == userId);

            if (currentUserMatches.Any())
            {
                profileAndMatches.Matches = currentUserMatches.ToList();
                profileAndMatches.Wins    = _matchesStatisticsService.GetWins(profileAndMatches.Matches, userId);
                profileAndMatches.Losses  = _matchesStatisticsService.GetLosses(profileAndMatches.Matches, userId);
                profileAndMatches.Winrate =
                    _matchesStatisticsService.GetWinrate(profileAndMatches.Wins, profileAndMatches.Losses);
                foreach (Match match in profileAndMatches.Matches)
                {
                    if (match.Round.IsGroupRound)
                    {
                        GroupRound groupRound = (GroupRound)match.Round;
                        _leagueContext.Entry(groupRound).Reference(gr => gr.Group).Load();
                        if (groupRound.Group.SeasonId == _leagueContext.Seasons.Last().Id)
                        {
                            profileAndMatches.CurrentSeasonMatches.Add(match);
                        }
                    }
                    else
                    {
                        PlayoffsRound playoffsRound = (PlayoffsRound)match.Round;
                        if (playoffsRound.SeasonId == _leagueContext.Seasons.Last().Id)
                        {
                            profileAndMatches.CurrentSeasonMatches.Add(match);
                        }
                    }
                }
                profileAndMatches.CurrentSeasonWins =
                    _matchesStatisticsService.GetWins(profileAndMatches.CurrentSeasonMatches, userId);
                profileAndMatches.CurrentSeasonLosses =
                    _matchesStatisticsService.GetLosses(profileAndMatches.CurrentSeasonMatches, userId);
                profileAndMatches.CurrentSeasonWinrate =
                    _matchesStatisticsService.GetWinrate(
                        profileAndMatches.CurrentSeasonWins,
                        profileAndMatches.CurrentSeasonLosses
                        );
            }
            return(View(profileAndMatches));
        }
        private void CreateRound(Season currentSeason, DateTime date)
        {
            PlayoffsRound firstPlayoffsRound = new PlayoffsRound
            {
                EventDate = date
            };

            currentSeason.PlayoffsRounds.Add(firstPlayoffsRound);
            _leagueContext.SaveChanges();
            _roundId = firstPlayoffsRound.Id;
        }
        public bool TryCreateNext(Season currentSeason, DateTime date)
        {
            if (!_leagueContext.PlayoffsRounds.Any())
            {
                return(false);
            }

            PlayoffsRound currentRound          = Current;
            int           nextRoundPlayersCount = currentRound.Matches.Count;

            if (nextRoundPlayersCount <= 1)
            {
                return(false);
            }

            List <int> winnersIds = new List <int>(nextRoundPlayersCount);

            foreach (Match match in currentRound.Matches)
            {
                int winnerId;
                if (!_matchService.TryGetWinner(match, out winnerId))
                {
                    if (new Randomizer().Bool())
                    {
                        winnerId = match.FirstPlayerId;
                    }
                    else
                    {
                        winnerId = match.SecondPlayerId;
                    }
                }
                winnersIds.Add(winnerId);
            }

            CreateRound(currentSeason, date);
            for (int i = 0; i < nextRoundPlayersCount; i += 2)
            {
                User firstPlayer  = _leagueContext.Users.Find(winnersIds[i]);
                User secondPlayer = _leagueContext.Users.Find(winnersIds[i + 1]);
                CreateMatch(firstPlayer, secondPlayer);
            }
            return(true);
        }