| 1 | package Bot::Backbone::Service::OFun::Dice; | ||
| 2 | $Bot::Backbone::Service::OFun::Dice::VERSION = '0.142230'; | ||
| 3 | use Bot::Backbone::Service; | ||
| 4 | |||
| 5 | with qw( | ||
| 6 | Bot::Backbone::Service::Role::Service | ||
| 7 | Bot::Backbone::Service::Role::Responder | ||
| 8 | ); | ||
| 9 | |||
| 10 | use List::MoreUtils qw( any ); | ||
| 11 | use List::Util qw( shuffle ); | ||
| 12 | use Readonly; | ||
| 13 | |||
| 14 | Readonly my $DICE_NOTATION => qr/(?<count>\d+)?d(?<sides>\d+)(?<modifier>[+-]\d+)?/i; | ||
| 15 | Readonly my @COIN_SIDES => qw( heads tails ); | ||
| 16 | |||
| 17 | # ABSTRACT: Tools for rolling dice, flipping coins, choosing, and shuffling | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | service_dispatcher as { | ||
| 22 | command '!roll' => given_parameters { | ||
| 23 | parameter 'dice' => ( | ||
| 24 | match => $DICE_NOTATION, | ||
| 25 | default => 'd6', | ||
| 26 | ); | ||
| 27 | } respond_by_method 'roll_dice'; | ||
| 28 | |||
| 29 | command '!flip' => given_parameters { | ||
| 30 | parameter 'count' => ( match => qr/\d+/, default => 1 ); | ||
| 31 | } respond_by_method 'flip_coin'; | ||
| 32 | |||
| 33 | command '!choose' => given_parameters { | ||
| 34 | parameter 'count' => ( match => qr/\d+/ ); | ||
| 35 | } respond_by_method 'choose_n'; | ||
| 36 | |||
| 37 | command '!choose' => respond_by_method 'choose_n'; | ||
| 38 | |||
| 39 | command '!shuffle' => respond_by_method 'choose_all'; | ||
| 40 | }; | ||
| 41 | |||
| 42 | |||
| 43 | sub roll_dice { | ||
| 44 | my ($self, $message) = @_; | ||
| 45 | |||
| 46 | my $dice = $message->parameters->{dice} // 'd6'; | ||
| 47 | my $success = $dice =~ $DICE_NOTATION; | ||
| 48 | return "I don't understand those dice." unless $success; | ||
| 49 | |||
| 50 | my $count = $+{count} // 1; | ||
| 51 | my $sides = $+{sides} // 6; | ||
| 52 | my $modifier = $+{modifier} // 0; | ||
| 53 | |||
| 54 | my @messages; | ||
| 55 | |||
| 56 | return "Not sure what to do with a $sides-sided die." | ||
| 57 | unless $sides >= 2; | ||
| 58 | |||
| 59 | if ($count > 100) { | ||
| 60 | my $verbing = $sides == 2 ? 'flipping' : 'rolling'; | ||
| 61 | return "You can't be serious. I'm not $verbing $count times." | ||
| 62 | } | ||
| 63 | |||
| 64 | unless (any { $sides == $_ } (2, 4, 6, 8, 10, 12, 20, 100)) { | ||
| 65 | push @messages, "You have INTERESTING dice."; | ||
| 66 | } | ||
| 67 | |||
| 68 | if ($sides eq 2) { | ||
| 69 | my @flips; | ||
| 70 | for (1 .. $count) { | ||
| 71 | push @flips, $COIN_SIDES[ int($sides * rand()) ]; | ||
| 72 | } | ||
| 73 | |||
| 74 | push @messages, "Flipped $count times: ".join(', ', @flips); | ||
| 75 | } | ||
| 76 | |||
| 77 | else { | ||
| 78 | my $sum = 0; | ||
| 79 | for (1 .. $count) { | ||
| 80 | $sum += int($sides * rand()) + 1; | ||
| 81 | } | ||
| 82 | $sum += $modifier; | ||
| 83 | |||
| 84 | push @messages, "Rolled $sum"; | ||
| 85 | } | ||
| 86 | |||
| 87 | return @messages; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | sub flip_coin { | ||
| 92 | my ($self, $message) = @_; | ||
| 93 | |||
| 94 | my $count = $message->parameters->{count}; | ||
| 95 | $message->parameters->{dice} = $count . 'd2'; | ||
| 96 | return $self->roll_dice($message); | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub _items { | ||
| 101 | my ($self, $message) = @_; | ||
| 102 | return shuffle grep /\S/, map { s/^\s+//; s/\s+$//; $_ } map { $_->original } $message->all_args; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub choose_n { | ||
| 106 | my ($self, $message) = @_; | ||
| 107 | |||
| 108 | my $count = $message->parameters->{count} // 1; | ||
| 109 | my @items = $self->_items($message); | ||
| 110 | my $n = scalar @items; | ||
| 111 | |||
| 112 | return "Wise-guy, eh? There's only $n items in that set, I can't pick $count items from it." | ||
| 113 | if $count > $n; | ||
| 114 | |||
| 115 | return "I choose " . join(', ', @items[ 0 .. $count-1 ]); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub choose_all { | ||
| 120 | my ($self, $message) = @_; | ||
| 121 | |||
| 122 | my @items = $self->_items($message); | ||
| 123 | |||
| 124 | $message->parameters->{count} = scalar @items; | ||
| 125 | |||
| 126 | return $self->choose_n($message); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub initialize { } | ||
| 130 | |||
| 131 | __PACKAGE__->meta->make_immutable; | ||
| 132 | |||
| 133 | __END__ |