< Summary

Class:RealmTrackerController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Common/Friends/RealmTrackerController.cs
Covered lines:19
Uncovered lines:58
Coverable lines:77
Total lines:177
Line coverage:24.6% (19 of 77)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RealmTrackerController(...)0%5.735069.23%
Dispose()0%6200%
AddHandler(...)0%4.374071.43%
RemoveHandler(...)0%6200%
RemoveAllHandlers()0%2.022083.33%
OnUpdateUserStatus(...)0%42600%
OnFriendsInitialized()0%12300%
ProcessFriendRealm(...)0%20400%
ProcessNewListener(...)0%42600%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Common/Friends/RealmTrackerController.cs

#LineLine coverage
 1using UnityEngine;
 2using System;
 3using System.Collections.Generic;
 4using Random = UnityEngine.Random;
 5
 6public class RealmTrackerController : IDisposable
 7{
 138    Dictionary<IRealmTrackerHandler, TrackedRealmInfo> listeners = new Dictionary<IRealmTrackerHandler, TrackedRealmInfo
 139    Dictionary<string, RealmTracker> friends = new Dictionary<string, RealmTracker>();
 10
 11    IFriendsController friendsController;
 12    Color[] friendColors;
 13
 1314    public RealmTrackerController(IFriendsController friendsController, Color[] friendColors)
 15    {
 1316        this.friendsController = friendsController;
 17
 1318        if (friendColors != null && friendColors.Length > 0)
 19        {
 1320            this.friendColors = friendColors;
 1321        }
 22        else
 23        {
 024            this.friendColors = new Color[] { Color.white };
 25        }
 26
 1327        if (friendsController != null)
 28        {
 029            if (!friendsController.isInitialized)
 30            {
 031                friendsController.OnInitialized += OnFriendsInitialized;
 32            }
 33
 034            friendsController.OnUpdateUserStatus += OnUpdateUserStatus;
 35        }
 1336    }
 37
 38    public void Dispose()
 39    {
 040        if (friendsController != null)
 41        {
 042            friendsController.OnInitialized -= OnFriendsInitialized;
 043            friendsController.OnUpdateUserStatus -= OnUpdateUserStatus;
 44        }
 045        listeners.Clear();
 046        friends.Clear();
 047    }
 48
 49    public void AddHandler(IRealmTrackerHandler listener)
 50    {
 51        TrackedRealmInfo wrapper;
 252        if (listeners.TryGetValue(listener, out wrapper))
 53        {
 054            return;
 55        }
 56
 257        wrapper = new TrackedRealmInfo(listener);
 58
 259        if (friendsController != null && friendsController.isInitialized)
 60        {
 061            ProcessNewListener(wrapper);
 62        }
 63
 264        listeners.Add(listener, wrapper);
 265    }
 66
 67    public void RemoveHandler(IRealmTrackerHandler listener)
 68    {
 69        TrackedRealmInfo wrapper;
 070        if (listeners.TryGetValue(listener, out wrapper))
 71        {
 072            wrapper.Dispose();
 073            listeners.Remove(listener);
 74        }
 075    }
 76
 77    public void RemoveAllHandlers()
 78    {
 179        using (var listenersIterator = listeners.GetEnumerator())
 80        {
 181            while (listenersIterator.MoveNext())
 82            {
 083                listenersIterator.Current.Value.Dispose();
 84            }
 185        }
 86
 187        listeners.Clear();
 188    }
 89
 90    void OnUpdateUserStatus(string userId, FriendsController.UserStatus status)
 91    {
 092        if (!friendsController.isInitialized)
 093            return;
 94
 95        RealmTracker friend;
 096        if (!friends.TryGetValue(userId, out friend))
 97        {
 098            friend = new RealmTracker(userId, friendColors[Random.Range(0, friendColors.Length)]);
 099            friends.Add(userId, friend);
 100        }
 101
 0102        friend.SetStatus(status);
 103
 0104        if (!friend.IsOnline())
 105        {
 0106            friend.RemoveAllListeners();
 0107        }
 108        else
 109        {
 0110            ProcessFriendRealm(friend, status.realm?.serverName);
 111        }
 0112    }
 113
 114    void OnFriendsInitialized()
 115    {
 0116        friendsController.OnInitialized -= OnFriendsInitialized;
 117
 0118        using (var friendsIterator = friendsController.GetFriends().GetEnumerator())
 119        {
 0120            while (friendsIterator.MoveNext())
 121            {
 0122                RealmTracker friend = new RealmTracker(friendsIterator.Current.Key, friendColors[Random.Range(0, friendC
 0123                friend.SetStatus(friendsIterator.Current.Value);
 0124                friends.Add(friendsIterator.Current.Key, friend);
 125            }
 0126        }
 127
 0128        using (var listenersIterator = listeners.GetEnumerator())
 129        {
 0130            while (listenersIterator.MoveNext())
 131            {
 0132                ProcessNewListener(listenersIterator.Current.Value);
 133            }
 0134        }
 0135    }
 136
 137    void ProcessFriendRealm(RealmTracker friend, string serverName)
 138    {
 0139        if (!friend.HasChangedRealm(serverName))
 0140            return;
 141
 0142        friend.RemoveAllListeners();
 143
 0144        using (var listenersIterator = listeners.GetEnumerator())
 145        {
 0146            while (listenersIterator.MoveNext())
 147            {
 0148                if (listenersIterator.Current.Value.ContainRealm(serverName))
 149                {
 0150                    friend.AddListener(listenersIterator.Current.Value);
 151                }
 152            }
 0153        }
 0154    }
 155
 156    void ProcessNewListener(TrackedRealmInfo listener)
 157    {
 0158        string friendRealm = string.Empty;
 0159        using (var friendIterator = friends.GetEnumerator())
 160        {
 0161            while (friendIterator.MoveNext())
 162            {
 0163                if (!friendIterator.Current.Value.IsOnline())
 164                {
 165                    continue;
 166                }
 167
 0168                friendRealm = friendIterator.Current.Value.status.realm?.serverName;
 169
 0170                if (listener.ContainRealm(friendRealm))
 171                {
 0172                    friendIterator.Current.Value.AddListener(listener);
 173                }
 174            }
 0175        }
 0176    }
 177}