Advertisement:

Author Topic: Dice roll mod ... again.  (Read 131992 times)

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Dice roll mod ... again.
« on: February 02, 2005, 08:49:07 AM »
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
Code: [Select]
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
Code: [Select]
$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 ?

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #1 on: February 02, 2005, 06:32:58 PM »
The one who helped me with this (great help, really) was Tyris if i remember correctly.

So ... could somebody help ? Pretty please ?

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Dice roll mod ... again.
« Reply #2 on: February 03, 2005, 01:45:42 AM »
Find:

Code: [Select]
$context['subject'] = addcslashes($form_subject, '"');
Add your code above that.

-[Unknown]
« Last Edit: February 03, 2005, 02:49:08 PM by [Unknown] »

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #3 on: February 03, 2005, 11:29:54 AM »
I'll try that.

Thanks, Unknown.

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #4 on: February 03, 2005, 11:39:50 AM »
No, it still does not work.

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

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Dice roll mod ... again.
« Reply #5 on: February 03, 2005, 02:55:13 PM »
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:

Code: [Select]
preparsecode($_POST['message']);
Add after:
Code: [Select]
$_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]

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #6 on: February 03, 2005, 03:37:12 PM »
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.

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #7 on: February 04, 2005, 06:19:16 AM »
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  :-[
« Last Edit: February 04, 2005, 08:20:41 AM by shanks »

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #8 on: February 04, 2005, 10:46:43 AM »
In post.php find:

Code: [Select]
function Post()
Add before:

Code: [Select]
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:

Code: [Select]
preparsecode($_POST['message']);
Add after:

Code: [Select]
$_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:

Code: [Select]
: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.

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #9 on: February 04, 2005, 04:19:42 PM »
Brilliant! It works!  8)

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #10 on: February 04, 2005, 05:31:19 PM »
I should probably make a package for this ...

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Dice roll mod ... again.
« Reply #11 on: February 04, 2005, 05:34:46 PM »
You'll want to global $settings and use $settings['images_url']...

-[Unknown]

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #12 on: February 04, 2005, 07:09:18 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] !

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #13 on: February 06, 2005, 09:28:25 AM »
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:

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

Code: [Select]
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!

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #14 on: February 06, 2005, 01:36:07 PM »
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.

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #15 on: February 06, 2005, 01:37:01 PM »
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 ;)

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #16 on: February 06, 2005, 03:18:21 PM »
Ah, indeed! Very good!  :D

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

Offline Adin

  • Jr. Member
  • **
  • Posts: 282
  • Gender: Male
    • SLICE-Design
Re: Dice roll mod ... again.
« Reply #17 on: February 06, 2005, 04:13:15 PM »
Let me see it once you're finished.

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #18 on: February 06, 2005, 04:25:00 PM »
Will do!

Offline shanks

  • Semi-Newbie
  • *
  • Posts: 88
Re: Dice roll mod ... again.
« Reply #19 on: February 14, 2005, 07:00:16 AM »
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.

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

An improvement to this is:

Code: [Select]
$_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.
« Last Edit: February 14, 2005, 01:32:52 PM by shanks »