vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Javascript dice roller in bbcode (https://vborg.vbsupport.ru/showthread.php?t=322729)

kerrghann 05-26-2016 06:20 PM

Javascript dice roller in bbcode
 
I'm attempting to make a javascript based dice roller inside of a bbcode.

Issue is that I'd like it to be [roll]{param1}d{param2}[/roll] and I'm pretty sure that isn't possible.

Am I barking up the wrong tree trying to do it this way? And if so, does anyone have advice on a better way to do this?

MarkFL 05-26-2016 06:32 PM

Have you looked at:

Dice Roller [BBCode] ?

kerrghann 05-26-2016 07:26 PM

I did. Oddly enough I can't even get it to install on 4.2.3

That it doesn't even make the database changes in threads and posts like it's suppose to, it just kind of stops after you upload it.

MarkFL 05-26-2016 09:18 PM

What kind of behavior are you looking for exactly? Are you looking for something static (the dice are "rolled" and the values stored in the database upon submitting the post), and remains fixed thereafter, or something that rolls the dice each time the page is loaded, with different results, or even something that can be clicked (an unlimited number of times) once the page is loaded?

kerrghann 05-26-2016 10:19 PM

I'm looking for something static, for sure.

MarkFL 05-26-2016 11:05 PM

Quote:

Originally Posted by kerrghann (Post 2571368)
I'm looking for something static, for sure.

In order for it to be static, you could use PHP, and parse the post either on preview or submitting the post (whichever happens first) to change pre-defined text into the dice roll before the post is saved to the db. You could choose to have the parsing only take place when the post is submitted, and skip the preview, allowing the tags or whatever code you define to be a dice roll remain until the post is actually submitted. My choice would be to parse the post in both places.

I do something similar in a couple of my products.

Let me know the parameters you want (number of dice, number of faces on each die, etc.) and I will see what I can come up with. :)

kerrghann 05-27-2016 12:02 AM

I'd really appreciate it. I was thinking of allowing something like xdy+z where x is the number of dice being thrown, y is the number of sides on the dice, and z is the modifier being applied to the dice. So if a member where to use [roll]3d10+2[/roll] it would roll three ten sided dice and add 2 to whatever the total was.

I'd appreciate any help I can get, but I really don't want to make you go out of your way: I do appreciate it though.

MarkFL 05-27-2016 12:10 AM

Quote:

Originally Posted by kerrghann (Post 2571371)
I'd really appreciate it. I was thinking of allowing something like xdy+z where x is the number of dice being thrown, y is the number of sides on the dice, and z is the modifier being applied to the dice. So if a member where to use [roll]3d10+2[/roll] it would roll three ten sided dice and add 2 to whatever the total was.

I'd appreciate any help I can get, but I really don't want to make you go out of your way: I do appreciate it though.

So, you would only need to display the sum of the faces and the modifier? To use you example, suppose a user posted [roll]3d10+2[/roll] and one die was 4, the second 7 and the third 2, then 15 would be displayed? Or would you want to see the number of faces, the individual dice, the modifier and the sum all neatly displayed in a box or tabular format?

kerrghann 05-27-2016 12:18 AM

Well, having them all in a nice box as 4,7,2 (+2) = 15 would be nice, having any number show up would be ideal. To be honest, I'd take whatever you feel would be best in this situation.

MarkFL 05-28-2016 01:46 PM

Okay, create a plugin hooked at "newpost_process" with the "Plugin PHP Code":

PHP Code:

$text $post['message'];
preg_match_all('/\[roll\](.*?)\[\/roll\]/i'$text$match);

foreach (
$match[0] AS $roll)
{
    
$row1 '';
    
$row2 '';
    
$rolltext strtolower(preg_replace('/\[roll\](.*?)\[\/roll\]/''$1'$roll));
    
$rt_parse explode('d'$rolltext);
    
$dice intval($rt_parse[0]);
    
preg_match('~[^0-9]~i'$rt_parse[1], $signposPREG_OFFSET_CAPTURE);
    
$sign substr($rt_parse[1], $signpos[0][1], 1);
    
$fm_parse explode($sign$rt_parse[1]);
    
$faces intval($fm_parse[0]);

    if (
$sign == '+')
    {
        
$mod intval($fm_parse[1]);
    }
    elseif (
$sign == '-')
    {
        
$mod = -intval($fm_parse[1]);
    }

    
$dsum 0;

    for (
$m 0$m $dice$m++)
    {
        
$d mt_rand(1$faces);
        
$dsum += $d;
        
$dnum $m 1;
        
$row1 .= '[TD] Die ' $dnum ' [/TD]';
        
$row2 .= '[TD]' $d '[/TD]';
    }

    
$dsum += $mod;
    
$row1 '[TR]' $row1 '[TD]Modifier[/TD][TD]Sum[/TD][/TR]';
    
$row2 '[TR]' $row2 '[TD]' $mod '[/TD][TD]' $dsum '[/TD][/TR]';
    
$replace '[TABLE="class: grid"]' $row1 $row2 '[/TABLE]';
    
$text str_replace($roll$replace$text);
}

$post['message'] = $text



All times are GMT. The time now is 03:52 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.01016 seconds
  • Memory Usage 1,757KB
  • 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_php_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete