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