Ejemplo n.º 1
0
        public void CanGetDiscountForAdvancement(int[] grantors, int expectedDiscount)
        {
            int         advancementId   = 1;
            Advancement testAdvancement = new Advancement {
                Id = advancementId
            };

            Collection <OwnedAdvancement> owned = new Collection <OwnedAdvancement>();

            foreach (int grantor in grantors)
            {
                owned.Add(
                    new OwnedAdvancement {
                    Advancement = new Advancement {
                        Id = grantor,
                        CreditAdvancementId    = advancementId,
                        CreditAdvancementValue = 10
                    }
                }
                    );
            }

            ActiveCiv testCiv = new ActiveCiv {
                OwnedAdvancements = owned
            };

            int actualDiscount = testCiv.GetDiscountForAdvancement(testAdvancement);

            Assert.Equal(expectedDiscount, actualDiscount);
        }
Ejemplo n.º 2
0
        /*
         * Tests the GetTotal<Category>Credit method. Called by specific test cases above
         */
        private void CanGetTotalCredit(Advancement.Category category, int[] credits, int expectedTotal)
        {
            ActiveCiv testCiv = new ActiveCiv
            {
                //Convert the array of credits into a collection of mocked advancements
                OwnedAdvancements = PrepareMockOwnedAdvancements(credits, category)
            };

            int actualTotal = -1;

            switch (category)
            {
            case Advancement.Category.Art:
                actualTotal = testCiv.GetTotalArtCredit();
                break;

            case Advancement.Category.Civic:
                actualTotal = testCiv.GetTotalCivicCredit();
                break;

            case Advancement.Category.Craft:
                actualTotal = testCiv.GetTotalCraftCredit();
                break;

            case Advancement.Category.Religion:
                actualTotal = testCiv.GetTotalReligionCredit();
                break;

            case Advancement.Category.Science:
                actualTotal = testCiv.GetTotalScienceCredit();
                break;
            }

            Assert.Equal(expectedTotal, actualTotal);
        }
Ejemplo n.º 3
0
        public void CanGetScore(int astPosition, int cities, int[] advancementValues, int expectedScore)
        {
            ICollection <OwnedAdvancement> ownedAdvancements = new Collection <OwnedAdvancement>();

            foreach (int pointValue in advancementValues)
            {
                //TODO: swap this to a mocked version
                ownedAdvancements.Add(
                    new OwnedAdvancement
                {
                    Advancement = new Advancement
                    {
                        Points = pointValue
                    }
                }
                    );
            }

            ActiveCiv testCiv = new ActiveCiv {
                AstPosition       = astPosition,
                Cities            = cities,
                OwnedAdvancements = ownedAdvancements
            };

            int actualScore = testCiv.GetScore();

            Assert.Equal(expectedScore, actualScore);
        }
        public async Task CreateGoesToGameLoopForActiveCiv()
        {
            //Get a test controller
            GameContext          gameContext    = GetTestContext("activeciv_create_successful");
            ActiveCivsController testController = new ActiveCivsController(gameContext);

            //Build a new ActiveCiv
            ActiveCiv activeCiv = new ActiveCiv {
                Id = 10, CivId = 1, GameName = "Create Game Test"
            };

            //Run the controller's Post: Create()
            IActionResult result = await testController.Create(activeCiv);

            //Assert that...
            //...we got a redirect back
            RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

            //...the controller is "GameLoop"
            Assert.Equal("GameLoop", redirect.ControllerName);
            //...the action is the index
            Assert.Equal(nameof(GameLoopController.Index), redirect.ActionName);
            //...the route data is the ID of the new active civ
            Assert.Equal(activeCiv.Id, redirect.RouteValues["id"]);
        }
Ejemplo n.º 5
0
        public void CanResolveMoveAstForward(string dbString, int astPosition, int cities, string expectedMessage)
        {
            //Get a test controller
            GameContext        context        = GetTestContext(dbString);
            GameLoopController testController = new GameLoopController(context);

            //Configure the active civ's status
            ActiveCiv activeCiv = context.ActiveCivs.Find(1);

            activeCiv.AstPosition  = astPosition;
            activeCiv.Cities       = cities;
            activeCiv.CurrentPhase = (int)ActiveCiv.Phases.MoveAST;
            context.SaveChanges();

            //Run the resolution (via the index, since MoveAST is private
            IActionResult result = testController.Index(1);

            //Assert that...
            //...we got a view back
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            //...the view model is an ActiveCiv
            Assert.IsAssignableFrom <ActiveCiv>(viewResult.ViewData.Model);
            //...the view bag has the message we expect
            Assert.Equal(expectedMessage, viewResult.ViewData["MovementMessage"]);
        }
Ejemplo n.º 6
0
        public void CanResolveMoveAstEnd()
        {
            //Get a test controller
            GameContext        context        = GetTestContext("gameloop_end");
            GameLoopController testController = new GameLoopController(context);

            //Configure the active civ's status
            ActiveCiv activeCiv = context.ActiveCivs.Find(1);

            activeCiv.AstPosition  = 16;
            activeCiv.Cities       = 5;
            activeCiv.CurrentPhase = (int)ActiveCiv.Phases.MoveAST;
            context.SaveChanges();

            //Run the controller's Details() without an Id
            IActionResult result = testController.Index(1);

            //Assert that...
            //...we got a redirect back
            RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

            //...the controller is "ActiveCivController"
            Assert.Equal("ActiveCivs", redirect.ControllerName);
            //...the action is the index
            Assert.Equal(nameof(ActiveCivsController.Details), redirect.ActionName);
            //...the route data is the ID of the new active civ
            Assert.Equal(activeCiv.Id, redirect.RouteValues["id"]);
        }
Ejemplo n.º 7
0
        public void CanResolveCalculateSpendLimit(int treasury, Commodity[] commodities, int expectedSpendLimit)
        {
            int expectedPhase = (int)ActiveCiv.Phases.BuyAdvances;

            //Prep the context
            DbContextOptions <GameContext> options = InMemoryContextFactory.GetContextOptions("calculate_spend_limit");
            GameContext context = new GameContext(options);

            //Create an active civ
            ActiveCiv activeCiv = context.ActiveCivs
                                  .Add(new ActiveCiv {
                OwnedAdvancements = new Collection <OwnedAdvancement> {
                }
            })
                                  .Entity;

            context.SaveChanges();

            //Make the game loop
            GameLoop testLoop = new GameLoop(context, activeCiv.Id);

            //Act
            testLoop.ResolveCalculateSpendLimit(treasury, commodities);

            //Assert
            Assert.Equal(expectedSpendLimit, testLoop.ActiveCiv.SpendLimit);
            Assert.Equal(expectedPhase, testLoop.ActiveCiv.CurrentPhase);
        }
Ejemplo n.º 8
0
        //TODO: build action for POST: CalculateSpendLimit
        //TODO: build action for POST: BuyAdvances

        //TODO: Consider moving this to the GameLoop service class
        //Resolves the Move AST phase. Called from Index()
        private IActionResult MoveAST(ActiveCiv activeCiv)
        {
            //Move the AST
            GameLoop loop      = new GameLoop(_context, activeCiv.Id);
            int      direction = loop.ResolveMoveAst();

            //If the game ended, go to the detailed active civilization view
            if (loop.CheckForGameEnd())
            {
                return(RedirectToAction(
                           nameof(ActiveCivsController.Details),
                           "ActiveCivs",
                           new { id = activeCiv.Id }
                           ));
            }

            //If the game didn't end, present a message to the player about what happened to the AST
            switch (direction)
            {
            case -1:
                ViewBag.MovementMessage = ASTRegresses;
                break;

            case 1:
                ViewBag.MovementMessage = ASTAdvances;
                break;

            case 0:
            default:
                ViewBag.MovementMessage = ASTStuck;
                break;
            }

            return(View("MoveAST", activeCiv));
        }
Ejemplo n.º 9
0
        public void CanCheckForGameEnd(int astPosition, bool expectedResult, int expectedPhase)
        {
            //Prep the context
            DbContextOptions <GameContext> options = InMemoryContextFactory.GetContextOptions("game_end");
            GameContext context = new GameContext(options);

            //Create an active civ
            ActiveCiv activeCiv = context.ActiveCivs
                                  .Add(new ActiveCiv
            {
                CurrentPhase = (int)ActiveCiv.Phases.MoveAST,
                Civ          = new Civ {
                    AstLateIron = 16
                },
                AstPosition = astPosition,
            })
                                  .Entity;

            context.SaveChanges();

            //Make the game loop
            GameLoop testLoop = new GameLoop(context, activeCiv.Id);

            //Act
            bool actualResult = testLoop.CheckForGameEnd();

            //Assert
            Assert.Equal(expectedResult, actualResult);
            Assert.Equal(expectedPhase, testLoop.ActiveCiv.CurrentPhase);
        }
Ejemplo n.º 10
0
        public void CanResolveMoveAst(int astPosition, int cities, int expectedResult)
        {
            //Prep the context
            DbContextOptions <GameContext> options = InMemoryContextFactory.GetContextOptions("move_ast");
            GameContext context = new GameContext(options);

            //Create an active civ
            ActiveCiv activeCiv = context.ActiveCivs
                                  .Add(new ActiveCiv {
                Civ = new Civ {
                    AstStone = 5, AstEarlyBronze = 8, AstLateBronze = 11, AstEarlyIron = 14, AstLateIron = 16
                },
                Cities            = cities,
                AstPosition       = astPosition,
                OwnedAdvancements = new Collection <OwnedAdvancement> {
                }
            })
                                  .Entity;

            context.SaveChanges();

            //Make the game loop
            GameLoop testLoop = new GameLoop(context, activeCiv.Id);

            //Act
            int actualResult = testLoop.ResolveMoveAst();

            //Assert
            Assert.Equal(expectedResult, actualResult);
        }
Ejemplo n.º 11
0
        public void CanCheckPlayerCanAffordAdvancements(string dbString, int spendLimit, int[] advancements, bool expectedResult)
        {
            //Prep the context
            DbContextOptions <GameContext> options = InMemoryContextFactory.GetContextOptions(dbString);
            GameContext context = new GameContext(options);

            //Add advancements
            foreach (int advancementId in advancements)
            {
                context.Advancements.Add(new Advancement {
                    Id = advancementId, BaseCost = 50
                });
            }

            //Create an active civ
            ActiveCiv activeCiv = context.ActiveCivs
                                  .Add(new ActiveCiv {
                SpendLimit = spendLimit, OwnedAdvancements = new Collection <OwnedAdvancement> {
                }
            })
                                  .Entity;

            context.SaveChanges();

            //Make the game loop
            GameLoop testLoop = new GameLoop(context, activeCiv.Id);

            //Act
            bool actualResult = testLoop.CheckPlayerCanAffordAdvancements(advancements);

            //Assert
            Assert.Equal(expectedResult, actualResult);
        }
