< 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:25
Coverable lines:87
Total lines:169
Line coverage:71.2% (62 of 87)
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 UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 9822    public List<string> blocked => model.blocked != null ? model.blocked : new List<string>();
 323    public List<string> muted => model.muted ?? new List<string>();
 024    public bool hasConnectedWeb3 => model.hasConnectedWeb3;
 025    public bool hasClaimedName => model.hasClaimedName;
 026    public AvatarModel avatar => model.avatar;
 027    public int tutorialStep => model.tutorialStep;
 11828    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 29
 030    public Texture2D faceSnapshot { get; private set; }
 31    private AssetPromise_Texture thumbnailPromise;
 32
 11833    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 34    {
 35        avatar = new AvatarModel()
 36    };
 37
 38    public void UpdateData(UserProfileModel newModel, bool downloadAssets = true)
 39    {
 17040        faceSnapshot = null;
 41
 17042        if (newModel == null)
 43        {
 044            model = null;
 045            return;
 46        }
 47
 17048        model.userId = newModel.userId;
 17049        model.ethAddress = newModel.ethAddress;
 17050        model.parcelsWithAccess = newModel.parcelsWithAccess;
 17051        model.tutorialStep = newModel.tutorialStep;
 17052        model.hasClaimedName = newModel.hasClaimedName;
 17053        model.name = newModel.name;
 17054        model.email = newModel.email;
 17055        model.description = newModel.description;
 17056        model.avatar.CopyFrom(newModel.avatar);
 17057        model.snapshots = newModel.snapshots;
 17058        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 17059        model.inventory = newModel.inventory;
 17060        model.blocked = newModel.blocked;
 17061        model.muted = newModel.muted;
 62
 17063        if (model.inventory != null)
 64        {
 1265            SetInventory(model.inventory);
 66        }
 67
 17068        if (downloadAssets && model.snapshots != null)
 69        {
 70            //NOTE(Brian): Get before forget to prevent referenceCount == 0 and asset unload
 8471            var newThumbnailPromise = ThumbnailsManager.GetThumbnail(model.snapshots.face256, OnFaceSnapshotReady);
 8472            ThumbnailsManager.ForgetThumbnail(thumbnailPromise);
 8473            thumbnailPromise = newThumbnailPromise;
 8474        }
 75        else
 76        {
 8677            ThumbnailsManager.ForgetThumbnail(thumbnailPromise);
 8678            thumbnailPromise = null;
 79        }
 80
 17081        OnUpdate?.Invoke(this);
 5482    }
 83
 84    public int GetItemAmount(string itemId)
 85    {
 373686        if (inventory == null || !inventory.ContainsKey(itemId))
 370287            return 0;
 88
 3489        return inventory[itemId];
 90    }
 91
 92    private void OnFaceSnapshotReady(Asset_Texture texture)
 93    {
 194        if (faceSnapshot != null)
 095            Destroy(faceSnapshot);
 96
 197        if (texture != null)
 098            faceSnapshot = texture.texture;
 99
 1100        OnUpdate?.Invoke(this);
 1101        OnFaceSnapshotReadyEvent?.Invoke(faceSnapshot);
 0102    }
 103
 104    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 105    {
 1106        if (model?.snapshots != null)
 107        {
 1108            if (thumbnailPromise != null)
 109            {
 0110                ThumbnailsManager.ForgetThumbnail(thumbnailPromise);
 0111                thumbnailPromise = null;
 112            }
 113
 1114            OnFaceSnapshotReady(null);
 115        }
 116
 1117        model.avatar.CopyFrom(newModel);
 1118        this.faceSnapshot = newFaceSnapshot;
 1119        OnUpdate?.Invoke(this);
 1120        OnFaceSnapshotReadyEvent?.Invoke(faceSnapshot);
 0121    }
 122
 123    public void SetAvatarExpression(string id)
 124    {
 2125        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 2126        avatar.expressionTriggerId = id;
 2127        avatar.expressionTriggerTimestamp = timestamp;
 2128        WebInterface.SendExpression(id, timestamp);
 2129        OnUpdate?.Invoke(this);
 2130        OnAvatarExpressionSet?.Invoke(id, timestamp);
 2131    }
 132
 133    public void SetInventory(string[] inventoryIds)
 134    {
 32135        inventory.Clear();
 257136        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 32137    }
 138
 0139    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 140
 141    internal static UserProfile ownUserProfile;
 142
 143    public static UserProfile GetOwnUserProfile()
 144    {
 1364145        if (ownUserProfile == null)
 146        {
 1147            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 148        }
 149
 1364150        return ownUserProfile;
 151    }
 152
 0153    public UserProfileModel CloneModel() => model.Clone();
 154
 155#if UNITY_EDITOR
 156    private void OnEnable()
 157    {
 118158        Application.quitting -= CleanUp;
 118159        Application.quitting += CleanUp;
 118160    }
 161
 162    private void CleanUp()
 163    {
 0164        Application.quitting -= CleanUp;
 0165        if (UnityEditor.AssetDatabase.Contains(this))
 0166            Resources.UnloadAsset(this);
 0167    }
 168#endif
 169}