Example #1
0
        public static void BuyImprovement(AgricolaPlayer player, ImprovementActionData data, List <GameActionNotice> resultingNotices)
        {
            var improvementData = (ImprovementActionData)data;
            var card            = ((AgricolaGame)player.Game).GetCard(improvementData.Id);
            var cost            = Curator.GetCardCost(player, improvementData.Id, improvementData.PaymentOption);

            if (cost is ResourceCardCost)
            {
                var rcCost = (ResourceCardCost)cost;
                foreach (var resource in rcCost.Resources)
                {
                    if (resource.Type.IsAnimal())
                    {
                        player.RemoveAnimals((AnimalResource)resource.Type, resource.Count);
                    }
                    else
                    {
                        player.AddResource(resource.Type, -resource.Count);
                    }
                }
            }
            else if (cost is ReturnCardCardCost)
            {
                var miCost       = (ReturnCardCardCost)cost;
                var ownedCardIds = player.OwnedCardIds;

                foreach (var id in miCost.Ids)
                {
                    if (ownedCardIds.Contains(id))
                    {
                        ((AgricolaGame)player.Game).ReturnCard(player, id);
                        break;
                    }
                }
            }
            else if (!(cost is FreeCardCost))
            {
                throw new NotImplementedException("Cost type not supported.");
            }

            // Apply the card
            if (card is MajorImprovement)
            {
                ((AgricolaGame)player.Game).AssignMajorImprovement(improvementData.Id, player);
            }
            else if (card is MinorImprovement)
            {
                if (((MinorImprovement)card).PassesLeft)
                {
                    ((AgricolaGame)player.Game).PassCardLeft(player, (MinorImprovement)card);
                }
                else
                {
                    player.PlayCard(card);
                }
            }

            resultingNotices.Add(new GameActionNotice(player.Name, NoticeVerb.PurchaseImprovement.ToString(), new IdPredicate(data.Id)));
            foreach (var evnt in card.OnPlayEvents)
            {
                evnt.Execute(player, null, card, resultingNotices);
            }
        }