From a6be5ad307ddaf4ca09c8db1ec2b4835bfc9723e Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sun, 22 Jan 2017 14:09:15 +0100 Subject: [PATCH] Fixed nonroot geotree, leafnode positions --- Common/Generics/Tree.cs | 11 ++++++----- Logic/Structure/Geography.cs | 2 +- Logic/Swarm/Positions.cs | 19 ++++++++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Common/Generics/Tree.cs b/Common/Generics/Tree.cs index ec51b7a6..ca64554b 100644 --- a/Common/Generics/Tree.cs +++ b/Common/Generics/Tree.cs @@ -24,10 +24,11 @@ namespace Swarmops.Common.Generics get { return this._nodeLookup[identity]; } } - public static Tree FromCollection (List collection) + public static Tree FromCollection (List collection, IHasParentIdentity rootNode = null) { Dictionary>> parentLookup = new Dictionary>>(); Tree result = new Tree(); + int rootParentNodeId = (rootNode == null ? 0 : rootNode.ParentIdentity); // To construct a tree from a random collection, start by keying all parent identities into a dictionary @@ -45,15 +46,15 @@ namespace Swarmops.Common.Generics // Then, recurse into that dictionary, starting at parentId zero which becomes the root level - result.RootNodes = ConstructRecurse (null, parentLookup); + result.RootNodes = ConstructRecurse (null, parentLookup, rootParentNodeId); return result; } - private static List> ConstructRecurse (TreeNode parent, Dictionary>> parentLookup) + private static List> ConstructRecurse (TreeNode parent, Dictionary>> parentLookup, int rootParentNodeId = 0) { List> result = new List>(); - int nodeId = parent == null ? 0 : parent.Data.Identity; + int nodeId = parent == null ? rootParentNodeId : parent.Data.Identity; if (!parentLookup.ContainsKey (nodeId)) { @@ -63,7 +64,7 @@ namespace Swarmops.Common.Generics foreach (TreeNode node in parentLookup[nodeId]) { node.Parent = parent; - node.Children = ConstructRecurse (node, parentLookup); + node.Children = ConstructRecurse (node, parentLookup, rootParentNodeId); result.Add (node); } diff --git a/Logic/Structure/Geography.cs b/Logic/Structure/Geography.cs index 2d9eb809..dc604e36 100644 --- a/Logic/Structure/Geography.cs +++ b/Logic/Structure/Geography.cs @@ -70,7 +70,7 @@ namespace Swarmops.Logic.Structure get { Geographies geographies = this.ThisAndBelow(); - return Tree.FromCollection (geographies); + return Tree.FromCollection (geographies, this); } } diff --git a/Logic/Swarm/Positions.cs b/Logic/Swarm/Positions.cs index b617eb09..5d534183 100644 --- a/Logic/Swarm/Positions.cs +++ b/Logic/Swarm/Positions.cs @@ -78,14 +78,19 @@ namespace Swarmops.Logic.Swarm Tree treeResult = result.Tree; - Tree geoTree = geography.Tree; + if (geography.ChildrenCount > 0) // Add subleaders if there are child geographies + { + Tree geoTree = geography.Tree; - Position expandingSubleaderNode = - Position.FromBasic(new BasicPosition(0, PositionLevel.GeographyDefault, organization.Identity, geography.Identity, 0, 0, 0, DateTime.Today, - "Geographic_ExpandingSubleaderNode", "UxElement", false, true, false, false, false, 0, 0, 0, 0)); // UX element, not real position - TreeNode expandingNode = treeResult.RootNodes[0].AddChild(expandingSubleaderNode); - - ForOrganizationGeographyRecurse (geoTree.RootNodes[0], expandingNode, geographyLeaderTemplate, 2); + Position expandingSubleaderNode = + Position.FromBasic(new BasicPosition(0, PositionLevel.GeographyDefault, organization.Identity, + geography.Identity, 0, 0, 0, DateTime.Today, + "Geographic_ExpandingSubleaderNode", "UxElement", false, true, false, false, false, 0, 0, 0, 0)); + // UX element, not real position + TreeNode expandingNode = treeResult.RootNodes[0].AddChild(expandingSubleaderNode); + + ForOrganizationGeographyRecurse(geoTree.RootNodes[0], expandingNode, geographyLeaderTemplate, 2); + } return treeResult; }