< Summary

Class:BuilderInWorldNFTController
Assembly:BuilderInWorldNFT
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/NFTs/BuilderInWorldNFTController.cs
Covered lines:30
Uncovered lines:20
Coverable lines:50
Total lines:127
Line coverage:60% (30 of 50)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldNFTController()0%110100%
Initialize()0%2100%
ClearNFTs()0%110100%
IsNFTInUse(...)0%4.024088.89%
StopUsingNFT(...)0%6.046090%
GetNfts()0%2100%
UseNFT(...)0%6.046090%
FetchNftsFromOwner()0%6200%
NftsFeteched(...)0%220100%
FetchNfts()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/NFTs/BuilderInWorldNFTController.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
 10public class BuilderInWorldNFTController
 11{
 12    public event System.Action OnNFTUsageChange;
 13    public event System.Action<List<NFTInfo>> OnNftsFetched;
 14
 15    private NFTOwner nftOwner;
 16
 17    private Coroutine fechNftsCoroutine;
 18
 19    private static BuilderInWorldNFTController instance;
 20
 121    private List<NFTInfo> nftsAlreadyInUse = new List<NFTInfo>();
 22
 23    private bool desactivateNFT = false;
 24
 25    public static BuilderInWorldNFTController i
 26    {
 27        get
 28        {
 5429            if (instance == null)
 30            {
 131                instance = new BuilderInWorldNFTController();
 32            }
 33
 5434            return instance;
 35        }
 36    }
 37
 38    public void Initialize()
 39    {
 040        UserProfile userProfile = UserProfile.GetOwnUserProfile();
 041        userProfile.OnUpdate += (x) => FetchNftsFromOwner();
 042    }
 43
 3244    public void ClearNFTs() { nftsAlreadyInUse.Clear(); }
 45
 46    public bool IsNFTInUse(string id)
 47    {
 348        if (desactivateNFT)
 049            return false;
 50
 751        foreach (NFTInfo info in nftsAlreadyInUse)
 52        {
 153            if (info.assetContract.address == id)
 154                return true;
 55        }
 256        return false;
 157    }
 58
 59    public void StopUsingNFT(string id)
 60    {
 161        if (desactivateNFT)
 062            return;
 63
 464        foreach (NFTInfo info in nftOwner.assets)
 65        {
 166            if (info.assetContract.address != id)
 67                continue;
 168            if (!nftsAlreadyInUse.Contains(info))
 69                continue;
 70
 171            nftsAlreadyInUse.Remove(info);
 172            OnNFTUsageChange?.Invoke();
 73        }
 174    }
 75
 076    public List<NFTInfo> GetNfts() { return nftOwner.assets; }
 77
 78    public void UseNFT(string id)
 79    {
 280        if (desactivateNFT)
 081            return;
 82
 883        foreach (NFTInfo info in nftOwner.assets)
 84        {
 285            if (info.assetContract.address != id)
 86                continue;
 287            if (nftsAlreadyInUse.Contains(info))
 88                continue;
 89
 290            nftsAlreadyInUse.Add(info);
 291            OnNFTUsageChange?.Invoke();
 92
 93        }
 294    }
 95
 96    private void FetchNftsFromOwner()
 97    {
 098        if (fechNftsCoroutine != null)
 099            CoroutineStarter.Stop(fechNftsCoroutine);
 0100        fechNftsCoroutine = CoroutineStarter.Start(FetchNfts());
 0101    }
 102
 103    public void NftsFeteched(NFTOwner nftOwner)
 104    {
 3105        this.nftOwner = nftOwner;
 3106        string json = JsonUtility.ToJson(nftOwner);
 3107        desactivateNFT = false;
 3108        OnNftsFetched?.Invoke(this.nftOwner.assets);
 3109    }
 110
 111    private IEnumerator FetchNfts()
 112    {
 0113        UserProfile userProfile = UserProfile.GetOwnUserProfile();
 114
 0115        string userId = userProfile.ethAddress;
 116
 0117        yield return NFTHelper.FetchNFTsFromOwner(userId, (nftOwner) =>
 118            {
 0119                NftsFeteched(nftOwner);
 0120            },
 121            (error) =>
 122            {
 0123                desactivateNFT = true;
 0124                Debug.Log($"error getting NFT from owner:  {error}");
 0125            });
 0126    }
 127}