Ejemplo n.º 12
0
        public void CanResolveTaxToSupport()
        {
            int expectedCities = 4;
            int expectedPhase  = (int)ActiveCiv.Phases.CalculateSpendLimit;

            //Prep the context
            DbContextOptions <GameContext> options = InMemoryContextFactory.GetContextOptions("Tax_to_support");
            GameContext context = new GameContext(options);

            //Create an active civ
            ActiveCiv activeCiv = context.ActiveCivs
                                  .Add(new ActiveCiv {
                OwnedAdvancements = new Collection <OwnedAdvancement> {
                }
            })
                                  .Entity;

            context.SaveChanges();

            //Make the game loop
            GameLoop testLoop = new GameLoop(context, activeCiv.Id);

            //Act
            testLoop.ResolveTaxToSupport(expectedCities);

            //Assert
            Assert.Equal(expectedCities, testLoop.ActiveCiv.Cities);
            Assert.Equal(expectedPhase, testLoop.ActiveCiv.CurrentPhase);
        }
        public async Task <IActionResult> Create([Bind("Id,CivId,GameName")] ActiveCiv activeCiv)
        {
            activeCiv.OwnedAdvancements = new Collection <OwnedAdvancement> {
            };
            if (ModelState.IsValid)
            {
                ActiveCiv newCiv = _context.Add(activeCiv).Entity;
                await _context.SaveChangesAsync();

                //Go to the game loop
                return(RedirectToAction(nameof(GameLoopController.Index), "GameLoop", new { id = newCiv.Id }));
            }
            ViewData["CivId"] = new SelectList(_context.Civs, "Id", "Id", activeCiv.CivId);
            return(View(activeCiv));
        }
        public async Task CreateReturnsToFormOnInvalidModel()
        {
            //Get a test controller
            GameContext          gameContext    = GetTestContext("activeciv_create_invalid");
            ActiveCivsController testController = new ActiveCivsController(gameContext);

            //Add an error to force the Post: Create() method to return to the form
            testController.ModelState.AddModelError("test error", "automated unit test error");

            //Build a new ActiveCiv
            ActiveCiv activeCiv = new ActiveCiv {
                Id = 10, CivId = 1, GameName = "Create Game Test"
            };

            //Run the controller's Post: Create()
            IActionResult result = await testController.Create(activeCiv);

            //Assert that...
            //...we got a view back
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            //...the view data has civ ids
            Assert.NotNull(viewResult.ViewData["CivId"]);
        }
Ejemplo n.º 15
0
 public GameLoop(GameContext gameContext, int activeCivId)
 {
     GameContext = gameContext;
     ActiveCiv   = GameContext.ActiveCivs.Find(activeCivId);
 }
Ejemplo n.º 16
0
        public void CanResolveBuyAdvances(string dbString, int[] newAdvances, int[] existingAdvances, bool expectedAffordability)
        {
            int expectedPhase = (int)ActiveCiv.Phases.MoveAST;

            //Build the list of expected advancements after purchase
            List <int> expectedAdvancements = new List <int>(existingAdvances);

            if (expectedAffordability)
            {
                foreach (int newAdvance in newAdvances)
                {
                    expectedAdvancements.Add(newAdvance);
                }
            }

            //Prep the context
            DbContextOptions <GameContext> options = InMemoryContextFactory.GetContextOptions(dbString);
            GameContext context = new GameContext(options);

            //Add advancements
            foreach (int advancementId in newAdvances)
            {
                context.Advancements.Add(new Advancement {
                    Id = advancementId, BaseCost = 50
                });
            }
            foreach (int advancementId in existingAdvances)
            {
                context.Advancements.Add(new Advancement {
                    Id = advancementId, BaseCost = 50
                });
            }

            //Create an active civ
            ActiveCiv activeCiv = context.ActiveCivs
                                  .Add(new ActiveCiv {
                SpendLimit = 100, OwnedAdvancements = new Collection <OwnedAdvancement> {
                }
            })
                                  .Entity;

            //Add the existing advancements to the active civ and build the list of expected advancements after buying the new ones
            foreach (int existingAdvance in existingAdvances)
            {
                activeCiv.OwnedAdvancements.Add(new OwnedAdvancement {
                    AdvancementId = existingAdvance
                });
            }
            context.SaveChanges();

            //Make the game loop
            GameLoop testLoop = new GameLoop(context, activeCiv.Id);

            //Act
            bool result = testLoop.ResolveBuyAdvances(newAdvances);

            //Assert

            //Assert that the advances were bought (or not) as expected
            Assert.Equal(expectedAffordability, result);

            //Assert correct phase
            Assert.Equal(expectedPhase, testLoop.ActiveCiv.CurrentPhase);
            //Assert expected number of owned advancements
            Assert.Equal(expectedAdvancements.Count, testLoop.ActiveCiv.OwnedAdvancements.Count);

            /* Assert that all of the expected owned advancements are present
             * (and with the previous assertion, only those advancements)
             */
            foreach (int expectedAdvancementId in expectedAdvancements)
            {
                Assert.Equal(
                    expectedAdvancementId,
                    testLoop.ActiveCiv.OwnedAdvancements.Single(a => a.AdvancementId == expectedAdvancementId).AdvancementId
                    );
            }
        }