< Summary

Class:UserProfile
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs
Covered lines:55
Uncovered lines:31
Coverable lines:86
Total lines:173
Line coverage:63.9% (55 of 86)
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%
AddToInventory(...)0%6200%
RemoveFromInventory(...)0%2100%
ContainsInInventory(...)0%2100%
GetInventoryItemsIds()0%2100%
GetOwnUserProfile()0%220100%
CloneModel()0%2100%
IsBlocked(...)0%220100%
Block(...)0%2.062075%
Unblock(...)0%2100%
HasEquipped(...)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{
 12    public enum EmoteSource
 13    {
 14        EmotesWheel,
 15        Shortcut,
 16        Command
 17    }
 18
 019    static DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 20    public event Action<UserProfile> OnUpdate;
 21    public event Action<string, long, EmoteSource> OnAvatarEmoteSet;
 22
 023    public string userId => model.userId;
 024    public string ethAddress => model.ethAddress;
 025    public string userName => model.name;
 026    public string description => model.description;
 027    public string email => model.email;
 028    public string bodySnapshotURL => model.snapshots.body;
 029    public string face256SnapshotURL => model.snapshots.face256;
 030    public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 29331    public List<string> blocked => model.blocked != null ? model.blocked : new List<string>();
 432    public List<string> muted => model.muted ?? new List<string>();
 033    public bool hasConnectedWeb3 => model.hasConnectedWeb3;
 034    public bool hasClaimedName => model.hasClaimedName;
 035    public bool isGuest => !model.hasConnectedWeb3;
 036    public AvatarModel avatar => model.avatar;
 037    public int tutorialStep => model.tutorialStep;
 38
 55539    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 40
 55541    public ILazyTextureObserver snapshotObserver = new LazyTextureObserver();
 55542    public ILazyTextureObserver bodySnapshotObserver = new LazyTextureObserver();
 43
 55544    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 45    {
 46        avatar = new AvatarModel()
 47    };
 48
 49    public void UpdateData(UserProfileModel newModel)
 50    {
 55451        if (newModel == null)
 52        {
 053            model = null;
 054            return;
 55        }
 55456        bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256;
 55457        bool bodySnapshotDirty = model.snapshots.body != newModel.snapshots.body;
 58
 55459        model.userId = newModel.userId;
 55460        model.ethAddress = newModel.ethAddress;
 55461        model.parcelsWithAccess = newModel.parcelsWithAccess;
 55462        model.tutorialStep = newModel.tutorialStep;
 55463        model.hasClaimedName = newModel.hasClaimedName;
 55464        model.name = newModel.name;
 55465        model.email = newModel.email;
 55466        model.description = newModel.description;
 55467        model.avatar.CopyFrom(newModel.avatar);
 55468        model.snapshots = newModel.snapshots;
 55469        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 55470        model.blocked = newModel.blocked;
 55471        model.muted = newModel.muted;
 72
 55473        if (model.snapshots != null && faceSnapshotDirty)
 74        {
 775            this.snapshotObserver.RefreshWithUri(model.snapshots.face256);
 76        }
 77
 55478        if (model.snapshots != null && bodySnapshotDirty)
 79        {
 180            bodySnapshotObserver.RefreshWithUri(model.snapshots.body);
 81        }
 82
 55483        OnUpdate?.Invoke(this);
 6284    }
 85
 86    public int GetItemAmount(string itemId)
 87    {
 543188        if (inventory == null || !inventory.ContainsKey(itemId))
 539389            return 0;
 90
 3891        return inventory[itemId];
 92    }
 93
 94    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 95    {
 196        model.avatar.CopyFrom(newModel);
 197        this.snapshotObserver.RefreshWithTexture(newFaceSnapshot);
 98
 199        OnUpdate?.Invoke(this);
 1100    }
 101
 102    public void SetAvatarExpression(string id, EmoteSource source)
 103    {
 1104        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 1105        avatar.expressionTriggerId = id;
 1106        avatar.expressionTriggerTimestamp = timestamp;
 1107        WebInterface.SendExpression(id, timestamp);
 1108        OnUpdate?.Invoke(this);
 1109        OnAvatarEmoteSet?.Invoke(id, timestamp, source);
 1110    }
 111
 112    public void SetInventory(string[] inventoryIds)
 113    {
 206114        inventory.Clear();
 2867115        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 206116    }
 117
 118    public void AddToInventory(string wearableId)
 119    {
 0120        if (inventory.ContainsKey(wearableId))
 0121            inventory[wearableId]++;
 122        else
 0123            inventory.Add(wearableId, 1);
 0124    }
 125
 0126    public void RemoveFromInventory(string wearableId) { inventory.Remove(wearableId); }
 127
 0128    public bool ContainsInInventory(string wearableId) => inventory.ContainsKey(wearableId);
 129
 0130    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 131
 132    internal static UserProfile ownUserProfile;
 133
 134    public static UserProfile GetOwnUserProfile()
 135    {
 2687136        if (ownUserProfile == null)
 137        {
 20138            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 139        }
 140
 2687141        return ownUserProfile;
 142    }
 143
 0144    public UserProfileModel CloneModel() => model.Clone();
 145
 86146    public bool IsBlocked(string userId) { return blocked != null && blocked.Contains(userId); }
 147
 148    public void Block(string userId)
 149    {
 19150        if (IsBlocked(userId))
 0151            return;
 19152        blocked.Add(userId);
 19153    }
 154
 0155    public void Unblock(string userId) { blocked.Remove(userId); }
 156
 0157    public bool HasEquipped(string wearableId) => avatar.wearables.Contains(wearableId);
 158
 159#if UNITY_EDITOR
 160    private void OnEnable()
 161    {
 555162        Application.quitting -= CleanUp;
 555163        Application.quitting += CleanUp;
 555164    }
 165
 166    private void CleanUp()
 167    {
 0168        Application.quitting -= CleanUp;
 0169        if (UnityEditor.AssetDatabase.Contains(this))
 0170            Resources.UnloadAsset(this);
 0171    }
 172#endif
 173}