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