#
# = plugin.dice.rb
#
# Dice Plugin for Lingrvant
#
# Copyright (c) 2007 Naoki Hiroshima
# You can redistribute it and/or modify it under the same terms as Lingrvant.
#
# Author:: Satoshi NAKAGAWA
# Author:: Naoki Hiroshima <n at h7a dot org>
#
# 99% of this code is just copied from the official botkit_sample.rb
#
require 'lingrvant'

class Dice < Lingrvant::Plugin
  def help
    '^((\d+)[Dd](\d+)(?:\+(\d+))?)$'
  end

  def on_user(room, mes)
    if mes['text'].match(/^((\d+)[Dd](\d+)(?:\+(\d+))?)$/) and $2.to_i > 0 and $3.to_i > 0
      dices = []
      sum = 0
      msg = ''
      $2.to_i.times do |i|
        dice = rand($3.to_i) + 1
        dices.push(dice)
        sum += dice
      end
      msg << (sum + ($4 || '0').to_i).to_s << ' = ' << dices.join(' + ')
      msg << ' ( + ' << $4 << ' )' if $4
      msg << ' ... ' << $1
      room.say msg
    end
  end
end

if __FILE__ == $0
  Dice.new.unit_test(:on_user)
end
