< Summary

Class:DCL.MarkersHandler
Assembly:GlobalUsersPositionMarker
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/GlobalUsersPositionMarker/Handlers/MarkersHandler.cs
Covered lines:57
Uncovered lines:2
Coverable lines:59
Total lines:152
Line coverage:96.6% (57 of 59)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MarkersHandler(...)0%220100%
SetExclusionArea(...)0%110100%
SetMarkers(...)0%330100%
Dispose()0%330100%
SetMarker(...)0%110100%
ApplyExclusionArea()0%330100%
ResfreshMarkersPoolLists(...)0%7.057090%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/GlobalUsersPositionMarker/Handlers/MarkersHandler.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using Random = UnityEngine.Random;
 5
 6namespace DCL
 7{
 8    /// <summary>
 9    /// Receive a list of hot scenes and handle what markers to show, what markers to hide and places them on the map
 10    /// </summary>
 11    internal class MarkersHandler : IDisposable
 12    {
 13        internal readonly List<UserPositionMarker> availableMarkers;
 14        internal readonly List<UserPositionMarker> usedMarkers;
 15        readonly Func<float, float, Vector3> coordToMapPosition;
 16
 17        readonly ExclusionArea exclusionArea;
 18        readonly ScenesFilter scenesFilter;
 19
 20        int maxMarkers;
 21
 22        /// <summary>
 23        /// Constructor
 24        /// </summary>
 25        /// <param name="markerPrefab">prefab for markers</param>
 26        /// <param name="overlayContainer">parent for markers</param>
 27        /// <param name="maxMarkers">max amount of markers (pool)</param>
 28        /// <param name="coordToMapPosFunc">function to transform coords to map position</param>
 11229        public MarkersHandler(UserMarkerObject markerPrefab, Transform overlayContainer, int maxMarkers, Func<float, flo
 30        {
 11231            this.maxMarkers = maxMarkers;
 11232            this.coordToMapPosition = coordToMapPosFunc;
 33
 11234            exclusionArea = new ExclusionArea();
 11235            scenesFilter = new ScenesFilter();
 36
 11237            availableMarkers = new List<UserPositionMarker>(maxMarkers);
 11238            usedMarkers = new List<UserPositionMarker>(maxMarkers);
 39
 2243440            for (int i = 0; i < maxMarkers; i++)
 41            {
 1110542                var marker = new UserPositionMarker(GameObject.Instantiate(markerPrefab, overlayContainer));
 1110543                availableMarkers.Add(marker);
 1110544                marker.SetActive(false);
 45            }
 11246        }
 47
 48        /// <summary>
 49        /// Set exclusion area. Markers inside this area will be hidden, to avoid overlapping with markers set with comm
 50        /// After set it will iterate through current markers to hide or show them respectively.
 51        /// </summary>
 52        /// <param name="center">center of the exclusion area</param>
 53        /// <param name="area">size of the exclusion area</param>
 54        public void SetExclusionArea(Vector2Int center, int area)
 55        {
 17756            exclusionArea.position = center;
 17757            exclusionArea.area = area;
 17758            ApplyExclusionArea();
 17759        }
 60
 61        /// <summary>
 62        /// Set scenes to set markers to. Scenes will be filtered and it coordinates will be extracted.
 63        /// Then markers will be set and will be shown or hidden according to the current exclusion area.
 64        /// </summary>
 65        /// <param name="hotScenes">list of populated scenes</param>
 66        public void SetMarkers(List<HotScenesController.HotSceneInfo> hotScenes)
 67        {
 268            var parcelList = scenesFilter.Filter(hotScenes, maxMarkers);
 269            ResfreshMarkersPoolLists(parcelList.Count);
 1470            for (int i = 0; i < parcelList.Count && i < usedMarkers.Count; i++)
 71            {
 572                SetMarker(usedMarkers[i], parcelList[i]);
 73            }
 274        }
 75
 76        public void Dispose()
 77        {
 73878            int markersCount = availableMarkers.Count;
 2368279            for (int i = 0; i < markersCount; i++)
 80            {
 1110381                availableMarkers[i].Dispose();
 82            }
 83
 148084            for (int i = 0; i < usedMarkers.Count; i++)
 85            {
 286                usedMarkers[i].Dispose();
 87            }
 88
 73889            availableMarkers.Clear();
 73890            usedMarkers.Clear();
 73891        }
 92
 93        private void SetMarker(UserPositionMarker marker, ParcelData parcelData)
 94        {
 595            marker.name = $"UsersPositionMarker({parcelData.coords.x},{parcelData.coords.y})";
 96
 597            marker.localPosition = coordToMapPosition(
 98                parcelData.coords.x + Random.Range(-0.5f, 0.5f),
 99                parcelData.coords.y + Random.Range(-0.5f, 0.5f));
 100
 5101            marker.coords = parcelData.coords;
 5102            marker.realmServer = parcelData.realmServer;
 5103            marker.realmLayer = parcelData.realmLayer;
 104
 5105            marker.SetActive(!exclusionArea.Contains(parcelData.coords));
 5106        }
 107
 108        private void ApplyExclusionArea()
 109        {
 177110            if (usedMarkers.Count == 0)
 111            {
 176112                return;
 113            }
 114
 1115            using (var iterator = usedMarkers.GetEnumerator())
 116            {
 4117                while (iterator.MoveNext())
 118                {
 3119                    iterator.Current.SetActive(!exclusionArea.Contains(iterator.Current.coords));
 120                }
 1121            }
 1122        }
 123
 124        private void ResfreshMarkersPoolLists(int amountToBeUsed)
 125        {
 2126            if (amountToBeUsed == usedMarkers.Count)
 0127                return;
 128
 2129            if (amountToBeUsed > usedMarkers.Count)
 130            {
 1131                int addAmount = amountToBeUsed - usedMarkers.Count;
 8132                for (int i = 0; i < addAmount && i < availableMarkers.Count; i++)
 133                {
 3134                    var marker = availableMarkers[i];
 3135                    availableMarkers.RemoveAt(i);
 3136                    usedMarkers.Add(marker);
 137                }
 0138            }
 139            else
 140            {
 1141                int removeAmount = usedMarkers.Count - amountToBeUsed;
 4142                for (int i = 0; i < removeAmount && i < usedMarkers.Count; i++)
 143                {
 1144                    var marker = usedMarkers[i];
 1145                    usedMarkers.RemoveAt(i);
 1146                    marker.SetActive(false);
 1147                    availableMarkers.Add(marker);
 148                }
 149            }
 2150        }
 151    }
 152}