public async Task <IActionResult> PurchaseGame([FromBody] PurchaseRoyaleGameRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

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

            Maybe <RoyalePublisher> publisher = await _royaleService.GetPublisher(request.PublisherID);

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

            if (!publisher.Value.User.Equals(currentUser))
            {
                return(Forbid());
            }

            var masterGame = await _interLeagueService.GetMasterGameYear(request.MasterGameID, publisher.Value.YearQuarter.YearQuarter.Year);

            if (masterGame.HasNoValue)
            {
                return(NotFound());
            }

            var purchaseResult = await _royaleService.PurchaseGame(publisher.Value, masterGame.Value);

            var viewModel = new PlayerClaimResultViewModel(purchaseResult);

            return(Ok(viewModel));
        }
Ejemplo n.º 2
0
    public async Task <IActionResult> PurchaseGame([FromBody] PurchaseRoyaleGameRequest request)
    {
        var currentUserResult = await GetCurrentUser();

        if (currentUserResult.IsFailure)
        {
            return(BadRequest(currentUserResult.Error));
        }
        var currentUser = currentUserResult.Value;

        RoyalePublisher?publisher = await _royaleService.GetPublisher(request.PublisherID);

        if (publisher is null)
        {
            return(NotFound());
        }

        if (!publisher.User.Equals(currentUser))
        {
            return(Forbid());
        }

        var masterGame = await _interLeagueService.GetMasterGameYear(request.MasterGameID, publisher.YearQuarter.YearQuarter.Year);

        if (masterGame is null)
        {
            return(NotFound());
        }

        await _royaleSemaphore.WaitAsync();

        try
        {
            var purchaseResult = await _royaleService.PurchaseGame(publisher, masterGame);

            var viewModel = new PlayerClaimResultViewModel(purchaseResult);
            return(Ok(viewModel));
        }
        finally
        {
            _royaleSemaphore.Release(1);
        }
    }
Ejemplo n.º 3
0
        public async Task <IActionResult> DraftGame([FromBody] DraftGameRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

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

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

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

            if (currentUser.UserID != publisher.Value.User.UserID)
            {
                return(Forbid());
            }

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

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

            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, false, masterGame, publisherPosition, overallPosition);

            ClaimResult result = await _fantasyCriticService.ClaimGame(domainRequest);

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

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

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

            return(Ok(viewModel));
        }