roll.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. require './app/commands/base_command.rb'
  2. class RollCommand < BaseCommand
  3. def self.opts
  4. {
  5. usage: {
  6. die: "Rolls a specified die. Can be a list of options, a name of a saved die, " +
  7. "or an indication of standard dice. Standard Die are designated as `xDy` " +
  8. "where `x` designates the number of die to roll, and `y` designates the number of sides" +
  9. " on the die. A modifier can be added with a + or - number: Added to total." +
  10. " If no die is specified, displays a list of all saved dice",
  11. combined: "Indicates you don't want to see the individual rolls, only the total." +
  12. " If left empty it will show each individual roll (unless more than 25 dice were rolled)"
  13. }
  14. }
  15. end
  16. def self.cmd
  17. desc = 'Roll a saved die, a comma separated list, or a standard die'
  18. @cmd ||= Command.new(:roll, desc, opts) do |event, die, array|
  19. # Save author
  20. author = event.author
  21. # Determine die and roll it
  22. # Display a list of saved dice
  23. if die.nil?
  24. dice_list
  25. elsif d = die.gsub(" ", "").match(/([0-9]*?)d([0-9]+)([\+|\-]?[0-9]+)?/i)
  26. # Roll the dice, and save it in the hash
  27. hash = DiceController.roll({
  28. number: d[1] == "" ? 1 : d[1].to_i ,
  29. sides: d[2].to_i,
  30. modifier: d[3]
  31. })
  32. # Determine if each result should be displayed
  33. # Format the results into an embed, and reply
  34. combine = array&.match(/combined?/i) || hash[:number] > 25 || hash[:number] == 1
  35. roll_results(author, hash, combine)
  36. # Create an array and roll it
  37. elsif die.match(/,/)
  38. hash = DiceController.roll(die.split(/\s?,\s?/))
  39. roll_results(author, hash)
  40. # Create/Update/Destroy a new dice
  41. elsif array && Util::Roles.admin?(author)
  42. # Attempt to find die, then update
  43. die_array = DieArray.find_by('name ilike ?', die)
  44. DiceController.edit_die(die_array, die, array&.split(/\s?,\s?/))
  45. # Roll a saved Die
  46. else
  47. die_array = DieArray.find_by!('name ilike ?', die)
  48. hash = DiceController.roll(die_array.sides)
  49. roll_results(author, hash)
  50. end
  51. rescue ActiveRecord::RecordNotFound => e
  52. error_embed("Record Not Found!", e.message)
  53. rescue StandardError => e
  54. error_embed(e.message)
  55. end
  56. end
  57. def self.example_command(event=nil)
  58. list = [
  59. 'Hotdog, Hamburger, Chilli Cheese Fries, Nachos, Fried Onions',
  60. 'A, B, C, D',
  61. 'Cat, Dog, Mouse, Frog, Squirrel, Turtle, Dragon',
  62. 'Eevee, Vaporeon, Flareon, Jolteon, Espeon, Umbreon, Glaceon, Leafeon, Sylveon',
  63. 'Sword, Sheild, Gun, Bomb, Tank, Fighter Jet'
  64. ].sample
  65. case ['', 'standard', 'modifier', 'list', 'named', 'combined'].sample
  66. when ''
  67. []
  68. when 'standard'
  69. [ "#{rand(1 .. 70)}d#{rand(2 .. 420)}" ]
  70. when 'modifier'
  71. [ "#{rand(1 .. 70)}d#{rand(2 .. 420)} #{['+', '-'].sample}#{rand(1 .. 10)}" ]
  72. when 'list'
  73. [ list ]
  74. when 'named'
  75. [ DiceArray.order('RANDOM()').first.name ]
  76. when 'combined'
  77. [ "#{rand(1 .. 70)}d#{rand(2 .. 420)}", 'combined' ]
  78. end
  79. end
  80. def self.admin_opts
  81. {
  82. usage: {
  83. die: "New die name",
  84. sides: "Sides of die, `delete` will delete the stored die"
  85. }
  86. }
  87. end
  88. end