How would you create such a DSL in Ruby?

I read that Ruby is great for domain languages. In the past few months, I have been creating a browser game, type rpg. At some point, I want users to be able to accept and complete quests. Quests can be anything: kill x number of mobs, kill the raid boss, possibly collect some items, etc.

The whole process sounds intriguing and error prone. I also thought it would be nice to create a DSL in this regard. A way to describe quests in simple language. But I do not have much experience with this.

Do you think this is a good idea? And if so, do you have any tips / tutorials?

+5
source share
2 answers

DSL, , , , . DSL , , . , ? , , " ".

, :

Qwest "Retrieve the Grail" do
  given_by :pope

  description "Some hethan dragon took my cup, go get it back!"

  condition "Slay a dragon" do
     dragon.is_dead?
  end

  condition "Grab the Grail" do
     player.inventory.contains :grail
  end

  reward :phat_loot
end

DSL , , , .

DSL, . , _the_lucky_stiff , (Dwemthys Array 6). , . Metaprogramming Ruby, .

+8

:

module RPG
  def quest
    puts "starting your quest"
    yield
  end

  def move direction
    puts "moving to the #{direction.to_s}"
    yield if block_given?
  end

  def door action
    puts "#{action.to_s} door"
    yield if block_given?
  end
end

:

require 'rpg'

include RPG

quest do
  move :left
  move :right
  door :open do
    move :left
  end
end

:

> ruby game.rb 
starting your quest
moving to the left
moving to the right
opening door
moving to the left
+8

All Articles