< Summary

Class:UserProfile
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs
Covered lines:57
Uncovered lines:31
Coverable lines:88
Total lines:176
Line coverage:64.7% (57 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%5.045088%
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        Backpack
 18    }
 19
 120    static DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 21    public event Action<UserProfile> OnUpdate;
 22    public event Action<string, long, EmoteSource> OnAvatarEmoteSet;
 23
 024    public string userId => model.userId;
 025    public string ethAddress => model.ethAddress;
 026    public string userName => model.name;
 027    public string description => model.description;
 028    public string email => model.email;
 029    public string bodySnapshotURL => model.ComposeCorrectUrl(model.snapshots.body);
 12230    public string face256SnapshotURL => model.ComposeCorrectUrl(model.snapshots.face256);
 031    public string baseUrl => model.baseUrl;
 032    public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 23833    public List<string> blocked => model.blocked != null ? model.blocked : new List<string>();
 434    public List<string> muted => model.muted ?? new List<string>();
 035    public bool hasConnectedWeb3 => model.hasConnectedWeb3;
 036    public bool hasClaimedName => model.hasClaimedName;
 037    public bool isGuest => !model.hasConnectedWeb3;
 038    public AvatarModel avatar => model.avatar;
 039    public int tutorialStep => model.tutorialStep;
 40
 61941    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 42
 61943    public ILazyTextureObserver snapshotObserver = new LazyTextureObserver();
 61944    public ILazyTextureObserver bodySnapshotObserver = new LazyTextureObserver();
 45
 61946    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 47    {
 48        avatar = new AvatarModel()
 49    };
 50
 51    public void UpdateData(UserProfileModel newModel)
 52    {
 62353        if (newModel == null)
 54        {
 055            model = null;
 056            return;
 57        }
 62358        bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256;
 62359        bool bodySnapshotDirty = model.snapshots.body != newModel.snapshots.body;
 60
 62361        model.userId = newModel.userId;
 62362        model.ethAddress = newModel.ethAddress;
 62363        model.parcelsWithAccess = newModel.parcelsWithAccess;
 62364        model.tutorialStep = newModel.tutorialStep;
 62365        model.hasClaimedName = newModel.hasClaimedName;
 62366        model.name = newModel.name;
 62367        model.email = newModel.email;
 62368        model.description = newModel.description;
 62369        model.baseUrl = newModel.baseUrl;
 62370        model.avatar.CopyFrom(newModel.avatar);
 62371        model.snapshots = newModel.snapshots;
 62372        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 62373        model.blocked = newModel.blocked;
 62374        model.muted = newModel.muted;
 75
 62376        if (model.snapshots != null && faceSnapshotDirty)
 77        {
 1478            snapshotObserver.RefreshWithUri(face256SnapshotURL);
 79        }
 80
 62381        if (model.snapshots != null && bodySnapshotDirty)
 82        {
 083            bodySnapshotObserver.RefreshWithUri(bodySnapshotURL);
 84        }
 85
 62386        OnUpdate?.Invoke(this);
 6387    }
 88
 89    public int GetItemAmount(string itemId)
 90    {
 543191        if (inventory == null || !inventory.ContainsKey(itemId))
 539392            return 0;
 93
 3894        return inventory[itemId];
 95    }
 96
 97    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 98    {
 199        model.avatar.CopyFrom(newModel);
 1100        this.snapshotObserver.RefreshWithTexture(newFaceSnapshot);
 101
 1102        OnUpdate?.Invoke(this);
 1103    }
 104
 105    public void SetAvatarExpression(string id, EmoteSource source)
 106    {
 1107        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 1108        avatar.expressionTriggerId = id;
 1109        avatar.expressionTriggerTimestamp = timestamp;
 1110        WebInterface.SendExpression(id, timestamp);
 1111        OnUpdate?.Invoke(this);
 1112        OnAvatarEmoteSet?.Invoke(id, timestamp, source);
 1113    }
 114
 115    public void SetInventory(string[] inventoryIds)
 116    {
 206117        inventory.Clear();
 2867118        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 206119    }
 120
 121    public void AddToInventory(string wearableId)
 122    {
 0123        if (inventory.ContainsKey(wearableId))
 0124            inventory[wearableId]++;
 125        else
 0126            inventory.Add(wearableId, 1);
 0127    }
 128
 0129    public void RemoveFromInventory(string wearableId) { inventory.Remove(wearableId); }
 130
 0131    public bool ContainsInInventory(string wearableId) => inventory.ContainsKey(wearableId);
 132
 0133    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 134
 135    internal static UserProfile ownUserProfile;
 136
 137    public static UserProfile GetOwnUserProfile()
 138    {
 4058139        if (ownUserProfile == null)
 140        {
 20141            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 142        }
 143
 4058144        return ownUserProfile;
 145    }
 146
 0147    public UserProfileModel CloneModel() => model.Clone();
 148
 62149    public bool IsBlocked(string userId) { return blocked != null && blocked.Contains(userId); }
 150
 151    public void Block(string userId)
 152    {
 19153        if (IsBlocked(userId))
 0154            return;
 19155        blocked.Add(userId);
 19156    }
 157
 0158    public void Unblock(string userId) { blocked.Remove(userId); }
 159
 0160    public bool HasEquipped(string wearableId) => avatar.wearables.Contains(wearableId);
 161
 162#if UNITY_EDITOR
 163    private void OnEnable()
 164    {
 619165        Application.quitting -= CleanUp;
 619166        Application.quitting += CleanUp;
 619167    }
 168
 169    private void CleanUp()
 170    {
 0171        Application.quitting -= CleanUp;
 0172        if (UnityEditor.AssetDatabase.Contains(this))
 0173            Resources.UnloadAsset(this);
 0174    }
 175#endif
 176}