| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Emotes; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Interface; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class EmotesCatalogBridge : MonoBehaviour, IEmotesCatalogBridge |
| | 11 | | { |
| | 12 | | [Serializable] |
| | 13 | | private class AddEmotesResponse |
| | 14 | | { |
| | 15 | | public EmoteItem[] emotes; |
| | 16 | | public string context; |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public event Action<WearableItem[]> OnEmotesReceived; |
| | 20 | | public event Action<WearableItem[], string> OnOwnedEmotesReceived; |
| | 21 | |
|
| 62 | 22 | | private readonly HashSet<string> emotesToRequestThisFrame = new HashSet<string>(); |
| | 23 | | private bool isAlreadyDestroyed; |
| | 24 | |
|
| | 25 | | public static EmotesCatalogBridge GetOrCreate() |
| | 26 | | { |
| 7 | 27 | | var brigeGO = SceneReferences.i?.bridgeGameObject; |
| 7 | 28 | | if (SceneReferences.i?.bridgeGameObject == null) |
| 7 | 29 | | return new GameObject("Bridge").AddComponent<EmotesCatalogBridge>(); |
| | 30 | |
|
| 0 | 31 | | return brigeGO.GetOrCreateComponent<EmotesCatalogBridge>(); |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public void RequestEmote(string emoteId) { emotesToRequestThisFrame.Add(emoteId); } |
| | 35 | |
|
| | 36 | | // Alex: If at some point base emotes are not embedded in the client but sent by kernel |
| | 37 | | // this call wouldn't be listened by any Promise and wont be processed. |
| | 38 | | // This issue can be solved easily by adding a different call AddBaseEmotes and a different event |
| | 39 | | public void AddEmotesToCatalog(string payload) |
| | 40 | | { |
| 0 | 41 | | AddEmotesResponse request = null; |
| | 42 | |
|
| | 43 | | try |
| | 44 | | { |
| 0 | 45 | | request = JsonUtility.FromJson<AddEmotesResponse>(payload); |
| 0 | 46 | | } |
| 0 | 47 | | catch (Exception e) |
| | 48 | | { |
| 0 | 49 | | Debug.LogError($"Fail to parse emote json {e}"); |
| 0 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | if (request == null) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | if (string.IsNullOrEmpty(request.context)) |
| 0 | 56 | | OnEmotesReceived?.Invoke(request.emotes); |
| | 57 | | else |
| 0 | 58 | | OnOwnedEmotesReceived?.Invoke(request.emotes, request.context); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public void RequestOwnedEmotes(string userId) |
| | 62 | | { |
| 0 | 63 | | WebInterface.RequestEmotes( |
| | 64 | | ownedByUser: userId, |
| | 65 | | emoteIds: null, |
| | 66 | | collectionIds: null, |
| | 67 | | context: $"{userId}" |
| | 68 | | ); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private void Update() |
| | 72 | | { |
| 1724 | 73 | | if (emotesToRequestThisFrame.Count == 0) |
| 1724 | 74 | | return; |
| | 75 | |
|
| 0 | 76 | | WebInterface.RequestEmotes( |
| | 77 | | ownedByUser: null, |
| | 78 | | emoteIds: emotesToRequestThisFrame.ToArray(), |
| | 79 | | collectionIds: null, |
| | 80 | | context: null |
| | 81 | | ); |
| | 82 | |
|
| | 83 | |
|
| 0 | 84 | | emotesToRequestThisFrame.Clear(); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void Dispose() |
| | 88 | | { |
| 0 | 89 | | if (isAlreadyDestroyed) |
| 0 | 90 | | return; |
| 0 | 91 | | Destroy(gameObject); |
| 0 | 92 | | } |
| | 93 | |
|
| 120 | 94 | | private void OnDestroy() { isAlreadyDestroyed = true; } |
| | 95 | | } |