Beispiel #1
0
        public override async Task OnConnectedAsync()
        {
            var currentUser = await _userManager.FindByNameAsync(Context.User.Identity.Name);

            var leagues = await _fantasyCriticService.GetLeaguesForUser(currentUser);

            List <LeagueYear> draftingLeagueYears = new List <LeagueYear>();

            foreach (var league in leagues)
            {
                foreach (var year in league.Years)
                {
                    var leagueYear = await _fantasyCriticService.GetLeagueYear(league.LeagueID, year);

                    if (leagueYear.HasValue && leagueYear.Value.PlayStatus.DraftIsActive)
                    {
                        draftingLeagueYears.Add(leagueYear.Value);
                    }
                }
            }

            foreach (var leagueYear in draftingLeagueYears)
            {
                await Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName(leagueYear));
            }
        }
Beispiel #2
0
    public async Task Subscribe(string leagueID, string year)
    {
        try
        {
            Guid leagueGUID = Guid.Parse(leagueID);
            int  yearInt    = int.Parse(year);
            var  leagueYear = await _fantasyCriticService.GetLeagueYear(leagueGUID, yearInt);

            if (leagueYear is null)
            {
                return;
            }

            if (!leagueYear.PlayStatus.DraftIsActive)
            {
                return;
            }

            await Groups.AddToGroupAsync(Context.ConnectionId, leagueYear.GetGroupName);
        }
        catch (Exception e)
        {
            _logger.Error("SignalR fail!");
            _logger.Error(e);
            throw;
        }
    }
Beispiel #3
0
    public async Task <ActionResult <List <PossibleMasterGameYearViewModel> > > MasterGameYearInLeagueContext(int year, Guid leagueID)
    {
        var currentUserResult = await GetCurrentUser();

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

        LeagueYear?leagueYear = await _fantasyCriticService.GetLeagueYear(leagueID, year);

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

        var userPublisher = leagueYear.GetUserPublisher(currentUser);

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

        var possibleMasterGames = await _gameSearchingService.GetAllPossibleMasterGameYearsForLeagueYear(leagueYear, userPublisher, year);

        var currentDate = _clock.GetToday();
        var viewModels  = possibleMasterGames.Select(x => new PossibleMasterGameYearViewModel(x, currentDate)).ToList();

        return(viewModels);
    }
Beispiel #4
0
        public async Task Subscribe(Guid leagueID, int year)
        {
            var leagueYear = await _fantasyCriticService.GetLeagueYear(leagueID, year);

            if (leagueYear.HasNoValue)
            {
                return;
            }

            await Groups.AddToGroupAsync(Context.ConnectionId, leagueYear.Value.GetGroupName);
        }
        public async Task Subscribe(string leagueID, string year)
        {
            Guid leagueGUID = Guid.Parse(leagueID);
            int  yearInt    = int.Parse(year);
            var  leagueYear = await _fantasyCriticService.GetLeagueYear(leagueGUID, yearInt);

            if (leagueYear.HasNoValue)
            {
                return;
            }

            await Groups.AddToGroupAsync(Context.ConnectionId, leagueYear.Value.GetGroupName);
        }
Beispiel #6
0
        public async Task <IActionResult> GetLeague(Guid id)
        {
            Maybe <League> league = await _fantasyCriticService.GetLeagueByID(id);

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

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

            var playersInLeague = await _fantasyCriticService.GetUsersInLeague(league.Value);

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

            var inviteesToLeague = await _fantasyCriticService.GetOutstandingInvitees(league.Value);

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

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

            bool neverStarted = true;

            foreach (var year in league.Value.Years)
            {
                var leagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, year);

                if (leagueYear.Value.PlayStatus.PlayStarted)
                {
                    neverStarted = false;
                }
            }

            bool isManager       = (league.Value.LeagueManager.UserID == currentUser.UserID);
            var  leagueViewModel = new LeagueViewModel(league.Value, isManager, playersInLeague, inviteesToLeague,
                                                       userIsInvitedToLeague, neverStarted);

            return(Ok(leagueViewModel));
        }
