| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using Decentraland.Renderer.KernelServices; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEditor; |
| | 8 | | using UnityEngine; |
| | 9 | | using Environment = DCL.Environment; |
| | 10 | |
|
| | 11 | | [CreateAssetMenu(fileName = "UserProfile", menuName = "UserProfile")] |
| | 12 | | public class UserProfile : ScriptableObject //TODO Move to base variable |
| | 13 | | { |
| | 14 | | public enum EmoteSource |
| | 15 | | { |
| | 16 | | EmotesWheel, |
| | 17 | | Shortcut, |
| | 18 | | Command, |
| | 19 | | Backpack |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public event Action<UserProfile> OnUpdate; |
| | 23 | | public event Action<string, long, EmoteSource> OnAvatarEmoteSet; |
| | 24 | |
|
| 525 | 25 | | public string userId => model.userId; |
| 53 | 26 | | public string ethAddress => model.ethAddress; |
| 218 | 27 | | public string userName => model.name; |
| 32 | 28 | | public string description => model.description; |
| 0 | 29 | | public string email => model.email; |
| 0 | 30 | | public string bodySnapshotURL => model.ComposeCorrectUrl(model.snapshots.body); |
| 150 | 31 | | public string face256SnapshotURL => model.ComposeCorrectUrl(model.snapshots.face256); |
| 0 | 32 | | public string baseUrl => model.baseUrl; |
| 0 | 33 | | public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess; |
| 198 | 34 | | public List<string> blocked => model.blocked != null ? model.blocked : new List<string>(); |
| 4 | 35 | | public List<string> muted => model.muted ?? new List<string>(); |
| 213 | 36 | | public bool hasConnectedWeb3 => model.hasConnectedWeb3; |
| 53 | 37 | | public bool hasClaimedName => model.hasClaimedName; |
| 20 | 38 | | public bool isGuest => !model.hasConnectedWeb3; |
| 737 | 39 | | public AvatarModel avatar => model.avatar; |
| 0 | 40 | | public int tutorialStep => model.tutorialStep; |
| | 41 | |
|
| 654 | 42 | | internal Dictionary<string, int> inventory = new Dictionary<string, int>(); |
| | 43 | |
|
| 654 | 44 | | public ILazyTextureObserver snapshotObserver = new LazyTextureObserver(); |
| 654 | 45 | | public ILazyTextureObserver bodySnapshotObserver = new LazyTextureObserver(); |
| | 46 | |
|
| 654 | 47 | | internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks |
| | 48 | | { |
| | 49 | | avatar = new AvatarModel() |
| | 50 | | }; |
| | 51 | |
|
| 654 | 52 | | private int emoteLamportTimestamp = 1; |
| | 53 | |
|
| | 54 | | public void UpdateData(UserProfileModel newModel) |
| | 55 | | { |
| 653 | 56 | | if (newModel == null) |
| | 57 | | { |
| 0 | 58 | | model = new UserProfileModel(); |
| 0 | 59 | | return; |
| | 60 | | } |
| | 61 | |
|
| 653 | 62 | | bool isModelDirty = !newModel.Equals(model); |
| 653 | 63 | | bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256; |
| 653 | 64 | | bool bodySnapshotDirty = model.snapshots.body != newModel.snapshots.body; |
| | 65 | |
|
| 653 | 66 | | model.userId = newModel.userId; |
| 653 | 67 | | model.ethAddress = newModel.ethAddress; |
| 653 | 68 | | model.parcelsWithAccess = newModel.parcelsWithAccess; |
| 653 | 69 | | model.tutorialStep = newModel.tutorialStep; |
| 653 | 70 | | model.hasClaimedName = newModel.hasClaimedName; |
| 653 | 71 | | model.name = newModel.name; |
| 653 | 72 | | model.email = newModel.email; |
| 653 | 73 | | model.description = newModel.description; |
| 653 | 74 | | model.baseUrl = newModel.baseUrl; |
| 653 | 75 | | model.avatar.CopyFrom(newModel.avatar); |
| 653 | 76 | | model.snapshots = newModel.snapshots; |
| 653 | 77 | | model.hasConnectedWeb3 = newModel.hasConnectedWeb3; |
| 653 | 78 | | model.blocked = newModel.blocked; |
| 653 | 79 | | model.muted = newModel.muted; |
| 653 | 80 | | model.version = newModel.version; |
| | 81 | |
|
| 653 | 82 | | if (model.snapshots != null && faceSnapshotDirty) |
| 33 | 83 | | snapshotObserver.RefreshWithUri(face256SnapshotURL); |
| | 84 | |
|
| 653 | 85 | | if (model.snapshots != null && bodySnapshotDirty) |
| 0 | 86 | | bodySnapshotObserver.RefreshWithUri(bodySnapshotURL); |
| | 87 | |
|
| 653 | 88 | | if (isModelDirty) |
| 632 | 89 | | OnUpdate?.Invoke(this); |
| 56 | 90 | | } |
| | 91 | |
|
| | 92 | | public int GetItemAmount(string itemId) |
| | 93 | | { |
| 4675 | 94 | | if (inventory == null || !inventory.ContainsKey(itemId)) |
| 4637 | 95 | | return 0; |
| | 96 | |
|
| 38 | 97 | | return inventory[itemId]; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot) |
| | 101 | | { |
| 1 | 102 | | model.avatar.CopyFrom(newModel); |
| 1 | 103 | | this.snapshotObserver.RefreshWithTexture(newFaceSnapshot); |
| | 104 | |
|
| 1 | 105 | | OnUpdate?.Invoke(this); |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | public void SetAvatarExpression(string id, EmoteSource source) |
| | 109 | | { |
| 1 | 110 | | int timestamp = emoteLamportTimestamp++; |
| 1 | 111 | | avatar.expressionTriggerId = id; |
| 1 | 112 | | avatar.expressionTriggerTimestamp = timestamp; |
| | 113 | |
|
| 1 | 114 | | ClientEmotesKernelService emotes = Environment.i.serviceLocator.Get<IRPC>().Emotes(); |
| | 115 | | // TODO: fix message `Timestamp` should NOT be `float`, we should use `int lamportTimestamp` or `long timeStamp` |
| 1 | 116 | | emotes?.TriggerExpression(new TriggerExpressionRequest() |
| | 117 | | { |
| | 118 | | Id = id, |
| | 119 | | Timestamp = timestamp |
| | 120 | | }); |
| | 121 | |
|
| 1 | 122 | | OnUpdate?.Invoke(this); |
| 1 | 123 | | OnAvatarEmoteSet?.Invoke(id, timestamp, source); |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | public void SetInventory(string[] inventoryIds) |
| | 127 | | { |
| 206 | 128 | | inventory.Clear(); |
| 2867 | 129 | | inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()); |
| 206 | 130 | | } |
| | 131 | |
|
| | 132 | | public void AddToInventory(string wearableId) |
| | 133 | | { |
| 0 | 134 | | if (inventory.ContainsKey(wearableId)) |
| 0 | 135 | | inventory[wearableId]++; |
| | 136 | | else |
| 0 | 137 | | inventory.Add(wearableId, 1); |
| 0 | 138 | | } |
| | 139 | |
|
| 0 | 140 | | public void RemoveFromInventory(string wearableId) { inventory.Remove(wearableId); } |
| | 141 | |
|
| 0 | 142 | | public bool ContainsInInventory(string wearableId) => inventory.ContainsKey(wearableId); |
| | 143 | |
|
| 0 | 144 | | public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); } |
| | 145 | |
|
| | 146 | | internal static UserProfile ownUserProfile; |
| | 147 | |
|
| | 148 | | public static UserProfile GetOwnUserProfile() |
| | 149 | | { |
| 2818 | 150 | | if (ownUserProfile == null) |
| | 151 | | { |
| 19 | 152 | | ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile"); |
| | 153 | | } |
| | 154 | |
|
| 2818 | 155 | | return ownUserProfile; |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | public UserProfileModel CloneModel() => model.Clone(); |
| | 159 | |
|
| 62 | 160 | | public bool IsBlocked(string userId) { return blocked != null && blocked.Contains(userId); } |
| | 161 | |
|
| | 162 | | public void Block(string userId) |
| | 163 | | { |
| 19 | 164 | | if (IsBlocked(userId)) |
| 0 | 165 | | return; |
| 19 | 166 | | blocked.Add(userId); |
| 19 | 167 | | } |
| | 168 | |
|
| 0 | 169 | | public void Unblock(string userId) { blocked.Remove(userId); } |
| | 170 | |
|
| 0 | 171 | | public bool HasEquipped(string wearableId) => avatar.wearables.Contains(wearableId); |
| | 172 | |
|
| | 173 | | #if UNITY_EDITOR |
| | 174 | | private void OnEnable() |
| | 175 | | { |
| 654 | 176 | | Application.quitting -= CleanUp; |
| 654 | 177 | | Application.quitting += CleanUp; |
| 654 | 178 | | } |
| | 179 | |
|
| | 180 | | private void CleanUp() |
| | 181 | | { |
| 0 | 182 | | Application.quitting -= CleanUp; |
| 0 | 183 | | if (AssetDatabase.Contains(this)) |
| 0 | 184 | | Resources.UnloadAsset(this); |
| 0 | 185 | | } |
| | 186 | | #endif |
| | 187 | | } |