< 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:175
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    }
 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
 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.ComposeCorrectUrl(model.snapshots.body);
 10629    public string face256SnapshotURL => model.ComposeCorrectUrl(model.snapshots.face256);
 030    public string baseUrl => model.baseUrl;
 031    public UserProfileModel.ParcelsWithAccess[] parcelsWithAccess => model.parcelsWithAccess;
 24932    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
 59740    internal Dictionary<string, int> inventory = new Dictionary<string, int>();
 41
 59742    public ILazyTextureObserver snapshotObserver = new LazyTextureObserver();
 59743    public ILazyTextureObserver bodySnapshotObserver = new LazyTextureObserver();
 44
 59745    internal UserProfileModel model = new UserProfileModel() //Empty initialization to avoid nullchecks
 46    {
 47        avatar = new AvatarModel()
 48    };
 49
 50    public void UpdateData(UserProfileModel newModel)
 51    {
 59752        if (newModel == null)
 53        {
 054            model = null;
 055            return;
 56        }
 59757        bool faceSnapshotDirty = model.snapshots.face256 != newModel.snapshots.face256;
 59758        bool bodySnapshotDirty = model.snapshots.body != newModel.snapshots.body;
 59
 59760        model.userId = newModel.userId;
 59761        model.ethAddress = newModel.ethAddress;
 59762        model.parcelsWithAccess = newModel.parcelsWithAccess;
 59763        model.tutorialStep = newModel.tutorialStep;
 59764        model.hasClaimedName = newModel.hasClaimedName;
 59765        model.name = newModel.name;
 59766        model.email = newModel.email;
 59767        model.description = newModel.description;
 59768        model.baseUrl = newModel.baseUrl;
 59769        model.avatar.CopyFrom(newModel.avatar);
 59770        model.snapshots = newModel.snapshots;
 59771        model.hasConnectedWeb3 = newModel.hasConnectedWeb3;
 59772        model.blocked = newModel.blocked;
 59773        model.muted = newModel.muted;
 74
 59775        if (model.snapshots != null && faceSnapshotDirty)
 76        {
 677            snapshotObserver.RefreshWithUri(face256SnapshotURL);
 78        }
 79
 59780        if (model.snapshots != null && bodySnapshotDirty)
 81        {
 082            bodySnapshotObserver.RefreshWithUri(bodySnapshotURL);
 83        }
 84
 59785        OnUpdate?.Invoke(this);
 6286    }
 87
 88    public int GetItemAmount(string itemId)
 89    {
 543190        if (inventory == null || !inventory.ContainsKey(itemId))
 539391            return 0;
 92
 3893        return inventory[itemId];
 94    }
 95
 96    public void OverrideAvatar(AvatarModel newModel, Texture2D newFaceSnapshot)
 97    {
 198        model.avatar.CopyFrom(newModel);
 199        this.snapshotObserver.RefreshWithTexture(newFaceSnapshot);
 100
 1101        OnUpdate?.Invoke(this);
 1102    }
 103
 104    public void SetAvatarExpression(string id, EmoteSource source)
 105    {
 1106        var timestamp = (long) (DateTime.UtcNow - epochStart).TotalMilliseconds;
 1107        avatar.expressionTriggerId = id;
 1108        avatar.expressionTriggerTimestamp = timestamp;
 1109        WebInterface.SendExpression(id, timestamp);
 1110        OnUpdate?.Invoke(this);
 1111        OnAvatarEmoteSet?.Invoke(id, timestamp, source);
 1112    }
 113
 114    public void SetInventory(string[] inventoryIds)
 115    {
 206116        inventory.Clear();
 2867117        inventory = inventoryIds.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
 206118    }
 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    {
 2741138        if (ownUserProfile == null)
 139        {
 20140            ownUserProfile = Resources.Load<UserProfile>("ScriptableObjects/OwnUserProfile");
 141        }
 142
 2741143        return ownUserProfile;
 144    }
 145
 0146    public UserProfileModel CloneModel() => model.Clone();
 147
 68148    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    {
 597164        Application.quitting -= CleanUp;
 597165        Application.quitting += CleanUp;
 597166    }
 167
 168    private void CleanUp()
 169    {
 0170        Application.quitting -= CleanUp;
 0171        if (UnityEditor.AssetDatabase.Contains(this))
 0172            Resources.UnloadAsset(this);
 0173    }
 174#endif
 175}