Beispiel #7
0
        public async Task <IActionResult> AddNewLeagueYear([FromBody] NewLeagueYearRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            var league = await _fantasyCriticService.GetLeagueByID(request.LeagueID);

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

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

            if (league.Value.Years.Contains(request.Year))
            {
                return(BadRequest());
            }

            var supportedYears = await _interLeagueService.GetSupportedYears();

            var selectedSupportedYear = supportedYears.SingleOrDefault(x => x.Year == request.Year);

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

            var userIsBetaUser = await _userManager.IsInRoleAsync(currentUser, "BetaTester");

            bool yearIsOpen = selectedSupportedYear.OpenForCreation || (userIsBetaUser && selectedSupportedYear.OpenForBetaUsers);

            if (!yearIsOpen)
            {
                return(BadRequest());
            }

            if (!league.Value.Years.Any())
            {
                throw new Exception("League has no initial year.");
            }

            var mostRecentYear       = league.Value.Years.Max();
            var mostRecentLeagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, mostRecentYear);

            if (mostRecentLeagueYear.HasNoValue)
            {
                throw new Exception("Most recent league year could not be found");
            }

            await _fantasyCriticService.AddNewLeagueYear(league.Value, request.Year, mostRecentLeagueYear.Value.Options, mostRecentLeagueYear.Value);

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

            var league = await _fantasyCriticService.GetLeagueByID(request.LeagueID);

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

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

            if (league.Value.Years.Contains(request.Year))
            {
                return(BadRequest());
            }

            var supportedYears = await _fantasyCriticService.GetSupportedYears();

            if (!supportedYears.Select(x => x.Year).Contains(request.Year))
            {
                return(BadRequest());
            }

            if (!league.Value.Years.Any())
            {
                throw new Exception("League has no initial year.");
            }

            var mostRecentYear       = league.Value.Years.Max();
            var mostRecentLeagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, mostRecentYear);

            if (mostRecentLeagueYear.HasNoValue)
            {
                throw new Exception("Most recent league year could not be found");
            }

            await _fantasyCriticService.AddNewLeagueYear(league.Value, request.Year, mostRecentLeagueYear.Value.Options);

            return(Ok());
        }
Beispiel #9
0
    protected async Task <GenericResultRecord <LeagueYearRecord> > GetExistingLeagueYear(Guid leagueID, int year,
                                                                                         ActionProcessingModeBehavior actionProcessingModeBehavior, RequiredRelationship requiredRelationship, RequiredYearStatus requiredYearStatus)
    {
        if (actionProcessingModeBehavior == ActionProcessingModeBehavior.Ban)
        {
            var systemWideSettings = await _interLeagueService.GetSystemWideSettings();

            if (systemWideSettings.ActionProcessingMode)
            {
                return(GetFailedResult <LeagueYearRecord>(BadRequest("Site is in read-only mode while actions process.")));
            }
        }

        var currentUserRecord = await GetCurrentUser();

        if ((requiredRelationship.MustBeLoggedIn || requiredRelationship.MustBeInOrInvitedToLeague || requiredRelationship.MustBeActiveInYear || requiredRelationship.MustBeLeagueManager) && currentUserRecord.IsFailure)
        {
            return(GetFailedResult <LeagueYearRecord>(Unauthorized()));
        }

        var leagueYear = await _fantasyCriticService.GetLeagueYear(leagueID, year);

        if (leagueYear is null)
        {
            return(GetFailedResult <LeagueYearRecord>(BadRequest("League year does not exist.")));
        }

        var yearStatusValid = requiredYearStatus.StateIsValid(leagueYear);

        if (yearStatusValid.IsFailure)
        {
            return(GetFailedResult <LeagueYearRecord>(BadRequest(yearStatusValid.Error)));
        }

        var playersInLeague = await _leagueMemberService.GetUsersWithRemoveStatus(leagueYear.League);

        var inviteesToLeague = await _leagueMemberService.GetOutstandingInvitees(leagueYear.League);

        var activeUsers = await _leagueMemberService.GetActivePlayersForLeagueYear(leagueYear.League, year);

        bool         isInLeague      = false;
        LeagueInvite?leagueInvite    = null;
        bool         isActiveInYear  = false;
        bool         isLeagueManager = false;
        bool         userIsAdmin     = false;

        if (currentUserRecord.IsSuccess)
        {
            userIsAdmin = await _userManager.IsInRoleAsync(currentUserRecord.Value, "Admin");

            isLeagueManager = leagueYear.League.LeagueManager.Id == currentUserRecord.Value.Id;
            if (requiredRelationship.MustBeLeagueManager && !isLeagueManager)
            {
                return(GetFailedResult <LeagueYearRecord>(Forbid()));
            }

            if (isLeagueManager)
            {
                isInLeague     = true;
                isActiveInYear = true;
            }
            else
            {
                isInLeague = playersInLeague.Any(x => x.User.Id == currentUserRecord.Value.Id);
                if (!isInLeague)
                {
                    leagueInvite = inviteesToLeague.GetMatchingInvite(currentUserRecord.Value.Email);
                }
                isActiveInYear = activeUsers.Any(x => x.Id == currentUserRecord.Value.Id);
            }
        }

        if (!isInLeague && leagueInvite is null && requiredRelationship.MustBeInOrInvitedToLeague)
        {
            return(GetFailedResult <LeagueYearRecord>(Forbid()));
        }

        if (!isActiveInYear && requiredRelationship.MustBeActiveInYear)
        {
            return(GetFailedResult <LeagueYearRecord>(Forbid()));
        }

        LeagueYearUserRelationship relationship = new LeagueYearUserRelationship(leagueInvite, isInLeague, isActiveInYear, isLeagueManager, userIsAdmin);

        return(new GenericResultRecord <LeagueYearRecord>(new LeagueYearRecord(currentUserRecord.ToNullable(), leagueYear, playersInLeague, activeUsers, inviteesToLeague, relationship), null));
    }