< Summary

Class:FriendTrackerController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Common/Friends/FriendTrackerController.cs
Covered lines:16
Uncovered lines:62
Coverable lines:78
Total lines:178
Line coverage:20.5% (16 of 78)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FriendTrackerController(...)0%5.095084.62%
Dispose()0%6200%
AddHandler(...)0%20400%
RemoveHandler(...)0%6200%
RemoveAllHandlers()0%2.022083.33%
OnUpdateUserStatus(...)0%20400%
OnFriendsInitialized()0%12300%
ProcessFriendLocation(...)0%20400%
ProcessNewListener(...)0%20400%

File(s)

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

#LineLine coverage
 1using UnityEngine;
 2using System;
 3using System.Collections.Generic;
 4using Random = UnityEngine.Random;
 5
 6public class FriendTrackerController : IDisposable
 7{
 398    Dictionary<IFriendTrackerHandler, TrackedSceneInfo> listeners = new Dictionary<IFriendTrackerHandler, TrackedSceneIn
 399    Dictionary<string, FriendTracker> friends = new Dictionary<string, FriendTracker>();
 10
 11    IFriendsController friendsController;
 12    Color[] friendColors;
 13
 3914    public FriendTrackerController(IFriendsController friendsController, Color[] friendColors)
 15    {
 3916        this.friendsController = friendsController;
 17
 3918        if (friendColors != null && friendColors.Length > 0)
 19        {
 020            this.friendColors = friendColors;
 021        }
 22        else
 23        {
 3924            this.friendColors = new Color[] { Color.white };
 25        }
 26
 3927        if (friendsController != null)
 28        {
 2729            if (!friendsController.isInitialized)
 30            {
 2731                friendsController.OnInitialized += OnFriendsInitialized;
 32            }
 33
 2734            friendsController.OnUpdateUserStatus += OnUpdateUserStatus;
 35        }
 3936    }
 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(IFriendTrackerHandler listener)
 50    {
 51        TrackedSceneInfo wrapper;
 052        if (listeners.TryGetValue(listener, out wrapper))
 53        {
 054            return;
 55        }
 56
 057        wrapper = new TrackedSceneInfo(listener);
 58
 059        if (friendsController != null && friendsController.isInitialized)
 60        {
 061            ProcessNewListener(wrapper);
 62        }
 63
 064        listeners.Add(listener, wrapper);
 065    }
 66
 67    public void RemoveHandler(IFriendTrackerHandler listener)
 68    {
 69        TrackedSceneInfo wrapper;
 070        if (listeners.TryGetValue(listener, out wrapper))
 71        {
 072            wrapper.Dispose();
 073            listeners.Remove(listener);
 74        }
 075    }
 76
 77    public void RemoveAllHandlers()
 78    {
 379        using (var listenersIterator = listeners.GetEnumerator())
 80        {
 381            while (listenersIterator.MoveNext())
 82            {
 083                listenersIterator.Current.Value.Dispose();
 84            }
 385        }
 86
 387        listeners.Clear();
 388    }
 89
 90    void OnUpdateUserStatus(string userId, FriendsController.UserStatus status)
 91    {
 092        if (!friendsController.isInitialized)
 093            return;
 94
 95        FriendTracker friend;
 096        if (!friends.TryGetValue(userId, out friend))
 97        {
 098            friend = new FriendTracker(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            ProcessFriendLocation(friend, new Vector2Int((int) status.position.x, (int) status.position.y));
 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                FriendTracker friend = new FriendTracker(friendsIterator.Current.Key, friendColors[Random.Range(0, frien
 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 ProcessFriendLocation(FriendTracker friend, Vector2Int coords)
 138    {
 0139        if (!friend.HasChangedLocation(coords))
 0140            return;
 141
 0142        friend.RemoveAllListeners();
 143
 0144        using (var listenersIterator = listeners.GetEnumerator())
 145        {
 0146            while (listenersIterator.MoveNext())
 147            {
 0148                if (listenersIterator.Current.Value.ContainCoords(coords))
 149                {
 0150                    friend.AddListener(listenersIterator.Current.Value);
 151                }
 152            }
 0153        }
 0154    }
 155
 156    void ProcessNewListener(TrackedSceneInfo listener)
 157    {
 0158        Vector2Int friendCoords = new Vector2Int();
 0159        using (var friendIterator = friends.GetEnumerator())
 160        {
 0161            while (friendIterator.MoveNext())
 162            {
 0163                if (!friendIterator.Current.Value.IsOnline())
 164                {
 165                    continue;
 166                }
 167
 0168                friendCoords.x = (int) friendIterator.Current.Value.status.position.x;
 0169                friendCoords.y = (int) friendIterator.Current.Value.status.position.y;
 170
 0171                if (listener.ContainCoords(friendCoords))
 172                {
 0173                    friendIterator.Current.Value.AddListener(listener);
 174                }
 175            }
 0176        }
 0177    }
 178}