< Summary

Class:EmotesCatalogBridge
Assembly:EmotesCatalog
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/EmotesCatalog/EmotesCatalogBridge.cs
Covered lines:7
Uncovered lines:40
Coverable lines:47
Total lines:138
Line coverage:14.8% (7 of 47)
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%42600%
ResolveMissingEmotesRejection(...)0%30500%
RequestOwnedEmotes(...)0%6200%
EmoteIdsToContextString(...)0%2100%
ContextStringToEmoteIds(...)0%2100%
Update()0%3.192033.33%
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.Helpers;
 6using DCL.Interface;
 7using UnityEngine;
 8
 9public class EmotesCatalogBridge : MonoBehaviour, IEmotesCatalogBridge
 10{
 11    [Serializable]
 12    private class AddEmotesResponse
 13    {
 14        public EmoteItem[] emotes;
 15        public string context;
 16    }
 17
 18    public event Action<WearableItem[]> OnEmotesReceived;
 19    public event EmoteRejectedDelegate OnEmoteRejected;
 20    public event Action<WearableItem[], string> OnOwnedEmotesReceived;
 21
 2322    private readonly HashSet<string> emotesToRequestThisFrame = new HashSet<string>();
 23    private bool isAlreadyDestroyed;
 24
 25    public static EmotesCatalogBridge GetOrCreate()
 26    {
 2027        var brigeGO = SceneReferences.i?.bridgeGameObject;
 2028        if (SceneReferences.i?.bridgeGameObject == null)
 2029            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        var context = request.context;
 056        if (string.IsNullOrEmpty(context))
 57        {
 058            Debug.LogError("EmotesCatalogBridge error: empty context is not supposed to be in the wearables request");
 59        }
 60        else
 61        {
 062            if (request.context.StartsWith("emotes:"))
 63            {
 064                ResolveMissingEmotesRejection(request.context, request.emotes);
 065                OnEmotesReceived?.Invoke(request.emotes);
 66            }
 67            else
 68            {
 069                OnOwnedEmotesReceived?.Invoke(request.emotes, request.context);
 70            }
 71        }
 072    }
 73
 74    private void ResolveMissingEmotesRejection(string requestContext, EmoteItem[] emotes)
 75    {
 076        var emoteIdsFromContext = ContextStringToEmoteIds(requestContext);
 077        var loadedEmoteIds = new HashSet<string>();
 078        foreach (var emote in emotes)
 79        {
 080            loadedEmoteIds.Add(emote.id);
 81        }
 82
 083        foreach(var emoteId in emoteIdsFromContext)
 084            if (!loadedEmoteIds.Contains(emoteId))
 85            {
 086                OnEmoteRejected?.Invoke(emoteId, "Emote from context not found in response: ");
 87            }
 088    }
 89
 90    public void RequestOwnedEmotes(string userId)
 91    {
 092        WebInterface.RequestEmotes(
 93            ownedByUser: userId,
 94            emoteIds: null,
 95            collectionIds: null,
 96            context: $"{userId}"
 97        );
 098    }
 99
 100    private static string EmoteIdsToContextString(IEnumerable<string> emoteIds)
 101    {
 0102        var commaJoinedEmoteIds = string.Join(",", emoteIds);
 0103        return "emotes:" + commaJoinedEmoteIds;
 104    }
 105
 106    private static string[] ContextStringToEmoteIds(string context)
 107    {
 0108        context = context.Replace("emotes:", "");
 0109        return context.Split(',');
 110    }
 111
 112    private void Update()
 113    {
 5505114        if (emotesToRequestThisFrame.Count == 0)
 5505115            return;
 116
 0117        var commaJoinedEmoteIds = string.Join(",", emotesToRequestThisFrame);
 118
 0119        WebInterface.RequestEmotes(
 120            ownedByUser: null,
 121            emoteIds: emotesToRequestThisFrame.ToArray(),
 122            collectionIds: null,
 123            context: EmoteIdsToContextString(emotesToRequestThisFrame)
 124        );
 125
 126
 0127        emotesToRequestThisFrame.Clear();
 0128    }
 129
 130    public void Dispose()
 131    {
 0132        if (isAlreadyDestroyed)
 0133            return;
 0134        Destroy(gameObject);
 0135    }
 136
 42137    private void OnDestroy() { isAlreadyDestroyed = true; }
 138}