| | 1 | | using DCL.Helpers; |
| | 2 | | using Newtonsoft.Json; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.EquippedEmotes |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Plugin feature that initialize the Equipped Emotes feature. |
| | 11 | | /// </summary> |
| | 12 | | public class EquippedEmotesInitializerPlugin : IPlugin |
| | 13 | | { |
| | 14 | | internal const string PLAYER_PREFS_EQUIPPED_EMOTES_KEY = "EquippedNFTEmotes"; |
| | 15 | |
|
| 0 | 16 | | internal DataStore_EmotesCustomization emotesCustomizationDataStore => DataStore.i.emotesCustomization; |
| 0 | 17 | | internal DataStore_Common commonDataStore => DataStore.i.common; |
| 0 | 18 | | internal DataStore_FeatureFlag featureFlagsDataStore => DataStore.i.featureFlags; |
| | 19 | | internal UserProfile ownUserProfile; |
| | 20 | |
|
| 0 | 21 | | public EquippedEmotesInitializerPlugin() |
| | 22 | | { |
| 0 | 23 | | ownUserProfile = UserProfile.GetOwnUserProfile(); |
| 0 | 24 | | ownUserProfile.OnUpdate += OnOwnUserProfileUpdated; |
| 0 | 25 | | LoadDefaultEquippedEmotes(); |
| | 26 | |
|
| 0 | 27 | | LoadEquippedEmotesFromLocalStorage(); |
| | 28 | |
|
| 0 | 29 | | emotesCustomizationDataStore.equippedEmotes.OnSet += OnEquippedEmotesSet; |
| 0 | 30 | | emotesCustomizationDataStore.equippedEmotes.OnAdded += OnEquippedEmoteAddedOrRemoved; |
| 0 | 31 | | emotesCustomizationDataStore.equippedEmotes.OnRemoved += OnEquippedEmoteAddedOrRemoved; |
| 0 | 32 | | commonDataStore.isSignUpFlow.OnChange += OnSignupFlowChanged; |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private void OnSignupFlowChanged(bool current, bool previous) |
| | 36 | | { |
| 0 | 37 | | if (current) |
| 0 | 38 | | LoadDefaultEquippedEmotes(); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private void OnOwnUserProfileUpdated(UserProfile userProfile) |
| | 42 | | { |
| 0 | 43 | | if (userProfile == null || userProfile.avatar == null || userProfile.avatar.emotes.Count == 0) |
| 0 | 44 | | return; |
| | 45 | |
|
| 0 | 46 | | List<string> equippedEmotes = new List<string> (Enumerable.Repeat((string) null, 10)); |
| 0 | 47 | | foreach (AvatarModel.AvatarEmoteEntry avatarEmoteEntry in userProfile.avatar.emotes) |
| | 48 | | { |
| 0 | 49 | | equippedEmotes[avatarEmoteEntry.slot] = avatarEmoteEntry.urn; |
| | 50 | | } |
| 0 | 51 | | SetEquippedEmotes(equippedEmotes); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | internal List<string> GetDefaultEmotes() |
| | 55 | | { |
| 0 | 56 | | return new List<string> |
| | 57 | | { |
| | 58 | | "handsair", |
| | 59 | | "wave", |
| | 60 | | "fistpump", |
| | 61 | | "dance", |
| | 62 | | "raiseHand", |
| | 63 | | "clap", |
| | 64 | | "money", |
| | 65 | | "kiss", |
| | 66 | | "headexplode", |
| | 67 | | "shrug" |
| | 68 | | }; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | internal void LoadDefaultEquippedEmotes() { SetEquippedEmotes(GetDefaultEmotes()); } |
| | 72 | |
|
| | 73 | | internal void LoadEquippedEmotesFromLocalStorage() |
| | 74 | | { |
| | 75 | | List<string> storedEquippedEmotes; |
| | 76 | |
|
| | 77 | | try |
| | 78 | | { |
| 0 | 79 | | storedEquippedEmotes = JsonConvert.DeserializeObject<List<string>>(PlayerPrefsUtils.GetString(PLAYER_PRE |
| 0 | 80 | | } |
| 0 | 81 | | catch |
| | 82 | | { |
| 0 | 83 | | storedEquippedEmotes = null; |
| 0 | 84 | | } |
| | 85 | |
|
| 0 | 86 | | if (storedEquippedEmotes == null) |
| 0 | 87 | | storedEquippedEmotes = GetDefaultEmotes(); |
| | 88 | |
|
| 0 | 89 | | SetEquippedEmotes(storedEquippedEmotes); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | internal void OnEquippedEmotesSet(IEnumerable<EquippedEmoteData> equippedEmotes) |
| | 93 | | { |
| | 94 | |
|
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | internal void OnEquippedEmoteAddedOrRemoved(EquippedEmoteData equippedEmote) |
| | 98 | | { |
| | 99 | |
|
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | internal void SaveEquippedEmotesInLocalStorage() |
| | 103 | | { |
| 0 | 104 | | List<string> emotesIdsToStore = new List<string>(); |
| 0 | 105 | | foreach (EquippedEmoteData equippedEmoteData in emotesCustomizationDataStore.equippedEmotes.Get()) |
| | 106 | | { |
| 0 | 107 | | emotesIdsToStore.Add(equippedEmoteData != null ? equippedEmoteData.id : null); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | // TODO: We should avoid static calls and create injectable interfaces |
| 0 | 111 | | PlayerPrefsUtils.SetString(PLAYER_PREFS_EQUIPPED_EMOTES_KEY, JsonConvert.SerializeObject(emotesIdsToStore)); |
| 0 | 112 | | PlayerPrefsUtils.Save(); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | internal void SetEquippedEmotes(List<string> storedEquippedEmotes) |
| | 116 | | { |
| 0 | 117 | | List<EquippedEmoteData> storedEquippedEmotesData = new List<EquippedEmoteData>(); |
| 0 | 118 | | foreach (string emoteId in storedEquippedEmotes) |
| | 119 | | { |
| 0 | 120 | | storedEquippedEmotesData.Add( |
| | 121 | | string.IsNullOrEmpty(emoteId) ? null : new EquippedEmoteData { id = emoteId, cachedThumbnail = null |
| | 122 | | } |
| 0 | 123 | | emotesCustomizationDataStore.equippedEmotes.Set(storedEquippedEmotesData); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public void Dispose() |
| | 127 | | { |
| 0 | 128 | | emotesCustomizationDataStore.equippedEmotes.OnSet -= OnEquippedEmotesSet; |
| 0 | 129 | | emotesCustomizationDataStore.equippedEmotes.OnAdded -= OnEquippedEmoteAddedOrRemoved; |
| 0 | 130 | | emotesCustomizationDataStore.equippedEmotes.OnRemoved -= OnEquippedEmoteAddedOrRemoved; |
| 0 | 131 | | commonDataStore.isSignUpFlow.OnChange -= OnSignupFlowChanged; |
| 0 | 132 | | if (ownUserProfile != null) |
| 0 | 133 | | ownUserProfile.OnUpdate -= OnOwnUserProfileUpdated; |
| 0 | 134 | | } |
| | 135 | | } |
| | 136 | | } |