< Summary

Class:MinimapMetadata
Assembly:MapRenderer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MinimapMetadata.cs
Covered lines:24
Uncovered lines:10
Coverable lines:34
Total lines:132
Line coverage:70.5% (24 of 34)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:10
Method coverage:80% (8 of 10)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MinimapMetadata()0%110100%
GetSceneInfo(...)0%220100%
AddSceneInfo(...)0%5.125083.33%
Clear()0%110100%
GetMetadata()0%220100%
Equals(...)0%72800%
Equals(...)0%20400%
GetHashCode()0%220100%
GetParcelsHashCode()0%4.034087.5%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MinimapMetadata.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5
 6[CreateAssetMenu(fileName = "MinimapMetadata", menuName = "MinimapMetadata")]
 7public class MinimapMetadata : ScriptableObject
 8{
 9    public enum TileType
 10    {
 11        MyParcel = 0,
 12        MyParcelsOnSale = 1,
 13        MyEstates = 2,
 14        MyEstatesOnSale = 3,
 15        WithAccess = 4,
 16        District = 5,
 17        Contribution = 6,
 18        Roads = 7,
 19        Plaza = 8,
 20        Taken = 9,
 21        OnSale = 10,
 22        Unowned = 11,
 23        Background = 12,
 24        Loading = 13,
 25    }
 26
 27    private static MinimapMetadata minimapMetadata;
 28
 129    private readonly HashSet<MinimapSceneInfo> scenesInfo = new ();
 30
 131    private readonly Dictionary<Vector2Int, MinimapSceneInfo> sceneInfoMap = new ();
 32
 33    public event Action<MinimapSceneInfo> OnSceneInfoUpdated;
 34
 9635    public IReadOnlyCollection<MinimapSceneInfo> SceneInfos => scenesInfo;
 36
 37    public MinimapSceneInfo GetSceneInfo(int x, int y) =>
 4838        sceneInfoMap.TryGetValue(new Vector2Int(x, y), out MinimapSceneInfo result)
 39            ? result
 40            : null;
 41
 42    public void AddSceneInfo(MinimapSceneInfo sceneInfo)
 43    {
 144        if (scenesInfo.Contains(sceneInfo))
 045            return;
 46
 147        int parcelsCount = sceneInfo.parcels.Count;
 48
 849        for (var i = 0; i < parcelsCount; i++)
 50        {
 351            if (sceneInfoMap.ContainsKey(sceneInfo.parcels[i]))
 52
 53                // NOTE: This removes outdated information for a particular parcel. Subsequent calls to update the
 54                // information for a parcel must override previously submitted information.
 055                sceneInfoMap.Remove(sceneInfo.parcels[i]);
 56
 357            sceneInfoMap.Add(sceneInfo.parcels[i], sceneInfo);
 58        }
 59
 160        scenesInfo.Add(sceneInfo);
 61
 62        // it's not clear why we invoke the callback if `scenesInfo` already contains the scene
 163        OnSceneInfoUpdated?.Invoke(sceneInfo);
 164    }
 65
 66    public void Clear()
 67    {
 168        scenesInfo.Clear();
 169        sceneInfoMap.Clear();
 170    }
 71
 72    public static MinimapMetadata GetMetadata()
 73    {
 16874        if (minimapMetadata == null)
 175            minimapMetadata = Resources.Load<MinimapMetadata>("ScriptableObjects/MinimapMetadata");
 76
 16877        return minimapMetadata;
 78    }
 79
 80    [Serializable]
 81    public class MinimapSceneInfo : IEquatable<MinimapSceneInfo>
 82    {
 83        public string name;
 84        public TileType type;
 85        public List<Vector2Int> parcels;
 86
 87        public bool isPOI;
 88        public string owner;
 89        public string description;
 90        public string previewImageUrl;
 91
 92        [NonSerialized] private int? cachedHash;
 93
 94        public bool Equals(MinimapSceneInfo other)
 95        {
 096            if (ReferenceEquals(null, other)) return false;
 097            if (ReferenceEquals(this, other)) return true;
 98
 99            // skip `previewImageUrl` on purpose
 0100            return name == other.name
 101                   && type == other.type
 102                   && parcels.SequenceEqual(other.parcels)
 103                   && isPOI == other.isPOI
 104                   && owner == other.owner
 105                   && description == other.description;
 106        }
 107
 108        public override bool Equals(object obj)
 109        {
 0110            if (ReferenceEquals(null, obj)) return false;
 0111            if (ReferenceEquals(this, obj)) return true;
 0112            if (obj.GetType() != this.GetType()) return false;
 0113            return Equals((MinimapSceneInfo)obj);
 114        }
 115
 116        public override int GetHashCode() =>
 1117            cachedHash ??= HashCode.Combine(name, (int)type, GetParcelsHashCode(), isPOI, owner, description);
 118
 119        private int GetParcelsHashCode()
 120        {
 1121            if (parcels == null || parcels.Count == 0)
 0122                return 0;
 123
 1124            var hashCode = parcels[0].GetHashCode();
 125
 6126            for (var i = 1; i < parcels.Count; i++)
 2127                hashCode = HashCode.Combine(hashCode, parcels[i].GetHashCode());
 128
 1129            return hashCode;
 130        }
 131    }
 132}