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
# File 'lib/cinch/plugins/dicebag.rb', line 27

def initialize(*args)
  super
  @storage = Cinch::Storage.new(config[:filename] || 'yaml/dice.yml')
  @bag = Bag.new(4 => 250, 6 => 500, 10 => 750, 20 => 1000)
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(m) ⇒ String

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

Parameters:

  • nick (String)

    Nickname of the user rolling.

  • channel (Cinch::Channel)

    The Channel object where the roll took place.

Returns:

  • (String)

    A description of the roll that took place



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

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

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

#roll(m, 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



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

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

#stats(m) ⇒ Object



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

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

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