< Summary

Class:UserProfile
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfile.cs
Covered lines:66
Uncovered lines:23
Coverable lines:89
Total lines:184
Line coverage:74.1% (66 of 89)
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%440100%
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;
 8using Environment = System.Environment;
 9
 10[CreateAssetMenu(fileName = "UserProfile", menuName = "UserProfile")]
 11public class UserProfile : ScriptableObject //TODO Move to base variable
 12{
 13    public enum EmoteSource
 14    {
 15        EmotesWheel,
 16        Shortcut,
 17        Command,
 18        Backpack
 19    }
 20
 121    static DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 22    public event Action<UserProfile> OnUpdate;
 23    public event Action<string, long, EmoteSource> OnAvatarEmoteSet;
 24
 56425    public string userId => model.userId;
 6526    public string ethAddress => model.ethAddress;
 23027    public string userName => model.name;
 3728    public string description => model.description;
 029    public string email => model.email;
 030    public string bodySnapshotURL => model.ComposeCorrectUrl(model.snapshots.body);
 12231    public string face256SnapshotURL => model.ComposeCorrectUrl(model.snapshots.face256);
 032    public string baseUrl => model.baseUrl;
 033    public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 23834    public List<string> blocked => model.blocked != null ? model.blocked : new List<string>();
 435    public List<string> muted => model.muted ?? new List<string>();
 26736    public bool hasConnectedWeb3 => model.hasConnectedWeb3;
 6537    public bool hasClaimedName => model.hasClaimedName;
 2438    public bool isGuest => !model.hasConnectedWeb3;
 88939    public AvatarModel avatar => model.avatar;
 040    public int tutorialStep => model.tutorialStep;
 41
 62642    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 43
 62644    public ILazyTextureObserver snapshotObserver = new LazyTextureObserver();
 62645    public ILazyTextureObserver bodySnapshotObserver = new LazyTextureObserver();
 46
 62647    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 48    {
 49        avatar = new AvatarModel()
 50    };
 51
 52    public void UpdateData(UserProfileModel newModel)
 53    {
 63054        if (newModel == null)
 55        {
 056            model = null;
 057            return;
 58        }
 63059        bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256;
 63060        bool bodySnapshotDirty = model.snapshots.body != newModel.snapshots.body;
 61
 63062        model.userId = newModel.userId;
 63063        model.ethAddress = newModel.ethAddress;
 63064        model.parcelsWithAccess = newModel.parcelsWithAccess;
 63065        model.tutorialStep = newModel.tutorialStep;
 63066        model.hasClaimedName = newModel.hasClaimedName;
 63067        model.name = newModel.name;
 63068        model.email = newModel.email;
 63069        model.description = newModel.description;
 63070        model.baseUrl = newModel.baseUrl;
 63071        model.avatar.CopyFrom(newModel.avatar);
 63072        model.snapshots = newModel.snapshots;
 63073        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 63074        model.blocked = newModel.blocked;
 63075        model.muted = newModel.muted;
 76
 63077        if (model.snapshots != null && faceSnapshotDirty)
 78        {
 1479            snapshotObserver.RefreshWithUri(face256SnapshotURL);
 80        }
 81
 63082        if (model.snapshots != null && bodySnapshotDirty)
 83        {
 084            bodySnapshotObserver.RefreshWithUri(bodySnapshotURL);
 85        }
 86
 63087        OnUpdate?.Invoke(this);
 6188    }
 89
 90    public int GetItemAmount(string itemId)
 91    {
 543192        if (inventory == null || !inventory.ContainsKey(itemId))
 539393            return 0;
 94
 3895        return inventory[itemId];
 96    }
 97
 98    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 99    {
 1100        model.avatar.CopyFrom(newModel);
 1101        this.snapshotObserver.RefreshWithTexture(newFaceSnapshot);
 102
 1103        OnUpdate?.Invoke(this);
 1104    }
 105
 106    public void SetAvatarExpression(string id, EmoteSource source)
 107    {
 1108        long timestamp = (long)(DateTime.UtcNow - epochStart).TotalMilliseconds;
 1109        avatar.expressionTriggerId = id;
 1110        avatar.expressionTriggerTimestamp = timestamp;
 111
 1112        ClientEmotesKernelService emotes = DCL.Environment.i.serviceLocator.Get<IRPC>().emotes;
 1113        emotes?.TriggerExpression(new TriggerExpressionRequest()
 114        {
 115            Id = id,
 116            Timestamp = timestamp
 117        });
 118
 1119        OnUpdate?.Invoke(this);
 1120        OnAvatarEmoteSet?.Invoke(id, timestamp, source);
 1121    }
 122
 123    public void SetInventory(string[] inventoryIds)
 124    {
 206125        inventory.Clear();
 2867126        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 206127    }
 128
 129    public void AddToInventory(string wearableId)
 130    {
 0131        if (inventory.ContainsKey(wearableId))
 0132            inventory[wearableId]++;
 133        else
 0134            inventory.Add(wearableId, 1);
 0135    }
 136
 0137    public void RemoveFromInventory(string wearableId) { inventory.Remove(wearableId); }
 138
 0139    public bool ContainsInInventory(string wearableId) => inventory.ContainsKey(wearableId);
 140
 0141    public string[] GetInventoryItemsIds() { return inventory.Keys.ToArray(); }
 142
 143    internal static UserProfile ownUserProfile;
 144
 145    public static UserProfile GetOwnUserProfile()
 146    {
 3346147        if (ownUserProfile == null)
 148        {
 20149            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 150        }
 151
 3346152        return ownUserProfile;
 153    }
 154
 0155    public UserProfileModel CloneModel() => model.Clone();
 156
 62157    public bool IsBlocked(string userId) { return blocked != null && blocked.Contains(userId); }
 158
 159    public void Block(string userId)
 160    {
 19161        if (IsBlocked(userId))
 0162            return;
 19163        blocked.Add(userId);
 19164    }
 165
 0166    public void Unblock(string userId) { blocked.Remove(userId); }
 167
 0168    public bool HasEquipped(string wearableId) => avatar.wearables.Contains(wearableId);
 169
 170#if UNITY_EDITOR
 171    private void OnEnable()
 172    {
 626173        Application.quitting -= CleanUp;
 626174        Application.quitting += CleanUp;
 626175    }
 176
 177    private void CleanUp()
 178    {
 0179        Application.quitting -= CleanUp;
 0180        if (UnityEditor.AssetDatabase.Contains(this))
 0181            Resources.UnloadAsset(this);
 0182    }
 183#endif
 184}