< 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:58
Uncovered lines:1
Coverable lines:59
Total lines:151
Line coverage:98.3% (58 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.017094.74%

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<Vector2Int, Vector2> 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>
 1429        public MarkersHandler(UserMarkerObject markerPrefab, Transform overlayContainer, int maxMarkers, Func<Vector2Int
 30        {
 1431            this.maxMarkers = maxMarkers;
 1432            this.coordToMapPosition = coordToMapPosFunc;
 33
 1434            exclusionArea = new ExclusionArea();
 1435            scenesFilter = new ScenesFilter();
 36
 1437            availableMarkers = new List<UserPositionMarker>(maxMarkers);
 1438            usedMarkers = new List<UserPositionMarker>(maxMarkers);
 39
 263840            for (int i = 0; i < maxMarkers; i++)
 41            {
 130542                var marker = new UserPositionMarker(GameObject.Instantiate(markerPrefab, overlayContainer));
 130543                availableMarkers.Add(marker);
 130544                marker.SetActive(false);
 45            }
 1446        }
 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        {
 1656            exclusionArea.position = center;
 1657            exclusionArea.area = area;
 1658            ApplyExclusionArea();
 1659        }
 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        {
 1878            int markersCount = availableMarkers.Count;
 264279            for (int i = 0; i < markersCount; i++)
 80            {
 130381                availableMarkers[i].Dispose();
 82            }
 83
 4084            for (int i = 0; i < usedMarkers.Count; i++)
 85            {
 286                usedMarkers[i].Dispose();
 87            }
 88
 1889            availableMarkers.Clear();
 1890            usedMarkers.Clear();
 1891        }
 92
 93        private void SetMarker(UserPositionMarker marker, ParcelData parcelData)
 94        {
 595            marker.name = $"UsersPositionMarker({parcelData.coords.x},{parcelData.coords.y})";
 96
 597            var coords = new Vector2(parcelData.coords.x + Random.Range(-0.5f, 0.5f), parcelData.coords.y + Random.Range
 598            marker.localPosition = coordToMapPosition(Vector2Int.RoundToInt(coords));
 99
 5100            marker.coords = parcelData.coords;
 5101            marker.realmServer = parcelData.realmServer;
 5102            marker.realmLayer = parcelData.realmLayer;
 103
 5104            marker.SetActive(!exclusionArea.Contains(parcelData.coords));
 5105        }
 106
 107        private void ApplyExclusionArea()
 108        {
 16109            if (usedMarkers.Count == 0)
 110            {
 15111                return;
 112            }
 113
 1114            using (var iterator = usedMarkers.GetEnumerator())
 115            {
 4116                while (iterator.MoveNext())
 117                {
 3118                    iterator.Current.SetActive(!exclusionArea.Contains(iterator.Current.coords));
 119                }
 1120            }
 1121        }
 122
 123        private void ResfreshMarkersPoolLists(int amountToBeUsed)
 124        {
 2125            if (amountToBeUsed == usedMarkers.Count)
 0126                return;
 127
 2128            if (amountToBeUsed > usedMarkers.Count)
 129            {
 1130                int addAmount = amountToBeUsed - usedMarkers.Count;
 8131                for (int i = 0; i < addAmount && i < availableMarkers.Count; i++)
 132                {
 3133                    var marker = availableMarkers[i];
 3134                    availableMarkers.RemoveAt(i);
 3135                    usedMarkers.Add(marker);
 136                }
 137            }
 138            else
 139            {
 1140                int removeAmount = usedMarkers.Count - amountToBeUsed;
 4141                for (int i = 0; i < removeAmount && i < usedMarkers.Count; i++)
 142                {
 1143                    var marker = usedMarkers[i];
 1144                    usedMarkers.RemoveAt(i);
 1145                    marker.SetActive(false);
 1146                    availableMarkers.Add(marker);
 147                }
 148            }
 2149        }
 150    }
 151}