示例#1
0
        private BracketNode CreateBracketNode(BracketNode parent, Match match)
        {
            BracketNode bracketNode = new BracketNode(parent, match);

            AddBracketNodeToTierList(bracketNode);

            return(bracketNode);
        }
示例#2
0
        private bool SoughtBracketNodeFound(BracketNode bracketNode, Guid matchId)
        {
            if (bracketNode == null || bracketNode.Match == null)
            {
                return(false);
            }

            return(bracketNode.Match.Id == matchId);
        }
示例#3
0
        // 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);
        }
示例#4
0
        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;
        }
示例#5
0
        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);
        }
示例#6
0
        // 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;
                }
            }
        }
示例#7
0
        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);
        }
示例#8
0
        public BracketNode GetBracketNodeByMatchId(Guid matchId)
        {
            BracketNode bracketNode = GetFinalBracketNode();

            return(GetBracketNodeByMatchIdRecursive(bracketNode, matchId));
        }