예제 #1
0
        /// <summary>
        /// Sort a spell list based on what kind of filters were used.
        /// </summary>
        public void Sort(List <Spell> list, SpellSearchFilter filter)
        {
            int cls = SpellParser.ParseClass(filter.Class) - 1;
            int id;

            Int32.TryParse(filter.Text, out id);

            // 1. if an effect is selected then sort by the effect strength
            // this is problematic since many spells have conditional effects
            //if (effect.Contains(@"(\d+)"))
            //{
            //    Sorting = "Results have been sorted by descending " + SearchEffect.Text + " strength.";
            //    var re = new Regex(effect, RegexOptions.IgnoreCase);
            //    Results.Sort((a, b) =>
            //    {
            //        int comp = b.ScoreEffect(re) - a.ScoreEffect(re);
            //        if (comp == 0)
            //            comp = a.ID - b.ID;
            //        return comp;
            //    });
            //}
            // 2. if a class is selected then sort by the casting levels for that class first
            // place castable spells before non castable effects (level == 0)
            if (cls >= 0)
            {
                list.Sort((a, b) =>
                {
                    if (a.Levels[cls] > 0 && b.Levels[cls] == 0)
                    {
                        return(-1);
                    }
                    if (b.Levels[cls] > 0 && a.Levels[cls] == 0)
                    {
                        return(1);
                    }
                    // move AA to bottom of list
                    if (a.Levels[cls] >= 250 && b.Levels[cls] < 250)
                    {
                        return(1);
                    }
                    if (b.Levels[cls] >= 250 && a.Levels[cls] < 250)
                    {
                        return(-1);
                    }
                    // b-a = descending
                    // a-b = ascending
                    int comp = b.Levels[cls] - a.Levels[cls];
                    if (comp == 0)
                    {
                        comp = String.Compare(StripRank(a.Name), StripRank(b.Name));
                    }
                    if (comp == 0)
                    {
                        comp = a.ID - b.ID;
                    }
                    return(comp);
                });
            }
            // 3. finally sort by name if no better method is found
            else
            {
                list.Sort((a, b) =>
                {
                    int comp = String.Compare(StripRank(a.Name), StripRank(b.Name));
                    if (comp == 0)
                    {
                        comp = a.ID - b.ID;
                    }
                    return(comp);
                });
            }

            // if searching by id, move the spell to the top of the results because it may be sorted below it's side effect spells
            if (id > 0)
            {
                var i = list.FindIndex(x => x.ID == id);
                if (i > 0)
                {
                    var move = list[i];
                    list.RemoveAt(i);
                    list.Insert(0, move);
                }
            }
            // move entries that begin with the search text to the front of the results
            else if (!String.IsNullOrEmpty(filter.Text))
            {
                var move = list.FindAll(x => x.Name.StartsWith(filter.Text, StringComparison.InvariantCultureIgnoreCase));
                if (move.Count > 0)
                {
                    list.RemoveAll(x => x.Name.StartsWith(filter.Text, StringComparison.InvariantCultureIgnoreCase));
                    list.InsertRange(0, move);
                }
            }
        }
예제 #2
0
        public List <Spell> Search(SpellSearchFilter filter)
        {
            //return filter.Apply(SpellList);
            if (filter.ClassMaxLevel == 0)
            {
                filter.ClassMinLevel = 1;
            }
            if (filter.ClassMaxLevel == 0)
            {
                filter.ClassMaxLevel = 255;
            }

            IEnumerable <Spell> query = SpellList;

            // if the spell text filter is an integer then just do a quick search by ID and ignore the other filters
            int id;

            if (Int32.TryParse(filter.Text, out id))
            {
                return(query.Where(x => x.ID == id || x.GroupID == id).ToList());
            }

            //  spell name and description are checked for literal text
            if (!String.IsNullOrEmpty(filter.Text))
            {
                query = query.Where(x => x.ID.ToString() == filter.Text ||
                                    x.Name.IndexOf(filter.Text, StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                                    (x.Stacking != null && x.Stacking.Any(y => y.IndexOf(filter.Text, StringComparison.InvariantCultureIgnoreCase) >= 0)) ||
                                    (x.Desc != null && x.Desc.IndexOf(filter.Text, StringComparison.InvariantCultureIgnoreCase) >= 0));
            }

            // level filter is only used when a class is selected
            int levelArrayIndex = SpellParser.ParseClass(filter.Class) - 1;

            if (filter.Class == "Any PC")
            {
                query = query.Where(x => x.ClassesMask != 0);
            }
            else if (filter.Class == "Non PC")
            {
                //query = query.Where(x => x.ClassesMask == 0);
                query = query.Where(x => x.ExtLevels.All(y => y == 0));
            }
            else if (!String.IsNullOrEmpty(filter.Class) && filter.Category != "AA")
            {
                if (levelArrayIndex >= 0)
                {
                    query = query.Where(x => x.ClassesLevels != "ALL/254" && x.ExtLevels[levelArrayIndex] >= filter.ClassMinLevel && x.ExtLevels[levelArrayIndex] <= filter.ClassMaxLevel);
                }
            }

            if (!String.IsNullOrEmpty(filter.Category))
            {
                if (filter.Category == "AA" && levelArrayIndex >= 0)
                {
                    query = query.Where(x => x.ExtLevels[levelArrayIndex] == 254);
                }
                else if (filter.Category == "AA")
                {
                    query = query.Where(x => x.ExtLevels.Any(y => y == 254));
                }
                else
                {
                    query = query.Where(x => x.Categories.Any(y => y.StartsWith(filter.Category, StringComparison.InvariantCultureIgnoreCase)));
                }
            }

            // effect filter can be a literal string or a regex
            for (int i = 0; i < filter.Effect.Length; i++)
            {
                if (!String.IsNullOrEmpty(filter.Effect[i]))
                {
                    string effect = null;
                    if (SpellSearchFilter.CommonEffects.ContainsKey(filter.Effect[i]))
                    {
                        effect = SpellSearchFilter.CommonEffects[filter.Effect[i]];
                    }

                    if (!String.IsNullOrEmpty(effect))
                    {
                        var re   = new Regex(effect, RegexOptions.IgnoreCase);
                        int?slot = filter.EffectSlot[i];
                        query = query.Where(x => x.HasEffect(re, slot ?? 0));
                    }
                    else
                    {
                        string text = filter.Effect[i];
                        int?   slot = filter.EffectSlot[i];
                        query = query.Where(x => x.HasEffect(text, slot ?? 0));
                    }
                }
            }

            //if (filter.Ranks == "Unranked")
            //    query = query.Where(x => x.Rank == 0);
            //if (filter.Ranks == "Rank 1")
            //    query = query.Where(x => x.Rank == 1);
            //if (filter.Ranks == "Rank 2")
            //    query = query.Where(x => x.Rank == 2);
            //if (filter.Ranks == "Rank 3")
            //    query = query.Where(x => x.Rank == 3);
            //if (filter.Ranks == "Unranked + Rank 1")
            //    query = query.Where(x => x.Rank == 0 || x.Rank == 1);
            //if (filter.Ranks == "Unranked + Rank 2")
            //    query = query.Where(x => x.Rank == 0 || x.Rank == 2);
            //if (filter.Ranks == "Unranked + Rank 3")
            //    query = query.Where(x => x.Rank == 0 || x.Rank == 3);

            return(query.ToList());
        }