Skip to content

Instantly share code, notes, and snippets.

@DavidSandey
Created July 10, 2009 00:42
Show Gist options
  • Save DavidSandey/144130 to your computer and use it in GitHub Desktop.
Save DavidSandey/144130 to your computer and use it in GitHub Desktop.
Ubiquity dice rolling command
/*
Rolls dice!!
Arbitrary dice size and number.
Option to roll bonus dice for high results
i.e. exploding dice pool as per World of Darkness
Displays roll in the preview pane of Ubiquity.
On execute it displays the same result in a popup
toaster/growl message and copies it to the clipboard.
original Ubiquity noun by Brett Dalton
original idea based on Dustin Calhoun's Roll Verb
*/
var resultStr;
CmdUtils.CreateCommand({
names: ["roll"],
arguments: [{ role:"object",
nountype: noun_arb_text,
label: "dice to roll"}],
author: [{ name: "David Sandey",
email: "david@sandey-family.com"}],
license: ["Use it and Abuse it License"],
preview: function(pblock, arguments)
{
var msg = "Rolls dice with re-rolls <br><br> <b>Syntax </b><br> <b>4 </b> - rolls 1x4 sided dice <br><br> <b> 2d8</b> - rolls 2x8 sided dice <br><br> <b> 4d10x8 </b> - rolls 4x10 sided dice with extra rolls for any result of 8 or above.<br> Useful for World of Darkness games<br><br> <b>d6x </b>- rolls 1x6 with re-rolls equal to dice size, in this case 6 <br><br> The result will be copied to the clipboard so that it can be pasted where needed."
pblock.innerHTML = _(msg);
var i = 0;
var r = 0;
var d = 0;
var x = 0;
var randNum = 0;
var delim_D = 0;
var delim_X = 0;
resultStr = "";
// Find if the D and X are in the string
delim_D = arguments.object.text.indexOf("d") + arguments.object.text.indexOf("D") +1;
delim_X = arguments.object.text.indexOf("x") + arguments.object.text.indexOf("X") +1;
// try to parse the string regardless of if entire string exists
r = parseInt(arguments.object.text.substring(0));
d = parseInt(arguments.object.text.substring(delim_D+1));
x = parseInt(arguments.object.text.substring(delim_X +1));
if(isNaN(r))
{
r = 1;
}
if (delim_D == -1) // if there is no 'D' ie 12 or 12x10
{
d = r;
r = 1;
}
if (delim_X == -1) // if no 'X' make the ie 10 or 4d8
{
x = d+1;
}
if (delim_X +1 == arguments.object.text.length)
{
x = d;
}
// AVOID INFINITE LOOP
if (x == 1)
{
return -1;
};
// actually do the rand and gen the output
for(i==0; i<r; i++)
{
randNum = Math.floor(Math.random()*d +1);
tempStr = resultStr;
if(i==0)
{
resultStr = randNum;
if(randNum >= x)
{
r++;
}
}
else
{
resultStr = tempStr + ", " + randNum;
if(randNum >= x)
{
r++;
}
}
}
if (resultStr == "")
{
pblock.innerHTML = _(msg);
}
else
{
pblock.innerHTML = _(arguments.object.text + " = " + resultStr);
}
},
execute: function (arguments) {
var msg = arguments.object.text + " = " + resultStr;
displayMessage(_(msg));
CmdUtils.copyToClipboard(_(msg));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment