| 1 | package Bot::Cobalt::Plugin::Games::Dice; | ||
| 2 | $Bot::Cobalt::Plugin::Games::Dice::VERSION = '0.021003'; | ||
| 3 | use v5.10; | ||
| 4 | use strictures 2; | ||
| 5 | |||
| 6 | use Bot::Cobalt::Utils 'color'; | ||
| 7 | |||
| 8 | sub new { bless [], shift } | ||
| 9 | |||
| 10 | sub execute { | ||
| 11 | my ($self, $msg, $str) = @_; | ||
| 12 | return "Syntax: roll XdY [ +/- <modifier> ]" unless $str; | ||
| 13 | |||
| 14 | my ($dice, $modifier, $modify_by) = split ' ', $str; | ||
| 15 | |||
| 16 | if ($dice =~ /^(\d+)?d(\d+)?$/i) { ## Xd / dY / XdY syntax | ||
| 17 | my $n_dice = $1 || 1; | ||
| 18 | my $sides = $2 || 6; | ||
| 19 | |||
| 20 | my @rolls; | ||
| 21 | $n_dice = 10 if $n_dice > 10; | ||
| 22 | $sides = 10000 if $sides > 10000; | ||
| 23 | |||
| 24 | until (@rolls == $n_dice) { | ||
| 25 | push @rolls, (int rand $sides) + 1; | ||
| 26 | } | ||
| 27 | my $total; | ||
| 28 | $total += $_ for @rolls; | ||
| 29 | |||
| 30 | $modifier = undef unless $modify_by and $modify_by =~ /^\d+$/; | ||
| 31 | if ($modifier) { | ||
| 32 | $modifier eq '+' ? $total += $modify_by | ||
| 33 | : $modifier eq '-' ? $total -= $modify_by | ||
| 34 | : () | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | my $resp = "Rolled " | ||
| 39 | .color('bold', $n_dice) | ||
| 40 | .($sides > 1 ? ' dice of ' : ' die of ') | ||
| 41 | .color('bold', $sides) | ||
| 42 | ." sides: " . join ' ', @rolls; | ||
| 43 | my $potential = $n_dice * $sides; | ||
| 44 | $resp .= " [total: ".color('bold', $total)." / $potential]"; | ||
| 45 | return $resp | ||
| 46 | } | ||
| 47 | |||
| 48 | if ($dice =~ /^\d+$/) { | ||
| 49 | my $total = (int rand $dice) + 1; | ||
| 50 | $modifier = undef unless $modify_by and $modify_by =~ /^\d+$/; | ||
| 51 | if ($modifier) { | ||
| 52 | $modifier eq '+' ? $total += $modify_by | ||
| 53 | : $modifier eq '-' ? $total -= $modify_by | ||
| 54 | : () | ||
| 55 | } | ||
| 56 | my $resp = "Rolled single die of " | ||
| 57 | .color('bold', $dice) | ||
| 58 | ." sides: " | ||
| 59 | .color('bold', $total) ; | ||
| 60 | return $resp | ||
| 61 | } | ||
| 62 | |||
| 63 | "Syntax: roll XdY [ +/- <modifier> ]" | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | __END__ |