https://www.icyphoenix.com/viewtopic.php?f=28&t=723 ----------------------------------- Xusqui Mon 18 Dec, 2006 22:01 Help on Dice BBCode! ----------------------------------- Hi, neighbours and friends... I'm trying to make a new bbcode for Role Playing Games and whatever a random number generation could be for. I used to use it for making contests in my web. I've made this: open includes/bbcode.php Find (for instances) [code] 'fade' => array( 'nested' => true, 'inurl' => true, 'allow_empty' => false, ),[/code] Right after add [code] 'dice' => array( 'nested' => false, 'inurl' => false, 'allow_empty' => false, ),[/code] Find [code]// --- Disable Active Content Switch BEGIN ---[/code] Right before add [code] // BBcode dice, by Roland at ka-tet-corp.com. Based upon MOD-dice by >achary Aerogos (tribalgenesis at hotmail.com) if($tag === 'dice') { $Die = $content; $Expressions = explode('d', $content); if( (isset($Expressions[0])) && (isset($Expressions[1])) ) { $dice_Rolls = $Expressions[0]; $MT_Seed = (double)microtime()*1000000;//intval($Expressions[1]); } $Die_Count = $Expressions[0]; $Die_Type = $Expressions[1]; // Make sure we restore the MT gen to a random state after we are done... $Future_Seed = mt_rand(); mt_srand( $MT_Seed ); $Original_Roll_String = '(' . $content . ') Fixed'; $Die_Rolls = explode(' ', trim($dice_Rolls)); $header = 'Sort of roll: ' . $Die_Count . ' dices of ' . $Die_Type . ' sides ' . $Original_Roll_String . '
' . $Die_Type . '-Result: '; $footer = ''; $Die_Count = intval($Die_Count); if( $Die_Count == 0 ) $Die_Count = 1; $total = 0; // Loop Limit to prevent 500000d500000 sort of dices due to max. execution time limit if($Die_Count <= 200 && $Die_Type <= 100) { for( $i = 0; $i < $Die_Count; $i++ ) { if( $Die_Type == 100 ) { $value1 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10) * 10; $value2 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10); $total = $total + ($value1 + $value2); $footer = ($i != $Die_Count - 1) ? $footer . $value1 . '+' . $value2 . '(' .($value1 + $value2) . '), ' : $footer . $value1 . '+' . $value2 . '(' . ($value1 + $value2) . ')'; } else { $value = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * $Die_Type) + 1; $total = $total + $value; $footer = ($i != $Die_Count - 1) ? $footer . $value . ', ' : $footer . $value . ''; } } } // Loop limit else { $total = 0; $footer = 'Too many dices and/or too many sides'; } // I do b - /b on purpose... kills out some smilies that crop up. if( ($Die_Count > 1)) { $footer= $footer . ' (Total = ' . $total . ')
'; } else { $footer = $footer . '
'; } $footer = $footer. ""; $results = $header . $footer; // Restore MT randomness mt_srand($Future_Seed); //list($usec,$sec)=explode(" ",microtime()); mt_srand((double)microtime()*1000000);//$sec * $usec); // Loop to fix randomness problem of results when multiple die rolls are made in a single post - Hades mt_rand(); return array( 'valid' => true, 'html' => $results ); }[/code] To use it, you write: [code][dice]xdyy[/dice][/code] x means number of dices to roll yy means number of sides of each dice i.e [code][dice]2d10[/dice][/code] To roll 2 dices of 10 sides. But now comes the problem. Because of this line: [code]$MT_Seed = (double)microtime()*1000000;[/code] Everytime the post is reloaded, the result changes. I fixed it using precompiled posts, so the message is saved as is and the dices are not rolled every time the post is reloaded. But I would like to know what should I do to avoid the precompiled posts. I've tried this [code]$MT_Seed = $uid;[/code] to use a random number that won't change everytime the post is loaded, but, as $uid is null, I always get the same "Random" number. I mean, If I roll 100 times a dice like 1d10 in 100 different posts, I always get the same number... That's not too random, isn't it? Any idea is welcome! Thanks in advance!! ----------------------------------- Mighty Gorgon Sun 24 Dec, 2006 20:50 Re: Help on Dice BBCode! ----------------------------------- I think that the only way to do it is inserting the number in the DB when the post is submitted. But then you have to modify the posting function... :roll: ----------------------------------- Xusqui Tue 09 Jan, 2007 11:57 Re: Help on Dice BBCode! ----------------------------------- That would be a solution, but I don't want to mess up with the DB... There must be another solution, as the original mod dosen't make any modification in the DB and works... If any otherone is interested, I can send him/her the original BBCode to see if we find a solution... Thnx, MG!! ----------------------------------- Mighty Gorgon Wed 10 Jan, 2007 00:47 Re: Help on Dice BBCode! ----------------------------------- Attach the mod here... maybe I'll try to do it sooner or later... :lol: ----------------------------------- Intacto Sat 27 Oct, 2007 18:34 Re: Help On Dice BBCode! ----------------------------------- Looks like there's a very similar post right here..... http://www.icyphoenix.it/viewtopic.php?p=6422#p6422 Does anyone care for it? Excluding me, of course, since I'm not a programmer..... Ah, almost forgot: I'm zio_gilmor :wink: ----------------------------------- Mighty Gorgon Thu 01 Nov, 2007 22:05 Re: Help on Dice BBCode! ----------------------------------- [quote user="Intacto" post="20834"]Looks like there's a very similar post right here..... http://www.icyphoenix.it/viewtopic.php?p=6422#p6422 Does anyone care for it? Excluding me, of course, since I'm not a programmer..... Ah, almost forgot: I'm zio_gilmor :wink:[/quote] You are still keeping busy Raphael10 with this... plus I already told you something in the Italian support. Please do not cross post the same request and be patient. ----------------------------------- Yuri Menzl Tue 27 Nov, 2007 02:13 Re: Help On Dice BBCode! ----------------------------------- I finally fix the bug that always give the same result at rolled dices and add an increment value that can be +, -, * or /. I remakes the layout too. Find [code]# // BBcode dice, by Roland at ka-tet-corp.com. Based upon MOD-dice by >achary Aerogos (tribalgenesis at otmail.com) if($tag === 'dice') { $Die = $content; $Expressions = explode('d', $content); if( (isset($Expressions[0])) && (isset($Expressions[1])) ) { $dice_Rolls = $Expressions[0]; $MT_Seed = (double)microtime()*1000000;//intval($Expressions[1]); } $Die_Count = $Expressions[0]; $Die_Type = $Expressions[1]; // Make sure we restore the MT gen to a random state after we are done... $Future_Seed = mt_rand(); mt_srand( $MT_Seed ); $Original_Roll_String = '(' . $content . ') Fixed'; $Die_Rolls = explode(' ', trim($dice_Rolls)); $header = 'Sort of roll: ' . $Die_Count . ' dices of ' . $Die_Type . ' sides ' . $Original_Roll_String . '
' . $Die_Type . '-Result: '; $footer = ''; $Die_Count = intval($Die_Count); if( $Die_Count == 0 ) $Die_Count = 1; $total = 0; // Loop Limit to prevent 500000d500000 sort of dices due to max. execution time limit if($Die_Count <= 200 && $Die_Type <= 100) { for( $i = 0; $i < $Die_Count; $i++ ) { if( $Die_Type == 100 ) { $value1 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10) * 10; $value2 = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * 10); $total = $total + ($value1 + $value2); $footer = ($i != $Die_Count - 1) ? $footer . $value1 . '+' . $value2 . '(' .($value1 + $value2) . '), ' : $footer . $value1 . '+' . $value2 . '(' . ($value1 + $value2) . ')'; } else { $value = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * $Die_Type) + 1; $total = $total + $value; $footer = ($i != $Die_Count - 1) ? $footer . $value . ', ' : $footer . $value . ''; } } } // Loop limit else { $total = 0; $footer = 'Too many dices and/or too many sides'; } // I do b - /b on purpose... kills out some smilies that crop up. if( ($Die_Count > 1)) { $footer= $footer . ' (Total = ' . $total . ')
'; } else { $footer = $footer . '
'; } $footer = $footer. ""; $results = $header . $footer; // Restore MT randomness mt_srand($Future_Seed); //list($usec,$sec)=explode(" ",microtime()); mt_srand((double)microtime()*1000000);//$sec * $usec); // Loop to fix randomness problem of results when multiple die rolls are made in a single post - Hades mt_rand(); return array( 'valid' => true, 'html' => $results ); }[/code] Replace [code]// BBcode dice, by Roland at ka-tet-corp.com. Based upon MOD-dice by >achary Aerogos (tribalgenesis at hotmail.com)fixed by Yuri Menzl Celaschi if($tag === 'dice') { $Die = $content; $Expressions = explode('d', $content); if( (isset($Expressions[0])) && (isset($Expressions[1])) ) { $Dice_Rolls = $Expressions[0]; $MT_Seed = intval($Expressions[1]);//(double)microtime()*1000000; doesn't work as it causes the rolls to be re-rolled every time the topic/post is re-loaded// $Fixed = $Expressions[2]; } else { return; } $Die_Count = $Expressions[0]; $Die_Type = $Expressions[1]; /*// Make sure we restore the MT gen to a random state after we are done... $Future_Seed = mt_rand(); mt_srand( $MT_Seed ); */ $Original_Roll_String = '(' . $content . ')'; $Die_Rolls = explode(' ', trim($Dice_Rolls)); $Die_Count = substr($Die,0,strpos($Die,'d')); $Die_Type = substr($Die,strpos($Die,'d')+1); if( strpos($Die_Type, '+') ) { $Method = 1; $Modifier = substr($Die_Type,strpos($Die_Type,'+')+1); $Die_Type = substr($Die_Type,0,strpos($Die_Type,'+')); } else if( strpos($Die_Type, '-') ) { $Method = 2; $Modifier = substr($Die_Type,strpos($Die_Type,'-')+1); $Die_Type = substr($Die_Type,0,strpos($Die_Type,'-')); } else if( strpos($Die_Type, '*') ) { $Method = 3; $Modifier = substr($Die_Type,strpos($Die_Type,'*')+1); $Die_Type = substr($Die_Type,0,strpos($Die_Type,'*')); } else if( strpos($Die_Type, 'x') ) { $Method = 3; $Modifier = substr($Die_Type,strpos($Die_Type,'x')+1); $Die_Type = substr($Die_Type,0,strpos($Die_Type,'x')); } else if( strpos($Die_Type, '/') ) { $Method = 4; $Modifier = substr($Die_Type,strpos($Die_Type,'/')+1); $Die_Type = substr($Die_Type,0,strpos($Die_Type,'/')); } else { $Method = 0; $Modifier = 0; } $header = '
Dice rolled
You rolled ' . $Die_Count . ' dices of ' . $Die_Type . ' sides ' . $Original_Roll_String . '
Partial Result: '; $footer = ''; $Die_Count = intval($Die_Count); if( $Die_Count == 0 ) $Die_Count = 1; $total = 0; // Loop Limit to prevent 500000d500000 sort of dice due to max. execution time limit if($Die_Count <= 200 && $Die_Type <= 100) { for( $i = 0; $i < $Die_Count; $i++ ) { if( $Die_Type == 100 ) { $value1 = mt_rand(1,9) * 10; $value2 = mt_rand(1,10); $total = $total + ($value1 + $value2); $footer = ($i != $Die_Count - 1) ? $footer . $total . ' + ' : $footer . ($value1 + $value2) . '
'; } else { $value = mt_rand(1,$Die_Type); $total = $total + $value; $footer = ($i != $Die_Count - 1) ? $footer . $value . ' + ' : $footer . $value . ''; } } } // Loop limit else { $total = 0; $Modifier = 0; $footer = 'Too many dice and/or too many sides'; } switch( $Method ) { case 1: $footer = $footer . ' + ' . $Modifier . '
'; $total = $total + $Modifier; break; case 2: $footer = $footer . ' - ' . $Modifier . '
'; $total = $total - $Modifier; break; case 3: $footer = $footer . ' * ' . $Modifier . '
'; $total = $total * $Modifier; break; case 4: $footer = $footer . ' / ' . $Modifier . '
'; $total = $total / $Modifier; break; } // I do b - /b on purpose... kills out some smilies that crop up. if( ($Die_Count > 1) || ($Method != 0) ) { $footer= $footer . ' Total = ' . $total . '
-*-*-'; } else { $footer = $footer . '
-*-*-'; } $footer = $footer. ""; $results = $results . $header . $footer . '
'; // Restore MT randomness mt_srand($Future_Seed); //list($usec,$sec)=explode(" ",microtime()); mt_srand((double)microtime()*1000000);//$sec * $usec); // Loop to fix randomness problem of results when multiple die rolls are made in a single post - Hades mt_rand(); return array( 'valid' => true, 'html' => $results ); }[/code] Result of code [dice]2d6+3[/code] [center][b]Dice Rolled[/b] You rolled 2 dices of 6 sides [b](2d6+3)[/b] [b]Partial Result:[/b] 6 + 4 [color=green]+ 3[/color] [b]Total:[/b] 13[/center] Hope it's help ;) ----------------------------------- Mighty Gorgon Wed 28 Nov, 2007 10:42 Re: Help on Dice BBCode! ----------------------------------- Thanks for that. I've implemented a random number generator in latest RC version, but the value it is not stored in the DB... I will hopefully do it in the future... You can test it on this site. This will generate a random number between 1 and 100. [codeblock][random=100][/random][/codeblock] [random=100][/random] ----------------------------------- Intacto Sat 29 Dec, 2007 20:56 Re: Help On Dice BBCode! ----------------------------------- [quote user="Yuri Menzl" post="21841"]I finally fix the bug that always give the same result at rolled dices and add an increment value that can be +, -, * or /. I remakes the layout too.[/quote] WOW! This is a GREAT JOB!! :thanks: I just ask you a big help... Would you be so kind by packing it all in a file with some instructions so that I can try to implement the MOD in my site? I realize I'm asking a lot but it would help me a lot! [img]http://www.icyphoenix.com/images/smiles/jump.gif[/img] ----------------------------------- Intacto Wed 02 Jan, 2008 17:32 Re: Help On Dice BBCode! ----------------------------------- Done by myself!! I repeat: [b][i][rainbow]A GREAT JOB!!! THANKS AGAIN!!![/rainbow][/i][/b] :thanks: ----------------------------------- altamir Fri 03 Feb, 2012 12:01 Re: Help On Dice BBCode! ----------------------------------- Hey guys! I'm making a rpg forum with IP plataform and I need the dice bbcode... But, every time the topic page is reloaded the dice results change. Somebody can help me with this? Thanks for the help and sorry about the bad english... I'm brazilian. ----------------------------------- TheSteffen Thu 09 Feb, 2012 12:08 Re: Help on Dice BBCode! ----------------------------------- What version of IcyPhoenix are you using?