News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Dice roll mod ... again.

Started by Adin, February 02, 2005, 08:49:07 AM

Previous topic - Next topic

Adin

I once got great help here in making a dice roll mod for yabbse, which still works great, but i can't get it to work with smf.

Here's the code snips from the post.php

First the function
function rolldice($start,$dice,$sides,$end)
{
   $total = 0;
   $temp = '';
if(is_numeric($dice) && is_numeric($sides)) {
   for($i=0; $i < $dice; $i++)
   {
      $dv=rand(1,$sides);
      $temp .= 'roll ' . ($i + 1) . ': ' . $dv . ', ';
      $total = $total + $dv;
   }
   $temp .= 'total: ' . $total;
}
$temp = $start . "<img src=\"YaBBImages/dice.jpg\" border=\"0\">Rolling $dice $sides -sided dice:<br> <div style=\"background-color: #3F3F3F\">" . $temp . "</div>" . $end;
return $temp;
}


Then the part to replace the tag with the actual roll
$form_message = preg_replace('~(.*):dice roll (.*)d(.*):(.*)~eis', "rolldice('\$1','\$2','\$3','\$4')", $form_message);


The problem might be in placing all this correctly in the post php, or smf using new tags i don't know. Can somebody help, please ?

Adin

The one who helped me with this (great help, really) was Tyris if i remember correctly.

So ... could somebody help ? Pretty please ?

[Unknown]

#2
Find:

$context['subject'] = addcslashes($form_subject, '"');

Add your code above that.

-[Unknown]

Adin


Adin

No, it still does not work.

Is the preg replace right at all ? I don't know how it's handeled in SMF ...

[Unknown]

Oh duh, sorry that would do it on quote.  But, why are you dealing with $form_subject otherwise - it worked the same way in YaBB SE, more or less.

Find, Post.php:

preparsecode($_POST['message']);

Add after:
$_POST['message'] = preg_replace('~:dice roll (\d+)d(\d+):~eis', "rolldice('\$1', '\$2')", $_POST['message']);

function rolldice($dice, $sides)
{
$total = 0;
$temp = '';

for ($i=0; $i < $dice; $i++)
{
$dv = rand(1, $sides);
$temp .= 'roll ' . ($i + 1) . ': ' . $dv . ', ';
$total = $total + $dv;
}
$temp .= 'total: ' . $total;

return '<img src="YaBBImages/dice.jpg" border="0" alt="" />Rolling ' . $dice . ' ' . $sides . '-sided dice:<br /> <div style="background-color: #3F3F3F;">' . $temp . '</div>';
}


-[Unknown]

Adin

Thank Unkown ! You're da man :D

Although the function has to be added before 'function post()' to make it work.

Big thanks again for that eye opener with the replacement.

shanks

#7
Hey, excellent! I'll try this right now!

And cheers, Adin, for pointing us this way  :D

Additionally...

How do I use this?! I've tried moving the function around as per the above post but I must be getting the tage code that goes inside the post wrong  :-[

Adin

In post.php find:

function Post()

Add before:

function rolldice($dice, $sides)
{
       $total = 0;
      $temp = '';

      for ($i=0; $i < $dice; $i++)
   {
                $dv = rand(1, $sides);
             $temp .= 'roll ' . ($i + 1) . ': ' . $dv . ', ';
         $total = $total + $dv;
    }
        $temp .= 'total: ' . $total;

   return '<img src="yourthemedir/images/dice.gif" border="0" alt="" />Rolling ' . $dice . ' ' . $sides . '-sided dice:<br /> <div style="background-color: #3F3F3F;">' . $temp . '</div>';
}


Please observe that you'll have to change yourthemedir to the directory of your theme and also you'll have to find a nice picture for it. Mine has a black bg, so i could only offer it to you if your forum has a black bg, too. But i could perhaps make a picture with a transparent bg. Also, you'll probably have to change the colors of the font bg (<div style="background-color: #3F3F3F;">)

Find:

preparsecode($_POST['message']);

Add after:

$_POST['message'] = preg_replace('~:dice roll (\d+)d(\d+):~eis', "rolldice('\$1', '\$2')", $_POST['message']);

It should work, at least it does on my forum (signature).

To try it, post this on your forum:

:dice roll 2d6:

The first number represents the number of dice, the second represents the side. So 1d100 would be one 100-sided dice, 2d6 would be 2 6-sided dice.

Please let me know if this works.

shanks


Adin

I should probably make a package for this ...

[Unknown]

You'll want to global $settings and use $settings['images_url']...

-[Unknown]

Adin

Quote from: [Unknown] on February 04, 2005, 05:34:46 PM
You'll want to global $settings and use $settings['images_url']...

-[Unknown]

Don't worry i'll keep that in mind :) Thanks again for you help [Unknown] !

shanks

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!

Adin

The roll returns a css style that normally can not be done by members except by using the quote tag (which of course would be too obvious). Also, if they try to edit their post to change the numbers, the result will be transformed to html and therefor be shown as raw code, which is also obvious.

Adin

At least that is how it was in YabbSE, i haven't tested it on SMF yet. I was pleased enough to see it working ;)

shanks

Ah, indeed! Very good!  :D

Just need to get the modifier bit sorted out now 8)

Adin

Let me see it once you're finished.

shanks


shanks

#19
Right, time to pick on your brains here people!

I am working on the way that the dice roller is called. Currently the method below requires a user to enter the amount of rolls, the number of sides to the die, any positive modifier and any negative modifier. eg 2d6,3,0 for a 2d6+3 roll.

$_POST['message'] = preg_replace('~:dice (\d+)d(\d+),(\d+),(\d+):~eis', "rolldice('\$1', '\$2', '\$3', '\$4')", $_POST['message']);


An improvement to this is:

$_POST['message'] = preg_replace('~:dice (\d+)d(\d+)(\D+)(\d+):~eis', "rolldice('\$1', '\$2', '\$3', '\$4')", $_POST['message']);

The (\D+)(\d+) in this case picks up the sign and the modifier so to roll 2d6+3 you enter 2d6+3 instead of 2d6,3,0.

The only problem here is that if no modifier and sign is entered the code doesn't work. Toll roll 2d6 you need to enter 2d6+0.

How do I get preg_replace to ignore (\D+)(\d+) if nothing is entered?

* * * * *

Or is there a way for me to pick up a string variable as opposed to multiple character and integer vars?

This method would be preferable as I'd then be able to hand the string to the rolldice function and break it down into the roll/rolls there.

Advertisement: