Example #1
0
		public Boolean Buy(Supply supply)
		{
			Card supplyCard = supply.TopCard;

			PlayerMode previousMode = this.PlayerMode;
			this.PlayerMode = Players.PlayerMode.Buying;
			Boolean cancelled = false;
			if (CardBuying != null)
			{
				CardBuyEventArgs cbea = new CardBuyEventArgs(_Game, supplyCard);
				CardBuying(this, cbea);
				cancelled = cbea.Cancelled;
			}
			if (!cancelled)
			{
				CurrentTurn.Bought(supplyCard);
				supplyCard.Bought(this);
				supply.Bought(this);

				if (CardBought != null)
				{
					CardBuyEventArgs cbea = null;
					List<Object> handledBy = new List<Object>();
					Boolean actionPerformed = false;
					do
					{
						actionPerformed = false;
						cbea = new CardBuyEventArgs(_Game, supplyCard);
						cbea.HandledBy.AddRange(handledBy);
						CardBought(this, cbea);
						handledBy = cbea.HandledBy;

						Boolean isAnyRequired = false;
						List<String> options = new List<String>();
						IEnumerable<Type> cardTypes = cbea.Actions.Keys;
						foreach (Type key in cardTypes)
						{
							options.Add(cbea.Actions[key].Text);
							isAnyRequired |= cbea.Actions[key].IsRequired;
						}

						if (options.Count > 0)
						{
							options.Sort();
							Choice choice = new Choice(String.Format("You bought {0}", supplyCard), null, new CardCollection() { supplyCard }, options, this, cbea, false, isAnyRequired ? 1 : 0, 1);
							ChoiceResult result = this.MakeChoice(choice);

							if (result.Options.Count > 0)
							{
								cbea.Actions.First(kvp => kvp.Value.Text == result.Options[0]).Value.Method(this, ref cbea);
								actionPerformed = true;
							}
						}

					} while (CardBought != null && actionPerformed);
				}
			}

			if (CardBuyFinished != null)
			{
				CardBuyEventArgs cbea = new CardBuyEventArgs(_Game, supplyCard);
				cbea.Cancelled = cancelled;
				CardBuyFinished(this, cbea);
			}

			if (!cancelled)
			{
				this.Gain(supply, true);

				this.SpendCurrency(new Currency(_Game.ComputeCost(supplyCard)));
				this.Buys--;
			}

			this.PlayerMode = previousMode;
			return cancelled;
		}