Class: Cinch::Plugins::Dicebag

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/dicebag.rb,
lib/cinch/plugins/dicebag/bag.rb,
lib/cinch/plugins/dicebag/die.rb

Overview

Cinch Plugin to allow dice rolling.

Defined Under Namespace

Modules: Die Classes: Bag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Dicebag

Returns a new instance of Dicebag.



27
28
29
30
31
32
33
34
35
# File 'lib/cinch/plugins/dicebag.rb', line 27

def initialize(*args)
  super
  # initialize storage
  @storage = Cinch::Storage.new(config[:filename] || 'yaml/dice.yml')

  # Create a bag of dice, pass a hash of the maxcount for each type
  #   for random rolls.
  @bag = Bag.new(4 => 250, 6 => 750, 10 => 1500, 20 => 2000)
end

Instance Attribute Details

#storageObject

Returns the value of attribute storage.



16
17
18
# File 'lib/cinch/plugins/dicebag.rb', line 16

def storage
  @storage
end

Instance Method Details

#dicebag(message) ⇒ String

Roll a random assortment of dice, total the rolls, and record the score.

Parameters:

  • message (Message)

    Nickname of the user rolling.

Returns:

  • (String)

    A description of the roll that took place



40
41
42
43
44
45
46
47
48
# File 'lib/cinch/plugins/dicebag.rb', line 40

def dicebag(message)
  return if Cinch::Toolbox.sent_via_private_message?(message)

  @bag.roll
  user = message.user.nick
  channel = message.channel.name
  message.reply "#{user} rolls a #{@bag.size} bag of dice totalling " \
                "#{@bag.score}. #{score_check(user, channel, @bag.score)}"
end

#roll(message, dice = '1d20') ⇒ String

Roll a specific set of dice and return the pretty result

Parameters:

  • nick (String)

    Nickname of the user rolling.

  • dice (String) (defaults to: '1d20')

    Space delimited string of dice to role. (i.e. ‘6d12 4d20 d10’

Returns:

  • (String)

    String describing the dice that were rolled



65
66
67
68
69
70
71
# File 'lib/cinch/plugins/dicebag.rb', line 65

def roll(message, dice = '1d20')
  result = Die.roll(dice.split(' '))
  if result.is_a?(Integer)
    result = "#{message.user.nick} rolls #{dice} totalling #{result}"
  end
  message.reply result
end

#stats(message) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/cinch/plugins/dicebag.rb', line 50

def stats(message)
  return if Cinch::Toolbox.sent_via_private_message?(message)

  message.user.send 'Top ten dicebag rolls:'
  top10 = top_ten_rolls(message.channel.name)
  top10.each_with_index do |r, i|
    message.user.send "#{i + 1} - #{r.first} [#{r.last}]"
  end
end