cinch-dicebag 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,7 +56,9 @@ module Cinch::Plugins
56
56
 
57
57
  result = roll_dice(dice.split(' '))
58
58
 
59
- unless result.nil?
59
+ if result.is_a?(String)
60
+ m.reply result
61
+ elsif result.is_a?(Integer)
60
62
  m.reply "#{m.user.nick} rolls #{dice} totalling #{result}"
61
63
  end
62
64
  end
@@ -70,28 +72,39 @@ module Cinch::Plugins
70
72
  # Clean out anything invalid
71
73
  dice.delete_if { |d| d.match(/\d*d\d+/).nil? }
72
74
 
75
+ return roll_check?(dice) if roll_check?(dice)
76
+
73
77
  total = 0
74
78
 
75
79
  # Roll each group and total up the returned value
76
80
  dice.each do |die|
77
- count = die[/(\d+)d\d+/, 1] || 1
78
- sides = die[/\d?d(\d+)/, 1]
79
- unless count.nil? || sides.nil?
80
- total += roll_die(sides.to_i, count.to_i)
81
- end
81
+ total += roll_die(die)
82
82
  end
83
83
 
84
- return total unless total.zero?
84
+ return total.to_i unless total.zero?
85
+ end
86
+
87
+ # Takes an array of rolls and does sanity on it.
88
+ # @param [Array] dice Array of strings that correspond to valid die rolls.
89
+ # (i.e. ['4d6', '6d10']
90
+ # @return [Fixnum] The total from rolling all of the dice.
91
+ def roll_check?(dice)
92
+ # Check to make sure it's not a stupid large roll, they clog threads.
93
+ count = dice.map { |die| die[/(\d+)d\d+/, 1].to_i || 1 }.inject(0, :+)
94
+ return 'I don\'t have that many dice in my bag!' unless count <= 10_000
95
+ false
85
96
  end
86
97
 
87
98
  # Rolls an n-sided die a given amount of times and returns the total
88
- # @param [Fixnum] sides Number of sides of the die we're rolling.
89
- # @param [Fixnum] count Number of times to roll the die.
99
+ # @param [Fixn] count Number of times to roll the die.
90
100
  # @return [Fixnum] The total from rolling the die.
91
- def roll_die(sides, count)
92
- return 0 if sides < 1 || count < 1
101
+ def roll_die(die)
102
+ count = (die[/(\d+)d\d+/, 1] || 1).to_i
103
+ sides = die[/\d?d(\d+)/, 1].to_i
104
+ return 0 if count.nil? || sides.nil? || sides < 1 || count < 1
93
105
  total = 0
94
106
  count.times { total += rand(sides) + 1 }
107
+
95
108
  total
96
109
  end
97
110
 
@@ -2,6 +2,6 @@
2
2
  module Cinch
3
3
  # Versioning information for plugin
4
4
  module Dicebag
5
- VERSION = '1.0.4'
5
+ VERSION = '1.0.5'
6
6
  end
7
7
  end
@@ -8,61 +8,71 @@ describe Cinch::Plugins::Dicebag do
8
8
  end
9
9
 
10
10
  describe 'rolling specific dice' do
11
- it "should return a roll from rolling dice" do
11
+ it 'should return a roll from rolling dice' do
12
12
  get_replies(make_message(@bot, '!roll 3d3', { nick: 'ted' })).first.
13
13
  should_not be_nil
14
14
  end
15
15
 
16
- it "should return a roll in bounds from rolling dice" do
16
+ it 'should return a roll in bounds from rolling dice' do
17
17
  roll = get_replies(make_message(@bot, '!roll 3d3', { nick: 'ted' })).first.text
18
18
  roll = roll[/totalling (\d+)/, 1]
19
- (3..9).should include(roll.to_i)
19
+ (3..9).should include roll.to_i
20
20
  end
21
21
 
