Exemple #1
0
        public Task <PlayerResources> TradeGold(string gameName, string playerName, TradeResources tradeResources)
        {
            if (String.IsNullOrEmpty(playerName) || String.IsNullOrEmpty(playerName))
            {
                throw new ArgumentException("names can't be null or empty");
            }
            string url = $"{HostName}/api/catan/resource/tradegold/{gameName}/{playerName}";

            return(Post <PlayerResources>(url, Serialize(tradeResources)));
        }
        async Task Knight()
        {
            GameInfo gameInfo = new GameInfo()
            {
                YearOfPlenty = 0,
                Knight       = 1,
                VictoryPoint = 0,
                Monopoly     = 0,
                RoadBuilding = 0
            };

            using (var helper = new TestHelper(gameInfo))
            {
                var players = await helper.CreateGame();

                var tr = new TradeResources()
                {
                    Ore   = 1,
                    Wheat = 1,
                    Sheep = 1
                };

                _ = await helper.GrantResourcesAndAssertResult(players[0], tr);

                PlayerResources resources = await helper.BuyDevCard(players[0], DevCardType.Knight);

                Assert.Equal(0, resources.Roads);
                Assert.NotNull(resources);
                Assert.True(resources.Knights > 0);



                resources = await helper.Proxy.KnightPlayed(helper.GameName, players[0], null);

                Assert.NotNull(resources);
                Assert.Contains(DevCardType.Knight, resources.PlayedDevCards);
            }
        }
Exemple #3
0
        public Task <PlayerResources> PlayYearOfPlenty(string gameName, string playerName, TradeResources tr)
        {
            if (String.IsNullOrEmpty(gameName) || String.IsNullOrEmpty(playerName))
            {
                throw new ArgumentException("names can't be null or empty");
            }
            string url = $"{HostName}/api/catan/devcard/play/yearofplenty/{gameName}/{playerName}";

            return(Post <PlayerResources>(url, Serialize(tr)));
        }
Exemple #4
0
        /// <summary>
        ///     Buy all entitlemens of a given type.
        ///     Will Grant resources as needed. Does 3 things
        ///     1. Buys all the resources
        ///     2. Get the log and then "undoes" each of the actions
        ///     3. Gets the log and "redoes" each of the actions
        ///     4. verifies that the Undo/Redo path yeilds the same results as the intial buy path.
        /// </summary>
        /// <param name="helper">The Test Helper that holds the CatanProxy and the GameName</param>
        /// <param name="entitlement">The Entitlement to buy</param>
        /// <param name="tr">The Traderesource that represents the cost of the entitlement</param>
        /// <returns></returns>
        private async Task <PlayerResources> PurchaseAll(TestHelper helper, Entitlement entitlement, TradeResources tr)
        {
            string player = (await helper.CreateGame())[0];

            Assert.NotNull(player);
            Assert.NotEmpty(player);
            PlayerResources startingResources = await helper.Proxy.GetResources(helper.GameName, player);

            List <LogHeader> logCollection = await helper.Proxy.Monitor(helper.GameName, player); // this wipes out any logs up till now -- just a player log

            PlayerResources resources;

            do
            {
                resources = await helper.GrantResourcesAndAssertResult(player, tr);

                Assert.NotNull(resources);
                resources = await helper.Proxy.BuyEntitlement(helper.GameName, player, entitlement);
            } while (resources != null);

            Assert.Equal(CatanError.LimitExceeded, helper.Proxy.LastError.Error);
            PlayerResources resourcesAfterPurchaseLoop = await helper.Proxy.GetResources(helper.GameName, player);

            Assert.NotNull(resourcesAfterPurchaseLoop);


            //
            //  get the log records -- normally this is done in a monitor
            logCollection = await helper.Proxy.Monitor(helper.GameName, player); // this are only the ones we just added

            //
            //  now undo everything
            PlayerResources resourcesAfterUndo = null;

            for (int i = 0; i < logCollection.Count; i++)
            {
                LogHeader logEntry = logCollection[^ (i + 1)] as LogHeader;
        async Task YearOfPlenty()
        {
            GameInfo gameInfo = new GameInfo()
            {
                YearOfPlenty = 1,
                Knight       = 0,
                VictoryPoint = 0,
                Monopoly     = 0,
                RoadBuilding = 0
            };

            using (var helper = new TestHelper(gameInfo))
            {
                var players = await helper.CreateGame();

                var tr = new TradeResources()
                {
                    Ore   = 1,
                    Wheat = 1,
                    Sheep = 1,
                    Brick = 1,
                    Wood  = 0
                };
                //
                //  give 1 of each resource to everybody
                foreach (string p in players)
                {
                    _ = await helper.GrantResourcesAndAssertResult(p, tr);
                }

                PlayerResources resources = await helper.BuyDevCard(players[0], DevCardType.YearOfPlenty);

                Assert.NotNull(resources);
                Assert.True(resources.YearOfPlenty > 0);

                resources = await helper.Proxy.PlayYearOfPlenty(helper.GameName, players[0], tr);

                Assert.Null(resources);
                Assert.Equal(CatanError.BadTradeResources, helper.Proxy.LastError.Error);
                Assert.NotNull(helper.Proxy.LastError.CantanRequest.Body);
                Assert.Equal(BodyType.TradeResources, helper.Proxy.LastError.CantanRequest.BodyType);
                tr = helper.Proxy.LastError.CantanRequest.Body as TradeResources;
                Assert.NotNull(tr);
                tr = new TradeResources()
                {
                    Ore   = 1,
                    Wheat = 1,
                    Sheep = 0,
                    Brick = 0,
                    Wood  = 0
                };
                resources = await helper.Proxy.PlayYearOfPlenty(helper.GameName, players[0], tr);

                Assert.NotNull(resources);
                Assert.Equal(2, resources.Ore);
                Assert.Equal(2, resources.Wheat);
                Assert.Equal(1, resources.Sheep);
                Assert.Equal(1, resources.Brick);
                Assert.Equal(0, resources.Wood);
                Assert.Equal(0, resources.GoldMine);
                //
                //  make sure we didn't impact other players
                for (int i = 1; i < players.Count; i++)
                {
                    resources = await helper.Proxy.GetResources(helper.GameName, players[i]);

                    Assert.Equal(1, resources.Ore);
                    Assert.Equal(1, resources.Wheat);
                    Assert.Equal(1, resources.Sheep);
                    Assert.Equal(1, resources.Brick);
                    Assert.Equal(0, resources.Wood);
                    Assert.Equal(0, resources.GoldMine);
                }
            }
        }
        async Task DevCardPurchase()
        {
            int      maxCards = 2;
            GameInfo gameInfo = new GameInfo()
            {
                YearOfPlenty = maxCards,
                Knight       = maxCards,
                VictoryPoint = maxCards,
                Monopoly     = maxCards,
                RoadBuilding = maxCards
            };



            using (var helper = new TestHelper(gameInfo))
            {
                string player = (await helper.CreateGame())[0]; // game is now started

                //
                //  get enough resources to buy all the cards - 5 * maxCards worth of devcarts
                var tr = new TradeResources()
                {
                    Ore   = maxCards * 5,
                    Wheat = maxCards * 5,
                    Sheep = maxCards * 5
                };

                var resources = await helper.GrantResourcesAndAssertResult(player, tr);

                Assert.Empty(resources.PlayedDevCards);
                //
                //   buy dev cards
                for (int i = 0; i < maxCards * 5; i++)
                {
                    resources = await helper.Proxy.DevCardPurchase(helper.GameName, player);

                    Assert.Equal(maxCards * 5 - i - 1, resources.Ore);
                    Assert.Equal(maxCards * 5 - i - 1, resources.Wheat);
                    Assert.Equal(maxCards * 5 - i - 1, resources.Sheep);
                    Assert.Equal(i + 1, resources.UnplayedDevCards);
                    Assert.Null(helper.Proxy.LastError);
                    Assert.Empty(helper.Proxy.LastErrorString);
                    PurchaseLog purchaseLog = await helper.MonitorGetLastRecord <PurchaseLog>(player);

                    Assert.Equal(CatanAction.Purchased, purchaseLog.Action);
                    Assert.Equal(player, purchaseLog.PlayerName);
                    Assert.Equal(Entitlement.DevCard, purchaseLog.Entitlement);
                    Assert.Null(purchaseLog.UndoRequest);
                }

                Assert.Equal(0, resources.Wood);
                Assert.Equal(0, resources.Wheat);
                Assert.Equal(0, resources.Sheep);
                Assert.Equal(0, resources.Brick);
                Assert.Equal(0, resources.Ore);
                Assert.Equal(0, resources.GoldMine);



                //
                //  these are for the "normal" game
                Assert.Equal(gameInfo.Knight, resources.Knights);
                Assert.Equal(gameInfo.VictoryPoint, resources.VictoryPoints);
                Assert.Equal(gameInfo.YearOfPlenty, resources.YearOfPlenty);
                Assert.Equal(gameInfo.Monopoly, resources.Monopoly);
                Assert.Equal(gameInfo.RoadBuilding, resources.RoadBuilding);

                //
                //  try to buy a card w/ no resources
                resources = await helper.Proxy.DevCardPurchase(helper.GameName, player);

                Assert.Null(resources);
                Assert.NotNull(helper.Proxy.LastError);
                Assert.NotEmpty(helper.Proxy.LastErrorString);
                Assert.Equal(CatanError.DevCardsSoldOut, helper.Proxy.LastError.Error);

                //
                //  grant resources for a devcard
                tr.Ore    = tr.Wheat = tr.Sheep = 1;
                resources = await helper.Proxy.GrantResources(helper.GameName, player, tr);

                Assert.Equal(1, resources.Ore);
                Assert.Equal(1, resources.Wheat);
                Assert.Equal(1, resources.Sheep);
                Assert.Null(helper.Proxy.LastError);
                Assert.Empty(helper.Proxy.LastErrorString);

                var resourceLog = await helper.MonitorGetLastRecord <ResourceLog>(player);

                //
                // try to buy when you have resources -- still get an error
                resources = await helper.Proxy.DevCardPurchase(helper.GameName, player);

                Assert.Null(resources);
                Assert.NotNull(helper.Proxy.LastError);
                Assert.NotEmpty(helper.Proxy.LastErrorString);
                Assert.Equal(CatanError.DevCardsSoldOut, helper.Proxy.LastError.Error);

                //
                //  setup to play YoP
                tr = new TradeResources()
                {
                    Wood  = 1,
                    Brick = 1
                };

                tr.Brick = 1;
                for (int i = 0; i < resources.YearOfPlenty; i++)
                {
                    resources = await helper.Proxy.PlayYearOfPlenty(helper.GameName, player, tr);

                    Assert.NotNull(resources);
                    Assert.Equal(1, resources.Ore);
                    Assert.Equal(1, resources.Wheat);
                    Assert.Equal(1, resources.Sheep);
                    Assert.Equal(i + 1, resources.Wood);
                    Assert.Equal(i + 1, resources.Brick);
                    Assert.Equal(0, resources.GoldMine);
                }

                resources = await helper.Proxy.PlayRoadBuilding(helper.GameName, player);

                Assert.NotNull(resources);
                resources = await helper.Proxy.PlayRoadBuilding(helper.GameName, player);

                Assert.NotNull(resources);
                resources = await helper.Proxy.PlayRoadBuilding(helper.GameName, player);

                Assert.Equal(CatanError.NoMoreResource, helper.Proxy.LastError.Error);
            }
        }
        async Task MonopolyTest()
        {
            GameInfo gameInfo = new GameInfo()
            {
                YearOfPlenty = 0,
                Knight       = 0,
                VictoryPoint = 0,
                Monopoly     = 1,
                RoadBuilding = 0
            };

            using (var helper = new TestHelper(gameInfo))
            {
                var players = await helper.CreateGame();

                var tr = new TradeResources()
                {
                    Ore   = 1,
                    Wheat = 1,
                    Sheep = 1,
                    Brick = 1,
                    Wood  = 1
                };
                //
                //  give 1 of each resource to everybody
                foreach (string p in players)
                {
                    _ = await helper.GrantResourcesAndAssertResult(p, tr);
                }

                // buy a dev card - gameInfo says there is only 1 of them and it is Monopoly

                var resources = await helper.Proxy.DevCardPurchase(helper.GameName, players[0]);

                Assert.NotNull(resources);
                Assert.True(resources.Monopoly > 0);
                resources = await helper.Proxy.PlayMonopoly(helper.GameName, players[0], ResourceType.Wood);

                Assert.Equal(4, resources.Wood);
                Assert.Equal(0, resources.Ore);
                Assert.Equal(0, resources.Wheat);
                Assert.Equal(0, resources.Sheep);
                Assert.Equal(1, resources.Brick);
                Assert.Equal(0, resources.GoldMine);


                for (int i = 1; i < players.Count; i++)
                {
                    resources = await helper.Proxy.GetResources(helper.GameName, players[i]);

                    Assert.Equal(1, resources.Ore);
                    Assert.Equal(1, resources.Wheat);
                    Assert.Equal(1, resources.Sheep);
                    Assert.Equal(0, resources.Wood);
                    Assert.Equal(1, resources.Brick);
                    Assert.Equal(0, resources.GoldMine);
                }
                //
                //  try to play it again
                resources = await helper.Proxy.PlayMonopoly(helper.GameName, players[0], ResourceType.Wood);

                Assert.Null(resources);
                Assert.NotEmpty(helper.Proxy.LastErrorString);
                Assert.NotNull(helper.Proxy.LastError);
                Assert.Equal(CatanError.NoResource, helper.Proxy.LastError.Error);
            }
        }
Exemple #8
0
        public Task <List <PlayerResources> > Trade(string gameName, string fromPlayer, TradeResources from, string toPlayer, TradeResources to)
        {
            if (String.IsNullOrEmpty(fromPlayer) || String.IsNullOrEmpty(toPlayer) || String.IsNullOrEmpty(gameName))
            {
                throw new Exception("names can't be null or empty");
            }
            string url  = $"{HostName}/api/catan/resource/trade/{gameName}/{fromPlayer}/{toPlayer}";
            var    body = CatanProxy.Serialize <TradeResources[]>(new TradeResources[] { from, to });

            return(Post <List <PlayerResources> >(url, body));
        }
Exemple #9
0
        public Task <PlayerResources> ReturnResource(string gameName, string playerName, TradeResources resources)
        {
            if (String.IsNullOrEmpty(gameName) || String.IsNullOrEmpty(playerName))
            {
                throw new Exception("names can't be null or empty");
            }
            string url  = $"{HostName}/api/catan/resource/return/{gameName}/{playerName}";
            var    body = CatanProxy.Serialize <TradeResources>(resources);

            return(Post <PlayerResources>(url, body));
        }
        public async Task GrantResources()
        {
            TestHelper helper = new TestHelper();

            using (helper)
            {
                var players = await helper.CreateGame();

                const int startingResourceCount = 1;
                var       startingResource      = new TradeResources()
                {
                    Wood     = startingResourceCount,
                    Wheat    = startingResourceCount,
                    Brick    = startingResourceCount,
                    Ore      = startingResourceCount,
                    GoldMine = startingResourceCount,
                    Sheep    = startingResourceCount
                };
                var startingResources = await helper.Proxy.GrantResources(helper.GameName, players[0], startingResource);

                Assert.True(startingResources.Equivalent(startingResource));
                foreach (ResourceType restype in Enum.GetValues(typeof(ResourceType)))
                {
                    //
                    // grant each resource one at a time, then undo it.
                    var loopTempResourceAsk = new TradeResources()
                    {
                    };
                    switch (restype)
                    {
                    case ResourceType.Sheep:
                        loopTempResourceAsk.Sheep++;
                        break;

                    case ResourceType.Wood:
                        loopTempResourceAsk.Wood++;
                        break;

                    case ResourceType.Ore:
                        loopTempResourceAsk.Ore++;
                        break;

                    case ResourceType.Wheat:
                        loopTempResourceAsk.Wheat++;
                        break;

                    case ResourceType.Brick:
                        loopTempResourceAsk.Brick++;
                        break;

                    case ResourceType.GoldMine:
                        loopTempResourceAsk.GoldMine++;
                        break;

                    case ResourceType.Desert:
                    case ResourceType.Back:
                    case ResourceType.None:
                    case ResourceType.Sea:
                    default:
                        continue;
                    }



                    var resourcesAfterGrant = await helper.Proxy.GrantResources(helper.GameName, players[0], loopTempResourceAsk);

                    Assert.NotNull(resourcesAfterGrant);
                    //
                    //  make sure we got what we asked for
                    Assert.True(resourcesAfterGrant.Equivalent(loopTempResourceAsk + startingResource));

                    //
                    //  get the log record - it should have resources we just granted
                    List <LogHeader> logCollection = await helper.Proxy.Monitor(helper.GameName, players[0]);

                    var resourceLog = logCollection[^ 1] as ResourceLog;