< 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:152
Line coverage:98.3% (58 of 59)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:7
Method coverage:100% (7 of 7)

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 MainScripts.DCL.Controllers.HotScenes;
 2using System;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using Random = UnityEngine.Random;
 6
 7namespace DCL
 8{
 9    /// <summary>
 10    /// Receive a list of hot scenes and handle what markers to show, what markers to hide and places them on the map
 11    /// </summary>
 12    internal class MarkersHandler : IDisposable
 13    {
 14        internal readonly List<UserPositionMarker> availableMarkers;
 15        internal readonly List<UserPositionMarker> usedMarkers;
 16        readonly Func<Vector2Int, Vector2> coordToMapPosition;
 17
 18        readonly ExclusionArea exclusionArea;
 19        readonly ScenesFilter scenesFilter;
 20
 21        int maxMarkers;
 22
 23        /// <summary>
 24        /// Constructor
 25        /// </summary>
 26        /// <param name="markerPrefab">prefab for markers</param>
 27        /// <param name="overlayContainer">parent for markers</param>
 28        /// <param name="maxMarkers">max amount of markers (pool)</param>
 29        /// <param name="coordToMapPosFunc">function to transform coords to map position</param>
 130        public MarkersHandler(UserMarkerObject markerPrefab, Transform overlayContainer, int maxMarkers, Func<Vector2Int
 31        {
 132            this.maxMarkers = maxMarkers;
 133            this.coordToMapPosition = coordToMapPosFunc;
 34
 135            exclusionArea = new ExclusionArea();
 136            scenesFilter = new ScenesFilter();
 37
 138            availableMarkers = new List<UserPositionMarker>(maxMarkers);
 139            usedMarkers = new List<UserPositionMarker>(maxMarkers);
 40
 1241            for (int i = 0; i < maxMarkers; i++)
 42            {
 543                var marker = new UserPositionMarker(GameObject.Instantiate(markerPrefab, overlayContainer));
 544                availableMarkers.Add(marker);
 545                marker.SetActive(false);
 46            }
 147        }
 48
 49        /// <summary>
 50        /// Set exclusion area. Markers inside this area will be hidden, to avoid overlapping with markers set with comm
 51        /// After set it will iterate through current markers to hide or show them respectively.
 52        /// </summary>
 53        /// <param name="center">center of the exclusion area</param>
 54        /// <param name="area">size of the exclusion area</param>
 55        public void SetExclusionArea(Vector2Int center, int area)
 56        {
 257            exclusionArea.position = center;
 258            exclusionArea.area = area;
 259            ApplyExclusionArea();
 260        }
 61
 62        /// <summary>
 63        /// Set scenes to set markers to. Scenes will be filtered and it coordinates will be extracted.
 64        /// Then markers will be set and will be shown or hidden according to the current exclusion area.
 65        /// </summary>
 66        /// <param name="hotScenes">list of populated scenes</param>
 67        public void SetMarkers(List<IHotScenesController.HotSceneInfo> hotScenes)
 68        {
 269            var parcelList = scenesFilter.Filter(hotScenes, maxMarkers);
 270            ResfreshMarkersPoolLists(parcelList.Count);
 1471            for (int i = 0; i < parcelList.Count && i < usedMarkers.Count; i++)
 72            {
 573                SetMarker(usedMarkers[i], parcelList[i]);
 74            }
 275        }
 76
 77        public void Dispose()
 78        {
 179            int markersCount = availableMarkers.Count;
 880            for (int i = 0; i < markersCount; i++)
 81            {
 382                availableMarkers[i].Dispose();
 83            }
 84
 685            for (int i = 0; i < usedMarkers.Count; i++)
 86            {
 287                usedMarkers[i].Dispose();
 88            }
 89
 190            availableMarkers.Clear();
 191            usedMarkers.Clear();
 192        }
 93
 94        private void SetMarker(UserPositionMarker marker, ParcelData parcelData)
 95        {
 596            marker.name = $"UsersPositionMarker({parcelData.coords.x},{parcelData.coords.y})";
 97
 598            var coords = new Vector2(parcelData.coords.x + Random.Range(-0.5f, 0.5f), parcelData.coords.y + Random.Range
 599            marker.localPosition = coordToMapPosition(Vector2Int.RoundToInt(coords));
 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        {
 2110            if (usedMarkers.Count == 0)
 111            {
 1112                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                }
 138            }
 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}