< Summary

Class:UserProfile
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs
Covered lines:62
Uncovered lines:26
Coverable lines:88
Total lines:170
Line coverage:70.4% (62 of 88)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UserProfile()0%110100%
UserProfile()0%110100%
UpdateData(...)0%6.016093.1%
GetItemAmount(...)0%330100%
OnFaceSnapshotReady(...)0%6.975057.14%
OverrideAvatar(...)0%8.327070%
SetAvatarExpression(...)0%330100%
SetInventory(...)0%440100%
GetInventoryItemsIds()0%2100%
GetOwnUserProfile()0%220100%
CloneModel()0%2100%
OnEnable()0%110100%
CleanUp()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL;
 5using DCL.Interface;
 6using UnityEngine;
 7
 8[CreateAssetMenu(fileName = "UserProfile", menuName = "UserProfile")]
 9public class UserProfile : ScriptableObject //TODO Move to base variable
 10{
 111    static DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 12    public event Action<UserProfile> OnUpdate;
 13    public event Action<Texture2D> OnFaceSnapshotReadyEvent;
 14    public event Action<string, long> OnAvatarExpressionSet;
 15
 016    public string userId => model.userId;
 017    public string ethAddress => model.ethAddress;
 018    public string userName => model.name;
 019    public string description => model.description;
 020    public string email => model.email;
 021    public string bodySnapshotURL => model.snapshots.body;
 022    public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 10023    public List<string> blocked => model.blocked != null ? model.blocked : new List<string>();
 324    public List<string> muted => model.muted ?? new List<string>();
 025    public bool hasConnectedWeb3 => model.hasConnectedWeb3;
 026    public bool hasClaimedName => model.hasClaimedName;
 027    public AvatarModel avatar => model.avatar;
 028    public int tutorialStep => model.tutorialStep;
 11829    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 30
 031    public Texture2D faceSnapshot { get; private set; }
 32    private AssetPromise_Texture thumbnailPromise;
 33
 11834    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 35    {
 36        avatar = new AvatarModel()
 37    };
 38
 39    public void UpdateData(UserProfileModel newModel, bool downloadAssets = true)
 40    {
 17241        faceSnapshot = null;
 42
 17243        if (newModel == null)
 44        {
 045            model = null;
 046            return;
 47        }
 48
 17249        model.userId = newModel.userId;
 17250        model.ethAddress = newModel.ethAddress;
 17251        model.parcelsWithAccess = newModel.parcelsWithAccess;
 17252        model.tutorialStep = newModel.tutorialStep;
 17253        model.hasClaimedName = newModel.hasClaimedName;
 17254        model.name = newModel.name;
 17255        model.email = newModel.email;
 17256        model.description = newModel.description;
 17257        model.avatar.CopyFrom(newModel.avatar);
 17258        model.snapshots = newModel.snapshots;
 17259        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 17260        model.inventory = newModel.inventory;
 17261        model.blocked = newModel.blocked;
 17262        model.muted = newModel.muted;
 63
 17264        if (model.inventory != null)
 65        {
 1266            SetInventory(model.inventory);
 67        }
 68
 17269        if (downloadAssets && model.snapshots != null)
 70        {
 71            //NOTE(Brian): Get before forget to prevent referenceCount == 0 and asset unload
 8672            var newThumbnailPromise = ThumbnailsManager.GetThumbnail(model.snapshots.face256, OnFaceSnapshotReady);
 8673            ThumbnailsManager.ForgetThumbnail(thumbnailPromise);
 8674            thumbnailPromise = newThumbnailPromise;
 8675        }
 76        else
 77        {
 8678            ThumbnailsManager.ForgetThumbnail(thumbnailPromise);
 8679            thumbnailPromise = null;
 80        }
 81
 17282        OnUpdate?.Invoke(this);
 5683    }
 84
 85    public int GetItemAmount(string itemId)
 86    {
 434287        if (inventory == null || !inventory.ContainsKey(itemId))
 430888            return 0;
 89
 3490        return inventory[itemId];
 91    }
 92
 93    private void OnFaceSnapshotReady(Asset_Texture texture)
 94    {
 195        if (faceSnapshot != null)
 096            Destroy(faceSnapshot);
 97
 198        if (texture != null)
 099            faceSnapshot = texture.texture;
 100
 1101        OnUpdate?.Invoke(this);
 1102        OnFaceSnapshotReadyEvent?.Invoke(faceSnapshot);
 0103    }
 104
 105    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 106    {
 1107        if (model?.snapshots != null)
 108        {
 1109            if (thumbnailPromise != null)
 110            {
 0111                ThumbnailsManager.ForgetThumbnail(thumbnailPromise);
 0112                thumbnailPromise = null;
 113            }
 114
 1115            OnFaceSnapshotReady(null);
 116        }
 117
 1118        model.avatar.CopyFrom(newModel);
 1119        this.faceSnapshot = newFaceSnapshot;
 1120        OnUpdate?.Invoke(this);
 1121        OnFaceSnapshotReadyEvent?.Invoke(faceSnapshot);
 0122    }
 123
 124    public void SetAvatarExpression(string id)
 125    {
 2126        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 2127        avatar.expressionTriggerId = id;
 2128        avatar.expressionTriggerTimestamp = timestamp;
 2129        WebInterface.SendExpression(id, timestamp);
 2130        OnUpdate?.Invoke(this);
 2131        OnAvatarExpressionSet?.Invoke(id, timestamp);
 2132    }
 133
 134    public void SetInventory(string[] inventoryIds)
 135    {
 32136        inventory.Clear();
 257137        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 32138    }
 139
 0140    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 141
 142    internal static UserProfile ownUserProfile;
 143
 144    public static UserProfile GetOwnUserProfile()
 145    {
 1470146        if (ownUserProfile == null)
 147        {
 1148            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 149        }
 150
 1470151        return ownUserProfile;
 152    }
 153
 0154    public UserProfileModel CloneModel() => model.Clone();
 155
 156#if UNITY_EDITOR
 157    private void OnEnable()
 158    {
 118159        Application.quitting -= CleanUp;
 118160        Application.quitting += CleanUp;
 118161    }
 162
 163    private void CleanUp()
 164    {
 0165        Application.quitting -= CleanUp;
 0166        if (UnityEditor.AssetDatabase.Contains(this))
 0167            Resources.UnloadAsset(this);
 0168    }
 169#endif
 170}