Post by xan1112 on Jun 2, 2012 2:53:11 GMT -8
Hi guys,
I found the dice roller linked in the code database, but it didn't really work so well for the needs of my group and we found it to be a little flakey, especially when combined with other code snippets (like the spoiler tags) etc.
To this end, I've coded my own, using the fantastic rolz.org site's API. I thought this might be of interest to some people so I thought I would share it here.
Some caveats:
The code works by adding a button to the tags bar. When clicked this button:
Any questions or suggestions, let me know. I hope some of you may find it useful in some form.
Cheers,
xan
I found the dice roller linked in the code database, but it didn't really work so well for the needs of my group and we found it to be a little flakey, especially when combined with other code snippets (like the spoiler tags) etc.
To this end, I've coded my own, using the fantastic rolz.org site's API. I thought this might be of interest to some people so I thought I would share it here.
Some caveats:
- Does not attempt to prevent cheating. This is for use in trusted communities only.
- Requires the jQuery javascript library to be included for this to work. This is to make the AJAX calls easier, I'm sure this can be coded around, but I don't have the time. It's probably a non-issue since the library is the CDN hosted version from google, it'll be client side cached after the first hit anyway and you can always reduce the scope by putting this at the granularity of a specific board. You'll have to decide this depending on your scenario.
- It currently outputs the result in [ spoiler] tags. If you don't want this, just edit the output string at: add("")
- It will currently display any error messages it receives.
The code works by adding a button to the tags bar. When clicked this button:
- Prompts for the dice expression (offering a default example).
- Makes an AJAX call to the rolz.org API to evaluate the roll.
- Pastes the result of the roll into the edit box (complete with spoiler tags).
<!-- Dice roller by xan version 1.0 -->
<!-- Feel free to share, but please keep the header intact. -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var diceButton = 'http://img100.imageshack.us/img100/6118/diceicon9rx.gif';
//Make dice roll and add to the page
function addDice()
{
var dice = prompt("Enter dice expression:","1d20+4");
if(dice != null) {
$.ajax({
url: "http://rolz.org/api/?" + dice + ".jsonp",
dataType: "jsonp",
success: function(data) {
add("[spoiler=roll]" + data.code + " = " + data.result + " (" + data.details + ")[/spoiler]");
},
error: function(status, error) {
add(status);
add(error);
}
});
}
return;
}
// Add dice button to posting page..
if(document.postForm)
{
var db = document.createElement('a');
db.innerHTML = '<img src="'+diceButton +'" border="0">';
db.href = 'javascript:addDice()';
document.postForm.color.parentNode.appendChild(db);
}
</script>
Any questions or suggestions, let me know. I hope some of you may find it useful in some form.
Cheers,
xan