Right... I've tweaked the code to include roll modifiers, but as I don't know php I've done it in a very clumsy way. I've also modified the output both to include the modifier but also cosmetically to something I prefer.
In the same places as before:
$_POST['message'] = preg_replace('~:dice (\d+)d(\d+),(\d+),(\d+):~eis', "rolldice('\$1', '\$2', '\$3', '\$4')", $_POST['message']);
function rolldice($dice, $sides, $plus, $minus)
{
$modifier = $plus-$minus;
$total = 0;
$temp = '(';
for ($i=0; $i < $dice; $i++)
{
$dv = rand(1, $sides);
if ($i != ($dice-1)){
$temp .= $dv . '+';
} else {
$temp .= $dv . ')' . '+' . $modifier . ': ';
}
$total = $total + $dv;
}
$total = $total + $modifier;
$temp .= 'Total = ' . $total;
if ($modifier>=0) {return '<img src="helios/images/dice.gif" border="0" alt="" />Rolling ' . $dice . 'd' . $sides . '+' . $modifier .':<br /> <div style="background-color: #3F3F3F;">' . $temp . '</div>';}
else {return '<img src="helios/images/dice.gif" border="0" alt="" />Rolling ' . $dice . 'd' . $sides . $modifier .':<br /> <div style="background-color: #3F3F3F;">' . $temp . '</div>';}
}
To call the function:
:dice 2d6,3,4:
The 3 is a positive modifer while the 4 is negative.
So to call just positive or just negative:
:dice 2d6,3,0:
:dice 2d6,0,3:
* * * * *
Is there a better way to call the rolldice function? I couldn't figure out how to get the sign to modifier to be called into the function and thus had to provision for a separate variable for the case of either a positive or negative modifier.
Additionally is there a better way to code my if statements? And what about generating the output: is there a more efficient way to achieve this?
And one final thing. I am concerned about the result of rolldice being simple text that can be modified after the post has been made, or even just being typed up without the function being called in the first place. Is there perhaps a way to tie in the unique post id with how the random numbers are generated? Or having the result stored in a database? Or making a post uneditable if rolldice is used?
Cheers!