38 lines
997 B
Bash
Executable File
38 lines
997 B
Bash
Executable File
[ -f functions.sh ] && source functions.sh
|
|
|
|
roll_dice() {
|
|
local count=${1%[dD]*}
|
|
local sides=${1#*[dD]}
|
|
local i
|
|
local total=0
|
|
for i in $(seq $count) ; do
|
|
local die=$(($RANDOM % $sides + 1))
|
|
total=$((total + $die))
|
|
if [[ $i -le 10 ]]; then
|
|
if [[ $i -lt $count ]]; then
|
|
echo -n "${die}, "
|
|
else
|
|
if [[ $count -gt 1 ]]; then
|
|
echo -n "and ${die} "
|
|
else
|
|
echo -n "${die} "
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
if [[ $((count - 10)) -gt 1 ]]; then
|
|
echo -n "and $((count - 10)) more dice "
|
|
fi
|
|
if [[ $((count - 10)) -eq 1 ]]; then
|
|
echo -n "and 1 more die "
|
|
fi
|
|
echo "for a total of ${total}."
|
|
}
|
|
|
|
rollString="${3##* }"
|
|
if ! [[ "$rollString" =~ ^[1-9][0-9]*[dD][1-9][0-9]*$ ]]; then
|
|
msg "$2" "${1}: Usage is roll #d# where # is greater than 0."
|
|
else
|
|
msg "$2" "$(roll_dice ${rollString})"
|
|
fi
|