Example #1
0
        public Item(IPersistentData persistentData, JObject val, ItemSlot itemSlot = ItemSlot.Unequipable, bool isGem = false)
        {
            JsonBase = val;
            Slot     = itemSlot;

            Width  = val["w"].Value <int>();
            Height = val["h"].Value <int>();
            if (val["x"] != null)
            {
                X = val["x"].Value <int>();
            }
            if (val["y"] != null)
            {
                Y = val["y"].Value <int>();
            }

            if (val["name"] != null)
            {
                NameLine = FilterJsonString(val["name"].Value <string>());
            }

            JToken iconToken;

            if (val.TryGetValue("icon", out iconToken))
            {
                _iconUrl = iconToken.Value <string>();
            }

            Frame    = (FrameType)val["frameType"].Value <int>();
            TypeLine = FilterJsonString(val["typeLine"].Value <string>());
            if (isGem)
            {
                // BaseType will be null for socketed gems.
                ItemGroup = ItemGroup.Gem;
            }
            else
            {
                if (Frame == FrameType.Magic)
                {
                    BaseType = persistentData.EquipmentData.ItemBaseFromTypeline(TypeLine);
                }
                else if ((Frame == FrameType.Unique || Frame == FrameType.Foil) &&
                         persistentData.EquipmentData.UniqueBaseDictionary.ContainsKey(NameLine))
                {
                    BaseType = persistentData.EquipmentData.UniqueBaseDictionary[NameLine];
                }
                else
                {
                    // item is not unique or the unique is unknown
                    ItemBase iBase;
                    persistentData.EquipmentData.ItemBaseDictionary.TryGetValue(TypeLine, out iBase);
                    BaseType = iBase;
                }
                // For known bases, images are only downloaded if the item is unique or foil. All other items should
                // always have the same image. (except alt art non-uniques that are rare enough to be ignored)
                var loadImageFromIconUrl = _iconUrl != null &&
                                           (BaseType == null || Frame == FrameType.Unique || Frame == FrameType.Foil);
                if (BaseType == null)
                {
                    BaseType = new ItemBase(persistentData.EquipmentData.ItemImageService, itemSlot, TypeLine,
                                            Keywords == null ? "" : Keywords.FirstOrDefault(), Frame);
                }
                ItemType  = BaseType.ItemType;
                ItemGroup = BaseType.ItemGroup;
                if (loadImageFromIconUrl)
                {
                    Image = BaseType.Image.AsDefaultForImageFromUrl(
                        persistentData.EquipmentData.ItemImageService, _iconUrl);
                }
                else
                {
                    Image = BaseType.Image;
                }
            }

            if (val["properties"] != null)
            {
                foreach (var obj in val["properties"])
                {
                    Properties.Add(ItemModFromJson(obj, ModGroup.Property));
                }
                if (Properties.Any(m => !m.Value.Any()))
                {
                    // The name of one property of gems contains the Keywords of that gem.
                    Keywords = Properties.First(m => !m.Value.Any()).Attribute.Split(',').Select(i => i.Trim()).ToList();
                }
            }

            if (val["requirements"] != null)
            {
                var mods = val["requirements"].Select(t => ItemModFromJson(t, ModGroup.Requirement)).ToList();
                if (!mods.Any(m => m.Attribute.StartsWith("Requires ")))
                {
                    var modsToMerge = new []
                    {
                        mods.FirstOrDefault(m => m.Attribute == "Level #"),
                        mods.FirstOrDefault(m => m.Attribute == "# Str"),
                        mods.FirstOrDefault(m => m.Attribute == "# Dex"),
                        mods.FirstOrDefault(m => m.Attribute == "# Int")
                    }.Where(m => m != null).ToList();
                    modsToMerge.ForEach(m => mods.Remove(m));
                    mods.Add(new ItemMod(
                                 ItemType,
                                 "Requires " + string.Join(", ", modsToMerge.Select(m => m.Attribute)),
                                 ModGroup.Requirement)
                    {
                        Value      = modsToMerge.Select(m => m.Value).Flatten().ToList(),
                        ValueColor = modsToMerge.Select(m => m.ValueColor).Flatten().ToList()
                    });
                }
                _requirements.AddRange(mods);
            }


            if (val["implicitMods"] != null)
            {
                foreach (var s in val["implicitMods"].Values <string>())
                {
                    _implicitMods.Add(new ItemMod(ItemType, FixOldRanges(s), ModGroup.Implicit, Numberfilter));
                }
            }
            if (val["explicitMods"] != null)
            {
                foreach (var s in val["explicitMods"].Values <string>())
                {
                    ExplicitMods.Add(new ItemMod(ItemType, FixOldRanges(s), ModGroup.Explicit, Numberfilter));
                }
            }
            if (val["craftedMods"] != null)
            {
                foreach (var s in val["craftedMods"].Values <string>())
                {
                    CraftedMods.Add(new ItemMod(ItemType, FixOldRanges(s), ModGroup.Crafted, Numberfilter));
                }
            }

            if (val["flavourText"] != null)
            {
                FlavourText = string.Join("\r\n", val["flavourText"].Values <string>().Select(s => s.Replace("\r", "")));
            }

            var sockets = new List <int>();

            if (val["sockets"] != null)
            {
                foreach (var obj in (JArray)val["sockets"])
                {
                    sockets.Add(obj["group"].Value <int>());
                }
            }
            if (val["socketedItems"] != null)
            {
                int socket = 0;
                foreach (JObject obj in (JArray)val["socketedItems"])
                {
                    var item = new Item(persistentData, obj, isGem: true)
                    {
                        SocketGroup = sockets[socket++]
                    };
                    _gems.Add(item);
                }
            }
        }
