cinch-dicebag 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Cinch::Dicebag
1
+ # Cinch::Plugins::Dicebag
2
2
 
3
3
  Cinch Plugin to allow users to roll dice in channels.
4
4
 
@@ -33,29 +33,29 @@ module Cinch::Plugins
33
33
  return
34
34
  end
35
35
 
36
- nick = m.user.nick.downcase
37
- dice = [rand(250), rand(500), rand(750), rand(1000)].map { |d| d.floor }
38
- bag = "#{dice[0]}d4 #{dice[1]}d6 #{dice[2]}d10 #{dice[3]}d20"
39
- result = roll_dice(bag)
40
-
41
- total = dice.inject(:+)
42
- if total < 100
43
- size = 'tiny'
44
- elsif total < 500 && total >= 100
45
- size = 'small'
46
- elsif total < 1000 && total >= 500
47
- size = 'medium'
48
- elsif total < 1500 && total >= 1000
49
- size = 'large'
50
- elsif total < 2000 && total >= 1500
51
- size = 'hefty'
52
- else
53
- size = 'huge'
54
- end
36
+ dice = { :d4 => rand(250), :d6 => rand(500), :d10 => rand(750), :d20 => rand(1000) }
37
+ result = roll_dice(dice.map { |die, count| "#{count}#{die}" })
38
+
39
+ total = dice.values.inject(:+)
40
+ size = case total
41
+ when 0..100
42
+ 'tiny'
43
+ when 101..500
44
+ 'small'
45
+ when 501..1000
46
+ 'medium'
47
+ when 1001..1500
48
+ 'large'
49
+ when 1501..2000
50
+ 'hefty'
51
+ else
52
+ 'huge'
53
+ end
55
54
 
56
55
  m.reply "#{m.user.nick} rolls a #{size} bag of dice totalling #{result[:total]}."
57
56
 
58
57
  channel = m.channel.name
58
+ nick = m.user.nick.downcase
59
59
 
60
60
  unless @storage.data.key?(channel)
61
61
  @storage.data[channel] = Hash.new
@@ -72,16 +72,18 @@ module Cinch::Plugins
72
72
  m.reply "This is a new high score, their old score was #{old[:score]}, #{old[:time].ago.to_words}."
73
73
  end
74
74
 
75
- # Keep an eye on this and only do it on changes if it becomes a perf issue.
76
75
  synchronize(:dice_save) do
77
76
  @storage.save
78
77
  end
79
78
  end
80
79
 
81
80
  def roll_specific(m, bag)
82
- result = roll_dice(bag)
83
- response = "#{result[:rolls].join(', ')} totalling #{result[:total]}"
84
- m.reply "#{m.user.nick} rolls #{response}" unless response.nil?
81
+ result = roll_dice(bag.split(' '))
82
+ if result.nil?
83
+ m.reply "I'm sorry that's not the right way to roll dice.", true
84
+ else
85
+ m.reply "#{m.user.nick} rolls #{result[:rolls].join(', ')} totalling #{result[:total]}"
86
+ end
85
87
  end
86
88
 
87
89
  private
@@ -89,7 +91,9 @@ module Cinch::Plugins
89
91
  def roll_dice(dice)
90
92
  rolls = []
91
93
  total = 0
92
- dice = dice.split(' ')
94
+
95
+ # Clean out anything invalid
96
+ dice.delete_if { |d| d.match(/\d+d\d+/).nil? }
93
97
  dice.each do |die|
94
98
  if die.match(/\d+d\d+/)
95
99
  count = die.match(/(\d+)d\d+/)[1].to_i rescue 0
@@ -106,7 +110,11 @@ module Cinch::Plugins
106
110
  end
107
111
  end
108
112
  end
109
- return { :rolls => rolls, :total => total }
113
+ if rolls.empty? || total.zero?
114
+ return nil
115
+ else
116
+ return { :rolls => rolls, :total => total }
117
+ end
110
118
  end
111
119
 
112
120
  def roll_dice_type(sides, count)
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Dicebag
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-dicebag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-02 00:00:00.000000000 Z
12
+ date: 2013-05-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cinch