Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Dice: Fixes #3989 Set upper limit on max dice rolls and number of sid…
Browse files Browse the repository at this point in the history
…es per dice (#4078)

* Dice: Fixes #3989 Set upper limit on max dice rolls and number of sides per dice

* Minor correction on naming of constant: MAX_NUM_OF_DICE
  • Loading branch information
lernae authored and moollaza committed Apr 10, 2017
1 parent 394cf05 commit 011c78e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
12 changes: 9 additions & 3 deletions lib/DDG/Goodie/Dice.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ triggers start => "roll", "throw";
zci answer_type => "dice_roll";
zci is_cached => 0;

use constant MAX_NUM_OF_DICE => 10;
use constant MAX_NUM_OF_FACES => 31;

my %utf8_dice = (
1 => "\x{2680}",
2 => "\x{2681}",
Expand Down Expand Up @@ -90,7 +93,7 @@ handle remainder_lc => sub {
my $number_of_dice = set_num_dice($1, 2); # set number of dice, default 2
my $number_of_faces = 6; # number of utf8_dice

if ($number_of_dice !~ /^\d+$/) {
if ($number_of_dice !~ /^\d+$/ || $number_of_dice > MAX_NUM_OF_DICE) {
return;
}
for (1 .. $number_of_dice) { # for all rolls
Expand All @@ -112,12 +115,15 @@ handle remainder_lc => sub {
# 'w' is the German form of 'd'
my (@rolls, $output);
my $number_of_dice = set_num_dice($1, 1); # set number of dice, default 1
# check that input is not greater than or equal to 99
# check that input is not greater than MAX_NUM_OF_DICE
# check that input is not 0. ex. 'roll 0d3' should not return a value
if( $number_of_dice >= 100 or $1 eq '0'){
if( $number_of_dice > MAX_NUM_OF_DICE or $1 eq '0'){
return; # do not continue if conditions not met
}
my $min = my $number_of_faces = $2; # set min and number_of_faces to max possible roll
if($number_of_faces > MAX_NUM_OF_FACES) {
return;
}
my $max = my $sum = 0; # set max roll and sum to -
for (1 .. $number_of_dice) { # for each die
my $roll = roll_die( $number_of_faces ); # roll the die
Expand Down
30 changes: 6 additions & 24 deletions t/Dice.t
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,6 @@ ddg_goodie_test(
}
}
),
"roll twenty five dice" => test_zci(
re(qr/., ., ., ., .$/),
structured_answer => {
data => ignore(),
templates => {
group => 'text',
options => {
subtitle_content => 'DDH.dice.subtitle_content'
}
}
}
),
"roll fifty-four dice" => test_zci(
re(qr/., ., ., ., .$/),
structured_answer => {
data => ignore(),
templates => {
group => 'text',
options => {
subtitle_content => 'DDH.dice.subtitle_content'
}
}
}
),
"roll seven dices" => test_zci(
re(qr/., ., ., ., .$/),
structured_answer => {
Expand All @@ -147,6 +123,12 @@ ddg_goodie_test(
# Invalid numeric words
"roll foo dice" => undef,

# Out of range number of dice or number of face values
"roll 11d3" => undef,
"roll 2d32" => undef,
"roll twenty five dice" => undef,
"roll fifty-four dice" => undef,

"throw 1d20" => test_zci(
re(qr/^\d{1,2}$/),
structured_answer => {
Expand Down

0 comments on commit 011c78e

Please sign in to comment.