Example #2
0
        public Item(IPersistentData persistentData, JObject val, ItemSlot itemSlot = ItemSlot.Unequipable, bool isGem = false)
        {
            JsonBase = val;
            Slot     = itemSlot;

            Width  = val["w"].Value <int>();
            Height = val["h"].Value <int>();
            if (val["x"] != null)
            {
                X = val["x"].Value <int>();
            }
            if (val["y"] != null)
            {
                Y = val["y"].Value <int>();
            }

            if (val["name"] != null)
            {
                NameLine = FilterJsonString(val["name"].Value <string>());
            }

            JToken iconToken;

            if (val.TryGetValue("icon", out iconToken))
            {
                _iconUrl = iconToken.Value <string>();
            }

            Frame    = (FrameType)val["frameType"].Value <int>();
            TypeLine = FilterJsonString(val["typeLine"].Value <string>());
            if (isGem)
            {
                // BaseType will be null for socketed gems.
                _itemGroup = ItemGroup.Gem;
            }
            else
            {
                if (Frame == FrameType.Magic)
                {
                    _baseType = persistentData.EquipmentData.ItemBaseFromTypeline(TypeLine);
                }
                else
                {
                    persistentData.EquipmentData.BaseDictionary.TryGetValue(TypeLine, out _baseType);
                }
                // For known bases, images are only downloaded if the item is unique. All other items should always
                // have the same image. (except alt art non-uniques that are rare enough to be ignored)
                var loadImageFromIconUrl = _iconUrl != null && (_baseType == null || Frame == FrameType.Unique);
                if (_baseType == null)
                {
                    _baseType = new ItemBase(persistentData.Options, itemSlot, TypeLine,
                                             _keywords == null ? "" : _keywords.FirstOrDefault(), Frame);
                }
                _itemType  = BaseType.ItemType;
                _itemGroup = BaseType.ItemGroup;
                if (loadImageFromIconUrl)
                {
                    _image = new ItemImageFromOfficial(_baseType.Image, _iconUrl);
                }
                else
                {
                    _image = _baseType.Image;
                    _image.DownloadMissingImage();
                }

                FixOldItems();
            }

            if (val["properties"] != null)
            {
                foreach (var obj in val["properties"])
                {
                    Properties.Add(ItemModFromJson(obj, false));
                }
                if (Properties.Any(m => !m.Value.Any()))
                {
                    // The name of one property of gems contains the Keywords of that gem.
                    _keywords = Properties.First(m => !m.Value.Any()).Attribute.Split(',').Select(i => i.Trim()).ToList();
                }
            }

            if (val["requirements"] != null)
            {
                var mods = val["requirements"].Select(t => ItemModFromJson(t, true)).ToList();
                if (!mods.Any(m => m.Attribute.StartsWith("Requires ")))
                {
                    var modsToMerge = new []
                    {
                        mods.FirstOrDefault(m => m.Attribute == "Level #"),
                        mods.FirstOrDefault(m => m.Attribute == "# Str"),
                        mods.FirstOrDefault(m => m.Attribute == "# Dex"),
                        mods.FirstOrDefault(m => m.Attribute == "# Int")
                    }.Where(m => m != null).ToList();
                    modsToMerge.ForEach(m => mods.Remove(m));
                    mods.Add(new ItemMod(_itemType, "Requires " + string.Join(", ", modsToMerge.Select(m => m.Attribute)))
                    {
                        Value      = modsToMerge.Select(m => m.Value).Flatten().ToList(),
                        ValueColor = modsToMerge.Select(m => m.ValueColor).Flatten().ToList()
                    });
                }
                _requirements.AddRange(mods);
            }


            if (val["implicitMods"] != null)
            {
                foreach (var s in val["implicitMods"].Values <string>())
                {
                    _implicitMods.Add(new ItemMod(_itemType, s, Numberfilter));
                }
            }
            if (val["explicitMods"] != null)
            {
                foreach (var s in val["explicitMods"].Values <string>())
                {
                    ExplicitMods.Add(new ItemMod(_itemType, s, Numberfilter));
                }
            }
            if (val["craftedMods"] != null)
            {
                foreach (var s in val["craftedMods"].Values <string>())
                {
                    CraftedMods.Add(new ItemMod(_itemType, s, Numberfilter));
                }
            }

            if (val["flavourText"] != null)
            {
                FlavourText = string.Join("\r\n", val["flavourText"].Values <string>().Select(s => s.Replace("\r", "")));
            }


            if (isGem)
            {
                switch (val["colour"].Value <string>())
                {
                case "S":
                    _keywords.Add("Strength");
                    break;

                case "D":
                    _keywords.Add("Dexterity");
                    break;

                case "I":
                    _keywords.Add("Intelligence");
                    break;
                }
            }
            else
            {
                _gems = new List <Item>();
            }

            var sockets = new List <int>();

            if (val["sockets"] != null)
            {
                foreach (var obj in (JArray)val["sockets"])
                {
                    sockets.Add(obj["group"].Value <int>());
                }
            }
            if (val["socketedItems"] != null)
            {
                int socket = 0;
                foreach (JObject obj in (JArray)val["socketedItems"])
                {
                    var item = new Item(persistentData, obj, isGem: true)
                    {
                        _socketGroup = sockets[socket++]
                    };
                    _gems.Add(item);
                }
            }
        }