22
- it "should return a roll from rolling mixes of dice" do
22
+ it 'should disallow rolling stupid numbers of dice' do
23
+ get_replies(make_message(@bot, '!roll 100001d20', { nick: 'ted' })).first.
24
+ should include 'I don\'t have that many dice in my bag!'
25
+ end
26
+
27
+ it 'should disallow rolling stupid numbers of dice (multiple dice types)' do
28
+ get_replies(make_message(@bot, '!roll 5000d3 5000d20 1d30', { nick: 'ted' })).first.
29
+ should include 'I don\'t have that many dice in my bag!'
30
+ end
31
+
32
+ it 'should return a roll from rolling mixes of dice' do
23
33
  get_replies(make_message(@bot, '!roll 3d3 d7 3d21', { nick: 'ted' })).first.
24
34
  should_not be_nil
25
35
  end
26
36
 
27
- it "should return a roll in bounds from rolling mixes of dice" do
37
+ it 'should return a roll in bounds from rolling mixes of dice' do
28
38
  roll = get_replies(make_message(@bot, '!roll 3d3 d7 3d21', { nick: 'ted' })).first.text
29
39
  roll = roll[/totalling (\d+)/, 1]
30
- (7..79).should include(roll.to_i)
40
+ (7..79).should include roll.to_i
31
41
  end
32
42
 
33
- it "should return a roll from rolling a single die" do
43
+ it 'should return a roll from rolling a single die' do
34
44
  get_replies(make_message(@bot, '!roll d3', { nick: 'ted' })).first.
35
45
  should_not be_nil
36
46
  end
37
47
 
38
- it "should not return a string from rolling dice without a name" do
48
+ it 'should not return a string from rolling dice without a name' do
39
49
  get_replies(make_message(@bot, '!roll .' , { nick: 'ted' })).first.
40
50
  should be_nil
41
51
  end
42
52
 
43
- it "should return a string describing the dice that were rolled" do
53
+ it 'should return a string describing the dice that were rolled' do
44
54
  text = get_replies(make_message(@bot, '!roll 3d3', { nick: 'ted' })).first.text
45
55
  text.should match(/rolls\s3d3\stotalling\s\d+/)
46
56
  end
47
57
  end
48
58
 
49
59
  describe 'roll_dicebag' do
50
- it "should return a string" do
60
+ it 'should return a string' do
51
61
  get_replies(make_message(@bot, '!dicebag' , { nick: 'ted', channel: '#bar' })).first.
52
62
  should_not be_nil
53
63
  end
54
64
 
55
- it "should return an error if the user is not in a channel" do
65
+ it 'should return an error if the user is not in a channel' do
56
66
  get_replies(make_message(@bot, '!dicebag' , { nick: 'ted' })).first.text.
57
67
  should be_eql("You must use that command in the main channel")
58
68
  end
59
69
 
60
- it "should return a string describing the user's bag roll" do
70
+ it 'should return a string describing the user\'s bag roll' do
61
71
  get_replies(make_message(@bot, '!dicebag' , { nick: 'ted', channel: '#bar' })).first.text.
62
72
  should match(/ted rolls a [a-z]+ bag of dice totalling \d+/)
63
73
  end
64
74
 
65
- it "should announce a high score if the old score is higher" do
75
+ it 'should announce a high score if the old score is higher' do
66
76
  get_replies(make_message(@bot, '!dicebag' , { nick: 'brian', channel: '#foo' }))
67
77
  @bot.plugins.first.storage.data['#foo']['brian'] = { score: 1, time: Time.now }
68
78
  text = get_replies(make_message(@bot, '!dicebag' , { nick: 'brian', channel: '#foo' })).first.text
@@ -73,68 +83,67 @@ describe Cinch::Plugins::Dicebag do
73
83
  end
74
84
 
75
85
  describe 'roll_dice' do
76
- it "should return nil if the dice list is empty" do
86
+ it 'should return nil if the dice list is empty' do
77
87
  @bot.plugins.first.roll_dice([]).should be_nil
78
88
  end
79
89
 
80
- it "should return a non zero total on a normal dice list" do
90
+ it 'should return a non zero total on a normal dice list' do
81
91
  @bot.plugins.first.roll_dice(['3d3', '4d5']).should_not be_zero
