Browse Source

add roll command

Kylie Jo Swistak 6 năm trước cách đây
mục cha
commit
d7432c2529
4 tập tin đã thay đổi với 88 bổ sung1 xóa
  1. 20 0
      app/controllers/dice_controller.rb
  2. 3 0
      app/models/dice.rb
  3. 15 0
      app/responses/dice.rb
  4. 50 1
      bot.rb

+ 20 - 0
app/controllers/dice_controller.rb

@@ -0,0 +1,20 @@
+class DiceController
+  def self.roll(die)
+    case die
+    when String
+      match = /([0-9]*?)d([0-9]+)/.match(die)
+      num = match[1].to_i || 1
+      sides = match[2].to_i
+      num = 1 if num == 0
+
+      r = 0
+      num.times do
+        r += rand(1 .. sides)
+      end
+
+      r
+    when Array
+      die.sample
+    end
+  end
+end

+ 3 - 0
app/models/dice.rb

@@ -0,0 +1,3 @@
+class DieArray < ActiveRecord::Base
+  validates :sides, presence: true
+end

+ 15 - 0
app/responses/dice.rb

@@ -0,0 +1,15 @@
+def dice_embed
+  dice = DieArray.all
+  d = []
+
+  dice.each do |die|
+    d.push("**#{die.name}**: #{die.sides.join(", ")}")
+  end
+
+  desc = d.empty? ? 'No available dice' : d.join("\n")
+
+  Embed.new(
+    title: "Available Dice",
+    description: desc
+  )
+end

+ 50 - 1
bot.rb

@@ -774,6 +774,54 @@ rescue ActiveRecord::RecordNotFound => e
   error_embed(e.message)
 end
 
+desc = 'roll or create dice'
+opts = {
+  '' => 'shows die options',
+  'xdy' => 'rolls x number of y sided die',
+  'a,b,c,d' => 'rolls the between the provided options',
+  'die_name' => 'rolls the given die',
+  'die_name | a,b,c,d' => 'creates the die'
+}
+roll = Command.new(:roll, desc, opts) do |event, die, array|
+  usr = event.message.author
+  usr_name = usr.nickname || usr.name
+  color = usr.color&.combined
+
+  case die
+  when /([0-9]*?)d([0-9]+)/i
+    result = DiceController.roll(die)
+  when /,/
+    result = DiceController.roll(die.split(/\s?,\s?/))
+  when nil
+    result = dice_embed
+  else
+    unless usr.roles.map(&:name).include?('Guild Master')
+      d = DieArray.find_by(name: die)
+
+      if !d && array
+        result = DieArray.create(name: die, sides: array.split(/\s?,\s?/))
+      elsif d && array
+        d.update(sides: array.split(/\s?,\s?/))
+        d.reload
+        result = d
+      elsif d && !array
+        result = DiceController.roll(d.sides)
+      else
+        result = error_embed('Die not found!')
+      end
+    end
+  end
+
+  case result
+  when String, Integer
+    Embed.new(description: "#{usr_name} rolled #{result}!", color: color)
+  when DieArray
+    success_embed("Created Die, #{die}, with sides: #{result.sides}")
+  when Embed
+    result
+  end
+end
+
 # ---
 
 commands = [
@@ -790,7 +838,8 @@ commands = [
   afflict,
   cure,
   team,
-  stats
+  stats,
+  roll
 ]
 
 #locked_commands = [inv]