예제 #1
0
        /// <summary>
        /// Compares items for sorting. Returns -1 if item1 comes before item2. Returns 0 if items are of equal sort. Returns 1 if item1 should come after item2.
        /// </summary>
        /// <param name="thisItem">The item1.</param>
        /// <param name="thatItem">The item2.</param>
        /// <returns>System.Int32.</returns>
        public static int Compare(this ItemWrapper thisItem, ItemWrapper thatItem)
        {
            Logger.LogDebug("Comparing item {0} ({1}) to {2} ({3})", thisItem.Name, thisItem.InternalName, thatItem.Name, thatItem.InternalName);
            if (thisItem.DynamicId == thatItem.DynamicId)
            {
                return(0);
            }

            string thisInternalName = thisItem.InternalName.ToLower().Replace("x1_", "").Replace("p1_", "");
            string thatInternalName = thatItem.InternalName.ToLower().Replace("x1_", "").Replace("p1_", "");
            string thisSortName     = thisItem.Name;
            string thatSortName     = thatItem.Name;

            // Compare front to back, or back to front
            if (!thisItem.IsEquipment)
            {
                /*
                 *  Non-Equipent (Potions, Gems, Keys, Consumables, Follower Items)
                 */

                // Potions
                if (thisItem.ItemType == ItemType.Potion)
                {
                    if (thatItem.ItemType == ItemType.Potion)
                    {
                        return(String.Compare(thisSortName, thatSortName, StringComparison.InvariantCulture));
                    }
                    return(1);
                }
                if (thisItem.ItemType != ItemType.Potion && thatItem.ItemType == ItemType.Potion)
                {
                    return(-1);
                }

                // Gems
                if (thisItem.IsGem)
                {
                    if (thatItem.IsGem)
                    {
                        // Amethyst
                        // Diamond
                        // Emerald
                        // Ruby
                        // Topaz
                        var gemType = new[] { thisInternalName.Substring(0, 4), thatInternalName.Substring(0, 4) };
                        if (gemType[0] != gemType[1])
                        {
                            return(String.Compare(gemType[0], gemType[1], StringComparison.InvariantCulture));
                        }

                        if (thisItem.GemQuality == thatItem.GemQuality)
                        {
                            return(thisItem.ItemStackQuantity.CompareTo(thatItem.ItemStackQuantity) * -1);
                        }
                        return(thisItem.GemQuality.CompareTo(thatItem.GemQuality));
                    }
                    return(1);
                }
                if (thatItem.IsGem && !thisItem.IsGem)
                {
                    return(-1);
                }

                // Rift Keys
                if (thisItem.ItemType == ItemType.KeystoneFragment)
                {
                    // Greater
                    // Trial
                    // Normal
                    if (thatItem.ItemType == ItemType.KeystoneFragment)
                    {
                        return(thisItem.TieredLootRunKeyLevel.CompareTo(thatItem.TieredLootRunKeyLevel));
                    }
                    return(1);
                }
                if (thatItem.ItemType == ItemType.KeystoneFragment && thisItem.ItemType != ItemType.KeystoneFragment)
                {
                    return(-1);
                }

                // Ramadalini's Gift
                const string ramadalinisGiftInternalname = "Consumable_Add_Sockets";
                if (thisItem.Name.StartsWith(ramadalinisGiftInternalname))
                {
                    if (thatItem.Name.StartsWith(ramadalinisGiftInternalname))
                    {
                        return(0);
                    }
                    return(1);
                }
                if (thatItem.Name.StartsWith(ramadalinisGiftInternalname) && !thatItem.Name.StartsWith(ramadalinisGiftInternalname))
                {
                    return(-1);
                }

                // Item Quality
                if (thisItem.ItemQualityLevel != thatItem.ItemQualityLevel)
                {
                    return(thisItem.ItemQualityLevel.CompareTo(thatItem.ItemQualityLevel));
                }

                // Item Base Type
                if (thisItem.ItemBaseType != thatItem.ItemBaseType)
                {
                    return(thisItem.ItemBaseType.CompareTo(thatItem.ItemBaseType));
                }

                // Item Type
                if (thisItem.ItemType != thatItem.ItemType)
                {
                    return(thisItem.ItemType.CompareTo(thatItem.ItemType));
                }

                return(String.Compare(thisSortName, thatSortName, StringComparison.InvariantCulture));
            }
            if (thisItem.IsEquipment)
            {
                /*
                 *  Equipment (Weapons, Armor, Jewlery)
                 */

                // Two slots before one slots
                if (!thisItem.IsTwoSquareItem && thatItem.IsTwoSquareItem)
                {
                    return(-1);
                }
                if (thisItem.IsTwoSquareItem && !thatItem.IsTwoSquareItem)
                {
                    return(1);
                }

                // Sort Sets
                if (thisItem.IsSetItem && thatItem.IsSetItem && thisItem.IsTwoSquareItem)
                {
                    bool isSameSet = thisItem.ItemSetName == thatItem.ItemSetName;
                    if (isSameSet)
                    {
                        return(String.Compare(thisSortName, thatSortName, StringComparison.InvariantCulture));
                    }

                    return(String.Compare(thisItem.ItemSetName, thatItem.ItemSetName, StringComparison.InvariantCulture));
                }

                // Compare ItemQualityLevel - Legendaries come before other junk
                if (thisItem.ItemQualityLevel != thatItem.ItemQualityLevel)
                {
                    return(thisItem.ItemQualityLevel.CompareTo(thatItem.ItemQualityLevel));
                }

                // Compare ItemBaseType order
                if (thisItem.ItemBaseType == ItemBaseType.Weapon && thatItem.ItemBaseType != ItemBaseType.Weapon)
                {
                    return(1);
                }
                if (thatItem.ItemBaseType == ItemBaseType.Weapon && thisItem.ItemBaseType != ItemBaseType.Weapon)
                {
                    return(-1);
                }

                // Compare Armor
                if (thisItem.ItemBaseType == ItemBaseType.Armor)
                {
                    if (thatItem.ItemBaseType == ItemBaseType.Weapon)
                    {
                        return(-1);
                    }
                    if (thatItem.ItemBaseType != ItemBaseType.Armor)
                    {
                        return(1);
                    }

                    if (thisItem.ItemType != thatItem.ItemType)
                    {
                        if (thisItem.IsTwoSquareItem && !thatItem.IsTwoSquareItem)
                        {
                            return(1);
                        }

                        if (!thisItem.IsTwoSquareItem && thatItem.IsTwoSquareItem)
                        {
                            return(-1);
                        }

                        return(thisItem.ItemType.CompareTo(thatItem.ItemType));
                    }

                    return(String.Compare(thisSortName, thatSortName, StringComparison.InvariantCulture));
                }

                // Compare Jewlery
                if (thisItem.ItemBaseType == ItemBaseType.Jewelry)
                {
                    if (thatItem.ItemBaseType == ItemBaseType.Weapon)
                    {
                        return(-1);
                    }

                    if (thatItem.ItemBaseType == ItemBaseType.Armor)
                    {
                        return(-1);
                    }

                    if (thisItem.ItemType != thatItem.ItemType)
                    {
                        return(thisItem.ItemType.CompareTo(thatItem.ItemType));
                    }

                    return(String.Compare(thisSortName, thatSortName, StringComparison.InvariantCulture));
                }

                // Weapons, sort by Has Sockets, Average Damage
                if (thisItem.ItemBaseType == ItemBaseType.Weapon)
                {
                    if (thisItem.StatsData.Sockets > 0 && thatItem.StatsData.Sockets == 0)
                    {
                        return(1);
                    }
                    if (thatItem.StatsData.Sockets > 0 && thisItem.StatsData.Sockets == 0)
                    {
                        return(-1);
                    }

                    if (((thisItem.StatsData.MinDamage + thisItem.Stats.MaxDamage) / 2) > ((thatItem.StatsData.MinDamage + thatItem.StatsData.MaxDamage) / 2))
                    {
                        return(1);
                    }

                    if (((thisItem.StatsData.MinDamage + thisItem.Stats.MaxDamage) / 2) < ((thatItem.StatsData.MinDamage + thatItem.StatsData.MaxDamage) / 2))
                    {
                        return(-1);
                    }

                    if (thisItem.ItemType != thatItem.ItemType)
                    {
                        return(thisItem.ItemType.CompareTo(thatItem.ItemType));
                    }

                    return(thisItem.StatsData.WeaponDamagePerSecond.CompareTo(thatItem.StatsData.WeaponDamagePerSecond));
                }
                return(1);
            }
            return(-1);
        }