82
92
  end
83
93
 
84
- it "should clear out any invalid dice rolls" do
94
+ it 'should clear out any invalid dice rolls' do
85
95
  @bot.plugins.first.roll_dice(['33']).should be_nil
86
96
  end
87
97
  end
88
98
 
89
- describe "roll_die" do
90
- it "should return an acceptable value for a given roll" do
91
- @bot.plugins.first.roll_die(1, 1).should == 1
92
- (5..15).should include(@bot.plugins.first.roll_die(3, 5))
99
+ describe 'roll_die' do
100
+ it 'should return an acceptable value for a given roll' do
101
+ @bot.plugins.first.roll_die('1d1').should == 1
102
+ (5..15).should include(@bot.plugins.first.roll_die('3d5'))
93
103
  end
94
104
 
95
- it "should return 0 for any negetive values" do
96
- @bot.plugins.first.roll_die(-1, 1).should == 0
97
- @bot.plugins.first.roll_die( 1, -1).should == 0
98
- @bot.plugins.first.roll_die(-1, -1).should == 0
105
+ it 'should return 0 for any negetive values' do
106
+ @bot.plugins.first.roll_die('1d-1').should == 0
107
+ @bot.plugins.first.roll_die('-1d-1').should == 0
99
108
  end
100
109
  end
101
110
 
102
- describe "get_bag_size" do
103
- it "should return 'huge' for out of bounds queries" do
111
+ describe 'get_bag_size' do
112
+ it 'should return \'huge\' for out of bounds queries' do
104
113
  @bot.plugins.first.get_bag_size(50000).should == 'huge'
105
114
  end
106
115
 
107
- it "should return the proper size for tiny range" do
116
+ it 'should return the proper size for tiny range' do
108
117
  @bot.plugins.first.get_bag_size(0).should == 'tiny'
109
118
  @bot.plugins.first.get_bag_size(rand(100)).should == 'tiny'
110
119
  @bot.plugins.first.get_bag_size(100).should == 'tiny'
111
120
  end
112
121
 
113
- it "should return the proper size for small range" do
122
+ it 'should return the proper size for small range' do
114
123
  @bot.plugins.first.get_bag_size(101).should == 'small'
115
124
  @bot.plugins.first.get_bag_size(rand(399) + 101).should == 'small'
116
125
  @bot.plugins.first.get_bag_size(500).should == 'small'
117
126
  end
118
127
 
119
- it "should return the proper size for medium range" do
128
+ it 'should return the proper size for medium range' do
120
129
  @bot.plugins.first.get_bag_size(501).should == 'medium'
121
130
  @bot.plugins.first.get_bag_size(rand(499) + 501).should == 'medium'
122
131
  @bot.plugins.first.get_bag_size(1000).should == 'medium'
123
132
  end
124
133
 
125
- it "should return the proper size for large range" do
134
+ it 'should return the proper size for large range' do
126
135
  @bot.plugins.first.get_bag_size(1001).should == 'large'
127
136
  @bot.plugins.first.get_bag_size(rand(499) + 1001).should == 'large'
128
137
  @bot.plugins.first.get_bag_size(1500).should == 'large'
129
138
  end
130
139
 
131
- it "should return the proper size for hefty range" do
140
+ it 'should return the proper size for hefty range' do
132
141
  @bot.plugins.first.get_bag_size(1501).should == 'hefty'
133
142
  @bot.plugins.first.get_bag_size(rand(499) + 1501).should == 'hefty'
134
143
  @bot.plugins.first.get_bag_size(2000).should == 'hefty'
135
144
  end
136
145
 
137
- it "should return the proper size for huge range" do
146
+ it 'should return the proper size for huge range' do
138
147
  @bot.plugins.first.get_bag_size(2001).should == 'huge'
139
148
  @bot.plugins.first.get_bag_size(20001).should == 'huge'
140
149
  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: 1.0.4
4
+ version: 1.0.5
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-12-09 00:00:00.000000000 Z
12
+ date: 2014-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake