[Request] Adapt mod Dice phpBB2 to phpBB3

Discuss the development of future releases of phpBB (phpBB 3.x minor releases) and MODing/Coding related questions.
Post Reply
Dakin Quelia
New member
New member
Posts: 49
Joined: 12 Nov 2008, 01:26
Real name: Daniel
Location: Belgique

[Request] Adapt mod Dice phpBB2 to phpBB3

Post by Dakin Quelia »

Hello,

I'd like to adapt this [url=http://www.phpbbhacks.com/download/4366]Dice[/url] for phpBB3. I don't want the mods distributed on phpBB.com. ;)

I did this:

In the template file bbcode.html:

Code: Select all

<!-- BEGIN dice_title_open --><blockquote><div><cite>{TITLE}:</cite><!-- END dice_title_open -->
<!-- BEGIN dice_open --><blockquote class="uncited"><div><!-- END dice_open -->
<!-- BEGIN dice_close --></div></blockquote><!-- END dice_close -->


In the includes/bbcode.php I added:

Code: Select all

case&nbsp;13:
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->bbcode_cache[$bbcode_id]&nbsp;=&nbsp;array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'str'&nbsp;=>&nbsp;array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'[/dice:$uid]'&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;$this->bbcode_tpl('dice_close',&nbsp;$bbcode_id)
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;array('{TITLE}'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;'$1'),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Â


My main problem is message_parser.php and the bbcode.php. It must be like the Quote Tag. But it doesn't work.

Thanks per advance.

See you soon,
Last edited by Dakin Quelia on 05 Nov 2009, 03:11, edited 1 time in total.
User avatar
Obsidian
Supporter
Supporter
Posts: 736
Joined: 13 May 2008, 15:20
Real name: Damian
Contact:

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Obsidian »

Uh, did you write a function for $this->bbcode_second_pass_dice()?
Dakin Quelia
New member
New member
Posts: 49
Joined: 12 Nov 2008, 01:26
Real name: Daniel
Location: Belgique

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Dakin Quelia »

No. I justly need your help. ;)
Dakin Quelia
New member
New member
Posts: 49
Joined: 12 Nov 2008, 01:26
Real name: Daniel
Location: Belgique

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Dakin Quelia »

Little up. :)

I need your help to adapt to phpBB3. My main goal is to do like Dice mod phpBB2. ;)

It's code phpBB2.

