private void ResetTree(FightNode node)
        {
            node.IsFilled            = false;
            node.Fight.RedAthleteId  = null;
            node.Fight.BlueAthleteId = null;

            foreach (var child in node.Children)
            {
                ResetTree(child);
            }
        }
        private void NodeToList(FightNode node, List <Fight> destinationList)
        {
            var fight = node.Fight;

            fight.NextFight = node.Parent?.Fight;
            destinationList.Add(fight);

            foreach (var child in node.Children)
            {
                NodeToList(child, destinationList);
            }
        }
        private bool TossupFighterToTree(ApplicationUser fighter, FightNode node, Func <ApplicationUser, ApplicationUser, bool> compatibilityPredicate)
        {
            if (node.IsFilled)
            {
                return(false);
            }

            if (node.Children.Count == 0)
            {
                if (string.IsNullOrEmpty(node.Fight.RedAthleteId))
                {
                    node.Fight.RedAthleteId = fighter.Id;
                    node.Fight.RedAthlete   = fighter;
                    return(true);
                }

                if (string.IsNullOrEmpty(node.Fight.BlueAthleteId) && compatibilityPredicate(node.Fight.RedAthlete, fighter))
                {
                    node.Fight.BlueAthleteId = fighter.Id;
                    node.Fight.BlueAthlete   = fighter;
                    node.IsFilled            = true;
                    return(true);
                }

                return(false);
            }

            if (node.Children.Count == 1 && node.Children[0].IsFilled)
            {
                if (string.IsNullOrEmpty(node.Fight.BlueAthleteId) && compatibilityPredicate(node.Fight.RedAthlete, fighter))
                {
                    node.Fight.BlueAthleteId = fighter.Id;
                    node.Fight.BlueAthlete   = fighter;
                    node.IsFilled            = true;
                    return(true);
                }
                return(false);
            }

            foreach (var child in node.Children)
            {
                if (TossupFighterToTree(fighter, child, compatibilityPredicate))
                {
                    return(true);
                }
            }

            node.IsFilled = true;
            return(false);
        }
        public FightsTree(int contestId, int contestCategoryId, int fightStructureId, int fighterCount)
        {
            if (fighterCount < 1)
            {
                throw new ArgumentException("Cannot be less than 1", nameof(fighterCount));
            }

            _fightersCount     = fighterCount;
            _contestId         = contestId;
            _fightStructureId  = fightStructureId;
            _contestCategoryId = contestCategoryId;

            _root = new FightNode();
            BuildTree(_root, _fightersCount);
        }
        private void NodeToIndex(FightNode node, List <IndexedFight> index, int deepLevel)
        {
            var fight = node.Fight;

            fight.NextFight = node.Parent?.Fight;
            var indexedFight = new IndexedFight(fight)
            {
                DrawDeepLevel = deepLevel
            };

            index.Add(indexedFight);

            deepLevel++;
            foreach (var child in node.Children)
            {
                NodeToIndex(child, index, deepLevel);
            }
        }
        private void BuildTree(FightNode root, int fightersCount)
        {
            if (fightersCount == 1)
            {
                return;
            }

            root.Fight = new Fight
            {
                ContestId         = _contestId,
                StructureId       = _fightStructureId,
                ContestCategoryId = _contestCategoryId
            };


            int power        = (int)(Math.Log(fightersCount, 2));
            int twoPower     = (int)Math.Pow(2, power);
            int twoPowerPrev = (int)Math.Pow(2, power - 1);

            int leftFightersCount  = twoPower;
            int rightFightersCount = fightersCount - twoPower;

            if (rightFightersCount < twoPowerPrev)
            {
                rightFightersCount = twoPowerPrev;
                leftFightersCount  = fightersCount - twoPowerPrev;
            }

            if (leftFightersCount > 1)
            {
                FightNode leftNode = new FightNode(root);
                root.Children.Add(leftNode);
                BuildTree(leftNode, leftFightersCount);
            }

            if (rightFightersCount > 1)
            {
                FightNode rightNode = new FightNode(root);
                root.Children.Add(rightNode);
                BuildTree(rightNode, rightFightersCount);
            }
        }
        private void BuildTree(FightNode node, List <Fight> fights, FightNode parent)
        {
            if (fights.Count == 0)
            {
                return;
            }

            var nodeFight = fights.FirstOrDefault(fight => fight.NextFightId == parent?.Fight.Id);

            fights.Remove(nodeFight);
            node.Fight = nodeFight;

            var childrenFights = fights.Where(fight => fight.NextFightId == nodeFight.Id).ToList();

            foreach (var fight in childrenFights)
            {
                FightNode childNode = new FightNode(fight, node);
                node.Children.Add(childNode);
                BuildTree(node: childNode, fights: fights, parent: node);
            }
        }
        private void PrintTree(FightNode root, int level = 0)
        {
            string offset = string.Empty;

            for (int i = 0; i <= level; i++)
            {
                offset += "         ";
            }

            Console.WriteLine(offset + "Fight: ");
            if (root.Children.Count == 1)
            {
                Console.WriteLine(offset + "       " + root.Children[0].Fight.BlueAthleteId);
            }
            if (root.Children.Count == 0)
            {
                Console.WriteLine(offset + "       " + root.Fight.RedAthleteId);
                Console.WriteLine(offset + "       " + root.Fight.BlueAthleteId);
            }
            foreach (var node in root.Children)
            {
                PrintTree(node, level + 1);
            }
        }
 public FightsTree(List <Fight> fights)
 {
     _root = new FightNode();
     BuildTree(_root, fights, parent: null);
 }
示例#10
0
 public FightNode(Fight fight, FightNode parent) : this(parent)
 {
     Fight = fight;
 }
示例#11
0
 public FightNode(FightNode parentFight) : this()
 {
     Parent = parentFight;
 }