Dice Roller Comcode?

Post

Posted
Rating:
#3977 (In Topic #788)
Avatar
Standard member
mythus is in the usergroup ‘Community saint’
Hello,

I've been struggling to try to make a custom comcode that would well, roll dice. Basically I wanted to do something like

Code


[roll]1d12[/roll]

which would give me some random number from such a dice roll. I've tried to find similar bbcode dice rollers out there to have something to go by since comcode isn't too distant from bbcode (or is it), but I've had no luck.

Anyone have any ideas?


Like ocPortal? Want to thank Chris and gang somehow? Then help out in the chat room! It really needs your help! Just open it in a tab everytime you open your web browser, and when you hear a "ding", check it out!
 
"Those who want help should first be willing to give help."
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by mythus)
#3978
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
{$SET_RAND,1,2,3,4,5,6}


Become a fan of Composr on Facebook or add me as a friend. Add me on on Twitter. Follow me on Minds (where I am most active). Support me on Patreon

Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Online now: No Back to the top

Post

Posted
Rating:
#3981
Avatar
Standard member
mythus is in the usergroup ‘Community saint’
Thanks for the quick reply Chris. You really need a "buy me a beer" button lol.

Curious, this would obviously work for a simple d6 dice roll. Is there any way to make it more user defined? Lets say from my previous 1d12 example, it would take the number in front of the "d" to know how many dice to roll and the number after the "d" to know how many numbers to randomize.

EDIT: Testing the code out, there's one problem. It changes in the posts it was used in each time the page is refreshed.

Last edit: by mythus



Like ocPortal? Want to thank Chris and gang somehow? Then help out in the chat room! It really needs your help! Just open it in a tab everytime you open your web browser, and when you hear a "ding", check it out!
 
"Those who want help should first be willing to give help."
Online now: No Back to the top

Post

Posted
Rating:
#3984
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
Ok, that's pretty specific, would need some custom code. Could probably be done with a lot of Tempcode, but would probably be easier done via a custom miniblock.


Become a fan of Composr on Facebook or add me as a friend. Add me on on Twitter. Follow me on Minds (where I am most active). Support me on Patreon

Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Online now: No Back to the top

Post

Posted
Rating:
#3988
Avatar
Standard member
mythus is in the usergroup ‘Community saint’
Ah I kind of thought that might be the case. That's ok, I can use what you gave me to develop a simple d100 which would cover 90% of the dice rolling needs for my community. Anything beyond that well, dice rollers exist all over the place.

I just need to figure out a way to stop the rolled results from changing in the post after the page refreshes. Any hints Chris, or anyone else for that matter?


Like ocPortal? Want to thank Chris and gang somehow? Then help out in the chat room! It really needs your help! Just open it in a tab everytime you open your web browser, and when you hear a "ding", check it out!
 
"Those who want help should first be willing to give help."
Online now: No Back to the top

Post

Posted
Rating:
#3989
Avatar
Standard member
mythus is in the usergroup ‘Community saint’
Made this screenshot so you can see what I mean. Was gonna just edit my last post and add it, but saw someone read it and didn't want the screenshot missed.

Screenshot_20180217_234822.png


Like ocPortal? Want to thank Chris and gang somehow? Then help out in the chat room! It really needs your help! Just open it in a tab everytime you open your web browser, and when you hear a "ding", check it out!
 
"Those who want help should first be willing to give help."
Online now: No Back to the top

Post

Posted
Rating:
#4003
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
I could write some Tempcode that would give a unique value per-URL, by using the URL as a key to store a generated value against. But the shoutbox case is difficult, again I think would need custom code, or some super-contextual hack where the template is putting in the Timestamp to a Tempcode GET/SET variable and then the Tempcode using that. I wouldn't recommend, would be very messy.

Edit: Actually I'd struggle to even do against the URL, I can't see a way to save data from Tempcode.


Become a fan of Composr on Facebook or add me as a friend. Add me on on Twitter. Follow me on Minds (where I am most active). Support me on Patreon

Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Online now: No Back to the top

Post

Posted
Rating:
#4016
Avatar
Standard member
mythus is in the usergroup ‘Community saint’
Ah this is disappointing indeed. It is a shame that the number keeps regenerating in other posts/shouts when the screen refreshes. Seems like wasted resources IMO. I guess I won't be able to use comcode to have a simple dice roller.


Like ocPortal? Want to thank Chris and gang somehow? Then help out in the chat room! It really needs your help! Just open it in a tab everytime you open your web browser, and when you hear a "ding", check it out!
 
"Those who want help should first be willing to give help."
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by mythus)
#4057
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
Just because I like you ;)

First I had to make a code change to allow Custom Comcode tag hooks with PHP code (previously it had to only be Tempcode). This will be in the next patch release:
Changes to allow more dynamic Custom Comcode tag hooks · ocproducts/composr@09ea2cf · GitHub

Then here's a Custom Comcode tag hook…
sources_custom/hooks/systems/comcode/roller.php:

Code (PHP)

<?php

class Hook_comcode_roller
{
    public function get_tag()
    {
        return array(
            'tag_title' => 'Dice Roller',
            'tag_description' => 'Produce a random dice roll.',
            'tag_example' => '[roller="some_id"]1,2,3,4,5,6[/roller]',
            'tag_tag' => 'roller',
            'tag_replace' => array($this, 'tag_replace'),
            'tag_parameters' => 'param',
            'tag_block_tag' => 0,
            'tag_textual_tag' => 0,
            'tag_dangerous_tag' => 0,
        );
    }

    public function tag_replace($embed, $map)
    {
        $id = empty($map['param']) ? get_page_name() : $map['param'];
        $_embed = $embed->evaluate();

        $cache_bucket_id = fix_id('roller_' . $id . '_' . $_embed);

        $cached = get_value($cache_bucket_id, null, true);
        if ($cached !== null) {
            return make_string_tempcode(escape_html($cached));
        }

        $matches = array();
        if (preg_match('#^(\d+)d(\d+)$#', $_embed, $matches) == 0) {
            // Pick from a set style...

            if (!empty($_embed)) {
                $rand_options = array_map('trim', explode(',', $_embed));
            } else {
                $rand_options = array('1', '2', '3', '4', '5', '6');
            }

            shuffle($rand_options);

            $output = $rand_options[0];
        } else {
            // xdy format (where x is number of rolls and y is number of dice faces)...

            $num_rolls = intval($matches[1]);
            $num_faces = intval($matches[2]);

            $output = '';

            for ($i = 0; $i < $num_rolls; $i++) {
                if ($output != '') {
                    $output .= ' & ';
                }

                $output .= strval(mt_rand(1, $num_faces));
            }
        }

        if (!running_script('preview')) {
            set_value($cache_bucket_id, $output, true);
        }

        return make_string_tempcode(escape_html($output));
    }
}
 

You can use either like this to just give an option from a set of options:
[roller="some_id"]1,2,3,4,5,6[/roller]
or like this to do your multiple dice rolling (assumption of numeric dice starting with '1'):
[roller="some_id"]3d10[/roller]

It's important to pass some_id uniquely for each roll, as the tag will otherwise lose its value if the Comcode cache is emptied. The ID gives it a key to cache against.

It would be great if you could write this up into a tutorial (in a topic) and add it to the tutorial directory. I don't want to personally maintain this as an addon, but it's pretty straight-forward and a good programming example.


Become a fan of Composr on Facebook or add me as a friend. Add me on on Twitter. Follow me on Minds (where I am most active). Support me on Patreon

Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Online now: No Back to the top

Post

Posted
Rating:
#4059
Avatar
Standard member
mythus is in the usergroup ‘Community saint’
Haha thanks Chris! You are alright!

I'll test this out and tomorrow I'll see about putting it in tutorials for you. I did try to so something similar using php but got the fun little warning with composr protecting me from using the evil php.


Like ocPortal? Want to thank Chris and gang somehow? Then help out in the chat room! It really needs your help! Just open it in a tab everytime you open your web browser, and when you hear a "ding", check it out!
 
"Those who want help should first be willing to give help."
Online now: No Back to the top

Post

Posted
Rating:
#4062
Avatar
Standard member
mythus is in the usergroup ‘Community saint’
And done, because by tomorrow I obviously meant today.


Like ocPortal? Want to thank Chris and gang somehow? Then help out in the chat room! It really needs your help! Just open it in a tab everytime you open your web browser, and when you hear a "ding", check it out!
 
"Those who want help should first be willing to give help."
Online now: No Back to the top
1 guest and 0 members have just viewed this.

Statistics

Users online:

gabriel58, amit.nigam, Manu, Philip, babu, deepu_ms

Forum statistics:
  • 1,298 topics, 5,830 posts, 7,065 members
  • Our newest member is raguragavan
Birthdays:
Back to Top