cinch-dicebag 0.0.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ nguage: ruby
2
+ rvm:
3
+ - ruby-head
4
+ - 1.9.2
5
+ - 1.9.3
data/README.md CHANGED
@@ -1,4 +1,10 @@
1
- # Cinch::Plugins::Dicebag
1
+ #Cinch::Plugins::Dicebag
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/cinch-dicebag.png)](http://badge.fury.io/rb/cinch-dicebag)
4
+ [![Dependency Status](https://gemnasium.com/bhaberer/cinch-dicebag.png)](https://gemnasium.com/bhaberer/cinch-dicebag)
5
+ [![Build Status](https://travis-ci.org/bhaberer/cinch-dicebag.png?branch=master)](https://travis-ci.org/bhaberer/cinch-dicebag)
6
+ [![Coverage Status](https://coveralls.io/repos/bhaberer/cinch-dicebag/badge.png?branch=master)](https://coveralls.io/r/bhaberer/cinch-dicebag?branch=master)
7
+ [![Code Climate](https://codeclimate.com/github/bhaberer/cinch-dicebag.png)](https://codeclimate.com/github/bhaberer/cinch-dicebag)
2
8
 
3
9
  Cinch Plugin to allow users to roll dice in channels.
4
10
 
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -17,8 +17,13 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'cinch', '>= 2.0.0'
21
- gem.add_dependency 'time-lord', '1.0.1'
22
- gem.add_dependency 'cinch-cooldown'
23
- gem.add_dependency 'cinch-storage'
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'coveralls'
23
+
24
+ gem.add_dependency 'cinch', '>= 2.0.0'
25
+ gem.add_dependency 'time-lord', '~> 1.0.1'
26
+ gem.add_dependency 'cinch-cooldown', '>= 1.0.0'
27
+ gem.add_dependency 'cinch-storage', '~> 0.0.2'
28
+ gem.add_dependency 'cinch-toolbox', '~> 0.0.5'
24
29
  end
@@ -11,118 +11,99 @@ module Cinch::Plugins
11
11
 
12
12
  enforce_cooldown
13
13
 
14
- self.help = "Roll a random bag of dice with .dicebag, you can also use .roll (dice count)d(sides) to roll specific dice (e.g. '.roll 4d6 3d20')"
14
+ attr_accessor :storage
15
15
 
16
- class Score < Struct.new(:nick, :score, :time)
17
- def to_yaml
18
- { :nick => nick, :score => score, :time => time }
19
- end
20
- end
16
+ self.help = "Roll a random bag of dice with .dicebag, you can also use .roll (dice count)d(sides) to roll specific dice (e.g. '.roll 4d6 3d20')"
21
17
 
22
- match /dicebag/, method: :roll_bag
23
- match /roll (.*)/, method: :roll_specific
18
+ match /dicebag/
19
+ match /roll (.*)/
24
20
 
25
21
  def initialize(*args)
26
22
  super
27
23
  @storage = CinchStorage.new(config[:filename] || 'yaml/dice.yml')
28
24
  end
29
25
 
30
- def roll_bag(m)
31
- if m.channel.nil?
32
- m.user.msg "You must use that command in the main channel."
33
- return
34
- end
26
+ def execute(m, dice = nil)
27
+ m.reply (dice.nil? ? roll_dicebag(m.user.nick, m.channel) : roll(m.user.nick, dice))
28
+ end
35
29
 
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
54
-
55
- m.reply "#{m.user.nick} rolls a #{size} bag of dice totalling #{result[:total]}."
56
-
57
- channel = m.channel.name
58
- nick = m.user.nick.downcase
59
-
60
- unless @storage.data.key?(channel)
61
- @storage.data[channel] = Hash.new
62
- end
30
+ def roll_dicebag(nick, channel)
31
+ return "You must use that command in the main channel." if channel.nil?
63
32
 
64
- unless @storage.data[channel].key?(nick)
65
- @storage.data[channel][nick] = { :score => result[:total], :time => Time.now }
66
- end
33
+ dice = { :d4 => rand(250), :d6 => rand(500), :d10 => rand(750), :d20 => rand(1000) }
67
34
 
68
- if @storage.data[channel][nick][:score] < result[:total]
69
- old = @storage.data[channel][nick]
70
- @storage.data[channel][nick] = { :score => result[:total], :time => Time.now }
35
+ total = roll_dice(dice.map { |die, count| "#{count}#{die}" })
36
+ size = get_bag_size(dice.values.inject(:+))
71
37
 
72
- m.reply "This is a new high score, their old score was #{old[:score]}, #{old[:time].ago.to_words}."
73
- end
38
+ message = "#{nick} rolls a #{size} bag of dice totalling #{total}. " +
39
+ score_check(nick.downcase, channel.name, total)
74
40
 
75
- synchronize(:dice_save) do
76
- @storage.save
77
- end
41
+ return message
78
42
  end
79
43
 
80
- def roll_specific(m, bag)
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
44
+ def roll(nick, dice)
45
+ return nil if dice.nil? || nick.nil?
46
+
47
+ result = roll_dice(dice.split(' '))
48
+
49
+ return "#{nick} rolls #{dice} totalling #{result}" unless result.nil?
87
50
  end
88
51
 
89
- private
52
+ def roll_die(sides, count)
53
+ return 0 if sides < 1 || count < 1
54
+ total = 0
55
+ count.times { total += rand(sides) + 1 }
56
+ return total
57
+ end
90
58
 
91
59
  def roll_dice(dice)
92
- rolls = []
60
+ # Clean out anything invalid
61
+ dice.delete_if { |d| d.match(/\d*d\d+/).nil? }
62
+
93
63
  total = 0
94
64
 
95
- # Clean out anything invalid
96
- dice.delete_if { |d| d.match(/\d+d\d+/).nil? }
65
+ # Roll each group and total up the returned value
97
66
  dice.each do |die|
98
- if die.match(/\d+d\d+/)
99
- count = die.match(/(\d+)d\d+/)[1].to_i rescue 0
100
- sides = die.match(/\d+d(\d+)/)[1].to_i rescue 0
101
- elsif die.match(/d\d+/)
102
- count = 1
103
- sides = die.match(/d(\d+)/)[1].to_i rescue 0
104
- end
67
+ count = die[/(\d+)d\d+/, 1] || 1
68
+ sides = die[/\d?d(\d+)/, 1]
105
69
  unless count.nil? || sides.nil?
106
- roll = roll_dice_type(sides, count)
107
- unless roll.nil?
108
- rolls << roll[:text]
109
- total += roll[:total]
110
- end
70
+ total += roll_die(sides.to_i, count.to_i)
111
71
  end
112
72
  end
113
- if rolls.empty? || total.zero?
114
- return nil
73
+
74
+ return total
75
+ end
76
+
77
+ def get_bag_size(size)
78
+ case size
79
+ when 0..100
80
+ 'tiny'
81
+ when 101..500
82
+ 'small'
83
+ when 501..1000
84
+ 'medium'
85
+ when 1001..1500
86
+ 'large'
87
+ when 1501..2000
88
+ 'hefty'
115
89
  else
116
- return { :rolls => rolls, :total => total }
90
+ 'huge'
117
91
  end
118
92
  end
119
93
 
120
- def roll_dice_type(sides, count)
121
- unless sides < 1 || count < 1
122
- rolls = []
123
- count.times { rolls << rand(sides) + 1 }
124
- return {:total => rolls.inject(:+),
125
- :text => "#{count}d#{sides}" }
94
+ def score_check(nick, channel, score)
95
+ # If the chennel or nick are not already initialized, spin them up
96
+ @storage.data[channel] ||= Hash.new
97
+ @storage.data[channel][nick] ||= { :score => score, :time => Time.now }
98
+
99
+ # Check and see if this is a higher score.
100
+ old = @storage.data[channel][nick]
101
+ if @storage.data[channel][nick][:score] < score
102
+ @storage.data[channel][nick] = { :score => score, :time => Time.now }
103
+ @storage.synced_save(@bot)
104
+ return "A new high score! Their old high roll was #{old[:score]}, #{old[:time].ago.to_words}."
105
+ else
106
+ return ''
126
107
  end
127
108
  end
128
109
  end
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Dicebag
3
- VERSION = "0.0.2"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,126 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cinch::Plugins::Dicebag do
4
+
5
+ before(:all) do
6
+ @plugin = Cinch::Plugins::Dicebag.new
7
+ end
8
+
9
+ describe 'rolling specific dice' do
10
+ it "should return a string from rolling multiple dice" do
11
+ @plugin.roll('thom', '3d3').should_not be_nil
12
+ end
13
+
14
+ it "should return a string from rolling mixes of dice" do
15
+ @plugin.roll('thom', '3d3 d7 3d25').should_not be_nil
16
+ end
17
+
18
+ it "should return a string from rolling a single die" do
19
+ @plugin.roll('thom', 'd3').should_not be_nil
20
+ end
21
+
22
+ it "should not return a string from rolling dice without a name" do
23
+ @plugin.roll(nil, '3d3').should be_nil
24
+ end
25
+
26
+ it "should not return a string from rolling dice without a name" do
27
+ @plugin.roll('joe', nil).should be_nil
28
+ end
29
+
30
+ it "should return a string describing the dice that were rolled" do
31
+ text = @plugin.roll('thom', '3d3')
32
+ text.match(/rolls\s3d3\stotalling\s\d+/).should_not be_nil
33
+ end
34
+ end
35
+
36
+ describe 'roll_dicebag' do
37
+ it "should return a string" do
38
+ @plugin.roll_dicebag('user', Cinch::Channel.new('foo', fake_bot)).should_not be_nil
39
+ end
40
+
41
+ it "should return an error if the user is not in a channel" do
42
+ @plugin.roll_dicebag('user', nil).
43
+ should be_eql("You must use that command in the main channel.")
44
+ end
45
+
46
+ it "should return a string describing the user's bag roll" do
47
+ text = @plugin.roll_dicebag('user', Cinch::Channel.new('foo', fake_bot))
48
+ text.match(/user rolls a [a-z]+ bag of dice totalling \d+/).should_not be_nil
49
+ end
50
+
51
+ it "should announce a high score if the old score is higher" do
52
+ @plugin.storage.data['foo']['brian'] = { :score => 1, :time => Time.now }
53
+ text = @plugin.roll_dicebag('brian', Cinch::Channel.new('foo', fake_bot))
54
+ text.match(/A new high score/).should_not be_nil
55
+ text.match(/Their old high roll was \d+/).should_not be_nil
56
+ end
57
+ end
58
+
59
+ describe 'roll_dice' do
60
+ it "should return zero if the dice list is empty" do
61
+ @plugin.roll_dice([]).should be_zero
62
+ end
63
+
64
+ it "should return a non zero total on a normal dice list" do
65
+ @plugin.roll_dice(['3d3', '4d5']).should_not be_zero
66
+ end
67
+
68
+ it "should clear out any invalid dice rolls" do
69
+ @plugin.roll_dice(['33']).should be_zero
70
+ end
71
+ end
72
+
73
+ describe "roll_die" do
74
+ it "should return an acceptable value for a given roll" do
75
+ @plugin.roll_die(1, 1).should == 1
76
+ (5..15).should include(@plugin.roll_die(3, 5))
77
+ end
78
+
79
+ it "should return 0 for any negetive values" do
80
+ @plugin.roll_die(-1, 1).should == 0
81
+ @plugin.roll_die( 1, -1).should == 0
82
+ @plugin.roll_die(-1, -1).should == 0
83
+ end
84
+ end
85
+
86
+ describe "get_bag_size" do
87
+ it "should return 'huge' for out of bounds queries" do
88
+ @plugin.get_bag_size(50000).should == 'huge'
89
+ end
90
+
91
+ it "should return the proper size for tiny range" do
92
+ @plugin.get_bag_size(0).should == 'tiny'
93
+ @plugin.get_bag_size(rand(100)).should == 'tiny'
94
+ @plugin.get_bag_size(100).should == 'tiny'
95
+ end
96
+
97
+ it "should return the proper size for small range" do
98
+ @plugin.get_bag_size(101).should == 'small'
99
+ @plugin.get_bag_size(rand(399) + 101).should == 'small'
100
+ @plugin.get_bag_size(500).should == 'small'
101
+ end
102
+
103
+ it "should return the proper size for medium range" do
104
+ @plugin.get_bag_size(501).should == 'medium'
105
+ @plugin.get_bag_size(rand(499) + 501).should == 'medium'
106
+ @plugin.get_bag_size(1000).should == 'medium'
107
+ end
108
+
109
+ it "should return the proper size for large range" do
110
+ @plugin.get_bag_size(1001).should == 'large'
111
+ @plugin.get_bag_size(rand(499) + 1001).should == 'large'
112
+ @plugin.get_bag_size(1500).should == 'large'
113
+ end
114
+
115
+ it "should return the proper size for hefty range" do
116
+ @plugin.get_bag_size(1501).should == 'hefty'
117
+ @plugin.get_bag_size(rand(499) + 1501).should == 'hefty'
118
+ @plugin.get_bag_size(2000).should == 'hefty'
119
+ end
120
+
121
+ it "should return the proper size for huge range" do
122
+ @plugin.get_bag_size(2001).should == 'huge'
123
+ @plugin.get_bag_size(20001).should == 'huge'
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,26 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+ require 'cinch-dicebag'
4
+
5
+ def fake_bot
6
+ bot = Cinch::Bot.new do
7
+ configure do |c|
8
+ c.plugins.options[Cinch::Plugins::Dicebag][:filename] = '/dev/null'
9
+ end
10
+ end
11
+ bot.loggers.level = :fatal
12
+ bot
13
+ return bot
14
+ end
15
+
16
+ module Cinch
17
+ module Plugin
18
+ def initialize(opts = {})
19
+ @bot = fake_bot
20
+ @handlers = []
21
+ @timers = []
22
+ # Don't init the bot
23
+ # __register
24
+ end
25
+ end
26
+ 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.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-04 00:00:00.000000000 Z
12
+ date: 2013-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: coveralls
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
14
62
  - !ruby/object:Gem::Dependency
15
63
  name: cinch
16
64
  requirement: !ruby/object:Gem::Requirement
@@ -32,7 +80,7 @@ dependencies:
32
80
  requirement: !ruby/object:Gem::Requirement
33
81
  none: false
34
82
  requirements:
35
- - - '='
83
+ - - ~>
36
84
  - !ruby/object:Gem::Version
37
85
  version: 1.0.1
38
86
  type: :runtime
@@ -40,7 +88,7 @@ dependencies:
40
88
  version_requirements: !ruby/object:Gem::Requirement
41
89
  none: false
42
90
  requirements:
43
- - - '='
91
+ - - ~>
44
92
  - !ruby/object:Gem::Version
45
93
  version: 1.0.1
46
94
  - !ruby/object:Gem::Dependency
@@ -50,7 +98,7 @@ dependencies:
50
98
  requirements:
51
99
  - - ! '>='
52
100
  - !ruby/object:Gem::Version
53
- version: '0'
101
+ version: 1.0.0
54
102
  type: :runtime
55
103
  prerelease: false
56
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,23 +106,39 @@ dependencies:
58
106
  requirements:
59
107
  - - ! '>='
60
108
  - !ruby/object:Gem::Version
61
- version: '0'
109
+ version: 1.0.0
62
110
  - !ruby/object:Gem::Dependency
63
111
  name: cinch-storage
64
112
  requirement: !ruby/object:Gem::Requirement
65
113
  none: false
66
114
  requirements:
67
- - - ! '>='
115
+ - - ~>
68
116
  - !ruby/object:Gem::Version
69
- version: '0'
117
+ version: 0.0.2
70
118
  type: :runtime
71
119
  prerelease: false
72
120
  version_requirements: !ruby/object:Gem::Requirement
73
121
  none: false
74
122
  requirements:
75
- - - ! '>='
123
+ - - ~>
76
124
  - !ruby/object:Gem::Version
77
- version: '0'
125
+ version: 0.0.2
126
+ - !ruby/object:Gem::Dependency
127
+ name: cinch-toolbox
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 0.0.5
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 0.0.5
78
142
  description: Cinch Plugin that allows uses in the channel to roll specific dice or
79
143
  roll a random assortment of dice to compete for high scores.
80
144
  email:
@@ -84,6 +148,7 @@ extensions: []
84
148
  extra_rdoc_files: []
85
149
  files:
86
150
  - .gitignore
151
+ - .travis.yml
87
152
  - Gemfile
88
153
  - LICENSE.txt
89
154
  - README.md
@@ -92,6 +157,8 @@ files:
92
157
  - lib/cinch-dicebag.rb
93
158
  - lib/cinch/plugins/dicebag/dicebag.rb
94
159
  - lib/cinch/plugins/dicebag/version.rb
160
+ - spec/cinch-dicebag_spec.rb
161
+ - spec/spec_helper.rb
95
162
  homepage: https://github.com/bhaberer/cinch-dicebag
96
163
  licenses: []
97
164
  post_install_message:
@@ -116,4 +183,6 @@ rubygems_version: 1.8.24
116
183
  signing_key:
117
184
  specification_version: 3
118
185
  summary: ! 'Cinch Plugin: Dicebag and Dice rolls'
119
- test_files: []
186
+ test_files:
187
+ - spec/cinch-dicebag_spec.rb
188
+ - spec/spec_helper.rb