< Summary

Class:BIWNFTController
Assembly:BuilderInWorldNFT
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/NFTs/BIWNFTController.cs
Covered lines:4
Uncovered lines:62
Coverable lines:66
Total lines:162
Line coverage:6% (4 of 66)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWNFTController()0%110100%
StartFetchingNft()0%6200%
UserProfileUpdated(...)0%6200%
Dispose()0%12300%
StartEditMode()0%2100%
ExitEditMode()0%2100%
ClearNFTs()0%2100%
IsNFTInUse(...)0%20400%
StopUsingNFT(...)0%42600%
GetNfts()0%2100%
UseNFT(...)0%56700%
FetchNftsFromOwner()0%6200%
NftsFeteched(...)0%6200%
FetchNfts()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/NFTs/BIWNFTController.cs

#LineLine coverage
 1using DCL.Configuration;
 2using DCL.Helpers.NFT;
 3using System;
 4using System.Collections;
 5using System.Collections.Generic;
 6using System.Security.Cryptography;
 7using System.Text;
 8using UnityEngine;
 9
 10// Note: we should make this class a part of BIWController and only fetch the NFTs if the biw is active instead of only
 11// when you enter builder in world
 12public class BIWNFTController
 13{
 14    public event System.Action OnNFTUsageChange;
 15    public event System.Action<List<NFTInfo>> OnNftsFetched;
 16
 17    private NFTOwner nftOwner;
 18
 19    private Coroutine fechNftsCoroutine;
 20
 21    private static BIWNFTController instance;
 22
 123    private List<NFTInfo> nftsAlreadyInUse = new List<NFTInfo>();
 24
 25    private bool desactivateNFT = false;
 26    private bool isInit = false;
 27    private bool isActive = false;
 28    private UserProfile userProfile;
 29
 30    public static BIWNFTController i
 31    {
 32        get
 33        {
 1934            if (instance == null)
 35            {
 136                instance = new BIWNFTController();
 37            }
 38
 1939            return instance;
 40        }
 41    }
 42
 43    public void StartFetchingNft()
 44    {
 045        if (!isInit)
 46        {
 047            userProfile = UserProfile.GetOwnUserProfile();
 048            userProfile.OnUpdate += UserProfileUpdated;
 049            FetchNftsFromOwner();
 050            isInit = true;
 51        }
 052    }
 53
 54    private void UserProfileUpdated(UserProfile profile)
 55    {
 056        if (!isActive)
 057            return;
 58
 059        FetchNftsFromOwner();
 060    }
 61
 62    public void Dispose()
 63    {
 064        if (userProfile != null)
 065            userProfile.OnUpdate -= UserProfileUpdated;
 66
 067        if (fechNftsCoroutine != null)
 068            CoroutineStarter.Stop(fechNftsCoroutine);
 069    }
 70
 71    public void StartEditMode()
 72    {
 073        isActive = true;
 074        ClearNFTs();
 075    }
 76
 077    public void ExitEditMode() { isActive = false; }
 78
 079    public void ClearNFTs() { nftsAlreadyInUse.Clear(); }
 80
 81    public bool IsNFTInUse(string id)
 82    {
 083        if (desactivateNFT)
 084            return false;
 85
 086        foreach (NFTInfo info in nftsAlreadyInUse)
 87        {
 088            if (info.assetContract.address == id)
 089                return true;
 90        }
 91
 092        return false;
 093    }
 94
 95    public void StopUsingNFT(string id)
 96    {
 097        if (desactivateNFT)
 098            return;
 99
 0100        foreach (NFTInfo info in nftOwner.assets)
 101        {
 0102            if (info.assetContract.address != id)
 103                continue;
 0104            if (!nftsAlreadyInUse.Contains(info))
 105                continue;
 106
 0107            nftsAlreadyInUse.Remove(info);
 0108            OnNFTUsageChange?.Invoke();
 109        }
 0110    }
 111
 0112    public List<NFTInfo> GetNfts() { return nftOwner.assets; }
 113
 114    public void UseNFT(string id)
 115    {
 0116        if (desactivateNFT || nftOwner.assets == null)
 0117            return;
 118
 0119        foreach (NFTInfo info in nftOwner.assets)
 120        {
 0121            if (info.assetContract.address != id)
 122                continue;
 0123            if (nftsAlreadyInUse.Contains(info))
 124                continue;
 125
 0126            nftsAlreadyInUse.Add(info);
 0127            OnNFTUsageChange?.Invoke();
 128        }
 0129    }
 130
 131    private void FetchNftsFromOwner()
 132    {
 0133        if (fechNftsCoroutine != null)
 0134            CoroutineStarter.Stop(fechNftsCoroutine);
 0135        fechNftsCoroutine = CoroutineStarter.Start(FetchNfts());
 0136    }
 137
 138    public void NftsFeteched(NFTOwner nftOwner)
 139    {
 0140        this.nftOwner = nftOwner;
 0141        string json = JsonUtility.ToJson(nftOwner);
 0142        desactivateNFT = false;
 0143        OnNftsFetched?.Invoke(this.nftOwner.assets);
 0144    }
 145
 146    private IEnumerator FetchNfts()
 147    {
 0148        string userId = userProfile.ethAddress;
 0149        if (!string.IsNullOrEmpty(userId))
 150        {
 0151            yield return NFTUtils.FetchNFTsFromOwner(userId, (nftOwner) =>
 152                {
 0153                    NftsFeteched(nftOwner);
 0154                },
 155                (error) =>
 156                {
 0157                    desactivateNFT = true;
 0158                    Debug.LogError($"error getting NFT from owner:  {error}");
 0159                });
 160        }
 0161    }
 162}