vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Modification Requests/Questions (Unpaid) (https://vborg.vbsupport.ru/forumdisplay.php?f=112)
-   -   Dice roller from phpBB (https://vborg.vbsupport.ru/showthread.php?t=143246)

solinarius 03-26-2007 07:39 PM

Dice roller from phpBB
 
Hello.
Is someone able to change code for vbulletin?
link to mod http://www.phpbb.com/community/viewt...it=dice+bbcode
but link in that topic is dead, so here is code from package:

Code:

#
#-----[ OPEN ]------------------------------------------
#
includes/bbcode.php

#
#-----[ FIND ]------------------------------------------
#

  $bbcode_tpl['email'] = str_replace('{EMAIL}', '\\1', $bbcode_tpl['email']);

#
#-----[ AFTER, ADD ]------------------------------------------
#

  // --------------------------------
  // DICE MOD CODE
  // --------------------------------
   
  $bbcode_tpl['dice_open2'] = str_replace('{ROLLED}', $lang['Dice_Mod_Rolled'] . " - \\1", $bbcode_tpl['dice_open']);
  $bbcode_tpl['dice_open'] = str_replace('{ROLLED}', $lang['Dice_Mod_Rolled'], $bbcode_tpl['dice_open']);
   
  // --------------------------------
  // END DICE MOD CODE
  // --------------------------------

#
#-----[ FIND ]------------------------------------------
#
 
  $replacements[] = $bbcode_tpl['email'];


#
#-----[ AFTER, ADD ]------------------------------------------
#

  // --------------------------------
  // BEGIN DICE MOD CODE
  // --------------------------------
   
  // [dice] tags for rolling dice
  $text = bbencode_second_pass_pda($text, "/\[dice:$uid\]/is", "[/dice:$uid]", 'replace_dice_results', $bbcode_tpl['dice_open'], $bbcode_tpl['dice_close'] );
  $text = preg_replace("/\[dice:$uid=(?:\"?([^\"]*)\"?)\]/si",  $bbcode_tpl['dice_open2'] . "[dice:$uid=\"\\1\"]", $text);
  $text = bbencode_second_pass_pda($text, "/\[dice:$uid=(\\\".*\\\")\]/is", "[/dice:$uid]", 'replace_dice_results', '', $bbcode_tpl['dice_close']);
   
  // --------------------------------
  // END DICE MOD CODE
  // --------------------------------

#
#-----[ FIND ]------------------------------------------
#
function bbencode_first_pass($text, $uid)
{

#
#-----[ BEFORE, ADD ]------------------------------------------
#

// --------------------------------
// BEGIN DICE MOD CODE
// --------------------------------

function replace_dice_results($text, $uid)
{
  global $lang;
  // Take the desired Die string ($text) and generate values for each roll.

  $Expressions = explode('=', $text);
  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;
  }

  // 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 = (isset($Fixed)) ? $Dice_Rolls . ' <b><span style="color: #AA0000">' . $lang['Dice_Mod_Fixed'] . '</span></b>': $Dice_Rolls;
  $Die_Rolls = explode(' ', trim($Dice_Rolls));

  while( $Die = array_shift($Die_Rolls))
  {
      $footer = '';
      $header = '';
      $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 = '<b>' . $lang['Dice_Mod_Roll_String'] . '</b>: ' . $Original_Roll_String . '<br /><b>' . $lang['Dice_Results'] . $Die_Type . $lang['Dice_Sides'] . '</b>';
      $footer = '';
      $Die_Count = intval($Die_Count);
      if( $Die_Count == 0 ) $Die_Count = 1;
      $total = 0;

      if( $Method )
      {
        $footer = $footer . "(";
      }

      // 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 = (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) . '<b></b>), ' : $footer .  $value1 . '+' . $value2 . '(' . ($value1 + $value2) . '<b></b>)';
            }
            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;
        $Modifier = 0;
        $footer = 'Too many dice and/or too many sides';
      }

      switch( $Method )
      {
      case 1:
        $footer =  $footer . '<b></b>) + ' . $Modifier . ' ';
        $total = $total + $Modifier;
        break;
      case 2:
        $footer =  $footer . '<b></b>) - ' . $Modifier . ' ';
        $total = $total - $Modifier;
        break;
      case 3:
        $footer =  $footer . '<b></b>) * ' . $Modifier . ' ';
        $total = $total * $Modifier;
        break;
      case 4:
        $footer =  $footer . '<b></b>) / ' . $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 . ' (<b>' . $lang[Dice_Total] . '</b>' . $total . '<b></b>)<BR>';
      }
      else
      {
        $footer =  $footer . '<BR>';
      }
      $footer = $footer. "";
      $results = $results . $header . $footer;
  }
  // Restore MT randomness
  mt_srand($Future_Seed);

  return $results;
}

