< Summary

Class:UserProfile
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs
Covered lines:59
Uncovered lines:31
Coverable lines:90
Total lines:181
Line coverage:65.5% (59 of 90)
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.056088.89%
GetItemAmount(...)0%330100%
OverrideAvatar(...)0%220100%
SetAvatarExpression(...)0%330100%
SetInventory(...)0%550100%
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
 119    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    public event Action<Dictionary<string, int>> OnInventorySet;
 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.snapshots.body;
 030    public string face256SnapshotURL => model.snapshots.face256;
 031    public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 28332    public List<string> blocked => model.blocked != null ? model.blocked : new List<string>();
 433    public List<string> muted => model.muted ?? new List<string>();
 034    public bool hasConnectedWeb3 => model.hasConnectedWeb3;
 035    public bool hasClaimedName => model.hasClaimedName;
 036    public bool isGuest => !model.hasConnectedWeb3;
 037    public AvatarModel avatar => model.avatar;
 038    public int tutorialStep => model.tutorialStep;
 39
 55040    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 41
 55042    public ILazyTextureObserver snapshotObserver = new LazyTextureObserver();
 55043    public ILazyTextureObserver bodySnapshotObserver = new LazyTextureObserver();
 44
 55045    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 46    {
 47        avatar = new AvatarModel()
 48    };
 49
 50    public void UpdateData(UserProfileModel newModel)
 51    {
 55052        if (newModel == null)
 53        {
 054            model = null;
 055            return;
 56        }
 55057        bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256;
 55058        bool bodySnapshotDirty = model.snapshots.body != newModel.snapshots.body;
 59
 55060        model.userId = newModel.userId;
 55061        model.ethAddress = newModel.ethAddress;
 55062        model.parcelsWithAccess = newModel.parcelsWithAccess;
 55063        model.tutorialStep = newModel.tutorialStep;
 55064        model.hasClaimedName = newModel.hasClaimedName;
 55065        model.name = newModel.name;
 55066        model.email = newModel.email;
 55067        model.description = newModel.description;
 55068        model.avatar.CopyFrom(newModel.avatar);
 55069        model.snapshots = newModel.snapshots;
 55070        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 55071        model.inventory = newModel.inventory;
 55072        model.blocked = newModel.blocked;
 55073        model.muted = newModel.muted;
 74
 55075        if (model.inventory != null)
 76        {
 14077            SetInventory(model.inventory);
 78        }
 79
 55080        if (model.snapshots != null && faceSnapshotDirty)
 81        {
 682            this.snapshotObserver.RefreshWithUri(model.snapshots.face256);
 83        }
 84
 55085        if (model.snapshots != null && bodySnapshotDirty)
 86        {
 087            bodySnapshotObserver.RefreshWithUri(model.snapshots.body);
 88        }
 89
 55090        OnUpdate?.Invoke(this);
 6191    }
 92
 93    public int GetItemAmount(string itemId)
 94    {
 543195        if (inventory == null || !inventory.ContainsKey(itemId))
 539396            return 0;
 97
 3898        return inventory[itemId];
 99    }
 100
 101    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 102    {
 1103        model.avatar.CopyFrom(newModel);
 1104        this.snapshotObserver.RefreshWithTexture(newFaceSnapshot);
 105
 1106        OnUpdate?.Invoke(this);
 1107    }
 108
 109    public void SetAvatarExpression(string id, EmoteSource source)
 110    {
 1111        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 1112        avatar.expressionTriggerId = id;
 1113        avatar.expressionTriggerTimestamp = timestamp;
 1114        WebInterface.SendExpression(id, timestamp);
 1115        OnUpdate?.Invoke(this);
 1116        OnAvatarEmoteSet?.Invoke(id, timestamp, source);
 1117    }
 118
 119    public void SetInventory(string[] inventoryIds)
 120    {
 206121        inventory.Clear();
 2867122        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 206123        OnInventorySet?.Invoke(inventory);
 33124    }
 125
 126    public void AddToInventory(string wearableId)
 127    {
 0128        if (inventory.ContainsKey(wearableId))
 0129            inventory[wearableId]++;
 130        else
 0131            inventory.Add(wearableId, 1);
 0132    }
 133
 0134    public void RemoveFromInventory(string wearableId) { inventory.Remove(wearableId); }
 135
 0136    public bool ContainsInInventory(string wearableId) => inventory.ContainsKey(wearableId);
 137
 0138    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 139
 140    internal static UserProfile ownUserProfile;
 141
 142    public static UserProfile GetOwnUserProfile()
 143    {
 2618144        if (ownUserProfile == null)
 145        {
 3146            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 147        }
 148
 2618149        return ownUserProfile;
 150    }
 151
 0152    public UserProfileModel CloneModel() => model.Clone();
 153
 86154    public bool IsBlocked(string userId) { return blocked != null && blocked.Contains(userId); }
 155
 156    public void Block(string userId)
 157    {
 19158        if (IsBlocked(userId))
 0159            return;
 19160        blocked.Add(userId);
 19161    }
 162
 0163    public void Unblock(string userId) { blocked.Remove(userId); }
 164
 0165    public bool HasEquipped(string wearableId) => avatar.wearables.Contains(wearableId);
 166
 167#if UNITY_EDITOR
 168    private void OnEnable()
 169    {
 550170        Application.quitting -= CleanUp;
 550171        Application.quitting += CleanUp;
 550172    }
 173
 174    private void CleanUp()
 175    {
 0176        Application.quitting -= CleanUp;
 0177        if (UnityEditor.AssetDatabase.Contains(this))
 0178            Resources.UnloadAsset(this);
 0179    }
 180#endif
 181}