< Summary

Class:LandHelper
Assembly:TheGraphInterfaces
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/TheGraph/Interfaces/Land/LandHelper.cs
Covered lines:7
Uncovered lines:35
Coverable lines:42
Total lines:139
Line coverage:16.6% (7 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ConvertQueryResult(...)0%56.4212032.43%
FromParcel(...)0%90900%
FromEstate(...)0%1101000%
CoordsToId(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/TheGraph/Interfaces/Land/LandHelper.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3
 4internal static class LandHelper
 5{
 6    public static List<Land> ConvertQueryResult(LandQueryResult queryResult, string lowerCaseAddress)
 7    {
 48        List<Land> lands = new List<Land>();
 9
 10        // parcels and estates that I own
 811        for (int i = 0; i < queryResult.ownerParcels.Length; i++)
 12        {
 013            lands.Add(FromParcel(queryResult.ownerParcels[i], LandRole.OWNER));
 14        }
 15
 816        for (int i = 0; i < queryResult.ownerEstates.Length; i++)
 17        {
 018            lands.Add(FromEstate(queryResult.ownerEstates[i], LandRole.OWNER));
 19        }
 20
 21        // parcels and estates that I operate
 822        for (int i = 0; i < queryResult.updateOperatorParcels.Length; i++)
 23        {
 024            lands.Add(FromParcel(queryResult.updateOperatorParcels[i], LandRole.OPERATOR));
 25        }
 26
 827        for (int i = 0; i < queryResult.updateOperatorEstates.Length; i++)
 28        {
 029            lands.Add(FromEstate(queryResult.updateOperatorEstates[i], LandRole.OPERATOR));
 30        }
 31
 32        // I'm operator of all the lands from addresses that gave me UpdateManager permission
 833        for (int i = 0; i < queryResult.operatorAuthorizations.Length; i++)
 34        {
 035            var owner = queryResult.operatorAuthorizations[i].owner;
 036            for (int j = 0; j < owner.parcels.Length; j++)
 37            {
 038                var authLand = FromParcel(owner.parcels[j], LandRole.OPERATOR);
 039                authLand.operators.Add(lowerCaseAddress);
 40
 41                // skip if already owned or operated
 042                if (!lands.Exists(l => l.id == authLand.id))
 43                {
 044                    lands.Add(authLand);
 45                }
 46            }
 47
 048            for (int j = 0; j < owner.estates.Length; j++)
 49            {
 050                if (owner.estates[j].parcels.Length == 0)
 51                    continue;
 52
 053                var authEstate = FromEstate(owner.estates[j], LandRole.OPERATOR);
 054                authEstate.operators.Add(lowerCaseAddress);
 55
 56                // skip if already owned or operated
 057                if (!lands.Exists(l => l.id == authEstate.id))
 58                {
 059                    lands.Add(authEstate);
 60                }
 61            }
 62        }
 63
 464        return lands
 065               .Where(land => land.type == LandType.PARCEL || land.parcels.Count > 0)
 66               .ToList();
 67    }
 68
 69    static Land FromParcel(ParcelFields parcel, LandRole role)
 70    {
 071        string id = CoordsToId(parcel.x, parcel.y);
 072        Land result = new Land()
 73        {
 74            id = id,
 75            name = parcel.data?.name ?? $"Parcel {id}",
 76            type = LandType.PARCEL,
 77            role = role,
 78            description = parcel.data?.description,
 79            owner = parcel.owner.address,
 80            operators = new List<string>()
 81        };
 82
 083        if (int.TryParse(parcel.x, out int x) && int.TryParse(parcel.y, out int y))
 84        {
 085            result.x = x;
 086            result.y = y;
 087            result.parcels = new List<Parcel>() { new Parcel() { id = CoordsToId(parcel.x, parcel.y), x = x, y = y } };
 88        }
 89
 090        if (!string.IsNullOrEmpty(parcel.updateOperator))
 91        {
 092            result.operators.Add(parcel.updateOperator);
 93        }
 94
 095        return result;
 96    }
 97
 98    static Land FromEstate(EstateFields estate, LandRole role)
 99    {
 0100        string id = estate.id;
 101
 0102        Land result = new Land()
 103        {
 104            id = id,
 105            name = estate.data?.name ?? $"Parcel {id}",
 106            type = LandType.ESTATE,
 107            role = role,
 108            description = estate.data?.description,
 109            size = estate.size,
 110            parcels = new List<Parcel>(),
 111            owner = estate.owner.address,
 112            operators = new List<string>()
 113        };
 114
 0115        for (int i = 0; i < estate.parcels.Length; i++)
 116        {
 0117            if (!(int.TryParse(estate.parcels[i].x, out int x) && (int.TryParse(estate.parcels[i].y, out int y))))
 118            {
 119                continue;
 120            }
 121
 0122            result.parcels.Add(new Parcel()
 123            {
 124                x = x,
 125                y = y,
 126                id = CoordsToId(estate.parcels[i].x, estate.parcels[i].y)
 127            });
 128        }
 129
 0130        if (!string.IsNullOrEmpty(estate.updateOperator))
 131        {
 0132            result.operators.Add(estate.updateOperator);
 133        }
 134
 0135        return result;
 136    }
 137
 0138    private static string CoordsToId(string x, string y) { return $"{x},{y}"; }
 139}