public void GetTransformerScoreReturnsActionResult_WithAScore()
        {
            Models.DB.Transformer transformer = new Models.DB.Transformer()
            {
                Name             = "Autobot",
                AllegianceTypeId = 1,
                Courage          = 1,
                Endurance        = 1,
                Firepower        = 1,
                Intelligence     = 1,
                Rank             = 1,
                Skill            = 1,
                Speed            = 1,
                Strength         = 1
            };
            int score = transformer.Courage + transformer.Endurance + transformer.Firepower + transformer.Intelligence + transformer.Rank + transformer.Skill + transformer.Speed + transformer.Strength;

            _mockWarsService.Setup(warService => warService.GetScore(1)).ReturnsAsync(transformer.Score).Verifiable();
            var controller = new TransformerController(_mockWarsService.Object, _mockLogger.Object);
            var result     = controller.GetScore(1);

            var viewResult = Assert.IsType <Task <ActionResult <int> > >(result).Result;

            Assert.Equal(score, viewResult.Value);
        }
        public void GetTransformerReturns_WithATransformer()
        {
            using (var context = getDBContext())
            {
                Models.DB.Transformer autoBotTransformer = new Models.DB.Transformer()
                {
                    Name             = "Autobot",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Courage          = 1,
                    Endurance        = 1,
                    Firepower        = 1,
                    Intelligence     = 1,
                    Rank             = 1,
                    Skill            = 1,
                    Speed            = 1,
                    Strength         = 1
                };


                context.Transformers.Add(autoBotTransformer);
                context.SaveChanges();

                var warsService   = new WarService(context, _mockLogger.Object);
                var autoBotResult = warsService.GetTransformer(autoBotTransformer.TransformerId).Result;
                Assert.Equal(autoBotTransformer.Name, autoBotResult.Name);
                context.Database.EnsureDeleted();
            }
        }
 public TransformerRequest(Models.DB.Transformer transformer)
 {
     _name           = transformer.Name;
     _allegianceType = (AllegianceTypes)transformer.AllegianceTypeId;
     _rank           = transformer.Rank;
     _strength       = transformer.Strength;
     _intelligence   = transformer.Intelligence;
     _speed          = transformer.Speed;
     _endurance      = transformer.Endurance;
     _courage        = transformer.Courage;
     _firepower      = transformer.Firepower;
     _skill          = transformer.Skill;
 }
        public void GetTransformerReturnsActionResult_WithATransformer()
        {
            Models.DB.Transformer transformer = new Models.DB.Transformer()
            {
                Name = "Autobot"
            };
            _mockWarsService.Setup(warService => warService.GetTransformer(1)).ReturnsAsync(transformer).Verifiable();
            var controller = new TransformerController(_mockWarsService.Object, _mockLogger.Object);
            var result     = controller.Get(1);

            var viewResult = Assert.IsType <Task <ActionResult <Transformer> > >(result).Result;

            Assert.Equal(transformer.Name, viewResult.Value.Name);
        }
        public void RunWarsReturns_WithNoVictor_WhenNameIsOptimusVsPredaking()
        {
            using (var context = getDBContext())
            {
                Models.DB.Transformer autoBotTransformer = new Models.DB.Transformer()
                {
                    Name             = "Optimus",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Courage          = 10,
                    Endurance        = 10,
                    Firepower        = 10,
                    Intelligence     = 10,
                    Rank             = 10,
                    Skill            = 10,
                    Speed            = 10,
                    Strength         = 10
                };

                Models.DB.Transformer decepticonTransformer = new Models.DB.Transformer()
                {
                    Name             = "Predaking",
                    AllegianceTypeId = (int)AllegianceTypes.Decepticon,
                    Courage          = 1,
                    Endurance        = 1,
                    Firepower        = 1,
                    Intelligence     = 1,
                    Rank             = 1,
                    Skill            = 1,
                    Speed            = 1,
                    Strength         = 1
                };
                context.Transformers.Add(autoBotTransformer);
                context.Transformers.Add(decepticonTransformer);
                context.SaveChanges();


                var warsService = new WarService(context, _mockLogger.Object);
                var warResult   = warsService.RunWar().Result;
                Assert.Equal(0, warResult.Autobots.Victors.Count);
                Assert.Equal(0, warResult.Decepticon.Victors.Count);

                context.Database.EnsureDeleted();
            }
        }
        public void GetTransformersReturns_WithAListOfTransformers()
        {
            using (var context = getDBContext())
            {
                Models.DB.Transformer autoBotTransformer = new Models.DB.Transformer()
                {
                    Name             = "Autobot",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Courage          = 1,
                    Endurance        = 1,
                    Firepower        = 1,
                    Intelligence     = 1,
                    Rank             = 1,
                    Skill            = 1,
                    Speed            = 1,
                    Strength         = 1
                };

                Models.DB.Transformer decepticonTransformer = new Models.DB.Transformer()
                {
                    Name             = "Decepticon",
                    AllegianceTypeId = (int)AllegianceTypes.Decepticon,
                    Courage          = 1,
                    Endurance        = 1,
                    Firepower        = 1,
                    Intelligence     = 1,
                    Rank             = 1,
                    Skill            = 1,
                    Speed            = 1,
                    Strength         = 1
                };

                context.Transformers.Add(autoBotTransformer);
                context.Transformers.Add(decepticonTransformer);
                context.SaveChanges();

                var warsService   = new WarService(context, _mockLogger.Object);
                var autoBotResult = warsService.GetTransformers(Models.AllegianceTypes.Autobot).Result;
                Assert.Collection(autoBotResult, item => Assert.Contains("Autobot", item.Name));
                var decepticonResult = warsService.GetTransformers(Models.AllegianceTypes.Decepticon).Result;
                Assert.Collection(decepticonResult, item => Assert.Contains("Decepticon", item.Name));
                context.Database.EnsureDeleted();
            }
        }
        public void RunBattleReturns_WithTransformerWithHigherScore()
        {
            using (var context = getDBContext())
            {
                Models.DB.Transformer autoBotTransformer = new Models.DB.Transformer()
                {
                    Name             = "TestA",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Courage          = 2,
                    Endurance        = 2,
                    Firepower        = 2,
                    Intelligence     = 2,
                    Rank             = 2,
                    Skill            = 2,
                    Speed            = 2,
                    Strength         = 2
                };

                Models.DB.Transformer decepticonTransformer = new Models.DB.Transformer()
                {
                    Name             = "TestB",
                    AllegianceTypeId = (int)AllegianceTypes.Decepticon,
                    Courage          = 1,
                    Endurance        = 1,
                    Firepower        = 1,
                    Intelligence     = 1,
                    Rank             = 1,
                    Skill            = 1,
                    Speed            = 1,
                    Strength         = 1
                };

                var warsService  = new WarService(context, _mockLogger.Object);
                var winnerResult = warsService.GetWinner(autoBotTransformer, decepticonTransformer);
                Assert.Equal(AllegianceTypes.Autobot, winnerResult);
                context.Database.EnsureDeleted();
            }
        }
        public void RunBattleReturns_ErrorWithSameAllegiance()
        {
            using (var context = getDBContext())
            {
                Models.DB.Transformer autoBotTransformer = new Models.DB.Transformer()
                {
                    Name             = "TestA",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Courage          = 1,
                    Endurance        = 1,
                    Firepower        = 1,
                    Intelligence     = 1,
                    Rank             = 1,
                    Skill            = 1,
                    Speed            = 1,
                    Strength         = 1
                };

                Models.DB.Transformer decepticonTransformer = new Models.DB.Transformer()
                {
                    Name             = "TestB",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Courage          = 1,
                    Endurance        = 1,
                    Firepower        = 1,
                    Intelligence     = 1,
                    Rank             = 2,
                    Skill            = 1,
                    Speed            = 1,
                    Strength         = 1
                };

                var warsService = new WarService(context, _mockLogger.Object);
                Assert.Throws <Exception>(() => warsService.GetWinner(autoBotTransformer, decepticonTransformer));
                context.Database.EnsureDeleted();
            }
        }
        public AllegianceTypes GetWinner(Models.DB.Transformer autobot, Models.DB.Transformer decepticon)
        {
            _logger.LogInformation("WarService.GetWinner {autobot} vs {decepticon}", autobot.Name, decepticon.Name);

            if (autobot.Allegiance == decepticon.Allegiance)
            {
                throw new Exception("Invalid Battle");
            }

            if (autobot.Name.Equals(OPTIMUS, StringComparison.InvariantCultureIgnoreCase) || autobot.Name.Equals(PREDAKING, StringComparison.OrdinalIgnoreCase))
            {
                _logger.LogInformation("autobot won by name: " + autobot.Name);

                return(AllegianceTypes.Autobot);
            }
            if (decepticon.Name.Equals(OPTIMUS, StringComparison.OrdinalIgnoreCase) || decepticon.Name.Equals(PREDAKING, StringComparison.OrdinalIgnoreCase))
            {
                _logger.LogInformation("decepticon won by name: " + decepticon.Name);

                return(AllegianceTypes.Decepticon);
            }

            if (autobot.Strength - decepticon.Strength >= 3 && decepticon.Courage < 5)
            {
                _logger.LogInformation("autobot won, decepticon ran away");

                return(AllegianceTypes.Autobot);
            }

            if (decepticon.Strength - autobot.Strength >= 3 && autobot.Courage < 5)
            {
                _logger.LogInformation("decepticon won, autobot ran away");

                return(AllegianceTypes.Decepticon);
            }

            if (Math.Abs(autobot.Skill - decepticon.Skill) >= 5)
            {
                if (autobot.Skill > decepticon.Skill)
                {
                    _logger.LogInformation("autobot won by skill");

                    return(AllegianceTypes.Autobot);
                }
                else
                {
                    _logger.LogInformation("decepticon won by skill");
                    return(AllegianceTypes.Decepticon);
                }
            }


            if (autobot.Score == decepticon.Score)
            {
                _logger.LogInformation("Its a tie. Choose randomly");
                int n1 = random.Next(1, 10);
                int n2 = random.Next(1, 10);
                return(n1 > n2 ? AllegianceTypes.Autobot : AllegianceTypes.Decepticon);
            }

            if (autobot.Score > decepticon.Score)
            {
                _logger.LogInformation("autobot won by Score");

                return(AllegianceTypes.Autobot);
            }
            else
            {
                _logger.LogInformation("decepticon won by Score");

                return(AllegianceTypes.Decepticon);
            }
        }
        public void RunWarsReturns_WithResult()
        {
            using (var context = getDBContext())
            {
                Models.DB.Transformer transformer = new Models.DB.Transformer()
                {
                    Name             = "Optimus",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Courage          = 10,
                    Endurance        = 9,
                    Firepower        = 1,
                    Intelligence     = 3,
                    Rank             = 1,
                    Skill            = 1,
                    Speed            = 4,
                    Strength         = 2
                };
                context.Transformers.Add(transformer);

                transformer = new Models.DB.Transformer()
                {
                    Name             = "Decepticon#4",
                    AllegianceTypeId = (int)AllegianceTypes.Decepticon,
                    Rank             = 4,
                    Strength         = 10,
                    Intelligence     = 9,
                    Speed            = 1,
                    Endurance        = 2,
                    Courage          = 3,
                    Firepower        = 4,
                    Skill            = 5
                };
                context.Transformers.Add(transformer);

                transformer = new Models.DB.Transformer()
                {
                    Name             = "Autobot#10",
                    AllegianceTypeId = (int)AllegianceTypes.Autobot,
                    Rank             = 10,
                    Strength         = 10,
                    Intelligence     = 9,
                    Speed            = 1,
                    Endurance        = 2,
                    Courage          = 3,
                    Firepower        = 4,
                    Skill            = 5
                };
                context.Transformers.Add(transformer);

                context.SaveChanges();


                var warsService = new WarService(context, _mockLogger.Object);
                var warResult   = warsService.RunWar().Result;
                Assert.Equal(0, warResult.Autobots.Losers.Count);
                Assert.Equal(1, warResult.Autobots.Victors.Count);
                Assert.Equal(1, warResult.Autobots.Survivors.Count);
                Assert.Equal(1, warResult.Decepticon.Losers.Count);
                Assert.Equal(0, warResult.Decepticon.Victors.Count);
                Assert.Equal(0, warResult.Decepticon.Survivors.Count);

                context.Database.EnsureDeleted();
            }
        }