1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Making a dice roller BBCode plugin

Discussion in 'XenForo Development Discussions' started by Deek, Nov 7, 2011.

  1. Deek

    Deek Member

    So I'm making a dice roller BBCode plugin and am running into a bit of a problem, every time the page is reloaded the value is reloaded.

    Is there a way to have the code only rendered once and not re-evaluated each time?
    PHP:
        protected $_tags;
        public function 
    getTags()
        {
            
    $this->_tags parent::getTags();
            
    $this->_tags['dice'] = array(
                
    'hasOption' => true,
                
    'optionRegex' => '/^[0-9]+?$/i',
                
    'callback' => array($this'renderTagDice')
            );
            return 
    $this->_tags;
        }
        public function 
    renderTagDice(array $tag, array $rendererStates)
        {
            
    $output "A ".$tag['option']." sided die was rolled it came up: ".rand(1,$tag['option']);
           
            return 
    $output;
        }
    I'm following the guide at http://xenforo.com/community/thread...b-code-in-xenforo-a-comprehensive-guide.6320/ and while its a great start I know I'm missing something
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    Posts are rendered on page view. The only way would be able to do this was to create a multiple option system takes an ID for the post or actual roll and store it in a database. This could be really tricky to accomplish for you.
     
  3. Deek

    Deek Member

    Is there a way to take the result after the output has been define and replace the [dice] tags with the resulting text? So when they type in the post it will look like [dice=20][/dice] and after its posted the database would only see "A 20 sided die was rolled and it came up: 19" removing the BBCode so it doesn't get generated again on reload.
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    That would require determining the post ID that its being rendered from. This also results in multiple implications:
    1. Direct post editing inside of the database
    2. Could possibly overrun your maximum post size, and would need to be handled.
    3. Not easy to modify the dice size if needed at a later date.
    4. Where to handle the dice roll & save point
    This could be a very, very difficult task. I'm not even sure if the post id is passed to the tag being rendered.
     
  5. Deek

    Deek Member

    If I could get the date of the post somehow I could always use that as a seed value for random number generation. However I think this will have the same problems that you've outlined above as far as passing information.

    Does anyone know of a way for the to extend the BbCode model to allow passing of post information to the Formatter?
     
  6. Jeremy

    Jeremy XenForo Moderator Staff Member

    You'll be modifying the post parser, not BBCode. You would want to modify the contents of the array passed to $tag. I haven't looked into a way to retrieve said information. You could be better served by creating a separate "roll" add-on where the tag option is the ID of a previous roll created via your add-on. It may not be the easiest, but it could allieviate some technical difficulties.
     
  7. Hoffi

    Hoffi Well-Known Member

    I am writing a Dice Roller with a huge amount of possibilities in the backend. I think late Nov I'll release it.
     
    SchmitzIT likes this.
  8. SchmitzIT

    SchmitzIT Well-Known Member

    Interesting. Can you reveal a bit more about it, perhaps?
     
  9. Hoffi

    Hoffi Well-Known Member

    Sure.
    The Backend is nearly done.
    What can you modify in the backend:
    • You can define own dice with a couple of options as number of sizes with values.
      eg. d20 normal 20sided die, double 10. 20sided die with two times each side 1 to 10. You can specify own die like: a seven sided die with the values 2,5,6,6,10,11,30
    • The rules, to "win" a roll can be set with PHP Callback functions. Multiple functions are delivered by default like highestValue or rollXTimesOverY.
    • You can build wiresets, that can be rolles. Here you can select 1 to all die, a winning rule and some other options.
    • you can modify the look of the die. Each die can have it's own layout in the frontend. This works with background-images. The dieresult will be text in front of the die.
    in work:
    • Icon in Editor, that opens a dialog, to create the bb-code for the roll.
    A Code to roll a die may be look like:
    [dice=wod]9,7[/dice]
    (Roll 9 10sided die with a difficulty of 7 in the set of wod. This set contiains information, that a die may explode).
    or
    [dice=sw]2*d4-d6-d10,32[/sw]
    (Roll two d4 a d6 and a d10 in the set of sw. This set contains the rule, that the sum must be 32 to win the roll).
    You can set in the options which Usergroup can roll die in wich forum and in conversations.
    the moderator right "delete roll" is implemented.
    And finally:
    • integrated into AdminSearch
    • full Phrased
    • a roll can only made by creating a post. If you edit a post, there is only a placeholder for the roll, so that it can't be edited.
     
  10. Deek

    Deek Member

    Any chance you'd be willing to share a glance of the source? We're looking to get something simply implemented soon. I'd be happy to donate to fund some caffeine purchases.
     
  11. Hoffi

    Hoffi Well-Known Member

    I will release it for free here, when it's done. ATM only the backend really works good, and I am working at the frontend.
     
  12. digitalpoint

    digitalpoint Well-Known Member

    You could do some hash trickery with unique information about the post (for example post ID) so the dice roll doesn't change on a per post basis, and you also wouldn't need to muck with storing it in the database. It WOULD make it so multiple dice rolls in a single post would yield the same result though.

    An example of what I mean could be something like this:

    PHP:
    <?php
        $postId 
    1;
        
    $sides 20;

        echo (
    hexdec(substr(md5($postId), 06)) % $sides);
    Obviously not super advanced, but if you want to be able to generate a random number on a per post basis WITHOUT needing to store the result in a backend database/table, it could be useful...
     
    Jeremy likes this.
  13. Hoffi

    Hoffi Well-Known Member

    Here some Screenshots from Admin and RollerEngine.
     

    Attached Files:

  14. Deek

    Deek Member

    Looking good. I can't wait to see the code itself! :D
     
  15. The Dark Wizard

    The Dark Wizard Well-Known Member

    Can't wait for this! We run a RP Community and would love something like this. If you need any testers let me know :D!
     
  16. Deek

    Deek Member

    Any more progress on this plugin?
     
  17. Hoffi

    Hoffi Well-Known Member

    Actually, after I haven't much time during business to develop personal things, I am nearly at an Alpha State.
     
  18. Xon

    Xon Well-Known Member

    cclaerhout likes this.

Share This Page