// --------------------------------
// END DICE MOD CODE
// --------------------------------

#
#-----[ FIND ]------------------------------------------
# NOTE: The original line is much longer, do NOT paste the next code in this line.

  $text = preg_replace("#\[img\]((http|ftp|https|ftps)://)

#
#-----[ AFTER, ADD ]------------------------------------------
#

  // --------------------------------
  // BEGIN DICE MOD CODE
  // --------------------------------
   
  $occurances = preg_match_all("#\[dice\]([-\s\w+/*]*?)\[/dice\]#i", $text, $temp);
   
  //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
  for($i=0;$i<=$occurances;$i++)
  {
  $dice_seed = mt_rand();
  $text = preg_replace("#\[dice\]([-\s\w+/*]*?)\[/dice\]#i", "[dice:$uid]\\1 = $dice_seed [/dice:$uid] $limit", $text, 1);
  $dice_seed = mt_rand();
  $text = preg_replace("#\[dice=(\\\\\".*?\\\\\")\]([-\s\w+/*]*?)\[/dice\]#i", "[dice:$uid=\\1]\\2 = $dice_seed [/dice:$uid] $limit", $text, 1);
  }
  $text = preg_replace("#\[dice\]([-\s\w+/*]*?)=([\s\d]*?)\[/dice\]#i", "[dice:$uid]\\1 = \\2 = Fixed[/dice:$uid]", $text);
  $text = preg_replace("#\[dice\]([-\s\w+/*]*?)=([\s\d]*?)=\s*?Fixed\s*?\[/dice\]#i", "[dice:$uid]\\1 = \\2 = Fixed[/dice:$uid]", $text);
  $text = preg_replace("#\[dice=(\\\\\".*?\\\\\")\]([-\s\w+/*]*?)=([\s\d]*?)\[/dice\]#i", "[dice:$uid=\\1]\\2 = \\3 = Fixed[/dice:$uid]", $text);
  $text = preg_replace("#\[dice=(\\\\\".*?\\\\\")\]([-\s\w+/*]*?)=([\s\d]*?)=\s*?Fixed\s*?\[/dice\]#i", "[dice:$uid=\\1]\\2 = \\3 = Fixed[/dice:$uid]", $text);
  // --------------------------------
  // END DICE MOD CODE
  // --------------------------------

#
#-----[ FIND ]------------------------------------------
#

function bbencode_second_pass_code($text, $uid, $bbcode_tpl)

#
#-----[ BEFORE, ADD ]------------------------------------------
#

// --------------------------------
// BEGIN DICE MOD CODE
// --------------------------------
function bbencode_second_pass_pda($text, $open_tag, $close_tag,$func,$open_template, $close_template)
{
  $open_tag_count = 0;

  $close_tag_length = strlen($close_tag);

  $use_function_pointer = ($func && ($func != ''));

  $stack = array();
  if (is_array($open_tag))
  {
      if (0 == count($open_tag))
      {
        // No opening tags to match, so return.
        return $text;
      }
      $open_tag_count = count($open_tag);
  }
  else
  {
      // only one opening tag. make it into a 1-element array.
      $open_tag_temp = $open_tag;
      $open_tag = array();
      $open_tag[0] = $open_tag_temp;
      $open_tag_count = 1;
  }
   
  $open_is_regexp = true;
   
  // Start at the 2nd char of the string, looking for opening tags.
  $curr_pos = 1;
  while ($curr_pos && ($curr_pos < strlen($text)))
  {
      $curr_pos = strpos($text, "[", $curr_pos);

      // If not found, $curr_pos will be 0, and the loop will end.
      if ($curr_pos)
      {
        // We found a [. It starts at $curr_pos.
        // check if it's a starting or ending tag.
        $found_start = false;
        $which_start_tag = "";
        $start_tag_index = -1;
        for ($i = 0; $i < $open_tag_count; $i++)
        {
            // Grab everything until the first "]"...
            $possible_start = substr($text, $curr_pos, strpos($text, "]", $curr_pos + 1) - $curr_pos + 1);

            //
            // We're going to try and catch usernames with "[' characters.
            //
            if( preg_match('/\[quote\=\\\\"/si', $possible_start) && !preg_match('/\[quote=\\\\"[^"]*\\\\"\]/si', $possible_start) )
            {
              //
              // OK we are in a quote tag that probably contains a ] bracket.
              // Grab a bit more of the string to hopefully get all of it..
              //
              $possible_start = substr($text, $curr_pos, strpos($text, "\"]", $curr_pos + 1) - $curr_pos + 2);
            }
            //
            // Now compare, either using regexp or not.
           
            $match_result = array();
            // PREG regexp comparison.
            if (preg_match($open_tag[$i], $possible_start, $match_result))
            {
              $found_start = true;
              $which_start_tag = $match_result[0];
              $start_tag_index = $i;
              break;
            }
        }

        if ($found_start)
        {
            // We have an opening tag.
            // Push its position, the text we matched, and its index in the open_tag array on to the stack, and then keep going to the right.
            $match = array("pos" => $curr_pos, "tag" => $which_start_tag, "index" => $start_tag_index);
            bbcode_array_push($stack, $match);
            ++$curr_pos;
        }
        else
        {
            // check for a closing tag..
            $possible_end = substr($text, $curr_pos, $close_tag_length);
            if (0 == strcasecmp($close_tag, $possible_end))
            {
              // We have an ending tag.
              // Check if we've already found a matching starting tag.
              if (sizeof($stack) > 0)
              {
                  // There exists a starting tag.
                  $curr_nesting_depth = sizeof($stack);
                  // We need to do 2 replacements now.
                  $match = bbcode_array_pop($stack);
                  $start_index = $match['pos'];
                  $start_tag = $match['tag'];
                  $start_length = strlen($start_tag);
                  $start_tag_index = $match['index'];

                  // everything before the opening tag.
                  $before_start_tag = substr($text, 0, $start_index);

                  // everything after the opening tag, but before the closing tag.
                  $between_tags = substr($text, $start_index + $start_length, $curr_pos - $start_index - $start_length);

                  // Run the given function on the text between the tags..
                  if ($use_function_pointer)
                  {
                    $between_tags = $func($between_tags, $uid);
                  }

                  // everything after the closing tag.
                  $after_end_tag = substr($text, $curr_pos + $close_tag_length);

                  // Mark the lowest nesting level if needed.
                  $text = $before_start_tag . $open_template . $between_tags . $close_template . $after_end_tag;

                  // Now.. we've screwed up the indices by changing the length of the string.
                  // So, if there's anything in the stack, we want to resume searching just after it.
                  // otherwise, we go back to the start.
                  if (sizeof($stack) > 0)
                  {
                    $match = bbcode_array_pop($stack);
                    $curr_pos = $match['pos'];
                    bbcode_array_push($stack, $match);
                    ++$curr_pos;
                  }
                  else
                  {
                    $curr_pos = 1;
                  }
              }
              else
              {
                  // No matching start tag found. Increment pos, keep going.
                  ++$curr_pos;
              }
            }
            else
            {
              // No starting tag or ending tag.. Increment pos, keep looping.,
              ++$curr_pos;
            }
        }
      }
  } // while

  return $text;

}
// --------------------------------
// END DICE MOD CODE
// --------------------------------

#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/bbcode.tpl

#
#-----[ FIND ]------------------------------------------
#

<!-- BEGIN email --><a href="mailto:{EMAIL}">{EMAIL}</A><!-- END email -->

#
#-----[ AFTER, ADD ]------------------------------------------
#

<!-- BEGIN dice_open -->
<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center">
<tr>
    <td><span class="genmed"><b>{ROLLED}:</b></span></td>
  </tr>
  <tr>
    <td class="quote"><!-- END dice_open -->

<!-- BEGIN dice_close --></td>
  </tr>
</table>
<span class="postbody"><!-- END dice_close -->

Thanx!


All times are GMT. The time now is 01:44 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01623 seconds
  • Memory Usage 1,804KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (1)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete