Skip to content

Commit

Permalink
Merge pull request #510 from dNitza/add-rhystic-study
Browse files Browse the repository at this point in the history
Rhystic Study
  • Loading branch information
radar committed Apr 26, 2024
2 parents b1cf985 + 5d9ea60 commit 50eccfb
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/magic/cards/rhystic_study.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Magic
module Cards
RhysticStudy = Enchantment("Rhystic Study") do
type "Enchantment"
cost generic: 2, blue: 1
end

class RhysticStudy < Enchantment
class Choice < Magic::Choice
attr_reader :owner, :caster

def initialize(owner:, caster:)
@owner = owner
@caster = caster
end

def costs
@costs ||= [Costs::Mana.new(generic: 1)]
end

def pay(player:, payment:)
cost = costs.first
cost.pay!(player:, payment:)
end

def resolve!
if !costs.all?(&:paid?)
owner.draw!
end
end
end

# Whenever an opponent casts a spell,
# you may draw a card unless that player pays {1}
def event_handlers
{
Events::SpellCast => ->(receiver, event) do
return if event.player == receiver.controller

game
.choices
.add(
Magic::Cards::RhysticStudy::Choice.new(owner: receiver.controller,
caster: event.player)
)
end
}
end
end
end
end
61 changes: 61 additions & 0 deletions spec/cards/rhystic_study_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "spec_helper"

RSpec.describe Magic::Cards::RhysticStudy do
include_context "two player game"

let!(:rhystic_study) { ResolvePermanent("Rhystic Study", owner: p1) }

context "when opponent casts spell" do
let!(:wood_elves_1) { Card("Wood Elves") }

it "p2 casts 1 wood elf, p2 doesn't pay the 1, p1 draws" do
expect(p1).to receive(:draw!)
p2.add_mana({green: 4})
p2.cast(card: wood_elves_1) do
_1.pay_mana(generic: {green: 2}, green: 1)
end

choice = game.choices.first
expect(choice).to be_a(Magic::Cards::RhysticStudy::Choice)
game.resolve_choice!
end

it "p1 casts 1 wood elf, Rhystic Study doesn't trigger" do
expect(p1).to_not receive(:draw!)
p1.add_mana({green: 4})
p1.cast(card: wood_elves_1) do
_1.pay_mana(generic: {green: 2}, green: 1)
end

choice = game.choices.first
expect(choice).to be_nil
end

it "p2 casts 1 wood elf, p2 pays the 1, p1 doesn't draws" do
expect(p1).to_not receive(:draw!)
p2.add_mana({green: 4})
p2.cast(card: wood_elves_1) do
_1.pay_mana(generic: {green: 2}, green: 1)
end

choice = game.choices.first
expect(choice).to be_a(Magic::Cards::RhysticStudy::Choice)
choice.pay(player: p2, payment: {generic: {green: 1}})
choice.resolve!
end

it "p2 casts 1 wood elf, p2 tries to pay 3, which is not allowed" do
expect {
expect(p1).to_not receive(:draw!)
p2.add_mana({green: 7})
p2.cast(card: wood_elves_1) do
_1.pay_mana(generic: {green: 2}, green: 1)
end

choice = game.choices.first
expect(choice).to be_a(Magic::Cards::RhysticStudy::Choice)
choice.pay(player: p2, payment: {generic: {green: 3}})
}.to raise_error(Magic::Costs::Mana::Overpayment)
end
end
end

0 comments on commit 50eccfb

Please sign in to comment.