Ejemplo n.º 1
0
        public bool EvaluateGroupFilter(Contract_AnimeSeries contractSerie, Contract_JMMUser curUser)
        {
            //Directories don't count
            if ((this.FilterType & (int) GroupFilterType.Directory) == (int) GroupFilterType.Directory)
                return false;

            // make sure the user has not filtered this out
            if ((curUser != null) &&
                curUser.HideCategories.FindInEnumerable(contractSerie.AniDBAnime.AniDBAnime.AllTags))
                return false;

            NumberStyles style = NumberStyles.Number;
            CultureInfo culture = CultureInfo.InvariantCulture;

            // now check other conditions
            foreach (GroupFilterCondition gfc in Conditions)
            {
                switch (gfc.ConditionTypeEnum)
                {
                    case GroupFilterConditionType.MissingEpisodes:
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include &&
                            (contractSerie.MissingEpisodeCount > 0 || contractSerie.MissingEpisodeCountGroups > 0) ==
                            false) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude &&
                            (contractSerie.MissingEpisodeCount > 0 || contractSerie.MissingEpisodeCountGroups > 0) ==
                            true) return false;
                        break;

                    case GroupFilterConditionType.MissingEpisodesCollecting:
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include &&
                            contractSerie.MissingEpisodeCountGroups > 0 == false) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude &&
                            contractSerie.MissingEpisodeCountGroups > 0 == true) return false;
                        break;
                    case GroupFilterConditionType.Tag:
                        if ((gfc.ConditionOperatorEnum == GroupFilterOperator.In || gfc.ConditionOperatorEnum == GroupFilterOperator.Include) &&
                            !contractSerie.AniDBAnime.AniDBAnime.AllTags.Contains(gfc.ConditionParameter, StringComparer.InvariantCultureIgnoreCase)) return false;
                        if ((gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn || gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude) &&
                            contractSerie.AniDBAnime.AniDBAnime.AllTags.Contains(gfc.ConditionParameter, StringComparer.InvariantCultureIgnoreCase)) return false;
                        break;
                    case GroupFilterConditionType.Year:
                        if (!contractSerie.AniDBAnime.AniDBAnime.AirDate.HasValue)
                            return false;
                        string year = contractSerie.AniDBAnime.AniDBAnime.AirDate.Value.Year.ToString();
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && year != gfc.ConditionParameter)
                            return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && year == gfc.ConditionParameter)
                            return false;
                        break;

                    case GroupFilterConditionType.HasWatchedEpisodes:
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include &&
                            contractSerie.WatchedEpisodeCount > 0 == false)
                            return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude &&
                            contractSerie.WatchedEpisodeCount > 0 == true)
                            return false;
                        break;

                    case GroupFilterConditionType.HasUnwatchedEpisodes:
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include &&
                            contractSerie.UnwatchedEpisodeCount > 0 == false)
                            return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude &&
                            contractSerie.UnwatchedEpisodeCount > 0 == true)
                            return false;
                        break;

                    case GroupFilterConditionType.AssignedTvDBInfo:
                        bool tvDBInfoMissing = contractSerie.CrossRefAniDBTvDBV2 == null ||
                                               contractSerie.CrossRefAniDBTvDBV2.Count == 0;
                        bool supposedToHaveTvDBLink = contractSerie.AniDBAnime.AniDBAnime.AnimeType != (int) enAnimeType.Movie &&
                                                      !(contractSerie.AniDBAnime.AniDBAnime.Restricted > 0);
                        tvDBInfoMissing &= supposedToHaveTvDBLink;

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && tvDBInfoMissing) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && !tvDBInfoMissing) return false;
                        break;

                    case GroupFilterConditionType.AssignedMALInfo:
                        bool malMissing = contractSerie.CrossRefAniDBMAL == null ||
                                          contractSerie.CrossRefAniDBMAL.Count == 0;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && malMissing) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && !malMissing) return false;
                        break;

                    case GroupFilterConditionType.AssignedMovieDBInfo:
                        bool movieMissing = contractSerie.CrossRefAniDBMovieDB == null;
                        bool supposedToHaveMovieLink = contractSerie.AniDBAnime.AniDBAnime.AnimeType ==
                                                       (int) enAnimeType.Movie &&
                                                       !(contractSerie.AniDBAnime.AniDBAnime.Restricted > 0);
                        movieMissing &= supposedToHaveMovieLink;

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && movieMissing) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && !movieMissing) return false;
                        break;

                    case GroupFilterConditionType.AssignedTvDBOrMovieDBInfo:

                        bool isMovie = contractSerie.AniDBAnime.AniDBAnime.AnimeType == (int) enAnimeType.Movie;
                        bool restricted = (contractSerie.AniDBAnime.AniDBAnime.Restricted > 0);

                        bool movieLinkMissing = contractSerie.CrossRefAniDBMovieDB == null && isMovie && !restricted;
                        bool tvlinkMissing = (contractSerie.CrossRefAniDBTvDBV2 == null ||
                                              contractSerie.CrossRefAniDBTvDBV2.Count == 0) && !isMovie && !restricted;
                        bool bothMissing = movieLinkMissing && tvlinkMissing;

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && bothMissing) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && !bothMissing) return false;
                        break;

                    case GroupFilterConditionType.CompletedSeries:
                        bool completed = contractSerie.AniDBAnime.AniDBAnime.EndDate.HasValue &&
                                         contractSerie.AniDBAnime.AniDBAnime.EndDate.Value < DateTime.Now &&
                                         !(contractSerie.MissingEpisodeCount > 0 ||
                                           contractSerie.MissingEpisodeCountGroups > 0);
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && !completed) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && completed) return false;
                        break;

                    case GroupFilterConditionType.FinishedAiring:
                        bool finished = contractSerie.AniDBAnime.AniDBAnime.EndDate.HasValue &&
                                        contractSerie.AniDBAnime.AniDBAnime.EndDate.Value < DateTime.Now;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && !finished) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && finished) return false;
                        break;

                    case GroupFilterConditionType.UserVoted:

                        bool voted = (contractSerie.AniDBAnime.UserVote != null) &&
                                     (contractSerie.AniDBAnime.UserVote.VoteType == (int) AniDBVoteType.Anime);
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && !voted) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && voted) return false;
                        break;

                    case GroupFilterConditionType.UserVotedAny:
                        bool votedany = contractSerie.AniDBAnime.UserVote != null;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && !votedany) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && votedany) return false;
                        break;

                    case GroupFilterConditionType.AirDate:
                        DateTime filterDate;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            int days = 0;
                            int.TryParse(gfc.ConditionParameter, out days);
                            filterDate = DateTime.Today.AddDays(0 - days);
                        }
                        else
                            filterDate = GroupFilterHelper.GetDateFromString(gfc.ConditionParameter);

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan ||
                            gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                            if (!contractSerie.AniDBAnime.AniDBAnime.AirDate.HasValue ||
                                contractSerie.AniDBAnime.AniDBAnime.AirDate.Value < filterDate) return false;

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
                            if (!contractSerie.AniDBAnime.AniDBAnime.AirDate.HasValue ||
                                contractSerie.AniDBAnime.AniDBAnime.AirDate.Value > filterDate) return false;
                        break;
                    case GroupFilterConditionType.LatestEpisodeAirDate:
                        DateTime filterDateEpisodeLastAired;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            int days = 0;
                            int.TryParse(gfc.ConditionParameter, out days);
                            filterDateEpisodeLastAired = DateTime.Today.AddDays(0 - days);
                        }
                        else
                            filterDateEpisodeLastAired = GetDateFromString(gfc.ConditionParameter);

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan ||
                            gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            if (!contractSerie.LatestEpisodeAirDate.HasValue) return false;
                            if (contractSerie.LatestEpisodeAirDate.Value < filterDateEpisodeLastAired) return false;
                        }
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
                        {
                            if (!contractSerie.LatestEpisodeAirDate.HasValue) return false;
                            if (contractSerie.LatestEpisodeAirDate.Value > filterDateEpisodeLastAired) return false;
                        }
                        break;
                    case GroupFilterConditionType.SeriesCreatedDate:
                        DateTime filterDateSeries;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            int days = 0;
                            int.TryParse(gfc.ConditionParameter, out days);
                            filterDateSeries = DateTime.Today.AddDays(0 - days);
                        }
                        else
                            filterDateSeries = GetDateFromString(gfc.ConditionParameter);

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan ||
                            gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            if (contractSerie.DateTimeCreated < filterDateSeries) return false;
                        }
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
                        {
                            if (contractSerie.DateTimeCreated > filterDateSeries) return false;
                        }
                        break;

                    case GroupFilterConditionType.EpisodeWatchedDate:
                        DateTime filterDateEpsiodeWatched;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            int days = 0;
                            int.TryParse(gfc.ConditionParameter, out days);
                            filterDateEpsiodeWatched = DateTime.Today.AddDays(0 - days);
                        }
                        else
                            filterDateEpsiodeWatched = GetDateFromString(gfc.ConditionParameter);

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan ||
                            gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            if (!contractSerie.WatchedDate.HasValue) return false;
                            if (contractSerie.WatchedDate.Value < filterDateEpsiodeWatched) return false;
                        }
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
                        {
                            if (contractSerie == null) return false;
                            if (!contractSerie.WatchedDate.HasValue) return false;
                            if (contractSerie.WatchedDate.Value > filterDateEpsiodeWatched) return false;
                        }
                        break;

                    case GroupFilterConditionType.EpisodeAddedDate:
                        DateTime filterDateEpisodeAdded;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            int days = 0;
                            int.TryParse(gfc.ConditionParameter, out days);
                            filterDateEpisodeAdded = DateTime.Today.AddDays(0 - days);
                        }
                        else
                            filterDateEpisodeAdded = GetDateFromString(gfc.ConditionParameter);

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan ||
                            gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
                        {
                            if (!contractSerie.EpisodeAddedDate.HasValue) return false;
                            if (contractSerie.EpisodeAddedDate.Value < filterDateEpisodeAdded) return false;
                        }
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
                        {
                            if (!contractSerie.EpisodeAddedDate.HasValue) return false;
                            if (contractSerie.EpisodeAddedDate.Value > filterDateEpisodeAdded) return false;
                        }
                        break;

                    case GroupFilterConditionType.EpisodeCount:

                        int epCount = -1;
                        int.TryParse(gfc.ConditionParameter, out epCount);

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan &&
                            contractSerie.AniDBAnime.AniDBAnime.EpisodeCount < epCount) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan &&
                            contractSerie.AniDBAnime.AniDBAnime.EpisodeCount > epCount) return false;
                        break;

                    case GroupFilterConditionType.AniDBRating:

                        decimal dRating = -1;
                        decimal.TryParse(gfc.ConditionParameter, style, culture, out dRating);

                        int totalVotes = contractSerie.AniDBAnime.AniDBAnime.VoteCount +
                                         contractSerie.AniDBAnime.AniDBAnime.TempVoteCount;
                        decimal totalRating = contractSerie.AniDBAnime.AniDBAnime.Rating*
                                              contractSerie.AniDBAnime.AniDBAnime.VoteCount +
                                              contractSerie.AniDBAnime.AniDBAnime.TempRating*
                                              contractSerie.AniDBAnime.AniDBAnime.TempVoteCount;
                        decimal thisRating = totalVotes == 0 ? 0 : totalRating/totalVotes/100;

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan && thisRating < dRating)
                            return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan && thisRating > dRating)
                            return false;
                        break;

                    case GroupFilterConditionType.UserRating:

                        decimal dUserRating = -1;
                        decimal.TryParse(gfc.ConditionParameter, style, culture, out dUserRating);
                        decimal val = contractSerie.AniDBAnime.UserVote?.VoteValue ?? 0;

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan && val < dUserRating)
                            return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan && val > dUserRating)
                            return false;
                        break;

                    case GroupFilterConditionType.CustomTags:

                        List<string> ctags =
                            gfc.ConditionParameter.Trim()
                                .Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries)
                                .Select(a => a.ToLowerInvariant().Trim())
                                .ToList();
                        bool foundTag =
                            ctags.FindInEnumerable(contractSerie.AniDBAnime.CustomTags.Select(a => a.TagName));
                        if ((gfc.ConditionOperatorEnum == GroupFilterOperator.In) && !foundTag) return false;
                        if ((gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn) && foundTag) return false;
                        break;

                    case GroupFilterConditionType.AnimeType:

                        List<string> ctypes =
                            gfc.ConditionParameter.Trim()
                                .Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries)
                                .Select(a => ((int)AniDB_Anime.RawToType(a.ToLowerInvariant())).ToString())
                                .ToList();
                        bool foundAnimeType = ctypes.Contains(contractSerie.AniDBAnime.AniDBAnime.AnimeType.ToString());
                        if ((gfc.ConditionOperatorEnum == GroupFilterOperator.In) && !foundAnimeType) return false;
                        if ((gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn) && foundAnimeType) return false;
                        break;

                    case GroupFilterConditionType.VideoQuality:

                        List<string> vqs =
                            gfc.ConditionParameter.Trim()
                                .Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries)
                                .Select(a => a.ToLowerInvariant().Trim())
                                .ToList();
                        bool foundVid = vqs.FindInEnumerable(contractSerie.AniDBAnime.Stat_AllVideoQuality);
                        bool foundVidAllEps =
                            vqs.FindInEnumerable(contractSerie.AniDBAnime.Stat_AllVideoQuality_Episodes);

                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.In && !foundVid) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn && foundVid) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.InAllEpisodes && !foundVidAllEps)
                            return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotInAllEpisodes && foundVidAllEps)
                            return false;
                        break;

                    case GroupFilterConditionType.AudioLanguage:
                        List<string> als =
                            gfc.ConditionParameter.Trim()
                                .Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries)
                                .Select(a => a.ToLowerInvariant().Trim())
                                .ToList();
                        bool foundLang = als.FindInEnumerable(contractSerie.AniDBAnime.Stat_AudioLanguages);
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.In && !foundLang) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn && foundLang) return false;
                        break;

                    case GroupFilterConditionType.SubtitleLanguage:
                        List<string> ass =
                            gfc.ConditionParameter.Trim()
                                .Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries)
                                .Select(a => a.ToLowerInvariant().Trim())
                                .ToList();
                        bool foundSub = ass.FindInEnumerable(contractSerie.AniDBAnime.Stat_AudioLanguages);
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.In && !foundSub) return false;
                        if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn && foundSub) return false;
                        break;
                }
            }

            return true;
        }