Code: Select all

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($Dice = array_shift($Dice_Rolls))
   { 
      $Die_Count = substr($Die,0,strpos($Dice,'d'));
      $Die_Type = substr($Die,strpos($Dice,'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>' . $Die_Type . '-Sided Dice Results: </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>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;
}
Dakin Quelia
New member
New member
Posts: 49
Joined: 12 Nov 2008, 01:26
Real name: Daniel
Location: Belgique

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Dakin Quelia »

I tested this code in includes/message_parser.php:

Code: Select all

   /**
   * Parse dice bbcode
   * Expects the argument to start with a tag
   */
   function bbcode_dice($in)
   {
      global $config, $user;
      
      /**
      * If you change this code, make sure the cases described within the following reports are still working:
      * [dice="test"]1d20[/dice]
      * [dice]1d20[/dice]
      */
      
      /*
      $in = str_replace("\r\n", "\n", str_replace('\"', '"', trim($in)));

      if (!$in)
      {
         return '';
      }*/

      if ((isset($in[0])) && (isset($in[1])) )
      {
         $dice_rolls = $in[0];
         $mt_seed = intval($in[1]);
         $fixed = $in[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 . ' <strong><span style="color: #AA0000">' . $user->lang['DICE_FIXED'] . '</span></strong>': $dice_rolls;
      $dice_rolls = explode(' ', trim($dice_rolls));
      
      while($dice = array_shift($dice_rolls))
      { 
         $dice_count = substr($dice,0,strpos($dice,'d'));
         $die_type = substr($dice,strpos($dice,'d')+1);
     
         if(strpos($dice_type, '+'))
         {
            $method = 1;
            $modifier = substr($dice_type,strpos($dice_type,'+')+1);
            $die_type = substr($dice_type,0,strpos($dice_type,'+'));
         }
         else if(strpos($dice_type, '-'))
         {
            $method = 2;
            $modifier = substr($dice_type,strpos($dice_type,'-')+1);
            $dice_type = substr($dice_type,0,strpos($dice_type,'-'));
         }
         else if(strpos($dice_type, '*'))
         {
            $method = 3;
            $modifier = substr($die_type,strpos($dice_type,'*')+1);
            $dice_type = substr($die_type,0,strpos($dice_type,'*'));
         }
         else if(strpos($dice_type, 'x'))
         {
            $method = 3;
            $modifier = substr($die_type,strpos($dice_type,'x')+1);
            $dice_type = substr($die_type,0,strpos($dice_type,'x'));
         }
         else if( strpos($dice_type, '/') )
         {
            $method = 4;
            $modifier = substr($dice_type,strpos($dice_type,'/')+1);
            $dice_type = substr($dice_type,0,strpos($dice_type,'/'));
         }
         else
         {
            $method = 0;
            $modifier = 0;
         }
         
         $header = '<strong>' . $user->lang['DICE_ROLL_STRING'] . '</strong>: ' . $original_roll_string . '<br /><strong>' . sprintf($user->lang['DICE_RESULTS'], $dice_type) . '</strong>';
         $footer = '';
         $dice_count = intval($dice_count);
         
         if($dice_count == 0)
         {
            $dice_count = 1;
         }
         
         $total = 0;

         if($method)
         {
            $footer = $footer . "(";
         }
         
         // Loop Limit to prevent 500000d500000 sort of dice due to max. execution time limit
         if($dice_count <= 200 && $dice_type <= 100)
         {
             for($i = 0; $i < $dice_count; $i++)
             {
               if($dice_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 != $dice_count - 1) ? $footer . $value1 . '+' . $value2 . '(' .($value1 + $value2) . '<strong></strong>), ' : $footer .  $value1 . '+' . $value2 . '(' . ($value1 + $value2) . '<strong></strong>)';
               }
               else
               {
                  $value = (integer)(((double)mt_rand()/(double)mt_getrandmax()) * $dice_type) + 1;
                  $total = $total + $value;
                  $footer = ($i != $dice_count - 1) ? $footer . $value . ', ' : $footer . $value . '';
               }
             }
         }
         // Loop limit
         else
         {
            $total = 0;
            $modifier = 0;
            $footer = $user->lang['DICE_TOO_LONG'];
         }
         
         switch($method)
         {
            case 1:
               $footer =  $footer . '<strong></strong>) + ' . $modifier . ' ';
               $total = $total + $modifier;
            break;
            case 2:
               $footer =  $footer . '<strong></strong>) - ' . $modifier . ' ';
               $total = $total - $modifier;
            break;
            case 3:
               $footer =  $footer . '<strong></strong>) * ' . $modifier . ' ';
               $total = $total * $modifier;
            break;
            case 4:
               $footer =  $footer . '<strong></strong>) / ' . $modifier . ' ';
               $total = $total / $modifier;
            break;
         }
         
         $this->parsed_items['dice']++;
         
         // I do b - /b on purpose...  kills out some smilies that crop up.
         if( ($dice_count > 1) || ($method != 0) )
         {
            $footer = $footer . ' (<strong>Total =</strong> ' . $total . '<strong></strong>)<br />';
         }
         else
         {
            $footer =  $footer . '<br />';
         }
         
         $footer = $footer. "";
         $out = $out . $header . $footer;
      }
      
      // Restore MT randomness
      mt_srand($future_seed);
      
      return $out;
   }


And includes/bbcodes.php

Code: Select all

&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Second&nbsp;parse&nbsp;dice&nbsp;tag
&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;bbcode_second_pass_dice($title,&nbsp;$dice)
&
nbsp;&nbsp;&nbsp;&nbsp;{
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;when&nbsp;using&nbsp;the&nbsp;/e&nbsp;modifier,&nbsp;preg_replace&nbsp;slashes&nbsp;double-quotes&nbsp;but&nbsp;does&nbsp;not
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;seem&nbsp;to&nbsp;slash&nbsp;anything&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$dice&nbsp;=&nbsp;str_replace('"',&nbsp;'"',&nbsp;$dice);
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$title&nbsp;=&nbsp;str_replace('"',&nbsp;'"',&nbsp;$title);

&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;remove&nbsp;newline&nbsp;at&nbsp;the&nbsp;beginning
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($dice&nbsp;==&nbsp;"\n")
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$dice&nbsp;=&nbsp;'';
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$dice&nbsp;=&nbsp;(($title)&nbsp;?&nbsp;str_replace('$1',&nbsp;$title,&nbsp;$this->bbcode_tpl('dice_title_open'))&nbsp;:&nbsp;$this->bbcode_tpl('dice_open'))&nbsp;.&nbsp;$dice;

&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$dice;
&
nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Â


It doesn't work.
User avatar
Obsidian
Supporter
Supporter
Posts: 736
Joined: 13 May 2008, 15:20
Real name: Damian
Contact:

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Obsidian »

Hmm. I couldn't say what the exact problem is, but I can say that I saw this: http://www.phpbb.com/community/viewtopi ... &t=1816135
Looks like an alpha version for a dice MOD, in phpBB3. You may want to take a look at how that author is coding it, and do a compare/contrast with your code to see where your code could be having issues.
Dakin Quelia
New member
New member
Posts: 49
Joined: 12 Nov 2008, 01:26
Real name: Daniel
Location: Belgique

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Dakin Quelia »

I saw it but I don't want this mod. This mod uses a SQL Table. The phpBB2 Dice Mod doesn't use SQL Table. ;)

I'd like adapt dice mod phpBB2 to phpBB3. I like his functionning. :D
User avatar
Obsidian
Supporter
Supporter
Posts: 736
Joined: 13 May 2008, 15:20
Real name: Damian
Contact:

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Obsidian »

I recommend that you at least open up the other author's MOD and learn how the other author integrated their code into the BBcodes system -- that's the best thing you can do here, and it allows you to see what you could be doing wrong.
Dakin Quelia
New member
New member
Posts: 49
Joined: 12 Nov 2008, 01:26
Real name: Daniel
Location: Belgique

Re: [Request] Adapt mod Dice phpBB2 to phpBB3

Post by Dakin Quelia »

I'm sorry but I didn't found MODs using the BBCode System. :?

I watched Hide BBCode. I'm searching...

-Edit by Dakin Quelia-
Reason: Finally, I decided to adapt this [url=http://www.phpbb.com/community/viewtopic.php?f=70&t=545565]Dice Mod[/url].

Here are result:
[img]http://image-heberg.fr.cr/files/1257391421323222.png[/img]

Thank you very much. ;)
Post Reply