< Summary

Class:WearableItem
Assembly:AvatarAssets
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Models/AvatarAssets/WearableItem.cs
Covered lines:47
Uncovered lines:13
Coverable lines:60
Total lines:237
Line coverage:78.3% (47 of 60)
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%
IsSmart()0%9.089090%
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
 244650    private readonly Dictionary<string, string> cachedI18n = new Dictionary<string, string>();
 51
 52    public Representation GetRepresentation(string bodyShapeType)
 53    {
 374654        if (data?.representations == null)
 255            return null;
 56
 761057        for (int i = 0; i < data.representations.Length; i++)
 58        {
 380359            if (data.representations[i].bodyShapes.Contains(bodyShapeType))
 60            {
 374261                return data.representations[i];
 62            }
 63        }
 64
 265        return null;
 66    }
 67
 244668    private readonly Dictionary<string, ContentProvider> cachedContentProviers = new Dictionary<string, ContentProvider>
 69
 70    public ContentProvider GetContentProvider(string bodyShapeType)
 71    {
 1372        var representation = GetRepresentation(bodyShapeType);
 73
 1374        if (representation == null)
 075            return null;
 76
 1377        if (!cachedContentProviers.ContainsKey(bodyShapeType))
 78        {
 1279            var contentProvider = CreateContentProvider(baseUrl, representation.contents);
 1280            contentProvider.BakeHashes();
 1281            cachedContentProviers.Add(bodyShapeType, contentProvider);
 82        }
 83
 1384        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    {
 374098        if (data?.representations == null)
 099            return false;
 100
 10098101        for (int i = 0; i < data.representations.Length; i++)
 102        {
 4154103            if (data.representations[i].bodyShapes.Contains(bodyShapeType))
 104            {
 2845105                return true;
 106            }
 107        }
 108
 895109        return false;
 110    }
 111
 112    public string[] GetReplacesList(string bodyShapeType)
 113    {
 3711114        var representation = GetRepresentation(bodyShapeType);
 115
 3711116        if (representation?.overrideReplaces == null || representation.overrideReplaces.Length == 0)
 3709117            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 bool IsSmart()
 135    {
 36136        if (data?.representations == null) return false;
 137
 140138        for (var i = 0; i < data.representations.Length; i++)
 139        {
 36140            var representation = data.representations[i];
 98141            var containsGameJs = representation.contents?.Any(pair => pair.key.EndsWith("game.js")) ?? false;
 38142            if (containsGameJs) return true;
 143        }
 144
 34145        return false;
 146    }
 147
 148    public string GetName(string langCode = "en")
 149    {
 37150        if (!cachedI18n.ContainsKey(langCode))
 151        {
 36152            cachedI18n.Add(langCode, i18n.FirstOrDefault(x => x.code == langCode)?.text);
 153        }
 154
 37155        return cachedI18n[langCode];
 156    }
 157
 158    public int GetIssuedCountFromRarity(string rarity)
 159    {
 160        switch (rarity)
 161        {
 162            case WearableLiterals.ItemRarity.RARE:
 4163                return 5000;
 164            case WearableLiterals.ItemRarity.EPIC:
 27165                return 1000;
 166            case WearableLiterals.ItemRarity.LEGENDARY:
 2167                return 100;
 168            case WearableLiterals.ItemRarity.MYTHIC:
 2169                return 10;
 170            case WearableLiterals.ItemRarity.UNIQUE:
 2171                return 1;
 172        }
 173
 0174        return int.MaxValue;
 175    }
 176
 288177    public string ComposeThumbnailUrl() { return baseUrl + thumbnail; }
 178
 179    public static HashSet<string> CompoundHidesList(string bodyShapeId, List<WearableItem> wearables)
 180    {
 0181        HashSet<string> result = new HashSet<string>();
 182        //Last wearable added has priority over the rest
 0183        for (int i = wearables.Count - 1; i >= 0; i--)
 184        {
 0185            var wearableItem = wearables[i];
 186
 0187            if (result.Contains(wearableItem.data.category)) //Skip hidden elements to avoid two elements hiding each ot
 188                continue;
 189
 0190            var wearableHidesList = wearableItem.GetHidesList(bodyShapeId);
 0191            if (wearableHidesList != null)
 192            {
 0193                result.UnionWith(wearableHidesList);
 194            }
 195        }
 196
 0197        return result;
 198    }
 199
 200    //Workaround to know the net of a wearable.
 201    //Once wearables are allowed to be moved from Ethereum to Polygon this method wont be reliable anymore
 202    //To retrieve this properly first we need the catalyst to send the net of each wearable, not just the ID
 203    public bool IsInL2()
 204    {
 36205        if (id.StartsWith("urn:decentraland:matic") || id.StartsWith("urn:decentraland:mumbai"))
 0206            return true;
 36207        return false;
 208    }
 209}
 210
 211[System.Serializable]
 212public class WearablesRequestResponse
 213{
 214    public WearableItem[] wearables;
 215    public string context;
 216}
 217
 218[System.Serializable]
 219public class WearablesRequestFailed
 220{
 221    public string error;
 222    public string context;
 223}
 224
 225[System.Serializable]
 226public class WearableContent
 227{
 228    public string file;
 229    public string hash;
 230}
 231
 232[System.Serializable]
 233public class i18n
 234{
 235    public string code;
 236    public string text;
 237}