Skip to content

Commit

Permalink
Implement Cast#auto_pay_mana
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Feb 18, 2024
1 parent 87b92fd commit 6c4407e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/magic/actions/cast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ def pay_mana(payment)
self
end

def auto_pay_mana
mana_cost.auto_pay(player: player)
self
end

def pay_kicker(payment)
kicker_cost.pay(player:, payment:)
end
Expand Down
13 changes: 11 additions & 2 deletions lib/magic/costs/mana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def pay(player:, payment:)
pay_colors(payment.slice(*Magic::Mana::COLORS))
end

def auto_pay(player)
def auto_pay(player:)
raise CannotPay unless can_pay?(player)

pay_colors(color_costs)
pay_generic(generic: cost[:generic]) if cost[:generic]
auto_pay_generic_costs(player) if cost[:generic]
end

def finalize!(player)
Expand Down Expand Up @@ -132,6 +132,15 @@ def pay_generic(payment)
@payments[:generic].merge!(payment) { |key, old_value, new_value| old_value + new_value }
end


def auto_pay_generic_costs(player)
available_mana = player.mana_pool.flat_map do |color, amount|
[color] * amount
end

pay_generic(available_mana.take(cost[:generic]).tally)
end

def pay_colors(color_payments)
color_payments.each_with_object(balance) do |(color, amount), remaining_balance|
remaining_balance[color] -= amount
Expand Down
2 changes: 1 addition & 1 deletion spec/cards/titanic_growth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
it "gets +4/+4 until end of turn" do
p1.add_mana(green: 2)
p1.cast(card: Card("Titanic Growth")) do
_1.pay_mana(green: 1, generic: { green: 1 })
_1.auto_pay_mana
_1.targeting(wood_elves)
end

Expand Down

0 comments on commit 6c4407e

Please sign in to comment.