private BracketNode CreateBracketNode(BracketNode parent, Match match) { BracketNode bracketNode = new BracketNode(parent, match); AddBracketNodeToTierList(bracketNode); return(bracketNode); }
private bool SoughtBracketNodeFound(BracketNode bracketNode, Guid matchId) { if (bracketNode == null || bracketNode.Match == null) { return(false); } return(bracketNode.Match.Id == matchId); }
// Makes it easier to attain the root node from any node in the tree. public BracketNode GetFinalBracketNode() { BracketNode bracketNode = this; while (bracketNode.Parent != null) { bracketNode = bracketNode.Parent; } return(bracketNode); }
internal BracketNode(BracketNode parent, Match match) { if (match == null) { throw new ArgumentNullException(nameof(match)); } Children = new BracketNode[2]; Parent = parent; Match = match; BracketTier = (Parent == null) ? 0 : Parent.BracketTier + 1; }
private void AddBracketNodeToTierList(BracketNode bracketNode) { if (bracketNode == null) { throw new ArgumentException(nameof(bracketNode)); } while (bracketNode.BracketTier >= bracketNodesByTier.Count) { bracketNodesByTier.Add(new List <BracketNode>()); } bracketNodesByTier[bracketNode.BracketTier].Add(bracketNode); }
// Creates one bracket node for each match in given list, minus the final. Loops over list backwards so that // the first bracket node is the final match in bracket. // // Example for a bracket containing three matches in total (1 Final, 2 Semifinals): // The bracket node representing the final is created by allocating a bracket node via the new-keyword. The other // two (semifinals) is allocated with this method. // To start the algorithm the final bracket node is added to a queue and is the one to have the second and third // match added to its list of children. Every bracket node that is added to a parent bracket node is added to the // queue to be processed later. When a bracket node has reached its capacity of children, the next bracket node // is dequeued and is the one to get children added to it. // // Just before a bracket node is allocated and added to a parent bracket node a check is made to determine if // there is any more matches in match list to draw from. When it detects that the first match has been processed // the algorithm is aborted. internal void Construct(List <Match> matches) { if (matches == null) { throw new ArgumentException(nameof(matches)); } bracketNodesByTier.Clear(); FinalNode = CreateBracketNode(null, matches.Last()); Queue <BracketNode> nodeQueue = new Queue <BracketNode>(); nodeQueue.Enqueue(FinalNode); int matchIndex = matches.Count - 2; while (true) { BracketNode currentNode = nodeQueue.Dequeue(); for (int childIndex = 0; childIndex < currentNode.Children.Length; ++childIndex) { if (matchIndex >= 0) { BracketNode childNode = CreateBracketNode(currentNode, matches[matchIndex--]); currentNode.Children[childIndex] = childNode; nodeQueue.Enqueue(childNode); } else { return; } } if (nodeQueue.Count <= 0) { // LOG Error: Ran out of nodes to construct bracket system with before matches. Something went horribly wrong. return; } } }
private BracketNode GetBracketNodeByMatchIdRecursive(BracketNode bracketNode, Guid matchId) { if (SoughtBracketNodeFound(bracketNode, matchId)) { return(bracketNode); } foreach (BracketNode childNode in bracketNode.Children) { if (childNode != null) { BracketNode foundNode = GetBracketNodeByMatchIdRecursive(childNode, matchId); if (foundNode != null) { return(foundNode); } } } return(null); }
public BracketNode GetBracketNodeByMatchId(Guid matchId) { BracketNode bracketNode = GetFinalBracketNode(); return(GetBracketNodeByMatchIdRecursive(bracketNode, matchId)); }