< Summary

Class:EmotesCatalogBridge
Assembly:EmotesCatalog
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/EmotesCatalog/EmotesCatalogBridge.cs
Covered lines:7
Uncovered lines:23
Coverable lines:30
Total lines:95
Line coverage:23.3% (7 of 30)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EmotesCatalogBridge()0%110100%
GetOrCreate()0%6.566075%
RequestEmote(...)0%2100%
AddEmotesToCatalog(...)0%30500%
RequestOwnedEmotes(...)0%6200%
Update()0%2.862040%
Dispose()0%6200%
OnDestroy()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/EmotesCatalog/EmotesCatalogBridge.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL;
 5using DCL.Emotes;
 6using DCL.Helpers;
 7using DCL.Interface;
 8using UnityEngine;
 9
 10public 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
 6222    private readonly HashSet<string> emotesToRequestThisFrame = new HashSet<string>();
 23    private bool isAlreadyDestroyed;
 24
 25    public static EmotesCatalogBridge GetOrCreate()
 26    {
 727        var brigeGO = SceneReferences.i?.bridgeGameObject;
 728        if (SceneReferences.i?.bridgeGameObject == null)
 729            return new GameObject("Bridge").AddComponent<EmotesCatalogBridge>();
 30
 031        return brigeGO.GetOrCreateComponent<EmotesCatalogBridge>();
 32    }
 33
 034    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    {
 041        AddEmotesResponse request = null;
 42
 43        try
 44        {
 045            request = JsonUtility.FromJson<AddEmotesResponse>(payload);
 046        }
 047        catch (Exception e)
 48        {
 049            Debug.LogError($"Fail to parse emote json {e}");
 050        }
 51
 052        if (request == null)
 053            return;
 54
 055        if (string.IsNullOrEmpty(request.context))
 056            OnEmotesReceived?.Invoke(request.emotes);
 57        else
 058            OnOwnedEmotesReceived?.Invoke(request.emotes, request.context);
 059    }
 60
 61    public void RequestOwnedEmotes(string userId)
 62    {
 063        WebInterface.RequestEmotes(
 64            ownedByUser: userId,
 65            emoteIds: null,
 66            collectionIds: null,
 67            context: $"{userId}"
 68        );
 069    }
 70
 71    private void Update()
 72    {
 172473        if (emotesToRequestThisFrame.Count == 0)
 172474            return;
 75
 076        WebInterface.RequestEmotes(
 77            ownedByUser: null,
 78            emoteIds: emotesToRequestThisFrame.ToArray(),
 79            collectionIds: null,
 80            context: null
 81        );
 82
 83
 084        emotesToRequestThisFrame.Clear();
 085    }
 86
 87    public void Dispose()
 88    {
 089        if (isAlreadyDestroyed)
 090            return;
 091        Destroy(gameObject);
 092    }
 93
 12094    private void OnDestroy() { isAlreadyDestroyed = true; }
 95}