public async Task <Result <League> > CreateLeague(LeagueCreationParameters parameters)
    {
        LeagueOptions options = new LeagueOptions(parameters.LeagueYearParameters);

        var validateOptions = options.Validate();

        if (validateOptions.IsFailure)
        {
            return(Result.Failure <League>(validateOptions.Error));
        }

        if (parameters.LeagueYearParameters.ScoringSystem.Name != DiminishingScoringSystem.StaticName)
        {
            return(Result.Failure <League>("That scoring mode is no longer supported."));
        }

        IEnumerable <int> years = new List <int>()
        {
            parameters.LeagueYearParameters.Year
        };
        League newLeague = new League(Guid.NewGuid(), parameters.LeagueName, parameters.Manager, years, parameters.PublicLeague, parameters.TestLeague, false, 0);
        await _fantasyCriticRepo.CreateLeague(newLeague, parameters.LeagueYearParameters.Year, options);

        return(Result.Success(newLeague));
    }
Пример #2
0
        public async Task <Result> EditLeague(League league, EditLeagueYearParameters parameters)
        {
            LeagueOptions options         = new LeagueOptions(parameters);
            var           validateOptions = options.Validate();

            if (validateOptions.IsFailure)
            {
                return(Result.Failure(validateOptions.Error));
            }

            var leagueYear = await GetLeagueYear(league.LeagueID, parameters.Year);

            if (leagueYear.HasNoValue)
            {
                throw new Exception($"League year cannot be found: {parameters.LeagueID}|{parameters.Year}");
            }

            IReadOnlyList <Publisher> publishers = await _publisherService.GetPublishersInLeagueForYear(leagueYear.Value);

            int maxStandardGames = publishers.Select(publisher => publisher.PublisherGames.Count(x => !x.CounterPick)).DefaultIfEmpty(0).Max();
            int maxCounterPicks  = publishers.Select(publisher => publisher.PublisherGames.Count(x => x.CounterPick)).DefaultIfEmpty(0).Max();

            if (maxStandardGames > options.StandardGames)
            {
                return(Result.Failure($"Cannot reduce number of standard games to {options.StandardGames} as a publisher has {maxStandardGames} draft games currently."));
            }
            if (maxCounterPicks > options.CounterPicks)
            {
                return(Result.Failure($"Cannot reduce number of counter picks to {options.CounterPicks} as a publisher has {maxCounterPicks} counter picks currently."));
            }

            int maxFreeGamesFreeDropped       = publishers.Select(publisher => publisher.FreeGamesDropped).DefaultIfEmpty(0).Max();
            int maxWillNotReleaseGamesDropped = publishers.Select(publisher => publisher.WillNotReleaseGamesDropped).DefaultIfEmpty(0).Max();
            int maxWillReleaseGamesDropped    = publishers.Select(publisher => publisher.WillReleaseGamesDropped).DefaultIfEmpty(0).Max();

            if (maxFreeGamesFreeDropped > options.FreeDroppableGames && options.FreeDroppableGames != -1)
            {
                return(Result.Failure($"Cannot reduce number of unrestricted droppable games to {options.FreeDroppableGames} as a publisher has already dropped {maxFreeGamesFreeDropped} games."));
            }
            if (maxWillNotReleaseGamesDropped > options.WillNotReleaseDroppableGames && options.WillNotReleaseDroppableGames != -1)
            {
                return(Result.Failure($"Cannot reduce number of 'will not release' droppable games to {options.WillNotReleaseDroppableGames} as a publisher has already dropped {maxWillNotReleaseGamesDropped} games."));
            }
            if (maxWillReleaseGamesDropped > options.WillReleaseDroppableGames && options.WillReleaseDroppableGames != -1)
            {
                return(Result.Failure($"Cannot reduce number of 'will release' droppable games to {options.WillReleaseDroppableGames} as a publisher has already dropped {maxWillReleaseGamesDropped} games."));
            }

            var eligibilityOverrides = await GetEligibilityOverrides(league, parameters.Year);

            LeagueYear newLeagueYear = new LeagueYear(league, parameters.Year, options, leagueYear.Value.PlayStatus, eligibilityOverrides);
            await _fantasyCriticRepo.EditLeagueYear(newLeagueYear);

            return(Result.Success());
        }
