< Summary

Class:UserProfile
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs
Covered lines:56
Uncovered lines:31
Coverable lines:87
Total lines:175
Line coverage:64.3% (56 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%5.055087.5%
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;
 29332    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
 55440    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 41
 55442    public ILazyTextureObserver snapshotObserver = new LazyTextureObserver();
 55443    public ILazyTextureObserver bodySnapshotObserver = new LazyTextureObserver();
 44
 55445    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.blocked = newModel.blocked;
 55072        model.muted = newModel.muted;
 73
 55074        if (model.snapshots != null && faceSnapshotDirty)
 75        {
 676            this.snapshotObserver.RefreshWithUri(model.snapshots.face256);
 77        }
 78
 55079        if (model.snapshots != null && bodySnapshotDirty)
 80        {
 081            bodySnapshotObserver.RefreshWithUri(model.snapshots.body);
 82        }
 83
 55084        OnUpdate?.Invoke(this);
 6185    }
 86
 87    public int GetItemAmount(string itemId)
 88    {
 543189        if (inventory == null || !inventory.ContainsKey(itemId))
 539390            return 0;
 91
 3892        return inventory[itemId];
 93    }
 94
 95    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 96    {
 197        model.avatar.CopyFrom(newModel);
 198        this.snapshotObserver.RefreshWithTexture(newFaceSnapshot);
 99
 1100        OnUpdate?.Invoke(this);
 1101    }
 102
 103    public void SetAvatarExpression(string id, EmoteSource source)
 104    {
 1105        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 1106        avatar.expressionTriggerId = id;
 1107        avatar.expressionTriggerTimestamp = timestamp;
 1108        WebInterface.SendExpression(id, timestamp);
 1109        OnUpdate?.Invoke(this);
 1110        OnAvatarEmoteSet?.Invoke(id, timestamp, source);
 1111    }
 112
 113    public void SetInventory(string[] inventoryIds)
 114    {
 206115        inventory.Clear();
 2867116        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 206117        OnInventorySet?.Invoke(inventory);
 33118    }
 119
 120    public void AddToInventory(string wearableId)
 121    {
 0122        if (inventory.ContainsKey(wearableId))
 0123            inventory[wearableId]++;
 124        else
 0125            inventory.Add(wearableId, 1);
 0126    }
 127
 0128    public void RemoveFromInventory(string wearableId) { inventory.Remove(wearableId); }
 129
 0130    public bool ContainsInInventory(string wearableId) => inventory.ContainsKey(wearableId);
 131
 0132    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 133
 134    internal static UserProfile ownUserProfile;
 135
 136    public static UserProfile GetOwnUserProfile()
 137    {
 2651138        if (ownUserProfile == null)
 139        {
 3140            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 141        }
 142
 2651143        return ownUserProfile;
 144    }
 145
 0146    public UserProfileModel CloneModel() => model.Clone();
 147
 86148    public bool IsBlocked(string userId) { return blocked != null && blocked.Contains(userId); }
 149
 150    public void Block(string userId)
 151    {
 19152        if (IsBlocked(userId))
 0153            return;
 19154        blocked.Add(userId);
 19155    }
 156
 0157    public void Unblock(string userId) { blocked.Remove(userId); }
 158
 0159    public bool HasEquipped(string wearableId) => avatar.wearables.Contains(wearableId);
 160
 161#if UNITY_EDITOR
 162    private void OnEnable()
 163    {
 554164        Application.quitting -= CleanUp;
 554165        Application.quitting += CleanUp;
 554166    }
 167
 168    private void CleanUp()
 169    {
 0170        Application.quitting -= CleanUp;
 0171        if (UnityEditor.AssetDatabase.Contains(this))
 0172            Resources.UnloadAsset(this);
 0173    }
 174#endif
 175}