| | 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 | | public 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 | |
|
| 1 | 21 | | private List<NFTInfo> nftsAlreadyInUse = new List<NFTInfo>(); |
| | 22 | |
|
| | 23 | | private bool desactivateNFT = false; |
| | 24 | |
|
| | 25 | | public static BuilderInWorldNFTController i |
| | 26 | | { |
| | 27 | | get |
| | 28 | | { |
| 54 | 29 | | if (instance == null) |
| | 30 | | { |
| 1 | 31 | | instance = new BuilderInWorldNFTController(); |
| | 32 | | } |
| | 33 | |
|
| 54 | 34 | | return instance; |
| | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public void Initialize() |
| | 39 | | { |
| 0 | 40 | | UserProfile userProfile = UserProfile.GetOwnUserProfile(); |
| 0 | 41 | | userProfile.OnUpdate += (x) => FetchNftsFromOwner(); |
| 0 | 42 | | } |
| | 43 | |
|
| 32 | 44 | | public void ClearNFTs() { nftsAlreadyInUse.Clear(); } |
| | 45 | |
|
| | 46 | | public bool IsNFTInUse(string id) |
| | 47 | | { |
| 3 | 48 | | if (desactivateNFT) |
| 0 | 49 | | return false; |
| | 50 | |
|
| 7 | 51 | | foreach (NFTInfo info in nftsAlreadyInUse) |
| | 52 | | { |
| 1 | 53 | | if (info.assetContract.address == id) |
| 1 | 54 | | return true; |
| | 55 | | } |
| 2 | 56 | | return false; |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | public void StopUsingNFT(string id) |
| | 60 | | { |
| 1 | 61 | | if (desactivateNFT) |
| 0 | 62 | | return; |
| | 63 | |
|
| 4 | 64 | | foreach (NFTInfo info in nftOwner.assets) |
| | 65 | | { |
| 1 | 66 | | if (info.assetContract.address != id) |
| | 67 | | continue; |
| 1 | 68 | | if (!nftsAlreadyInUse.Contains(info)) |
| | 69 | | continue; |
| | 70 | |
|
| 1 | 71 | | nftsAlreadyInUse.Remove(info); |
| 1 | 72 | | OnNFTUsageChange?.Invoke(); |
| | 73 | | } |
| 1 | 74 | | } |
| | 75 | |
|
| 0 | 76 | | public List<NFTInfo> GetNfts() { return nftOwner.assets; } |
| | 77 | |
|
| | 78 | | public void UseNFT(string id) |
| | 79 | | { |
| 2 | 80 | | if (desactivateNFT) |
| 0 | 81 | | return; |
| | 82 | |
|
| 8 | 83 | | foreach (NFTInfo info in nftOwner.assets) |
| | 84 | | { |
| 2 | 85 | | if (info.assetContract.address != id) |
| | 86 | | continue; |
| 2 | 87 | | if (nftsAlreadyInUse.Contains(info)) |
| | 88 | | continue; |
| | 89 | |
|
| 2 | 90 | | nftsAlreadyInUse.Add(info); |
| 2 | 91 | | OnNFTUsageChange?.Invoke(); |
| | 92 | |
|
| | 93 | | } |
| 2 | 94 | | } |
| | 95 | |
|
| | 96 | | private void FetchNftsFromOwner() |
| | 97 | | { |
| 0 | 98 | | if (fechNftsCoroutine != null) |
| 0 | 99 | | CoroutineStarter.Stop(fechNftsCoroutine); |
| 0 | 100 | | fechNftsCoroutine = CoroutineStarter.Start(FetchNfts()); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public void NftsFeteched(NFTOwner nftOwner) |
| | 104 | | { |
| 3 | 105 | | this.nftOwner = nftOwner; |
| 3 | 106 | | string json = JsonUtility.ToJson(nftOwner); |
| 3 | 107 | | desactivateNFT = false; |
| 3 | 108 | | OnNftsFetched?.Invoke(this.nftOwner.assets); |
| 3 | 109 | | } |
| | 110 | |
|
| | 111 | | private IEnumerator FetchNfts() |
| | 112 | | { |
| 0 | 113 | | UserProfile userProfile = UserProfile.GetOwnUserProfile(); |
| | 114 | |
|
| 0 | 115 | | string userId = userProfile.ethAddress; |
| | 116 | |
|
| 0 | 117 | | yield return NFTHelper.FetchNFTsFromOwner(userId, (nftOwner) => |
| | 118 | | { |
| 0 | 119 | | NftsFeteched(nftOwner); |
| 0 | 120 | | }, |
| | 121 | | (error) => |
| | 122 | | { |
| 0 | 123 | | desactivateNFT = true; |
| 0 | 124 | | Debug.Log($"error getting NFT from owner: {error}"); |
| 0 | 125 | | }); |
| 0 | 126 | | } |
| | 127 | | } |