* 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_DICEmaster
parent
394cf054f3
commit
011c78e854
|
@ -11,6 +11,9 @@ triggers start => "roll", "throw";
|
||||||
zci answer_type => "dice_roll";
|
zci answer_type => "dice_roll";
|
||||||
zci is_cached => 0;
|
zci is_cached => 0;
|
||||||
|
|
||||||
|
use constant MAX_NUM_OF_DICE => 10;
|
||||||
|
use constant MAX_NUM_OF_FACES => 31;
|
||||||
|
|
||||||
my %utf8_dice = (
|
my %utf8_dice = (
|
||||||
1 => "\x{2680}",
|
1 => "\x{2680}",
|
||||||
2 => "\x{2681}",
|
2 => "\x{2681}",
|
||||||
|
@ -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_dice = set_num_dice($1, 2); # set number of dice, default 2
|
||||||
my $number_of_faces = 6; # number of utf8_dice
|
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;
|
return;
|
||||||
}
|
}
|
||||||
for (1 .. $number_of_dice) { # for all rolls
|
for (1 .. $number_of_dice) { # for all rolls
|
||||||
|
@ -112,12 +115,15 @@ handle remainder_lc => sub {
|
||||||
# 'w' is the German form of 'd'
|
# 'w' is the German form of 'd'
|
||||||
my (@rolls, $output);
|
my (@rolls, $output);
|
||||||
my $number_of_dice = set_num_dice($1, 1); # set number of dice, default 1
|
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
|
# 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
|
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
|
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 -
|
my $max = my $sum = 0; # set max roll and sum to -
|
||||||
for (1 .. $number_of_dice) { # for each die
|
for (1 .. $number_of_dice) { # for each die
|
||||||
my $roll = roll_die( $number_of_faces ); # roll the die
|
my $roll = roll_die( $number_of_faces ); # roll the die
|
||||||
|
|
30
t/Dice.t
30
t/Dice.t
|
@ -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(
|
"roll seven dices" => test_zci(
|
||||||
re(qr/., ., ., ., .$/),
|
re(qr/., ., ., ., .$/),
|
||||||
structured_answer => {
|
structured_answer => {
|
||||||
|
@ -147,6 +123,12 @@ ddg_goodie_test(
|
||||||
# Invalid numeric words
|
# Invalid numeric words
|
||||||
"roll foo dice" => undef,
|
"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(
|
"throw 1d20" => test_zci(
|
||||||
re(qr/^\d{1,2}$/),
|
re(qr/^\d{1,2}$/),
|
||||||
structured_answer => {
|
structured_answer => {
|
||||||
|
|
Loading…
Reference in New Issue