< Summary

Class:UserProfileController
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfileController.cs
Covered lines:15
Uncovered lines:34
Coverable lines:49
Total lines:114
Line coverage:30.6% (15 of 49)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
LoadProfile(...)0%12300%
AddUserProfileToCatalog(...)0%2100%
AddUserProfilesToCatalog(...)0%6200%
AddUserProfileToCatalog(...)0%440100%
GetProfileByName(...)0%12300%
GetProfileByUserId(...)0%110100%
RemoveUserProfilesFromCatalog(...)0%6200%
RemoveUserProfileFromCatalog(...)0%6200%
ClearProfilesCatalog()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfileController.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using UnityEngine;
 4
 5public class UserProfileController : MonoBehaviour
 6{
 5727    public static UserProfileController i { get; private set; }
 8
 9    public event Action OnBaseWereablesFail;
 10
 11    private static UserProfileDictionary userProfilesCatalogValue;
 12    private bool baseWearablesAlreadyRequested = false;
 13
 14    public static UserProfileDictionary userProfilesCatalog
 15    {
 16        get
 17        {
 12918            if (userProfilesCatalogValue == null)
 19            {
 120                userProfilesCatalogValue = Resources.Load<UserProfileDictionary>("UserProfilesCatalog");
 21            }
 22
 12923            return userProfilesCatalogValue;
 24        }
 25    }
 26
 27    [NonSerialized] public UserProfile ownUserProfile;
 28
 29    public void Awake()
 30    {
 1631        i = this;
 1632        ownUserProfile = UserProfile.GetOwnUserProfile();
 1633    }
 34
 35    public void LoadProfile(string payload)
 36    {
 037        if (!baseWearablesAlreadyRequested)
 38        {
 039            baseWearablesAlreadyRequested = true;
 040            CatalogController.RequestBaseWearables()
 41                             .Catch((error) =>
 42                             {
 043                                 OnBaseWereablesFail?.Invoke();
 044                                 Debug.LogError(error);
 045                             });
 46        }
 47
 048        if (payload == null)
 049            return;
 50
 051        var model = JsonUtility.FromJson<UserProfileModel>(payload);
 52
 053        ownUserProfile.UpdateData(model);
 054        userProfilesCatalog.Add(model.userId, ownUserProfile);
 055    }
 56
 057    public void AddUserProfileToCatalog(string payload) { AddUserProfileToCatalog(JsonUtility.FromJson<UserProfileModel>
 58
 59    public void AddUserProfilesToCatalog(string payload)
 60    {
 061        var usersPayload = JsonUtility.FromJson<AddUserProfilesToCatalogPayload>(payload);
 062        var users = usersPayload.users;
 063        var count = users.Length;
 64
 065        for (var i = 0; i < count; ++i)
 066            AddUserProfileToCatalog(users[i]);
 067    }
 68
 69    public void AddUserProfileToCatalog(UserProfileModel model)
 70    {
 71        // TODO: the renderer should not alter the userId nor ethAddress, this is just a patch derived from a kernel iss
 1972        model.userId = model.userId.ToLower();
 1973        model.ethAddress = model.ethAddress?.ToLower();
 74
 1975        if (!userProfilesCatalog.TryGetValue(model.userId, out UserProfile userProfile))
 576            userProfile = ScriptableObject.CreateInstance<UserProfile>();
 77
 1978        userProfile.UpdateData(model);
 1979        userProfilesCatalog.Add(model.userId, userProfile);
 1980    }
 81
 82    public static UserProfile GetProfileByName(string targetUserName)
 83    {
 084        foreach (var userProfile in userProfilesCatalogValue)
 85        {
 086            if (userProfile.Value.userName.ToLower() == targetUserName.ToLower())
 087                return userProfile.Value;
 88        }
 89
 090        return null;
 091    }
 92
 493    public static UserProfile GetProfileByUserId(string targetUserId) { return userProfilesCatalog.Get(targetUserId); }
 94
 95    public void RemoveUserProfilesFromCatalog(string payload)
 96    {
 097        string[] usernames = JsonUtility.FromJson<string[]>(payload);
 098        for (int index = 0; index < usernames.Length; index++)
 99        {
 0100            RemoveUserProfileFromCatalog(userProfilesCatalog.Get(usernames[index]));
 101        }
 0102    }
 103
 104    public void RemoveUserProfileFromCatalog(UserProfile userProfile)
 105    {
 0106        if (userProfile == null)
 0107            return;
 108
 0109        userProfilesCatalog.Remove(userProfile.userId);
 0110        Destroy(userProfile);
 0111    }
 112
 0113    public void ClearProfilesCatalog() { userProfilesCatalog?.Clear(); }
 114}