Exemple #1
0
        public async Task <IActionResult> GetLeagueYear(Guid leagueID, int year)
        {
            Maybe <LeagueYear> leagueYear = await _fantasyCriticService.GetLeagueYear(leagueID, year);

            if (leagueYear.HasNoValue)
            {
                throw new Exception("Something went really wrong, no options are set up for this league.");
            }

            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            var usersInLeague = await _fantasyCriticService.GetUsersInLeague(leagueYear.Value.League);

            bool userIsInLeague = usersInLeague.Any(x => x.UserID == currentUser.UserID);

            var inviteesToLeague = await _fantasyCriticService.GetOutstandingInvitees(leagueYear.Value.League);

            bool userIsInvitedToLeague = inviteesToLeague.Any(x => x == currentUser.EmailAddress);

            if (!userIsInLeague && !userIsInvitedToLeague)
            {
                return(Unauthorized());
            }

            var publishersInLeague = await _fantasyCriticService.GetPublishersInLeagueForYear(leagueYear.Value.League, leagueYear.Value.Year);

            var supportedYear = (await _fantasyCriticService.GetSupportedYears()).SingleOrDefault(x => x.Year == year);

            if (supportedYear is null)
            {
                return(BadRequest());
            }

            StartDraftResult startDraftResult = await _fantasyCriticService.GetStartDraftResult(leagueYear.Value, publishersInLeague, usersInLeague);

            Maybe <Publisher> nextDraftPublisher = await _fantasyCriticService.GetNextDraftPublisher(leagueYear.Value);

            DraftPhase draftPhase = await _fantasyCriticService.GetDraftPhase(leagueYear.Value);

            var userPublisher = publishersInLeague.SingleOrDefault(x => x.User.UserID == currentUser.UserID);
            IReadOnlyList <PublisherGame> availableCounterPicks = new List <PublisherGame>();

            if (nextDraftPublisher.HasValue)
            {
                availableCounterPicks = await _fantasyCriticService.GetAvailableCounterPicks(leagueYear.Value, nextDraftPublisher.Value);
            }

            LeagueWideValues leagueWideValues = await _fantasyCriticService.GetLeagueWideValues();

            var leagueViewModel = new LeagueYearViewModel(leagueYear.Value, supportedYear, publishersInLeague, currentUser, userPublisher, _clock,
                                                          leagueYear.Value.PlayStatus, startDraftResult, usersInLeague, nextDraftPublisher, draftPhase, availableCounterPicks, leagueWideValues);

            return(Ok(leagueViewModel));
        }
        public async Task <IActionResult> ManagerDraftGame([FromBody] ManagerDraftGameRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var publisher = await _fantasyCriticService.GetPublisher(request.PublisherID);

            if (publisher.HasNoValue)
            {
                return(BadRequest());
            }

            var league = await _fantasyCriticService.GetLeagueByID(publisher.Value.League.LeagueID);

            if (league.HasNoValue)
            {
                return(BadRequest());
            }

            if (league.Value.LeagueManager.UserID != currentUser.UserID)
            {
                return(Forbid());
            }

            var leagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, publisher.Value.Year);

            if (leagueYear.HasNoValue)
            {
                return(BadRequest());
            }

            if (!leagueYear.Value.PlayStatus.DraftIsActive)
            {
                return(BadRequest("You can't draft a game if the draft isn't active."));
            }

            var nextPublisher = await _fantasyCriticService.GetNextDraftPublisher(leagueYear.Value);

            if (nextPublisher.HasNoValue)
            {
                return(BadRequest("There are no spots open to draft."));
            }

            if (!nextPublisher.Value.Equals(publisher.Value))
            {
                return(BadRequest("That publisher is not next up for drafting."));
            }

            Maybe <MasterGame> masterGame = Maybe <MasterGame> .None;

            if (request.MasterGameID.HasValue)
            {
                masterGame = await _fantasyCriticService.GetMasterGame(request.MasterGameID.Value);
            }

            int?publisherPosition = null;
            int?overallPosition   = null;
            var draftPhase        = await _fantasyCriticService.GetDraftPhase(leagueYear.Value);

            if (draftPhase.Equals(DraftPhase.StandardGames))
            {
                publisherPosition = publisher.Value.PublisherGames.Count(x => !x.CounterPick) + 1;
                var publishers = await _fantasyCriticService.GetPublishersInLeagueForYear(league.Value, leagueYear.Value.Year);

                overallPosition = publishers.SelectMany(x => x.PublisherGames).Count(x => !x.CounterPick) + 1;

                if (request.CounterPick)
                {
                    return(BadRequest("Not drafting counterPicks now."));
                }
            }

            if (draftPhase.Equals(DraftPhase.CounterPicks))
            {
                if (!request.CounterPick)
                {
                    return(BadRequest("Not drafting standard games now."));
                }
            }

            ClaimGameDomainRequest domainRequest = new ClaimGameDomainRequest(publisher.Value, request.GameName, request.CounterPick, request.ManagerOverride, masterGame, publisherPosition, overallPosition);

            ClaimResult result = await _fantasyCriticService.ClaimGame(domainRequest);

            bool draftCompleted = await _fantasyCriticService.CompleteDraft(leagueYear.Value);

            var viewModel = new ManagerClaimResultViewModel(result);
            await _hubcontext.Clients.All.SendAsync("RefreshLeagueYear", leagueYear.Value);

            if (draftCompleted)
            {
                await _hubcontext.Clients.All.SendAsync("DraftFinished", leagueYear.Value);
            }

            return(Ok(viewModel));
        }