News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Dice roll mod ... again.

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

Previous topic - Next topic

Adin

Good question. I think Unknown would be able to answer it. He's the php grandmaster around here ;)

It's interesting what you're working out Shanks :)

shanks

#21
OK, I've learnt how to get the preg_replace to pick up the sign and modifier using the (\D*)(\d*) method and I'm pretty much done.


function rolldice($dice, $sides, $sign, $modifier)
{
$total = 0;
$temp = '(';   
if ($dice > 1)
{
for ($i=0; $i < ($dice-1); $i++)
{
$dv = rand(1, $sides);
        $temp .= $dv . '+';
$total=$total+$dv;
}
}
        $dv = rand(1, $sides);
        $temp .= $dv;
$total=$total+$dv;

if ($sign = '+')
{       
$total=$total+$modifier;
} elseif ($sign = '-') {
$total=$total-$modifier;
} else {
$temp .= '): Total = ' . $total;
{return '<img src="Themes/default/images/dice.gif" border="0" alt="" />Rolling ' . $dice . 'd' . $sides .':<br /> <div style="background-color: #919191;">' . $temp . '</div>';}
}
        $temp .= ')' . $sign . $modifier . ': Total = ' . $total;
{return '<img src="Themes/default/images/dice.gif" border="0" alt="" />Rolling ' . $dice . 'd' . $sides . $sign . $modifier .':<br /> <div style="background-color: #919191;">' . $temp . '</div>';}
}




Firstly, there seems to be a problem with my final if statements. Regardless of the actual state of $sign the first if where $sign = '+' is always taken. Can anybody point out to me why this is so?

And additionally someone mentioned earlier in this thread that the image call in the return statement should be coded with global settings. Again, help in this would be appreciated!

-shanks

shanks

Wheee! I figured it!  8)

It works!  :D

My first php code!  :o

Now all I need is to polish it off by getting those global settings for the images sorted...


shanks

#23
OK... I've figured out how to do that as well...

However, there remain some issues:

Firstly, every since switching to global $settings, this has happened. The first post is with a definite path, the second with a relative path as set by $settings.

This is the code being used:

{return '<img src="' . $settings['images_url'] . '/dice.gif" border="0" alt="" />Rolling ' . $dice . 'd' . $sides .':<br /> <div style="background-color: #919191;">' . $temp . '</div>';}

Secondly, while it is possible to have multiple rolls in the same post the order in which the rolls are made can be very important. This is with regards to a roll without any modifier...

:dice 3d6:

:dice 3d6+1:


Results in the first being rolled and the second just being output as "dice 3d6+1:"

If there were any text between the two rolls then that too would disappear.

I am guessing that it has something to do with no sign or modifier being called by the function in the case of :dice 3d6:. Because of the loss of the leading colon on the second roll I tested this by entering:

:dice 3d6::

Which doesn't change anything. Nor does adding yet another colon.

I've tried setting the $sign and $modifier variables to arbitrary values in the hope that when the function is called again it would make a difference. In light of the failure of this I can only assume that the problem lies within the preg_replace line:

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


This only happens following a roll that has no modifier.

Anybody have any ideas for either of these?

[Unknown]

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

Try that.  I don't see the problem you're naming for the difference between the two posts, though, except they're using an opaque image (no transparency) and one has &nbsp; between the <br /> and </div>.

-[Unknown]

shanks

Cheers, [Unknown]! That preg_replace works a treat!

With regards to the second issue... it's now sorted!  8)

[Unknown]

It's because it's so long, most likely.  If you remove the <br /> you won't get the blank line.

-[Unknown]

shanks

Big thanks to [Unknown] for his assistance in the final stages of the roller!

If you want to add the roller to your own forums:

In post.php

Find
function Post()

Add before

function rolldice($dice, $sides, $sign, $modifier)
{
global $settings;

$total = 0;
$temp = '(';   

if ($dice>40) {return 'Sorry, too many rolls!';}
if ($sides>100) {return 'Sorry, too many sides!';}

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

if ($sign == '+')
{       
$total=$total+$modifier;
} elseif ($sign == '-') {
$total=$total-$modifier;
} else {
$temp .= '): Total = ' . $total;
{return '<img src="' . $settings['images_url'] . '/dice.gif" alt="" />Rolling ' . $dice . 'd' . $sides .': <div style="background-color: #919191;">' . $temp . '</div>';}
}
        $temp .= ')' . $sign . $modifier . ': Total = ' . $total;
{return '<img src="' . $settings['images_url'] . '/dice.gif" alt="" />Rolling ' . $dice . 'd' . $sides . $sign . $modifier .': <div style="background-color: #919191;">' . $temp . '</div>';}
}


Find
preparsecode($_POST['message']);

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


To use the roller:

:dice [number of rolls]d[number of sides][+ or -][modifier]:

eg to roll 3d6

:dice 3d6:

eg to roll 3d6+1

:dice 3d6+1:

If you want to roll a single die then you must enter the number of rolls as 1, you can't leave it blank.

eg :dice 1d6: not :dice d6:


The roller sets the max rolls at 40 and max number of sides at 100 to stop stupid rolls!

That is all  8)

-shanks

Adin

Thank you for improving this, Shanks. And thanks again to Unknown, the lord of support ;)

Adin

Could you please check if this is ok ?

dice_roll.mod
<id>
Dice Roll Mod
</id>

<version>
1.0
</version>

<mod info>
This mod will allow you users to play one of the oldest games in the history of mankind: Roll the dice.

To use it the following tag is used: :dice [number of rolls]d[number of sides][+ or -][modifier]:

