< Summary

Class:UserProfile
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs
Covered lines:54
Uncovered lines:23
Coverable lines:77
Total lines:156
Line coverage:70.1% (54 of 77)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UserProfile()0%2100%
UserProfile()0%110100%
UpdateData(...)0%5.015091.67%
GetItemAmount(...)0%330100%
OverrideAvatar(...)0%220100%
SetAvatarExpression(...)0%330100%
SetInventory(...)0%440100%
GetInventoryItemsIds()0%2100%
GetOwnUserProfile()0%220100%
CloneModel()0%2100%
IsBlocked(...)0%220100%
Block(...)0%2.062075%
Unblock(...)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.Helpers;
 6using DCL.Interface;
 7using UnityEngine;
 8
 9[CreateAssetMenu(fileName = "UserProfile", menuName = "UserProfile")]
 10public class UserProfile : ScriptableObject //TODO Move to base variable
 11{
 012    static DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 13    public event Action<UserProfile> OnUpdate;
 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 string face128SnapshotURL => model.snapshots.face128;
 023    public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 20424    public List<string> blocked => model.blocked != null ? model.blocked : new List<string>();
 325    public List<string> muted => model.muted ?? new List<string>();
 026    public bool hasConnectedWeb3 => model.hasConnectedWeb3;
 027    public bool hasClaimedName => model.hasClaimedName;
 028    public AvatarModel avatar => model.avatar;
 029    public int tutorialStep => model.tutorialStep;
 30
 15631    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 32
 15633    public ILazyTextureObserver snapshotObserver = new LazyTextureObserver();
 34
 15635    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 36    {
 37        avatar = new AvatarModel()
 38    };
 39
 40    public void UpdateData(UserProfileModel newModel)
 41    {
 22942        if (newModel == null)
 43        {
 044            model = null;
 045            return;
 46        }
 47
 22948        bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256;
 49
 22950        model.userId = newModel.userId;
 22951        model.ethAddress = newModel.ethAddress;
 22952        model.parcelsWithAccess = newModel.parcelsWithAccess;
 22953        model.tutorialStep = newModel.tutorialStep;
 22954        model.hasClaimedName = newModel.hasClaimedName;
 22955        model.name = newModel.name;
 22956        model.email = newModel.email;
 22957        model.description = newModel.description;
 22958        model.avatar.CopyFrom(newModel.avatar);
 22959        model.snapshots = newModel.snapshots;
 22960        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 22961        model.inventory = newModel.inventory;
 22962        model.blocked = newModel.blocked;
 22963        model.muted = newModel.muted;
 64
 22965        if (model.inventory != null)
 66        {
 2967            SetInventory(model.inventory);
 68        }
 69
 22970        if (model.snapshots != null && faceSnapshotDirty)
 71        {
 572            this.snapshotObserver.RefreshWithUri(model.snapshots.face256);
 73        }
 74
 22975        OnUpdate?.Invoke(this);
 7176    }
 77
 78    public int GetItemAmount(string itemId)
 79    {
 499280        if (inventory == null || !inventory.ContainsKey(itemId))
 495881            return 0;
 82
 3483        return inventory[itemId];
 84    }
 85
 86    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 87    {
 188        model.avatar.CopyFrom(newModel);
 189        this.snapshotObserver.RefreshWithTexture(newFaceSnapshot);
 90
 191        OnUpdate?.Invoke(this);
 192    }
 93
 94    public void SetAvatarExpression(string id)
 95    {
 296        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 297        avatar.expressionTriggerId = id;
 298        avatar.expressionTriggerTimestamp = timestamp;
 299        WebInterface.SendExpression(id, timestamp);
 2100        OnUpdate?.Invoke(this);
 2101        OnAvatarExpressionSet?.Invoke(id, timestamp);
 2102    }
 103
 104    public void SetInventory(string[] inventoryIds)
 105    {
 92106        inventory.Clear();
 1067107        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 92108    }
 109
 0110    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 111
 112    internal static UserProfile ownUserProfile;
 113
 114    public static UserProfile GetOwnUserProfile()
 115    {
 1058116        if (ownUserProfile == null)
 117        {
 1118            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 119        }
 120
 1058121        return ownUserProfile;
 122    }
 123
 0124    public UserProfileModel CloneModel() => model.Clone();
 125
 126    public bool IsBlocked(string userId)
 127    {
 50128        return blocked != null && blocked.Contains(userId);
 129    }
 130
 131    public void Block(string userId)
 132    {
 18133        if (IsBlocked(userId)) return;
 18134        blocked.Add(userId);
 18135    }
 136
 137    public void Unblock(string userId)
 138    {
 0139        blocked.Remove(userId);
 0140    }
 141
 142#if UNITY_EDITOR
 143    private void OnEnable()
 144    {
 156145        Application.quitting -= CleanUp;
 156146        Application.quitting += CleanUp;
 156147    }
 148
 149    private void CleanUp()
 150    {
 0151        Application.quitting -= CleanUp;
 0152        if (UnityEditor.AssetDatabase.Contains(this))
 0153            Resources.UnloadAsset(this);
 0154    }
 155#endif
 156}