| | 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> OnAvatarExpressionSet; |
| | 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 face128SnapshotURL => model.snapshots.face128; |
| 0 | 23 | | public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess; |
| 94 | 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 | |
|
| 118 | 31 | | internal Dictionary<string, int> inventory = new Dictionary<string, int>(); |
| | 32 | |
|
| 118 | 33 | | public ILazyTextureObserver snapshotObserver = new LazyTextureObserver(); |
| | 34 | |
|
| 118 | 35 | | internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks |
| | 36 | | { |
| | 37 | | avatar = new AvatarModel() |
| | 38 | | }; |
| | 39 | |
|
| | 40 | | public void UpdateData(UserProfileModel newModel) |
| | 41 | | { |
| 175 | 42 | | if (newModel == null) |
| | 43 | | { |
| 0 | 44 | | model = null; |
| 0 | 45 | | return; |
| | 46 | | } |
| | 47 | |
|
| 175 | 48 | | bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256; |
| | 49 | |
|
| 175 | 50 | | model.userId = newModel.userId; |
| 175 | 51 | | model.ethAddress = newModel.ethAddress; |
| 175 | 52 | | model.parcelsWithAccess = newModel.parcelsWithAccess; |
| 175 | 53 | | model.tutorialStep = newModel.tutorialStep; |
| 175 | 54 | | model.hasClaimedName = newModel.hasClaimedName; |
| 175 | 55 | | model.name = newModel.name; |
| 175 | 56 | | model.email = newModel.email; |
| 175 | 57 | | model.description = newModel.description; |
| 175 | 58 | | model.avatar.CopyFrom(newModel.avatar); |
| 175 | 59 | | model.snapshots = newModel.snapshots; |
| 175 | 60 | | model.hasConnectedWeb3 = newModel.hasConnectedWeb3; |
| 175 | 61 | | model.inventory = newModel.inventory; |
| 175 | 62 | | model.blocked = newModel.blocked; |
| 175 | 63 | | model.muted = newModel.muted; |
| | 64 | |
|
| 175 | 65 | | if (model.inventory != null) |
| | 66 | | { |
| 12 | 67 | | SetInventory(model.inventory); |
| | 68 | | } |
| | 69 | |
|
| 175 | 70 | | if (model.snapshots != null && faceSnapshotDirty) |
| | 71 | | { |
| 5 | 72 | | this.snapshotObserver.RefreshWithUri(model.snapshots.face256); |
| | 73 | | } |
| | 74 | |
|
| 175 | 75 | | OnUpdate?.Invoke(this); |
| 59 | 76 | | } |
| | 77 | |
|
| | 78 | | public int GetItemAmount(string itemId) |
| | 79 | | { |
| 4992 | 80 | | if (inventory == null || !inventory.ContainsKey(itemId)) |
| 4958 | 81 | | return 0; |
| | 82 | |
|
| 34 | 83 | | return inventory[itemId]; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot) |
| | 87 | | { |
| 1 | 88 | | model.avatar.CopyFrom(newModel); |
| 1 | 89 | | this.snapshotObserver.RefreshWithTexture(newFaceSnapshot); |
| | 90 | |
|
| 1 | 91 | | OnUpdate?.Invoke(this); |
| 1 | 92 | | } |
| | 93 | |
|
| | 94 | | public void SetAvatarExpression(string id) |
| | 95 | | { |
| 2 | 96 | | var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds; |
| 2 | 97 | | avatar.expressionTriggerId = id; |
| 2 | 98 | | avatar.expressionTriggerTimestamp = timestamp; |
| 2 | 99 | | WebInterface.SendExpression(id, timestamp); |
| 2 | 100 | | OnUpdate?.Invoke(this); |
| 2 | 101 | | OnAvatarExpressionSet?.Invoke(id, timestamp); |
| 2 | 102 | | } |
| | 103 | |
|
| | 104 | | public void SetInventory(string[] inventoryIds) |
| | 105 | | { |
| 44 | 106 | | inventory.Clear(); |
| 269 | 107 | | inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()); |
| 44 | 108 | | } |
| | 109 | |
|
| 0 | 110 | | public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); } |
| | 111 | |
|
| | 112 | | internal static UserProfile ownUserProfile; |
| | 113 | |
|
| | 114 | | public static UserProfile GetOwnUserProfile() |
| | 115 | | { |
| 1575 | 116 | | if (ownUserProfile == null) |
| | 117 | | { |
| 1 | 118 | | ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile"); |
| | 119 | | } |
| | 120 | |
|
| 1575 | 121 | | return ownUserProfile; |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | public UserProfileModel CloneModel() => model.Clone(); |
| | 125 | |
|
| | 126 | | #if UNITY_EDITOR |
| | 127 | | private void OnEnable() |
| | 128 | | { |
| 118 | 129 | | Application.quitting -= CleanUp; |
| 118 | 130 | | Application.quitting += CleanUp; |
| 118 | 131 | | } |
| | 132 | |
|
| | 133 | | private void CleanUp() |
| | 134 | | { |
| 0 | 135 | | Application.quitting -= CleanUp; |
| 0 | 136 | | if (UnityEditor.AssetDatabase.Contains(this)) |
| 0 | 137 | | Resources.UnloadAsset(this); |
| 0 | 138 | | } |
| | 139 | | #endif |
| | 140 | | } |