| | 1 | | using DCL.Configuration; |
| | 2 | | using DCL.Helpers.NFT; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Security.Cryptography; |
| | 7 | | using System.Text; |
| | 8 | | using 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 |
| | 12 | | public 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 | |
|
| 1 | 23 | | 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 | | { |
| 90 | 34 | | if (instance == null) |
| | 35 | | { |
| 1 | 36 | | instance = new BIWNFTController(); |
| | 37 | | } |
| | 38 | |
|
| 90 | 39 | | return instance; |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public void StartFetchingNft() |
| | 44 | | { |
| 3 | 45 | | if (!isInit) |
| | 46 | | { |
| 1 | 47 | | userProfile = UserProfile.GetOwnUserProfile(); |
| 1 | 48 | | userProfile.OnUpdate += UserProfileUpdated; |
| 1 | 49 | | FetchNftsFromOwner(); |
| 1 | 50 | | isInit = true; |
| | 51 | | } |
| 3 | 52 | | } |
| | 53 | |
|
| | 54 | | private void UserProfileUpdated(UserProfile profile) |
| | 55 | | { |
| 20 | 56 | | if (!isActive) |
| 0 | 57 | | return; |
| | 58 | |
|
| 20 | 59 | | FetchNftsFromOwner(); |
| 20 | 60 | | } |
| | 61 | |
|
| | 62 | | public void Dispose() |
| | 63 | | { |
| 8 | 64 | | if (userProfile != null) |
| 0 | 65 | | userProfile.OnUpdate -= UserProfileUpdated; |
| | 66 | |
|
| 8 | 67 | | if (fechNftsCoroutine != null) |
| 0 | 68 | | CoroutineStarter.Stop(fechNftsCoroutine); |
| 8 | 69 | | } |
| | 70 | |
|
| | 71 | | public void StartEditMode() |
| | 72 | | { |
| 5 | 73 | | isActive = true; |
| 5 | 74 | | ClearNFTs(); |
| 5 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | public void ExitEditMode() { isActive = false; } |
| | 78 | |
|
| 42 | 79 | | public void ClearNFTs() { nftsAlreadyInUse.Clear(); } |
| | 80 | |
|
| | 81 | | public bool IsNFTInUse(string id) |
| | 82 | | { |
| 3 | 83 | | if (desactivateNFT) |
| 0 | 84 | | return false; |
| | 85 | |
|
| 7 | 86 | | foreach (NFTInfo info in nftsAlreadyInUse) |
| | 87 | | { |
| 1 | 88 | | if (info.assetContract.address == id) |
| 1 | 89 | | return true; |
| | 90 | | } |
| | 91 | |
|
| 2 | 92 | | return false; |
| 1 | 93 | | } |
| | 94 | |
|
| | 95 | | public void StopUsingNFT(string id) |
| | 96 | | { |
| 1 | 97 | | if (desactivateNFT) |
| 0 | 98 | | return; |
| | 99 | |
|
| 4 | 100 | | foreach (NFTInfo info in nftOwner.assets) |
| | 101 | | { |
| 1 | 102 | | if (info.assetContract.address != id) |
| | 103 | | continue; |
| 1 | 104 | | if (!nftsAlreadyInUse.Contains(info)) |
| | 105 | | continue; |
| | 106 | |
|
| 1 | 107 | | nftsAlreadyInUse.Remove(info); |
| 1 | 108 | | OnNFTUsageChange?.Invoke(); |
| | 109 | | } |
| 1 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | public List<NFTInfo> GetNfts() { return nftOwner.assets; } |
| | 113 | |
|
| | 114 | | public void UseNFT(string id) |
| | 115 | | { |
| 3 | 116 | | if (desactivateNFT || nftOwner.assets == null) |
| 0 | 117 | | return; |
| | 118 | |
|
| 12 | 119 | | foreach (NFTInfo info in nftOwner.assets) |
| | 120 | | { |
| 3 | 121 | | if (info.assetContract.address != id) |
| | 122 | | continue; |
| 3 | 123 | | if (nftsAlreadyInUse.Contains(info)) |
| | 124 | | continue; |
| | 125 | |
|
| 3 | 126 | | nftsAlreadyInUse.Add(info); |
| 3 | 127 | | OnNFTUsageChange?.Invoke(); |
| | 128 | | } |
| 3 | 129 | | } |
| | 130 | |
|
| | 131 | | private void FetchNftsFromOwner() |
| | 132 | | { |
| 21 | 133 | | if (fechNftsCoroutine != null) |
| 3 | 134 | | CoroutineStarter.Stop(fechNftsCoroutine); |
| 21 | 135 | | fechNftsCoroutine = CoroutineStarter.Start(FetchNfts()); |
| 21 | 136 | | } |
| | 137 | |
|
| | 138 | | public void NftsFeteched(NFTOwner nftOwner) |
| | 139 | | { |
| 4 | 140 | | this.nftOwner = nftOwner; |
| 4 | 141 | | string json = JsonUtility.ToJson(nftOwner); |
| 4 | 142 | | desactivateNFT = false; |
| 4 | 143 | | OnNftsFetched?.Invoke(this.nftOwner.assets); |
| 4 | 144 | | } |
| | 145 | |
|
| | 146 | | private IEnumerator FetchNfts() |
| | 147 | | { |
| 21 | 148 | | string userId = userProfile.ethAddress; |
| 21 | 149 | | if (!string.IsNullOrEmpty(userId)) |
| | 150 | | { |
| 3 | 151 | | yield return NFTUtils.FetchNFTsFromOwner(userId, (nftOwner) => |
| | 152 | | { |
| 0 | 153 | | NftsFeteched(nftOwner); |
| 0 | 154 | | }, |
| | 155 | | (error) => |
| | 156 | | { |
| 0 | 157 | | desactivateNFT = true; |
| 0 | 158 | | Debug.LogError($"error getting NFT from owner: {error}"); |
| 0 | 159 | | }); |
| | 160 | | } |
| 21 | 161 | | } |
| | 162 | | } |