That means :dice 2d6: will roll 2 6-sided dice, :dice 2d6+1: will add the modifier of 1.

Special thanks to [Unknown] for all his help.
</mod info>

<author>
Tyris, Adin, Shanks
</author>

<homepage>
http://www.simplemachines.org/community/index.php?topic=26713.0
</homepage>

<edit file>
$sourcedir/Post.php
</edit file>

<search for>
function Post()
</search for>

<add before>
function rolldice($dice, $sides, $sign, $modifier)
{
global $settings;

$total = 0;
$temp = '(';   

if ($dice>40) {return 'Sorry, too many rolls!';}
if ($sides>100) {return 'Sorry, too many sides!';}

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

if ($sign == '+')
{       
$total=$total+$modifier;
} elseif ($sign == '-') {
$total=$total-$modifier;
} else {
$temp .= '): Total = ' . $total;
{return '<img src="' . $settings['images_url'] . '/dice.gif" alt="" />Rolling ' . $dice . 'd' . $sides .': <div style="background-color: #919191;">' . $temp . '</div>';}
}
        $temp .= ')' . $sign . $modifier . ': Total = ' . $total;
{return '<img src="' . $settings['images_url'] . '/dice.gif" alt="" />Rolling ' . $dice . 'd' . $sides . $sign . $modifier .': <div style="background-color: #919191;">' . $temp . '</div>';}
}
</add before>

<search for>
preparsecode($_POST['message']);
</search for>

<add after>
$_POST['message'] = preg_replace('~:dice (\d+)d(\d+)(\D*?)(\d*?):~ei', "rolldice('\$1', '\$2', '\$3', '\$4')", $_POST['message']);
</add after>


package-info.xml
<?xml version="1.0"?>
<package-info xmlns="http://www.simplemachines.org/xml/package-info" xmlns:smf="http://www.simplemachines.org/">
<name>Dice_Roll_Mod</name>
<id>Dice Roll Mod</id>
<type>modification</type>
<version>1.0</version>
<install>

<require-file name="dice.gif" destination="$themedir/images" />

<readme type="inline">This mod will allow you users to play one of the oldest games in the history of mankind: Roll the dice.

To use it the following tag is used: :dice [number of rolls]d[number of sides][+ or -][modifier]:

That means :dice 2d6: will roll 2 6-sided dice, :dice 2d6+1: will add the modifier of 1.

Special thanks to [Unknown] for all his help.</readme>
<modification type="file" format="boardmod">dice_roll.mod</modification>
</install>
<uninstall>
<readme type="inline">Uninstall Information
</readme>
<modification type="file" format="boardmod" reverse="true">dice_roll.mod</modification>
</uninstall>
</package-info>


Zip-File contents:

  • dice_roll.mod
  • package-info.xml
  • dice.gif

shanks


Adin


shanks

I would have no idea! I'll try it out later though.

Also, perhaps it would be a good idea to edit the first post of this thread and put this updated roller in it?

Adin

Once i'm all done with this i'll make new thread for it in the mods section.

[Unknown]

Sorry.... it looks fine, but I'd use "adin:dice_roll_mod" or similar for the id.

-[Unknown]

Legatinho

Would be difficult to modify this rolling system and include a Storyteller based rolling system? To the ones that don´t know this system (Vampire The Masquerade, Mage, Exalted, etc) it uses only d10 to rolls, and don´t sum the numbers. Instead, it counts success (all dice you get 7 or over counts as a success). I´ve never seen a board with support to this system yet, would be really good to people who like to play RPG have something like this!  :D

Regards,
Carlos

shanks

#36
So... an amount of d10s are rolled and any that results in a 7 or higher is counted as a success and a total amount of successes is given?

That is:

Roll 5d10
Results: 1,3,5,7,9
Successes = 2

And are there ever any cases for there being modifiers?

Sounds simple enough. I'll be happy to do this for you, just drop me a PM to make sure that I have the rolling method correct. :)

-shanks

shanks

#37
Quote from: Legatinho on April 24, 2005, 10:10:12 AM
Would be difficult to modify this rolling system and include a Storyteller based rolling system?

In post.php, find:
function Post()

Add before:
function roll($rolls)
{
$successes=0;
$temp = 'Results = ';

if (rolls>40) {return 'Sorry, too many rolls!';}

if ($rolls>1)
{
for ($i=0; $i<($rolls-1); $i++)
{
$result=rand(1,10);
$temp.=$result.', ';
if ($result>=7) $successes=$successes+1;
}
}
$result=rand(1,10);
$temp.=$result;
if ($result>=7) $successes=$successes+1;

return 'Rolling '.$rolls.'d10<br><div style="background-color: #919191;">'.$temp.'<br>'.'<b>Successes = '.$successes.'</b></div>';
}


In post.php, find:
preparsecode($_POST['message']);

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

To use this roller to get successes for x amount of rolls, enter:

:roll x:

ie, to roll 4 dice and count successes enter:

:roll 4:

If a post containing a roll is then editted, the roll's output will be altered allowing for a GM to make sure that a success total is not editted.

If you would like to change the colour of the background for the results find <div style="background-color: #919191;"> and change the hex value as normal.

:)

Owdy

Former Lead Support Specialist

Tarvitsetko apua SMF foorumisi kanssa? Otan työtehtäviä vastaan, lue:http://www.simplemachines.org/community/index.php?topic=375918.0

shanks

#39
It rolls 10-sided dice and counts how many rolls are of a value of 7 or higher which are then counted as successes.

This is, I am told, how the Storyteller RPG system works its dice.

Advertisement: