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