Dice rolls with modifications

163 views
Skip to first unread message

Michael McDowell

unread,
May 29, 2015, 5:29:20 PM5/29/15
to ajax...@googlegroups.com
Hello all!

I am interested in using AJAX chat along with a SMF to host an on-line RPG campaign. I like how the chat works, and covers all my needs but one. I need to add the ability to allow for modifications to dice rolls, such as the "/roll 2d6+5" command or "/roll 3d8-2". Now the script already handles dice rolls well, but what would I need to add (and where) to get those types of rolls?

I am no programmer, but I know my way around code well enough to find and add the required code.

Any help would greatly appreciated.

Doc gWorldz

unread,
May 30, 2015, 12:30:32 AM5/30/15
to ajax...@googlegroups.com
This is the only dice mod I have ever known that does what you want but after rolls the result doesn't show for the person that rolls until they send another message but otherwise functions as expected. I lost the original mod however and this is an untested code pull from a private GM roll mod I wrote so its actually untested.

CHAT/lib/AJAXChat.php

Replace current function with this one ...
      function insertParsedMessageRoll($textParts) {
        if(count($textParts) == 1) {
           
// default is one d20:
            $number
= 1;
           
// need to set $parts to an empty array for the rest of this
            $dicestring
= "20";
       
} else {
           
// use lowercase, remove any user entered spaces and find a 'd'
            $diceParts
= explode('d', strtolower($textParts[1]).str_replace(' ', ''));
           
if(count($diceParts) == 1) {
               
// no 'd' was specified, so assume 1 die
                $number
= 1;
                $dicestring
= $diceParts[0];                
           
} else if(count($diceParts) == 2) {
               
// note that the $number could still be blank/zero
                $number
= (int)$diceParts[0];
                $dicestring
= $diceParts[1];
           
} else {
               
// if dice syntax is invalid, roll one d20:
               
// this culd be multiple 'd's but we can't deal with that
                $number
= 1;
                $dicestring
= "20";
           
}
       
}
       
// Dice number must be a positive integer, else roll only one:
        $number
= ($number > 0) ?  $number : 1;
   
       
// still need to parse sides and the modifiers
        $dicestring
= str_replace('+', ' + ', $dicestring);
        $dicestring
= str_replace('-', ' - ', $dicestring);
        $dicestring
= str_replace('x', ' x ', $dicestring);
        $dicestring
= str_replace('*', ' x ', $dicestring);
        $dicestring
= str_replace('/', ' / ', $dicestring);
       
// explode the whole thing to create tokens
        $parts
= explode(' ', $dicestring);
       
       
// Sides must be a positive integer , else use 20:
        $sides
= (int)$parts[0];
        $sides
= ($sides > 0) ?  $sides : 20;
       
// now roll the dice, and total them up        
        $text
= '/roll '.$this->getUserName().' '.$number.'d'.$sides.' (';
        $total
= 0;
       
for($i=0; $i<$number; $i++) {
            $roll
= $this->rollDice($sides);            
           
if($i != 0)
                $text
.= ',';
            $text
.= $roll;
            $total
+= $roll;
       
}
        $text
.= ')';
       
// add modifiers explicitly to $text
       
// modify the value of $total
       
// the rest of $parts should be modifier tokens - could check for the right number
       
// now apply all the modifiers to the roll, in the order the user specified them
       
for ($i = 1; $i < count($parts); $i+=2) {
           
// the value needs to be an integer
            $value
= (int)$parts[$i+1];
            $v
= $parts[$i+1];
           
if ("$value" != "$v") {
               
// if the value being used is not an integer, use 1?
                $value
= 1;            
           
}
           
// the token needs to be one of the operators - otherwise skip
            $token
= $parts[$i];
           
switch ($token) {
           
case '+':
               
// add
                $total
+= $value;
               
break;
           
case '-':
               
// subtract
                $total
-= $value;
               
// make minimum 1 - remove this like to allow 0 and lower
               
if ($total<1) $total=1;
               
break;
           
case 'x':
               
// multiply
                $total
*= $value;
               
break;
           
case '/':
               
// divide - round up so 1d6/3 will be the same as 1d2
                $total
= ceil($total/$value);
               
break;
           
default:
               
// ignore any modifier that we don't recognize
               
break;
           
}
           
// add the modifier to the display string
            $text
.= $token.$value;
       
}
        $text
.= '='.$total;
        // send the text generated to the channel
        $this->insertChatBotMessage(
           $this->getChannel(),
           $text

        );
     );

To fix the send issue for the person that does the roll Frug and others always suggested I split the function into a couple parts where one does the roll the other does the math from the roll but I never got around to correcting the issue. Good luck and if you happen to fix it, it would be nice to finally solve this issue :)

The GM Private Roll mod can be found at http://gworldz.com/cxc/AJAXChatMods/  if you are interested in it as well.

We usually change the case to match /d as well as /roll farther up the code also but that's not necessary.
// Rolling dice:
case '/d':
case '/roll':
$this->insertParsedMessageRoll($textParts);
break;

This mod is based on the Advanced Dice Mod originally shared by Thom Bartold if I recall correctly, searching for him might turn up his post somewhere on the web if you need it but I am lazy o.O

Doc gWorldz

unread,
May 30, 2015, 12:59:17 AM5/30/15
to ajax...@googlegroups.com
I managed to find my version of a 0.8.7 tested mod in another subdirectory http://gworldz.com/cxc/AJAXChatSalvage/ its the Advanced Dice Roller mod.

