Skip to content

Commit

Permalink
プラグイン DiceRoll 機能追加
Browse files Browse the repository at this point in the history
* 条件判定部において、コマンド接頭語を分離した
* ダイスを振る数が多すぎるときのエラーメッセージを定数化した
* dXX ロールを実装した
* 上記の変更をドキュメントに追記した
  • Loading branch information
koi-chan committed Mar 18, 2015
1 parent 1548138 commit 0682740
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
17 changes: 17 additions & 0 deletions doc/plugins/dice_roll.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ DiceRoll
> foo -> 1d6 = [1] = 1
```

### dXX ロール (`.roll dXX`)

出目をそのまま並べて数字にするダイスロールの結果を返します。
_XX_ に入れた数字それぞれがダイスそれぞれの面数として、桁数の分だけそのダイスを振ります。

_XX_ は1~20桁の数字を指定します。
ただし、途中に0を入れると、0を含めそれより後ろの数字を無視します。

####

```
.roll d66
> foo -> d66 = [3,6] = 36
.roll d234
> foo -> d567 = [3,2,7] = 327
```

ToDo
----

Expand Down
24 changes: 23 additions & 1 deletion lib/rgrb/plugin/dice_roll/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Plugin
module DiceRoll
# DiceRoll の出力テキスト生成器
class Generator
TOO_MANY_DICES = "ダイスが机から落ちてしまいましたの☆"

def initialize
@random = Random.new
end
Expand All @@ -20,12 +22,23 @@ def initialize
# @return [String]
def basic_dice(rolls, sides)
if rolls > 100
"#{rolls}d#{sides}: ダイスが机から落ちてしまいましたの☆"
"#{rolls}d#{sides}: #{TOO_MANY_DICES}"
else
dice_roll(rolls, sides).dice_roll_format
end
end

# dXX のようなダイスロールの結果を返す
# @param [String] rolls ダイスの面数と数
# @return [String]
def dxx_dice(rolls)
if rolls.size > 20
"d#{rolls}: #{TOO_MANY_DICES}"
else
dxx_roll(rolls)
end
end

# ダイスロールの結果を返す
# @param [Fixnum] rolls ダイスの個数
# @param [Fixnum] sides ダイスの最大値
Expand All @@ -34,6 +47,15 @@ def dice_roll(rolls, sides)
values = Array.new(rolls) { @random.rand(1..sides) }
DiceRollResult.new(rolls, sides, values)
end

# dXX ロールの結果を返す
# @param [String] rolls ダイスの面数と数
# @return [String]
def dxx_roll(rolls)
values = []
rolls.each_char { |max| values << @random.rand(1..max.to_i) }
"d#{rolls} = [#{values.join(',')}] = #{values.join('')}"
end
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/rgrb/plugin/dice_roll/irc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class IrcAdapter
include Cinch::Plugin

set(plugin_name: 'DiceRoll')
match(/roll[  ]+([1-9]\d*)d([1-9]\d*)/i, method: :basic_dice)
self.prefix = /\.roll[\s ]+/
match(/([1-9]\d*)d([1-9]\d*)/i, method: :basic_dice)
match(/d([1-9]+)/i, method: :dxx_dice)

def initialize(*args)
super
Expand All @@ -25,6 +27,13 @@ def basic_dice(m, n_dice, max)
message = @generator.basic_dice(n_dice.to_i, max.to_i)
m.target.send("#{m.user.nick} -> #{message}", true)
end

# d66 など、出目をそのままつなげるダイスロールの結果を返す
# @return [void]
def dxx_dice(m, rolls)
message = @generator.dxx_dice(rolls)
m.target.send("#{m.user.nick} -> #{message}", true)
end
end
end
end
Expand Down

0 comments on commit 0682740

Please sign in to comment.