Ejemplo n.º 2
0
        /*
        public void UpdateGroupFilterUser(JMMUser ruser)
        {
            AnimeGroupRepository repGroups = new AnimeGroupRepository();
            AnimeGroup_UserRepository repUserGroups = new AnimeGroup_UserRepository();
            JMMUserRepository repUser = new JMMUserRepository();
            GroupFilterRepository repGrpFilter = new GroupFilterRepository();
            List<JMMUser> users = new List<JMMUser>();
            if ((this.FilterType & (int) GroupFilterType.Directory) == (int) GroupFilterType.Directory)
                return;
            if (ruser != null)
                users.Add(ruser);
            else
                users = repUser.GetAll();
            bool change = false;
            foreach (JMMUser user in users)
            {
                List<AnimeGroup> allGrps = repGroups.GetAllTopLevelGroups(); // No Need of subgroups

                foreach (AnimeGroup grp in allGrps)
                {
                    if (EvaluateGroupFilter(grp.GetUserContract(user.JMMUserID),user.Contract))
                    {
                        if (!GroupsIds.ContainsKey(user.JMMUserID))
                        {
                            GroupsIds[user.JMMUserID] = new HashSet<int>();
                        }
                        if (!GroupsIds[user.JMMUserID].Contains(grp.AnimeGroupID))
                        {
                            GroupsIds[user.JMMUserID].Add(grp.AnimeGroupID);
                            change = true;
                        }
                    }
                    else
                    {
                        if (GroupsIds.ContainsKey(user.JMMUserID))
                        {
                            if (GroupsIds[user.JMMUserID].Contains(grp.AnimeGroupID))
                            {
                                GroupsIds[user.JMMUserID].Remove(grp.AnimeGroupID);
                                change = true;
                            }
                        }
                    }
                }
            }
            if (change)
                repGrpFilter.Save(this, true, null);
        }
        */
        public bool CalculateGroupFilterSeries(Contract_AnimeSeries ser, Contract_JMMUser user, int id)
        {
            bool change = false;
            if (EvaluateGroupFilter(ser, user))
            {
                if (!SeriesIds.ContainsKey(id))
                {
                    SeriesIds[id] = new HashSet<int>();
                }
                if (!SeriesIds[id].Contains(ser.AnimeSeriesID))
                {
                    SeriesIds[id].Add(ser.AnimeSeriesID);
                    change = true;
                }
            }
            else
            {
                if (SeriesIds.ContainsKey(id))
                {
                    if (SeriesIds[id].Contains(ser.AnimeSeriesID))
                    {
                        SeriesIds[id].Remove(ser.AnimeSeriesID);
                        change = true;
                    }
                }
            }
            return change;
        }