< Summary

Class:BIWCatalogManager
Assembly:BuilderInWorldCatalog
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Catalog/BIWCatalogManager.cs
Covered lines:120
Uncovered lines:8
Coverable lines:128
Total lines:256
Line coverage:93.7% (120 of 128)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Init()0%220100%
Dispose()0%110100%
ClearCatalog()0%110100%
GetCatalogItemPackList()0%440100%
GetCatalogItemPacksFilteredByCategories()0%9.19089.29%
AddSceneObject(...)0%110100%
AddSceneObject(...)0%2.032080%
AddSceneCatalog(...)0%110100%
AddSceneAssetPack(...)0%3.043083.33%
GetAssetPackNameById(...)0%3.043083.33%
ConvertCollectiblesPack(...)0%7.017095%
CreateNewCollectiblePack()0%220100%
CreateCatalogItemPack(...)0%330100%
CreateCatalogItem(...)0%55095%
CreateCatalogItem(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Catalog/BIWCatalogManager.cs

#LineLine coverage
 1using DCL;
 2using DCL.Configuration;
 3using DCL.Helpers.NFT;
 4using System.Collections.Generic;
 5using System.Linq;
 6using UnityEngine;
 7
 8public static class BIWCatalogManager
 9{
 10    public static bool VERBOSE = false;
 11    private static bool IS_INIT = false;
 12
 13    public static void Init()
 14    {
 6415        if (!IS_INIT)
 16        {
 1517            BIWNFTController.i.OnNftsFetched += ConvertCollectiblesPack;
 1518            AssetCatalogBridge.OnItemAdded += AddSceneObject;
 1519            AssetCatalogBridge.OnSceneCatalogItemAdded += AddSceneCatalog;
 1520            AssetCatalogBridge.OnSceneAssetPackAdded += AddSceneAssetPack;
 1521            IS_INIT = true;
 22        }
 6423    }
 24
 25    public static void Dispose()
 26    {
 1427        BIWNFTController.i.OnNftsFetched -= ConvertCollectiblesPack;
 1428        AssetCatalogBridge.OnItemAdded -= AddSceneObject;
 1429        AssetCatalogBridge.OnSceneCatalogItemAdded -= AddSceneCatalog;
 1430        AssetCatalogBridge.OnSceneAssetPackAdded -= AddSceneAssetPack;
 1431        IS_INIT = false;
 1432    }
 33
 34    public static void ClearCatalog()
 35    {
 6736        DataStore.i.builderInWorld.catalogItemDict.Clear();
 6737        DataStore.i.builderInWorld.catalogItemPackDict.Clear();
 6738    }
 39
 40    public static List<CatalogItemPack> GetCatalogItemPackList()
 41    {
 142        var assetPacks = DataStore.i.builderInWorld.catalogItemPackDict.GetValues();
 43
 444        foreach (CatalogItemPack catalogAssetPack in assetPacks)
 45        {
 646            foreach (CatalogItem catalogItem in catalogAssetPack.assets)
 47            {
 248                catalogItem.categoryName = catalogItem.category;
 49            }
 50        }
 51
 252        return assetPacks.OrderBy(x => x.title).ToList();
 53    }
 54
 55    public static List<CatalogItemPack> GetCatalogItemPacksFilteredByCategories()
 56    {
 257        var assetPacks = DataStore.i.builderInWorld.catalogItemPackDict.GetValues();
 58
 259        Dictionary<string, CatalogItemPack> assetPackDic = new Dictionary<string, CatalogItemPack>();
 60
 261        if (DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(BIWSettings.ASSETS_COLLECTIBLES))
 162            assetPackDic.Add(BIWSettings.ASSETS_COLLECTIBLES, DataStore.i.builderInWorld.catalogItemPackDict[BIWSettings
 63        else
 164            CreateNewCollectiblePack();
 65
 866        foreach (CatalogItemPack catalogAssetPack in assetPacks)
 67        {
 868            foreach (CatalogItem catalogItem in catalogAssetPack.assets)
 69            {
 270                if (catalogItem.IsNFT())
 71                    continue;
 172                if (!assetPackDic.ContainsKey(catalogItem.category))
 73                {
 174                    CatalogItemPack categoryAssetPack = new CatalogItemPack();
 175                    categoryAssetPack.SetThumbnailULR(catalogItem.thumbnailURL);
 176                    categoryAssetPack.title = catalogItem.category;
 177                    categoryAssetPack.assets = new List<CatalogItem>();
 178                    catalogItem.categoryName = catalogAssetPack.title;
 179                    categoryAssetPack.assets.Add(catalogItem);
 80
 181                    if (!string.IsNullOrEmpty(categoryAssetPack.title))
 82                    {
 183                        if (categoryAssetPack.title.Length == 1)
 084                            categoryAssetPack.title = categoryAssetPack.title.ToUpper();
 85                        else
 186                            categoryAssetPack.title = char.ToUpper(categoryAssetPack.title[0]) + categoryAssetPack.title
 87                    }
 88
 189                    assetPackDic.Add(catalogItem.category, categoryAssetPack);
 190                    continue;
 91                }
 92                else
 93                {
 094                    catalogItem.categoryName = catalogAssetPack.title;
 095                    assetPackDic[catalogItem.category].assets.Add(catalogItem);
 96                }
 97            }
 98        }
 99
 4100        return assetPackDic.Values.OrderBy(x => x.title).ToList();
 101    }
 102
 78103    public static void AddSceneObject(SceneObject sceneObject) { AddSceneObject(sceneObject, DataStore.i.builderInWorld.
 104
 105    public static void AddSceneObject(SceneObject sceneObject , BaseDictionary<string, CatalogItem> catalogToAdd )
 106    {
 40107        if (catalogToAdd.ContainsKey(sceneObject.id))
 0108            return;
 109
 40110        CatalogItem catalogItem = CreateCatalogItem(sceneObject);
 40111        catalogToAdd.Add(catalogItem.id, catalogItem);
 40112    }
 113
 2114    public static void AddSceneCatalog(SceneObject sceneObject) { AddSceneObject(sceneObject, DataStore.i.builderInWorld
 115
 116    public static void AddSceneAssetPack(SceneAssetPack sceneAssetPack)
 117    {
 32118        if (DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(sceneAssetPack.id))
 0119            return;
 120
 32121        CatalogItemPack catalogItemPack = CreateCatalogItemPack(sceneAssetPack);
 122
 32123        if (catalogItemPack.assets.Count != 0)
 31124            DataStore.i.builderInWorld.catalogItemPackDict.Add(catalogItemPack.id, catalogItemPack);
 32125    }
 126
 127    public static string GetAssetPackNameById(string assetPackId)
 128    {
 41129        if (string.IsNullOrEmpty(assetPackId))
 3130            return "";
 131
 38132        DataStore.i.builderInWorld.catalogItemPackDict.TryGetValue(assetPackId, out CatalogItemPack catalogItemPack);
 38133        if (catalogItemPack != null)
 0134            return catalogItemPack.title;
 135
 38136        return "";
 137    }
 138
 139    public static void ConvertCollectiblesPack(List<NFTInfo> nftList)
 140    {
 4141        if (nftList == null)
 0142            return;
 143
 144        CatalogItemPack collectiblesItemPack;
 145
 4146        if (!DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(BIWSettings.ASSETS_COLLECTIBLES))
 147        {
 3148            collectiblesItemPack = CreateNewCollectiblePack();
 3149        }
 150        else
 151        {
 1152            collectiblesItemPack = DataStore.i.builderInWorld.catalogItemPackDict[BIWSettings.ASSETS_COLLECTIBLES];
 4153            foreach (CatalogItem catalogItem in collectiblesItemPack.assets)
 154            {
 1155                if (DataStore.i.builderInWorld.catalogItemDict.ContainsKey(catalogItem.id))
 1156                    DataStore.i.builderInWorld.catalogItemDict.Remove(catalogItem.id);
 157            }
 158
 1159            collectiblesItemPack.assets.Clear();
 160        }
 161
 16162        foreach (NFTInfo info in nftList)
 163        {
 4164            CatalogItem catalogItem = CreateCatalogItem(info);
 4165            if (!DataStore.i.builderInWorld.catalogItemDict.ContainsKey(catalogItem.id))
 4166                DataStore.i.builderInWorld.catalogItemDict.Add(catalogItem.id, catalogItem);
 167
 4168            collectiblesItemPack.assets.Add(catalogItem);
 169        }
 4170    }
 171
 172    private static CatalogItemPack CreateNewCollectiblePack()
 173    {
 4174        CatalogItemPack collectiblesItemPack = new CatalogItemPack();
 4175        collectiblesItemPack.id = BIWSettings.ASSETS_COLLECTIBLES;
 4176        collectiblesItemPack.title = BIWSettings.ASSETS_COLLECTIBLES;
 4177        collectiblesItemPack.assets = new List<CatalogItem>();
 4178        if (!DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(collectiblesItemPack.id))
 4179            DataStore.i.builderInWorld.catalogItemPackDict.Add(collectiblesItemPack.id, collectiblesItemPack);
 4180        return collectiblesItemPack;
 181    }
 182
 183    public static CatalogItemPack CreateCatalogItemPack(SceneAssetPack sceneAssetPack)
 184    {
 32185        CatalogItemPack catalogItemPack = new CatalogItemPack();
 186
 32187        catalogItemPack.id = sceneAssetPack.id;
 32188        catalogItemPack.title = sceneAssetPack.title;
 189
 32190        catalogItemPack.assets = new List<CatalogItem>();
 191
 32192        catalogItemPack.SetThumbnailULR(sceneAssetPack.ComposeThumbnailUrl());
 193
 142194        foreach (SceneObject sceneObject in sceneAssetPack.assets)
 195        {
 196            //TODO: SmartItems disabled until kernel has them implemented
 39197            if (sceneObject.IsSmartItem())
 198                continue;
 199
 38200            catalogItemPack.assets.Add(CreateCatalogItem(sceneObject, sceneAssetPack.title));
 201        }
 202
 32203        return catalogItemPack;
 204    }
 205
 206    public static CatalogItem CreateCatalogItem(SceneObject sceneObject, string assetPackName = null)
 207    {
 78208        CatalogItem catalogItem = new CatalogItem();
 78209        catalogItem.id = sceneObject.id;
 78210        if (sceneObject.asset_pack_id == BIWSettings.VOXEL_ASSETS_PACK_ID)
 0211            catalogItem.isVoxel = true;
 78212        catalogItem.name = sceneObject.name;
 78213        catalogItem.model = sceneObject.model;
 78214        catalogItem.assetPackName = assetPackName == null ? GetAssetPackNameById(sceneObject.asset_pack_id) : assetPackN
 78215        catalogItem.thumbnailURL = sceneObject.GetComposedThumbnailUrl();
 78216        catalogItem.tags = sceneObject.tags;
 217
 78218        catalogItem.category = sceneObject.category;
 78219        catalogItem.categoryName = catalogItem.category;
 220
 78221        catalogItem.contents = sceneObject.contents;
 222
 78223        catalogItem.metrics = sceneObject.metrics;
 224
 78225        if (!string.IsNullOrEmpty(sceneObject.script))
 226        {
 1227            catalogItem.itemType = CatalogItem.ItemType.SMART_ITEM;
 1228            catalogItem.parameters = sceneObject.parameters;
 1229            catalogItem.actions = sceneObject.actions;
 1230        }
 231        else
 232        {
 77233            catalogItem.itemType = CatalogItem.ItemType.SCENE_OBJECT;
 234        }
 235
 78236        return catalogItem;
 237    }
 238
 239    public static CatalogItem CreateCatalogItem(NFTInfo nFTInfo)
 240    {
 4241        CatalogItem catalogItem = new CatalogItem();
 4242        catalogItem.itemType = CatalogItem.ItemType.NFT;
 243
 4244        catalogItem.id = nFTInfo.assetContract.address;
 4245        catalogItem.thumbnailURL = nFTInfo.thumbnailUrl;
 4246        catalogItem.name = nFTInfo.name;
 4247        catalogItem.assetPackName = BIWSettings.ASSETS_COLLECTIBLES;
 4248        catalogItem.category = nFTInfo.assetContract.name;
 4249        catalogItem.model = $"{BIWSettings.COLLECTIBLE_MODEL_PROTOCOL}{nFTInfo.assetContract.address}/{nFTInfo.tokenId}"
 4250        catalogItem.tags = new List<string>();
 4251        catalogItem.contents = new Dictionary<string, string>();
 4252        catalogItem.metrics = new SceneObject.ObjectMetrics();
 253
 4254        return catalogItem;
 255    }
 256}