< Summary

Class:UserProfileController
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfileController.cs
Covered lines:17
Uncovered lines:43
Coverable lines:60
Total lines:139
Line coverage:28.3% (17 of 60)
Covered branches:0
Total branches:0

Metrics

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

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using DCL;
 4using System.Collections.Generic;
 5using System.Threading;
 6using UnityEngine;
 7
 8public class UserProfileController : MonoBehaviour
 9{
 40010    public static UserProfileController i { get; private set; }
 11
 12    public event Action OnBaseWereablesFail;
 13
 14    private static UserProfileDictionary userProfilesCatalogValue;
 15
 1616    private readonly Dictionary<string, UniTaskCompletionSource<UserProfile>> pendingUserProfileTasks = new (StringCompa
 17    private bool baseWearablesAlreadyRequested = false;
 18
 19    public static UserProfileDictionary userProfilesCatalog
 20    {
 21        get
 22        {
 7123            if (userProfilesCatalogValue == null)
 24            {
 125                userProfilesCatalogValue = Resources.Load<UserProfileDictionary>("UserProfilesCatalog");
 26            }
 27
 7128            return userProfilesCatalogValue;
 29        }
 30    }
 31
 32    [NonSerialized] public UserProfile ownUserProfile;
 33
 34    public void Awake()
 35    {
 1636        i = this;
 1637        ownUserProfile = UserProfile.GetOwnUserProfile();
 1638    }
 39
 40    public void LoadProfile(string payload)
 41    {
 042        if (!baseWearablesAlreadyRequested)
 43        {
 044            baseWearablesAlreadyRequested = true;
 045            CatalogController.RequestBaseWearables()
 46                             .Catch((error) =>
 47                             {
 048                                 OnBaseWereablesFail?.Invoke();
 049                                 Debug.LogError(error);
 050                             });
 51        }
 52
 053        if (payload == null)
 054            return;
 55
 056        var model = JsonUtility.FromJson<UserProfileModel>(payload);
 57
 058        ownUserProfile.UpdateData(model);
 059        userProfilesCatalog.Add(model.userId, ownUserProfile);
 060    }
 61
 062    public void AddUserProfileToCatalog(string payload) { AddUserProfileToCatalog(JsonUtility.FromJson<UserProfileModel>
 63
 64    public void AddUserProfilesToCatalog(string payload)
 65    {
 066        var usersPayload = JsonUtility.FromJson<AddUserProfilesToCatalogPayload>(payload);
 067        var users = usersPayload.users;
 068        var count = users.Length;
 69
 070        for (var i = 0; i < count; ++i)
 071            AddUserProfileToCatalog(users[i]);
 072    }
 73
 74    public void AddUserProfileToCatalog(UserProfileModel model)
 75    {
 76        // TODO: the renderer should not alter the userId nor ethAddress, this is just a patch derived from a kernel iss
 1977        model.userId = model.userId.ToLower();
 1978        model.ethAddress = model.ethAddress?.ToLower();
 79
 1980        if (!userProfilesCatalog.TryGetValue(model.userId, out UserProfile userProfile))
 581            userProfile = ScriptableObject.CreateInstance<UserProfile>();
 82
 1983        userProfile.UpdateData(model);
 1984        userProfilesCatalog.Add(model.userId, userProfile);
 85
 1986        if (pendingUserProfileTasks.TryGetValue(userProfile.userId, out var existingTask))
 87        {
 088            existingTask.TrySetResult(userProfile);
 089            pendingUserProfileTasks.Remove(userProfile.userId);
 90        }
 1991    }
 92
 93    public static UserProfile GetProfileByName(string targetUserName)
 94    {
 095        foreach (var userProfile in userProfilesCatalogValue)
 96        {
 097            if (userProfile.Value.userName.ToLower() == targetUserName.ToLower())
 098                return userProfile.Value;
 99        }
 100
 0101        return null;
 0102    }
 103
 4104    public static UserProfile GetProfileByUserId(string targetUserId) { return userProfilesCatalog.Get(targetUserId); }
 105
 106    public void RemoveUserProfilesFromCatalog(string payload)
 107    {
 0108        string[] usernames = JsonUtility.FromJson<string[]>(payload);
 0109        for (int index = 0; index < usernames.Length; index++)
 110        {
 0111            RemoveUserProfileFromCatalog(userProfilesCatalog.Get(usernames[index]));
 112        }
 0113    }
 114
 115    public void RemoveUserProfileFromCatalog(UserProfile userProfile)
 116    {
 0117        if (userProfile == null)
 0118            return;
 119
 0120        userProfilesCatalog.Remove(userProfile.userId);
 0121        Destroy(userProfile);
 0122    }
 123
 0124    public void ClearProfilesCatalog() { userProfilesCatalog?.Clear(); }
 125
 126    public UniTask<UserProfile> RequestFullUserProfileAsync(string userId, CancellationToken cancellationToken = default
 127    {
 0128        cancellationToken.ThrowIfCancellationRequested();
 129
 0130        if (pendingUserProfileTasks.TryGetValue(userId, out var existingTask))
 0131            return existingTask.Task;
 132
 0133        var task = new UniTaskCompletionSource<UserProfile>();
 0134        cancellationToken.RegisterWithoutCaptureExecutionContext(() => task.TrySetCanceled());
 0135        pendingUserProfileTasks[userId] = task;
 136
 0137        return task.Task;
 138    }
 139}