Пример #3
0
        public async Task <Result <League> > CreateLeague(LeagueCreationParameters parameters)
        {
            LeagueOptions options = new LeagueOptions(parameters);

            var validateOptions = options.Validate();

            if (validateOptions.IsFailure)
            {
                return(Result.Failure <League>(validateOptions.Error));
            }

            IEnumerable <int> years = new List <int>()
            {
                parameters.InitialYear
            };
            League newLeague = new League(Guid.NewGuid(), parameters.LeagueName, parameters.Manager, years, parameters.PublicLeague, parameters.TestLeague, 0);
            await _fantasyCriticRepo.CreateLeague(newLeague, parameters.InitialYear, options);

            return(Result.Success(newLeague));
        }
        public async Task <Result> EditLeague(League league, EditLeagueYearParameters parameters)
        {
            LeagueOptions options         = new LeagueOptions(parameters);
            var           validateOptions = options.Validate();

            if (validateOptions.IsFailure)
            {
                return(Result.Fail(validateOptions.Error));
            }

            var leagueYear = await GetLeagueYear(league.LeagueID, parameters.Year);

            if (leagueYear.HasNoValue)
            {
                throw new Exception($"League year cannot be found: {parameters.LeagueID}|{parameters.Year}");
            }

            IReadOnlyList <Publisher> publishers = await GetPublishersInLeagueForYear(league, parameters.Year);

            int maxStandardGames = publishers.Select(publisher => publisher.PublisherGames.Count(x => !x.CounterPick)).DefaultIfEmpty(0).Max();
            int maxCounterPicks  = publishers.Select(publisher => publisher.PublisherGames.Count(x => x.CounterPick)).DefaultIfEmpty(0).Max();

            if (maxStandardGames > options.StandardGames)
            {
                return(Result.Fail($"Cannot reduce number of standard games to {options.StandardGames} as a publisher has {maxStandardGames} draft games currently."));
            }
            if (maxCounterPicks > options.CounterPicks)
            {
                return(Result.Fail($"Cannot reduce number of counter picks to {options.CounterPicks} as a publisher has {maxCounterPicks} counter picks currently."));
            }

            LeagueYear newLeagueYear = new LeagueYear(league, parameters.Year, options, leagueYear.Value.PlayStatus);
            await _fantasyCriticRepo.EditLeagueYear(newLeagueYear);

            return(Result.Ok());
        }
    public async Task <Result> EditLeague(LeagueYear leagueYear, LeagueYearParameters parameters)
    {
        if (leagueYear.SupportedYear.Finished)
        {
            return(Result.Failure("You cannot edit a completed year."));
        }

        var           league          = leagueYear.League;
        LeagueOptions options         = new LeagueOptions(parameters);
        var           validateOptions = options.Validate();

        if (validateOptions.IsFailure)
        {
            return(Result.Failure(validateOptions.Error));
        }

        if (parameters.ScoringSystem.Name != DiminishingScoringSystem.StaticName)
        {
            return(Result.Failure("That scoring mode is no longer supported."));
        }

        IReadOnlyList <Publisher> publishers = leagueYear.Publishers;
        int maxStandardGames = publishers.Select(publisher => publisher.PublisherGames.Count(x => !x.CounterPick)).DefaultIfEmpty(0).Max();
        int maxCounterPicks  = publishers.Select(publisher => publisher.PublisherGames.Count(x => x.CounterPick)).DefaultIfEmpty(0).Max();

        if (maxStandardGames > options.StandardGames)
        {
            return(Result.Failure($"Cannot reduce number of standard games to {options.StandardGames} as a publisher has {maxStandardGames} standard games currently."));
        }
        if (maxCounterPicks > options.CounterPicks)
        {
            return(Result.Failure($"Cannot reduce number of counter picks to {options.CounterPicks} as a publisher has {maxCounterPicks} counter picks currently."));
        }

        if (leagueYear.PlayStatus.DraftIsActive)
        {
            if (leagueYear.Options.GamesToDraft > parameters.GamesToDraft)
            {
                return(Result.Failure("Cannot decrease the number of drafted games during the draft. Reset the draft if you need to do this."));
            }

            if (leagueYear.Options.CounterPicksToDraft > parameters.CounterPicksToDraft)
            {
                return(Result.Failure("Cannot decrease the number of drafted counter picks during the draft. Reset the draft if you need to do this."));
            }
        }

        if (leagueYear.PlayStatus.DraftFinished)
        {
            if (leagueYear.Options.GamesToDraft != parameters.GamesToDraft)
            {
                return(Result.Failure("Cannot change the number of drafted games after the draft."));
            }

            if (leagueYear.Options.CounterPicksToDraft != parameters.CounterPicksToDraft)
            {
                return(Result.Failure("Cannot change the number of drafted counter picks after the draft."));
            }
        }

        int maxFreeGamesFreeDropped       = publishers.Select(publisher => publisher.FreeGamesDropped).DefaultIfEmpty(0).Max();
        int maxWillNotReleaseGamesDropped = publishers.Select(publisher => publisher.WillNotReleaseGamesDropped).DefaultIfEmpty(0).Max();
        int maxWillReleaseGamesDropped    = publishers.Select(publisher => publisher.WillReleaseGamesDropped).DefaultIfEmpty(0).Max();

        if (maxFreeGamesFreeDropped > options.FreeDroppableGames && options.FreeDroppableGames != -1)
        {
            return(Result.Failure($"Cannot reduce number of unrestricted droppable games to {options.FreeDroppableGames} as a publisher has already dropped {maxFreeGamesFreeDropped} games."));
        }
        if (maxWillNotReleaseGamesDropped > options.WillNotReleaseDroppableGames && options.WillNotReleaseDroppableGames != -1)
        {
            return(Result.Failure($"Cannot reduce number of 'will not release' droppable games to {options.WillNotReleaseDroppableGames} as a publisher has already dropped {maxWillNotReleaseGamesDropped} games."));
        }
        if (maxWillReleaseGamesDropped > options.WillReleaseDroppableGames && options.WillReleaseDroppableGames != -1)
        {
            return(Result.Failure($"Cannot reduce number of 'will release' droppable games to {options.WillReleaseDroppableGames} as a publisher has already dropped {maxWillReleaseGamesDropped} games."));
        }

        var slotAssignments      = GetNewSlotAssignments(parameters, leagueYear, publishers);
        var eligibilityOverrides = await GetEligibilityOverrides(league, parameters.Year);

        var tagOverrides = await GetTagOverrides(league, parameters.Year);

        var supportedYear = await _interLeagueService.GetSupportedYear(parameters.Year);

        LeagueYear newLeagueYear = new LeagueYear(league, supportedYear, options, leagueYear.PlayStatus, eligibilityOverrides,
                                                  tagOverrides, leagueYear.DraftStartedTimestamp, leagueYear.WinningUser, publishers);

        var managerPublisher = leagueYear.GetManagerPublisherOrThrow();
        var differenceString = options.GetDifferenceString(leagueYear.Options);

        if (differenceString is not null)
        {
            LeagueAction settingsChangeAction = new LeagueAction(managerPublisher, _clock.GetCurrentInstant(), "League Year Settings Changed", differenceString, true);
            await _fantasyCriticRepo.EditLeagueYear(newLeagueYear, slotAssignments, settingsChangeAction);
        }

        return(Result.Success());
    }