vForums Support > Programming & Coding :: Code Requests & Support :: > Dice rolls in post

Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 13th Mar 09 at 9:52pm
I would like to be able to add dice rolls in posts, Is that possible?

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 14th Mar 09 at 6:27am
I was trying to use this code. the header said it can be posted anywhere so I was trying to get it to work here.

Code:
 
  1. <style type="text/css">
  2. .dicebg {background-color: FFFFFF;border:solid 2px #000000;}
  3. .dicefont {background-color: FFFFFF;color: 000000; font-weight:bold;}
  4. </style>
  5. <script>
  6. //Dice Rolls In Posts v1.1
  7.  
  8. //Copyright 4-23-2007 ~Wonder
  9. //May be reposted anywhere as long as this header remains in tact
  10.  
  11. //Do you want the dice to line of horizontally(true) or vertically(false)
  12. diceAlignment=true;
  13.  
  14. //Enter URL of the image you want to appear as the dice ubbc button
  15. UBBCdiceImage="http://img100.imageshack.us/img100/6118/diceicon9rx.gif";
  16.  
  17. //Enter the default # of sides
  18. defaultSides=20;
  19.  
  20. //Enable dice in preview? true or false
  21. enablePreview=false;
  22.  
  23.  
  24.  
  25. rs="";mainForm="";
  26. if(document.postForm)
  27. {
  28. mainForm=document.postForm;
  29. if(location.href.match(/action\=modifypost/)){enablePreview=true;}
  30. mainForm.color.parentNode.innerHTML+="<a href=javascript:add(\"[dice="+defaultSides+"]\",\"\")><img src=\""+UBBCdiceImage+"\" alt=\"Insert Dice Roll\" border=\"0\"></a>";
  31. mainForm.onsubmit=addRand;
  32. mainForm.message.value=mainForm.message.value.replace(/(\[rand\=\d+\])/ig,"");
  33. rs=RegExp.$1; rs=(/\[rand\=/.test(rs))?rs:"";
  34. if(location.href.match(/quote\=\d+/))
  35. {
  36. mainForm.message.value=mainForm.message.value.replace(/(\[dice\=\d+\])/ig,"");
  37. rs="";
  38. }
  39. }
  40. else if(location.href.match(/action\=display/))
  41. {
  42. ta=document.getElementsByTagName("textarea");
  43. if(ta.length>0 && ta[0].name=="message")
  44. {
  45. mainForm=ta[0].parentNode;
  46. mainForm.onsubmit=addRand;
  47. }
  48. }
  49.  
  50.  
  51.  
  52. ///////////////////////
  53. if(location.href.match(/action\=(display|pmview|recent|userrecentposts|gotopost|search|calendarview)/) || (!location.href.match(/action\=/) && document.postForm && enablePreview))
  54. {
  55. hr=document.getElementsByTagName("hr");
  56. for(i=0;i<hr.length;i++)
  57. {
  58. if(typeof(hr[i].parentNode)!="undefined" && hr[i].parentNode.tagName=="TD" && typeof(hr[i].parentNode.lastChild)!="undefined" && typeof(hr[i].parentNode.lastChild.lastChild)!="undefined" && hr[i].parentNode.lastChild.lastChild.nodeType!=1)
  59. {
  60. n=hr[i].parentNode.lastChild;
  61. rand=n.innerHTML.match(/\[rand\=\d+\]/);
  62. if(rand!=null)
  63. {
  64. n.innerHTML=n.innerHTML.replace(rand[0],"");
  65. rand=rand[0].replace(/[^\d]/g,"");
  66. dice=n.innerHTML.match(/\[dice\=\d+(\+\d+)?\]/ig);
  67. if(dice!=null)
  68. {
  69. for(k=0;k<dice.length;k++)
  70. {
  71. numb=dice[k].match(/\d+(\+\d+)?/);
  72. numb=numb[0].split("+");
  73. addon=numb.length>1?parseInt(numb[1],10):0;
  74. numb=parseInt(numb[0],10);
  75. roll=Math.round((parseFloat(rand.substring(k,k+2)+"."+rand.substring(k+2,rand.length))/100)*(numb-1))+1+addon;
  76. n.innerHTML=n.innerHTML.replace(dice[k],"<table "+(diceAlignment?"style=\"display:inline\"":"")+" border=0 cellpadding=0 cellspacing=0><tr><td><table class=dicebg cellpadding=1 cellspacing=0><tr><td><center><font class=dicefont size=\"+1\"><b>"+roll+"</b><br><font size=\"1\">"+numb+" sides"+(addon>0?"+"+addon:"")+"</font></font></center></td></tr></table></td></tr></table>  ");
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. function addRand()
  84. {
  85. mainForm.message.value=mainForm.message.value.replace(/(\[rand\=\d+\])/ig,"");
  86. if((rs.length==0 && mainForm.message.value.match(/(\[dice\=\d+(\+\d+)?\])/)) && (enablePreview==true || (enablePreview==false && mainForm.nextaction.value=="post")))
  87. {
  88. mainForm.message.value+="[rand="+(Math.random()+"").replace(/0\./,"")+(Math.random()+"").replace(/0\./,"")+(Math.random()+"").replace(/0\./,"")+( Math.random()+"").replace(/0\./,"")+"]";
  89. }
  90. else
  91. {
  92. mainForm.message.value+=rs;
  93. }
  94. disable(mainForm);
  95. }
  96. </script>
  97.  
  98.  
 

Re: Dice rolls in post - Posted By Graham (amusedtodeath) on 14th Mar 09 at 9:38am
The code you're using is for the Proboards software, they won't work on vForums. I think someone can edit the classes so that it does work, however {Smile}

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 15th Mar 09 at 8:44pm
well that makes sense, I am hoping i can get a dice working here it's the only thing keeping me from moving both my proboards sites over, cause as a whole this forum is alot better imo

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 17th Mar 09 at 1:21pm
Just checking bases here does this seem doable or is this not going to work at this time? Thank you in advance for your assistance.

Re: Dice rolls in post - Posted By Ross (admin) on 17th Mar 09 at 2:03pm
I'll look at this for you a little later tonight (unless anyone else wants to do it first {Wink} ) {Smile}

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 19th Mar 09 at 1:30pm
Any Luck Ross?

Re: Dice rolls in post - Posted By Ross (admin) on 20th Mar 09 at 12:35am
Hi. Sorry it's taken a little longer than expected {Smile}

This should work if you want to give it a go. Global or Board Footer

Code:
 
  1. <style type="text/css">
  2. .dicebg {background-color: FFFFFF;border:solid 2px #000000;}
  3. .dicefont {background-color: FFFFFF;color: 000000; font-weight:bold;}
  4. </style>
  5. <script type="text/javascript">
  6. //Dice Rolls In Posts v1.1
  7.  
  8. //Copyright 4-23-2007 ~Wonder
  9. //May be reposted anywhere as long as this header remains in tact
  10. //Modified by Ross of vForums Support for vForums compatibility
  11.  
  12. //Do you want the dice to line of horizontally(true) or vertically(false)
  13. diceAlignment=true;
  14.  
  15. //Enter URL of the image you want to appear as the dice ubbc button
  16. UBBCdiceImage="http://img100.imageshack.us/img100/6118/diceicon9rx.gif";
  17.  
  18. //Enter the default # of sides
  19. defaultSides=20;
  20.  
  21. //Enable dice in preview? true or false
  22. enablePreview=false;
  23.  
  24.  
  25. rs="";
  26. mainForm="";
  27. if(document.post_form || document.quick_reply_form) {
  28. if(document.post_form) {
  29.     mainForm=document.post_form;
  30.     if(location.href.match(/action\/modify/)){enablePreview=true;}
  31.     ubbc_holder = document.createElement('span');
  32.     get('ubbc_buttons', ID).firstChild.insertBefore(ubbc_holder, get('ubbc_buttons', ID).firstChild.lastChild);
  33. } else {
  34.     ubbc_holder = get('quick_reply_ubbc', ID).firstChild.nextSibling;
  35.     mainForm=document.quick_reply_form;
  36. }
  37. ubbc_holder.innerHTML+="<a href=javascript:add_ubbc(\"[dice="+defaultSides+"] \",\"\")><img src=\""+UBBCdiceImage+"\" alt=\"Insert Dice Roll\" border=\"0\"></a>";
  38. mainForm.onsubmit=addRand;
  39. mainForm.message.value=mainForm.message.value.replace(/(\[rand\=\d+\])/ig,"");
  40. rs=RegExp.$1; rs=(/\[rand\=/.test(rs))?rs:"";
  41. if(location.href.match(/quote\/\d+/))
  42. {
  43. mainForm.message.value=mainForm.message.value.replace(/(\[dice\=\d+\])/ig,"");
  44. rs="";
  45. }
  46. }
  47.  
  48.  
  49.  
  50. ///////////////////////
  51. if(location.href.match(/action\/(view_topic|search|recent|pm_view|post|modify|create_poll)/)) {
  52. td=document.getElementsByTagName("td");
  53. topic_summary = false;
  54. for(i=0;i<td.length;i++) {
  55. if(td.item(i).className == 'post' || topic_summary) {
  56. // Remove from message preview
  57. if(td.item(i).className == 'post' && location.href.match(/action\/(post|modify|create_poll)/) && !enablePreview) {
  58.     td.item(i).innerHTML=td.item(i).innerHTML.replace(/\[rand\=(\d|\w|<|>|\/|\s)+\]/gi,"");
  59. }
  60. rand=td.item(i).innerHTML.match(/\[rand\=(\d|\w|<|>|\/|\s)+\]/);
  61. if(rand!=null)
  62. {
  63. td.item(i).innerHTML=td.item(i).innerHTML.replace(rand[0],"");
  64. rand=rand[0].replace(/[^\d]/g,"");
  65. dice=td.item(i).innerHTML.match(/\[dice\=\d+(\+\d+)?\]/ig);
  66. if(dice!=null)
  67. {
  68. for(k=0;k<dice.length;k++)
  69. {
  70. numb=dice[k].match(/\d+(\+\d+)?/);
  71. numb=numb[0].split("+");
  72. addon=numb.length>1?parseInt(numb[1],10):0;
  73. numb=parseInt(numb[0],10);
  74. roll=Math.round((parseFloat(rand.substring(k,k+2)+"."+rand.substring(k+2,rand.length))/100)*(numb-1))+1+addon;
  75. td.item(i).innerHTML=td.item(i).innerHTML.replace(dice[k],"<table "+(diceAlignment?"style=\"display:inline\"":"")+" border=0 cellpadding=0 cellspacing=0><tr><td><table class=dicebg cellpadding=1 cellspacing=0><tr><td><center><font class=dicefont size=\"+1\"><b>"+roll+"</b><br><font size=\"1\">"+numb+" sides"+(addon>0?"+"+addon:"")+"</font></font></center></td></tr></table></td></tr></table>  ");
  76. }
  77. }
  78. }
  79. } else if(td.item(i).className == 'title1' && td.item(i).innerHTML.match(/>Topic Summary</i)) {
  80.     topic_summary = true;
  81. }
  82. }
  83. }
  84. function addRand() {
  85. mainForm.message.value=mainForm.message.value.replace(/(\[rand\=\d+\])/ig,"");
  86. if((rs.length==0 && mainForm.message.value.match(/(\[dice\=\d+(\+\d+)?\])/))) {
  87. mainForm.message.value+="[rand="+(Math.random()+"").replace(/0\./,"")+(Math.random()+"").replace(/0\./,"")+(Math.random()+"").replace(/0\./,"")+( Math.random()+"").replace(/0\./,"")+"]";
  88. } else {
  89. mainForm.message.value+=rs;
  90. }
  91. disable(mainForm);
  92. }
  93. </script>
 

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 20th Mar 09 at 3:46am
No worries I appreciate the help.

I am not getting it to work, I put it in global first, didnt work. Moved it around in a diffrent order, still did not work. So I moved it to a board footer still didnt work, no UBBC button, and tried to just inout the UBBC code [dice=20] and that did not work either. Any thoughts?



http://swlounge.vforums.co.uk/

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 20th Mar 09 at 2:25pm
I played around with it again this morning removed all other codes and it's still not working.

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 20th Mar 09 at 2:52pm
It's sorta working now, part of it was my fault I have a bad habit of copy and pasting the code direct from the box instead of using the select all key...

I still have one small problem, that is it puts the dice UBBC button up but when you press it it does not do anything, if you use the dice ubbc code [dice=20] it works fine

Re: Dice rolls in post - Posted By Ross (admin) on 20th Mar 09 at 10:48pm
I've copied the code directly from my test board onto your forum. That seems to have gotten it working. I can only presume that something changed in the code either when I pasted it here or you copied it from here. Eitehr way, its' working now {Smile}

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 20th Mar 09 at 11:02pm
Yup its working perfect thank you!!

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 9th Jun 09 at 2:48pm
It no longer works with the new WYSIWYG Beta

Re: Dice rolls in post - Posted By Ross (admin) on 9th Jun 09 at 2:51pm
Hi, I just posted this in your bugs thread not realising. The actual functionality now works, it's just the layout which will need some tweaking to reposition the button where you want it.

The (updated) code:

Code:
 
  1.  <style type="text/css">
  2. .dicebg {background-color: FFFFFF;border:solid 2px #000000;}
  3. .dicefont {background-color: FFFFFF;color: 000000; font-weight:bold;}
  4. </style>
  5. <script type="text/javascript">
  6. //Dice Rolls In Posts v1.1
  7. //Copyright 4-23-2007 ~Wonder
  8. //May be reposted anywhere as long as this header remains in tact
  9. //Modified by Ross of vForums Support for vForums compatibility
  10. //Do you want the dice to line of horizontally(true) or vertically(false)
  11. diceAlignment=true;
  12. //Enter URL of the image you want to appear as the dice ubbc button
  13. UBBCdiceImage="http://img100.imageshack.us/img100/6118/diceicon9rx.gif";
  14. //Enter the default # of sides
  15. defaultSides=20;
  16. //Enable dice in preview? true or false
  17. enablePreview=false;
  18. rs="";
  19. mainForm="";
  20. if(document.post_form || document.quick_reply_form) {
  21. if(document.post_form) {
  22. mainForm=document.post_form;
  23. if(location.href.match(/action\/modify/)){enablePreview=true;}
  24. ubbc_holder = document.createElement('span');
  25. get('ubbc_buttons', ID).appendChild(ubbc_holder);
  26. } else {
  27. ubbc_holder = get('quick_reply_ubbc', ID);
  28. mainForm=document.quick_reply_form;
  29. }
  30. ubbc_holder.innerHTML+="<a href=javascript:add_ubbc(\"[dice="+defaultSides+"]\",\"\")><img src=\""+UBBCdiceImage+"\" alt=\"Insert Dice Roll\" border=\"0\"></a>";
  31. mainForm.onsubmit=addRand;
  32. mainForm.message.value=mainForm.message.value.replace(/(\[rand\=\d+\])/ig,"");
  33. rs=RegExp. ; rs=(/\[rand\=/.test(rs))?rs:"";
  34. if(location.href.match(/quote\/\d+/))
  35. {
  36. mainForm.message.value=mainForm.message.value.replace(/(\[dice\=\d+\])/ig,"");
  37. rs="";
  38. }
  39. }
  40. ///////////////////////
  41. if(location.href.match(/action\/(view_topic|search|recent|pm_view|post|modify|create_poll)/)) {
  42. td=document.getElementsByTagName("td");
  43. topic_summary = false;
  44. for(i=0;i<td.length;i++) {
  45. if(td.item(i).className == 'post' || topic_summary) {
  46. // Remove from message preview
  47. if(td.item(i).className == 'post' && location.href.match(/action\/(post|modify|create_poll)/) && !enablePreview) {
  48. td.item(i).innerHTML=td.item(i).innerHTML.replace(/\[rand\=(\d|\w|<|>|\/|\s)+\]/gi,"");
  49. }
  50. rand=td.item(i).innerHTML.match(/\[rand\=(\d|\w|<|>|\/|\s)+\]/);
  51. if(rand!=null) {
  52. td.item(i).innerHTML=td.item(i).innerHTML.replace(rand[0],"");
  53. rand=rand[0].replace(/[^\d]/g,"");
  54. dice=td.item(i).innerHTML.match(/\[dice\=\d+(\+\d+)?\]/ig);
  55. if(dice!=null) {
  56. for(k=0;k<dice.length;k++) {
  57. numb=dice[k].match(/\d+(\+\d+)?/);
  58. numb=numb[0].split("+");
  59. addon=numb.length>1?parseInt(numb[1],10):0;
  60. numb=parseInt(numb[0],10);
  61. roll=Math.round((parseFloat(rand.substring(k,k+2)+"."+rand.substring(k+2,rand.length))/100)*(numb-1))+1+addon;
  62. td.item(i).innerHTML=td.item(i).innerHTML.replace(dice[k],"<table "+(diceAlignment?"style=\"display:inline\"":"")+" border=0 cellpadding=0 cellspacing=0><tr><td><table class=dicebg cellpadding=1 cellspacing=0><tr><td><center><font class=dicefont size=\"+1\"><b>"+roll+"</b><br><font size=\"1\">"+numb+" sides"+(addon>0?"+"+addon:"")+"</font></font></center></td></tr></table></td></tr></table>  ");
  63. }
  64. }
  65. }
  66. } else if(td.item(i).className == 'title1' && td.item(i).innerHTML.match(/>Topic Summary</i)) {
  67. topic_summary = true;
  68. }
  69. }
  70. }
  71. function addRand() {
  72. tinyMCE.triggerSave(true, true);
  73. mainForm.message.value=mainForm.message.value.replace(/(\[rand\=\d+\])/ig,"");
  74. if((rs.length==0 && mainForm.message.value.match(/(\[dice\=\d+(\+\d+)?\])/))) {
  75. mainForm.message.value+="[rand="+(Math.random()+"").replace(/0\./,"")+(Math.random()+"").replace(/0\./,"")+(Math.random()+"").replace(/0\./,"")+( Math.random()+"").replace(/0\./,"")+"]";
  76. } else {
  77. mainForm.message.value+=rs;
  78. }
  79. disable(mainForm);
  80. }
  81. </script>
 

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 9th Jun 09 at 4:46pm
I swapped out the codes and it still does not seem to be working for me.  I tried just using the dice code since there was no button and that did not show up.

Re: Dice rolls in post - Posted By Ross (admin) on 9th Jun 09 at 4:56pm
 
I swapped out the codes and it still does not seem to be working for me.  I tried just using the dice code since there was no button and that did not show up.


You shouldn't of had to touch the code. I had already modified the version on your forum. And when I left it, it was working fine.

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 9th Jun 09 at 5:04pm
It does not appear to be working, I figured the button was not showing because it need to be positioned or what not but just typing in the code for it still does not work.

Re: Dice rolls in post - Posted By Ross (admin) on 9th Jun 09 at 5:06pm
The code I had posted seemed to get broken when you pasted it into your footer (not that you should have in the first place). I have changed it on your forum - it works. I'd recommend not touching it for now.

Re: Dice rolls in post - Posted By Jedi Slayer (jedislayer) on 9th Jun 09 at 5:11pm
I wont touch it, it was not clear from your post on the last page you had swapped it over...sorry for the misunderstanding.   And thank you for your assistance.  You do bring up a good point, nearly everytime I copy a code over and paste it into my headers or footers it seems to have some issue even if it is exactly the same thing you had there.  You have had to address my codes numerous times now and I am unsure on what is the issue.  Since I have never had any problem like this before. While this is my first Vforum it is my 5th site.