http://gworldz.com/cxc/AJAXChatSalvage/AJAXChat%20-%20Advanced%20Dice%20Roller%20Mod.html


Michael McDowell

unread,
May 30, 2015, 9:55:53 PM5/30/15
to ajax...@googlegroups.com


On Saturday, May 30, 2015 at 12:59:17 AM UTC-4, Doc gWorldz wrote:
I managed to find my version of a 0.8.7 tested mod in another subdirectory http://gworldz.com/cxc/AJAXChatSalvage/ its the Advanced Dice Roller mod.

http://gworldz.com/cxc/AJAXChatSalvage/AJAXChat%20-%20Advanced%20Dice%20Roller%20Mod.html



Thank you so much for the information! I have edited the chat as outlined in your mod, but I am having trouble getting the chat to work on my server for some reason, it won't run the "install.php" script. Ugh.

The error message is "Parse error: syntax error, unexpected '$config' (T_VARIABLE) in /path/to/home/forum/chat/install.php on line 29". Well, I went to line 29 and checked it, that's the database name. Well, it's right... so now I have to trouble shoot that. I might contact my webhost support, I've been having problems connection to via FTP all day too! Ugh!

Michael McDowell

unread,
May 30, 2015, 10:29:56 PM5/30/15
to ajax...@googlegroups.com
I figured it out, I forgot to enclose the database information with single quotes (') when I entered the information, as you replace the default values that are simply "null".

After re-uploading the config file, that chat installed fine and runs as expected. I am happy to report that the dice mod works as well. I used the "/d" mod in the chat script as advised, and I like that allot, a bit less typing involved now. :)

Thanks again Doc, you have been good as gold.

Markis Cook

unread,
Jun 3, 2015, 2:17:00 PM6/3/15
to ajax...@googlegroups.com
Hey, I'm quite new here and all, but as a Information Security specialist, I cannot recommend using the code as you have currently posted it.  No one would be able to steal your information, per say, but one could easily denial of service your website using this dice roller.  I just did it to my own server to test it out.  Basically you'd want to add limitations to this mod.

I did the following to DoS my server with this dice rolling mod:

/roll 200000d200000*200000

Now, that's not 'too bad' but if you had an individual that was 'spamming that' it would make it pretty difficult to even kick him.  You'd most likely have to remove the mod in order to ban the user and prevent him from continuing to do so.  I managed to limit it by modifying the code to not accept values over 100d100 which does help quite a bit.  I'll post it below.

// ~cXc~ Advanced Dice Roller

   
function insertParsedMessageRoll($textParts) {
       
if(count($textParts) == 1) {
           
// default is one d20:
            $number
= 1;
           
// need to set $parts to an empty array for the rest of this
            $dicestring
= "20";
       
} else {
           
// use lowercase, remove any user entered spaces and find a 'd'
            $diceParts
= explode('d', strtolower($textParts[1]).str_replace(' ', ''));
           
if(count($diceParts) == 1) {
               
// no 'd' was specified, so assume 1 die
                $number
= 1;


               
// Users limited from rolling more than 100 dice if input exceeds 100, user rolls 1 instead
                $number
= ($number > 0 && $number <= 100) ?  $number : 1;


                $dicestring
= $diceParts[0];                
           
} else if(count($diceParts) == 2) {
               
// note that the $number could still be blank/zero
                $number
= (int)$diceParts[0];


                               
// Users limited from rolling more than 100 dice if input exceeds 100, user rolls 1 instead
                $number
= ($number > 0 && $number <= 100) ?  $number : 1;


                $dicestring
= $diceParts[1];
           
} else {
               
// if dice syntax is invalid, roll one d20:
               
// this culd be multiple 'd's but we can't deal with that
                $number
= 1;


               
// Users limited from rolling more than 100 dice if input exceeds 100, user rolls 1 instead
                $number
= ($number > 0 && $number <= 100) ?  $number : 1;


                $dicestring
= "20";
           
}
       
}
       
// Dice number must be a positive integer, else roll only one:
        $number
= ($number > 0) ?  $number : 1;
   
       
// still need to parse sides and the modifiers
        $dicestring
= str_replace('+', ' + ', $dicestring);
        $dicestring
= str_replace('-', ' - ', $dicestring);
        $dicestring
= str_replace('x', ' x ', $dicestring);
        $dicestring
= str_replace('*', ' x ', $dicestring);
        $dicestring
= str_replace('/', ' / ', $dicestring);
       
// explode the whole thing to create tokens
        $parts
= explode(' ', $dicestring);
       
       
// Sides must be a positive integer , else use 20:
        $sides
= (int)$parts[0];
        $sides
= ($sides > 0) ?  $sides : 20;


       
// If input for number of sides exceeds 100 roll a 20 sided die instead
        $sides
= ($sides > 0 && $sides <= 100) ?  $sides : 20;
// ~cXc~ Advanced Dice Roller


That should do you, install that exactly as explained in the previous version and it should function exactly the same.  The only difference being that this one limits user input to a maximum of a 100d100.  Much safer to have at the hands of a community.

Michael McDowell

unread,
Jun 14, 2015, 11:40:09 PM6/14/15
to ajax...@googlegroups.com
That's some good advice, thank you. Will do this.
Reply all
Reply to author
Forward
0 new messages