2021-02-07 03:24:07 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Chronicle
|
2021-02-15 02:06:46 +00:00
|
|
|
module Addon
|
2021-02-07 03:24:07 +00:00
|
|
|
# Roll dice and get the results
|
2021-02-15 02:06:46 +00:00
|
|
|
class Roller
|
|
|
|
def self.register(bot)
|
|
|
|
addon_instance = new(bot)
|
|
|
|
addon_command = ['roll']
|
2021-02-07 03:24:07 +00:00
|
|
|
|
2021-02-15 02:06:46 +00:00
|
|
|
[addon_instance, addon_command]
|
|
|
|
end
|
2021-02-07 03:24:07 +00:00
|
|
|
|
2021-02-15 02:06:46 +00:00
|
|
|
def initialize(bot)
|
|
|
|
@bot = bot
|
|
|
|
end
|
2021-02-07 03:24:07 +00:00
|
|
|
|
2021-02-15 03:37:56 +00:00
|
|
|
# Provide help information for this command
|
|
|
|
#
|
2021-02-17 04:13:46 +00:00
|
|
|
# @param message [Message object] The relevant message object (Unused)
|
|
|
|
def help_command(_)
|
|
|
|
'Rolls dice, and provides a result' \
|
|
|
|
"\nUsage: !roll DICE\nExample: !roll 3d6"
|
2021-02-15 03:37:56 +00:00
|
|
|
end
|
|
|
|
|
2021-02-15 02:06:46 +00:00
|
|
|
# Handle a command from the Matrix protocol
|
|
|
|
#
|
|
|
|
# @param message [hash] The message data from Matrix
|
|
|
|
def matrix_command(message)
|
|
|
|
msgstr = message.content[:body]
|
|
|
|
.gsub(/!roll\s*/, '')
|
|
|
|
.strip
|
2021-02-07 03:24:07 +00:00
|
|
|
|
2021-02-15 02:06:46 +00:00
|
|
|
room = @bot.client.ensure_room(message.room_id)
|
|
|
|
|
|
|
|
res = roll(msgstr)
|
|
|
|
|
|
|
|
final_msg = res.reduce('') { |x, y| x + y + "\n" }
|
|
|
|
|
|
|
|
room.send_notice(final_msg)
|
|
|
|
end
|
2021-02-07 03:24:07 +00:00
|
|
|
|
|
|
|
# Solve an arithmatic forumla from a string
|
|
|
|
#
|
|
|
|
# @param string [String] The string representation of the formula
|
|
|
|
# @return Integer of the solution
|
2021-02-15 02:06:46 +00:00
|
|
|
def solve(string)
|
2021-02-07 03:24:07 +00:00
|
|
|
formatted = string.gsub(/\s+/, '')
|
|
|
|
formatted = formatted.gsub(/\[[\d,]*\]/) do |a|
|
|
|
|
a.scan(/\d*/).reduce(0) { |x, y| x + y.to_i }
|
|
|
|
end
|
|
|
|
|
|
|
|
if formatted.match(/\(.*\)/)
|
|
|
|
formatted = formatted.sub(/\(.*\)/) do |m|
|
|
|
|
solve(m[1..-2])
|
|
|
|
end
|
|
|
|
|
|
|
|
solve(formatted)
|
|
|
|
|
|
|
|
elsif formatted.match(/\d+\*\d+/)
|
|
|
|
formatted = formatted.sub(/\d+\*\d+/) do |m|
|
|
|
|
m.split('*').reduce(1) { |x, y| x * y.to_i }
|
|
|
|
end
|
|
|
|
|
|
|
|
solve(formatted)
|
|
|
|
|
|
|
|
elsif formatted.match(/\d+\/\d+/)
|
|
|
|
formatted = formatted.sub(/\d*\/\d*/) do |m|
|
|
|
|
m.split('/').reduce { |x, y| x.to_i / y.to_i }
|
|
|
|
end
|
|
|
|
|
|
|
|
solve(formatted)
|
|
|
|
|
|
|
|
elsif formatted.match(/\d+\+\d+/)
|
|
|
|
formatted = formatted.sub(/\d+\+\d+/) do |m|
|
|
|
|
m.split('+').reduce(0) { |x, y| x + y.to_i }
|
|
|
|
end
|
|
|
|
|
|
|
|
solve(formatted)
|
|
|
|
|
|
|
|
elsif formatted.match(/\d+\-\d+/)
|
|
|
|
formatted = formatted.sub(/\d+\-\d+/) do |m|
|
|
|
|
m.split('-').reduce { |x, y| x.to_i + -(y.to_i) }
|
|
|
|
end
|
|
|
|
|
|
|
|
solve(formatted)
|
|
|
|
|
|
|
|
else
|
|
|
|
formatted.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Pretty-print a result
|
|
|
|
#
|
|
|
|
# @param func [String] The name of the function
|
|
|
|
# @param orig [String] The original request
|
|
|
|
# @param string [String] The processed request
|
|
|
|
# @param res [String] The result of the process
|
|
|
|
# @return String re-formatted
|
2021-02-15 02:06:46 +00:00
|
|
|
def pretty(func, orig, string, res)
|
2021-02-07 03:24:07 +00:00
|
|
|
orig.gsub!(/[\+\-*\/]/) { |s| " #{s} " }
|
|
|
|
string.gsub!(/[\+\-*\/]/) { |s| " #{s} " }
|
|
|
|
"#{func.capitalize}: #{orig} (#{string}) ==> #{res}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Roll dice
|
|
|
|
#
|
|
|
|
# @param string [String] The string representation of the dice roll
|
|
|
|
# example: 2d4+6
|
|
|
|
# @return Array of the message and the result
|
2021-02-15 02:06:46 +00:00
|
|
|
def roll(string)
|
2021-02-07 03:24:07 +00:00
|
|
|
results = []
|
|
|
|
string.gsub(/\s+/,'').split(',').each do |roll|
|
|
|
|
orig = roll
|
|
|
|
res = roll.gsub(/[0-9]*d[0-9]*/) do |d|
|
|
|
|
num,sides = d.split('d')
|
|
|
|
num = num.empty? ? '1' : num
|
|
|
|
Array.new(num.to_i.abs) { Random.new.rand(sides.to_i) + 1 }
|
|
|
|
end
|
|
|
|
|
|
|
|
results << pretty("Roll", orig, res, solve(res))
|
|
|
|
end
|
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|