cinch-dicebag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cinch-dicebag.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Haberer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Cinch::Dicebag
2
+
3
+ Cinch Plugin to allow users to roll dice in channels.
4
+
5
+ Supports rolling specific dice as well as a random assortment of dice. (Leaderboards coming soon)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'cinch-dicebag'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cinch-dicebag
20
+
21
+ ## Usage
22
+
23
+ Just add the plugin to your list:
24
+
25
+ @bot = Cinch::Bot.new do
26
+ configure do |c|
27
+ c.plugins.plugins = [Cinch::Plugins::Dicebag]
28
+ end
29
+ end
30
+
31
+ Then in channel use .roll:
32
+
33
+ .roll 5d20
34
+
35
+ You can also use .dicebag to roll a random assortment of dice.
36
+
37
+ < Brian > .dicebag
38
+ < bot > Brian rolls a large bag of dice totalling 11052.
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cinch/plugins/dicebag/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "cinch-dicebag"
8
+ gem.version = Cinch::Dicebag::VERSION
9
+ gem.authors = ["Brian Haberer"]
10
+ gem.email = ["bhaberer@gmail.com"]
11
+ gem.description = %q{Cinch Plugin that allows uses in the channel to roll specific dice or roll a random assortment of dice to compete for high scores.}
12
+ gem.summary = %q{Cinch Plugin: Dicebag and Dice rolls}
13
+ gem.homepage = "https://github.com/bhaberer/cinch-dicebag"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
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'
24
+ end
@@ -0,0 +1,2 @@
1
+ require 'cinch/plugins/dicebag/version'
2
+ require 'cinch/plugins/dicebag/dicebag'
@@ -0,0 +1,121 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'cinch'
3
+ require 'cinch-toolbox'
4
+ require 'cinch-cooldown'
5
+ require 'cinch-storage'
6
+ require 'time-lord'
7
+
8
+ module Cinch::Plugins
9
+ class Dicebag
10
+ include Cinch::Plugin
11
+
12
+ enforce_cooldown
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')"
15
+
16
+ class Score < Struct.new(:nick, :score, :time)
17
+ def to_yaml
18
+ { :nick => nick, :score => score, :time => time }
19
+ end
20
+ end
21
+
22
+ match /dicebag/, method: :roll_bag
23
+ match /roll (.*)/, method: :roll_specific
24
+
25
+ def initialize(*args)
26
+ super
27
+ @storage = CinchStorage.new(config[:filename] || 'yaml/dice.yml')
28
+ end
29
+
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
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
55
+
56
+ m.reply "#{m.user.nick} rolls a #{size} bag of dice totalling #{result[:total]}."
57
+
58
+ channel = m.channel.name
59
+
60
+ unless @storage.data.key?(channel)
61
+ @storage.data[channel] = Hash.new
62
+ end
63
+
64
+ unless @storage.data[channel].key?(nick)
65
+ @storage.data[channel][nick] = { :score => result[:total], :time => Time.now }
66
+ end
67
+
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 }
71
+
72
+ m.reply "This is a new high score, their old score was #{old[:score]}, #{old[:time].ago.to_words}."
73
+ end
74
+
75
+ # Keep an eye on this and only do it on changes if it becomes a perf issue.
76
+ synchronize(:dice_save) do
77
+ @storage.save
78
+ end
79
+ end
80
+
81
+ 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?
85
+ end
86
+
87
+ private
88
+
89
+ def roll_dice(dice)
90
+ rolls = []
91
+ total = 0
92
+ dice = dice.split(' ')
93
+ dice.each do |die|
94
+ if die.match(/\d+d\d+/)
95
+ count = die.match(/(\d+)d\d+/)[1].to_i rescue 0
96
+ sides = die.match(/\d+d(\d+)/)[1].to_i rescue 0
97
+ elsif die.match(/d\d+/)
98
+ count = 1
99
+ sides = die.match(/d(\d+)/)[1].to_i rescue 0
100
+ end
101
+ unless count.nil? || sides.nil?
102
+ roll = roll_dice_type(sides, count)
103
+ unless roll.nil?
104
+ rolls << roll[:text]
105
+ total += roll[:total]
106
+ end
107
+ end
108
+ end
109
+ return { :rolls => rolls, :total => total }
110
+ end
111
+
112
+ def roll_dice_type(sides, count)
113
+ unless sides < 1 || count < 1
114
+ rolls = []
115
+ count.times { rolls << rand(sides) + 1 }
116
+ return {:total => rolls.inject(:+),
117
+ :text => "#{count}d#{sides}" }
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,5 @@
1
+ module Cinch
2
+ module Dicebag
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-dicebag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Haberer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cinch
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: time-lord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: cinch-cooldown
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: cinch-storage
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Cinch Plugin that allows uses in the channel to roll specific dice or
79
+ roll a random assortment of dice to compete for high scores.
80
+ email:
81
+ - bhaberer@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - cinch-dicebag.gemspec
92
+ - lib/cinch-dicebag.rb
93
+ - lib/cinch/plugins/dicebag/dicebag.rb
94
+ - lib/cinch/plugins/dicebag/version.rb
95
+ homepage: https://github.com/bhaberer/cinch-dicebag
96
+ licenses: []
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.24
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: ! 'Cinch Plugin: Dicebag and Dice rolls'
119
+ test_files: []