Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikh committed Feb 27, 2012
0 parents commit 156b0ba
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (C) 2012 by Dominik Honnef

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
@@ -0,0 +1,38 @@
# Dice plugin

This plugin provides dice for your channel.

## Installation
First install the gem by running:
[sudo] gem install cinch-dice

Then load it in your bot:
require "cinch"
require "cinch/plugins/dice"

bot = Cinch::Bot.new do
configure do |c|
# add all required options here
c.plugins.plugins = [Cinch::Plugins::Dice] # optionally add more plugins
end
end

bot.start

## Commands
roll [[<repeats>#]<rolls>]d<sides>[<+/-><offset>]

### Examples
roll 5d6 # roll 5 6-sided dice
roll 2#5d6 # roll 5 6-sided dice, twice

## Options
### :format

With this option you can set the format string used for saying the
dice score. The default is `"Your dice roll was: %d"`.

### Example configuration
configure do |c|
c.plugins.options[Cinch::Plugins::Dice][:format] = "Score: %d"
end
14 changes: 14 additions & 0 deletions cinch-dice.gemspec
@@ -0,0 +1,14 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = 'cinch-dice'
s.version = '0.0.1'
s.summary = 'Dice plugin, perfect for role-playing games or generic decision making.'
s.description = s.summary
s.authors = ['Dominik Honnef']
s.email = ['dominikh@fork-bomb.org']
s.homepage = 'http://rubydoc.info/github/cinchrb/cinch-dice'
s.required_ruby_version = '>= 1.9.1'
s.files = Dir['LICENSE', 'README.md', '{lib,examples}/**/*']
s.add_dependency("cinch", "~> 1.1")
s.license = "MIT"
end
33 changes: 33 additions & 0 deletions lib/cinch/plugins/dice.rb
@@ -0,0 +1,33 @@
require 'cinch'

module Cinch
module Plugins
class Dice
include Cinch::Plugin

# [[<repeats>#]<rolls>]d<sides>[<+/-><offset>]
match(/roll (?:(?:(\d+)#)?(\d+))?d(\d+)(?:([+-])(\d+))?/)
def execute(m, repeats, rolls, sides, offset_op, offset)
repeats = repeats.to_i
repeats = 1 if repeats < 1
rolls = rolls.to_i
rolls = 1 if rolls < 1

total = 0

repeats.times do
rolls.times do
score = rand(sides.to_i) + 1
if offset_op
score = score.send(offset_op, offset.to_i)
end
total += score
end
end

fmt = config[:format] || "Your dice roll was: %d"
m.reply fmt % total, true
end
end
end
end

0 comments on commit 156b0ba

Please sign in to comment.