< Summary

Class:WearableItem
Assembly:AvatarAssets
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Models/AvatarAssets/WearableItem.cs
Covered lines:41
Uncovered lines:13
Coverable lines:54
Total lines:223
Line coverage:75.9% (41 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WearableItem()0%110100%
GetRepresentation(...)0%660100%
GetContentProvider(...)0%3.023087.5%
CreateContentProvider(...)0%220100%
SupportsBodyShape(...)0%6.076087.5%
GetReplacesList(...)0%550100%
GetHidesList(...)0%550100%
IsCollectible()0%2100%
GetName(...)0%440100%
GetIssuedCountFromRarity(...)0%7.237083.33%
ComposeThumbnailUrl()0%110100%
CompoundHidesList(...)0%20400%
IsInL2()0%3.333066.67%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Models/AvatarAssets/WearableItem.cs

#LineLine coverage
 1using DCL;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5
 6[System.Serializable]
 7public class WearableItem
 8{
 9    [Serializable]
 10    public class MappingPair
 11    {
 12        public string key;
 13        public string hash;
 14    }
 15
 16    [Serializable]
 17    public class Representation
 18    {
 19        public string[] bodyShapes;
 20        public string mainFile;
 21        public MappingPair[] contents;
 22        public string[] overrideHides;
 23        public string[] overrideReplaces;
 24    }
 25
 26    [Serializable]
 27    public class Data
 28    {
 29        public Representation[] representations;
 30        public string category;
 31        public string[] tags;
 32        public string[] replaces;
 33        public string[] hides;
 34    }
 35
 36    public Data data;
 37    public string id;
 38
 39    public string baseUrl;
 40    public string baseUrlBundles;
 41
 42    public i18n[] i18n;
 43    public string thumbnail;
 44
 45    //This fields are temporary, once Kernel is finished we must move them to wherever they are placed
 46    public string rarity;
 47    public string description;
 48    public int issuedId;
 49
 445250    private readonly Dictionary<string, string> cachedI18n = new Dictionary<string, string>();
 51
 52    public Representation GetRepresentation(string bodyShapeType)
 53    {
 360854        if (data?.representations == null)
 255            return null;
 56
 733057        for (int i = 0; i < data.representations.Length; i++)
 58        {
 366359            if (data.representations[i].bodyShapes.Contains(bodyShapeType))
 60            {
 360461                return data.representations[i];
 62            }
 63        }
 64
 265        return null;
 66    }
 67
 445268    private readonly Dictionary<string, ContentProvider> cachedContentProviers = new Dictionary<string, ContentProvider>
 69
 70    public ContentProvider GetContentProvider(string bodyShapeType)
 71    {
 1472        var representation = GetRepresentation(bodyShapeType);
 73
 1474        if (representation == null)
 075            return null;
 76
 1477        if (!cachedContentProviers.ContainsKey(bodyShapeType))
 78        {
 1379            var contentProvider = CreateContentProvider(baseUrl, representation.contents);
 1380            contentProvider.BakeHashes();
 1381            cachedContentProviers.Add(bodyShapeType, contentProvider);
 82        }
 83
 1484        return cachedContentProviers[bodyShapeType];
 85    }
 86
 87    protected virtual ContentProvider CreateContentProvider(string baseUrl, MappingPair[] contents)
 88    {
 189        return new ContentProvider
 90        {
 91            baseUrl = baseUrl,
 192            contents = contents.Select(mapping => new ContentServerUtils.MappingPair() { file = mapping.key, hash = mapp
 93        };
 94    }
 95
 96    public bool SupportsBodyShape(string bodyShapeType)
 97    {
 359498        if (data?.representations == null)
 099            return false;
 100
 9708101        for (int i = 0; i < data.representations.Length; i++)
 102        {
 3991103            if (data.representations[i].bodyShapes.Contains(bodyShapeType))
 104            {
 2731105                return true;
 106            }
 107        }
 108
 863109        return false;
 110    }
 111
 112    public string[] GetReplacesList(string bodyShapeType)
 113    {
 3571114        var representation = GetRepresentation(bodyShapeType);
 115
 3571116        if (representation?.overrideReplaces == null || representation.overrideReplaces.Length == 0)
 3569117            return data.replaces;
 118
 2119        return representation.overrideReplaces;
 120    }
 121
 122    public string[] GetHidesList(string bodyShapeType)
 123    {
 3124        var representation = GetRepresentation(bodyShapeType);
 125
 3126        if (representation?.overrideHides == null || representation.overrideHides.Length == 0)
 2127            return data.hides;
 128
 1129        return representation.overrideHides;
 130    }
 131
 0132    public bool IsCollectible() { return !string.IsNullOrEmpty(rarity); }
 133
 134    public string GetName(string langCode = "en")
 135    {
 33136        if (!cachedI18n.ContainsKey(langCode))
 137        {
 32138            cachedI18n.Add(langCode, i18n.FirstOrDefault(x => x.code == langCode)?.text);
 139        }
 140
 33141        return cachedI18n[langCode];
 142    }
 143
 144    public int GetIssuedCountFromRarity(string rarity)
 145    {
 146        switch (rarity)
 147        {
 148            case WearableLiterals.ItemRarity.RARE:
 2149                return 5000;
 150            case WearableLiterals.ItemRarity.EPIC:
 25151                return 1000;
 152            case WearableLiterals.ItemRarity.LEGENDARY:
 2153                return 100;
 154            case WearableLiterals.ItemRarity.MYTHIC:
 2155                return 10;
 156            case WearableLiterals.ItemRarity.UNIQUE:
 2157                return 1;
 158        }
 159
 0160        return int.MaxValue;
 161    }
 162
 276163    public string ComposeThumbnailUrl() { return baseUrl + thumbnail; }
 164
 165    public static HashSet<string> CompoundHidesList(string bodyShapeId, List<WearableItem> wearables)
 166    {
 0167        HashSet<string> result = new HashSet<string>();
 168        //Last wearable added has priority over the rest
 0169        for (int i = wearables.Count - 1; i >= 0; i--)
 170        {
 0171            var wearableItem = wearables[i];
 172
 0173            if (result.Contains(wearableItem.data.category)) //Skip hidden elements to avoid two elements hiding each ot
 174                continue;
 175
 0176            var wearableHidesList = wearableItem.GetHidesList(bodyShapeId);
 0177            if (wearableHidesList != null)
 178            {
 0179                result.UnionWith(wearableHidesList);
 180            }
 181        }
 182
 0183        return result;
 184    }
 185
 186    //Workaround to know the net of a wearable.
 187    //Once wearables are allowed to be moved from Ethereum to Polygon this method wont be reliable anymore
 188    //To retrieve this properly first we need the catalyst to send the net of each wearable, not just the ID
 189    public bool IsInL2()
 190    {
 32191        if (id.StartsWith("urn:decentraland:matic") || id.StartsWith("urn:decentraland:mumbai"))
 0192            return true;
 32193        return false;
 194    }
 195}
 196
 197[System.Serializable]
 198public class WearablesRequestResponse
 199{
 200    public WearableItem[] wearables;
 201    public string context;
 202}
 203
 204[System.Serializable]
 205public class WearablesRequestFailed
 206{
 207    public string error;
 208    public string context;
 209}
 210
 211[System.Serializable]
 212public class WearableContent
 213{
 214    public string file;
 215    public string hash;
 216}
 217
 218[System.Serializable]
 219public class i18n
 220{
 221    public string code;
 222    public string text;
 223}