Skip to content

Instantly share code, notes, and snippets.

@shdwjk
Last active May 17, 2021 23:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shdwjk/b04d1fc8c48539bc5cd7 to your computer and use it in GitHub Desktop.
Save shdwjk/b04d1fc8c48539bc5cd7 to your computer and use it in GitHub Desktop.
Roll20 API: Weighted-Dice -- automated creation of rollable tables where a minimum value is weighted with values it replaces. (example d6min4 possible values: 4 4 4 4 5 6)
// GIST: https://gist.github.com/shdwjk/7a9c3ce232961662b69c
var WeightedDice = WeightedDice || {
version: 0.2,
schemaVersion: 0.1,
CheckInstall: function() {
if( ! _.has(state,'WeightedDice') || state.WeightedDice.schemaVersion != WeightedDice.schemaVersion)
{
/* Default Settings stored in the state. */
state.WeightedDice = {
version: WeightedDice.schemaVersion
}
}
},
HandleInput: function(tokens,msg) {
var sides = parseInt(tokens[0]);
var minroll = parseInt(tokens[1]);
if(
tokens.length <2
|| 2 != tokens.length
|| _.isNull(sides)
|| !_.isNumber(sides)
|| _.isNull(minroll)
|| !_.isNumber(minroll)
|| sides < minroll
)
{
sendChat('','/w gm Usage: !weighted-die [number of sides] [minimum roll number]');
return;
}
var tableName='d'+sides+'min'+minroll;
// see if it's already defined
var tables=findObjs({type: 'rollabletable', name: tableName});
if(tables.length)
{
sendChat('','/w gm Table '+tableName+' already exists.');
}
else
{
var newTable=fixNewObject(createObj('rollabletable',{name: tableName}));
_.each(_.range(minroll,(sides+1)), function(r){
var weight = ( (r == minroll) ? minroll : 1);
var newTableItem=fixNewObject(createObj('tableitem',{
_rollabletableid: newTable.id,
name: r,
weight: weight
}));
});
sendChat('','/w gm Table '+tableName+' created.');
}
},
RegisterEventHandlers: function(){
on("chat:message", function (msg) {
/* Exit if not an api command */
if (msg.type != "api") return;
var tokenized = msg.content.split(" ");
var command = tokenized[0];
switch(command)
{
case "!weighted-die":
if(isGM(msg.playerid))
{
WeightedDice.HandleInput(_.rest(tokenized),msg);
}
break;
}
});
}
};
on("ready",function(){
var Has_IsGM=false;
try {
_.isFunction(isGM);
Has_IsGM=true;
}
catch (err)
{
log('--------------------------------------------------------------');
log('WeightedDice requires the isGM module to work.');
log('isGM GIST: https://gist.github.com/shdwjk/8d5bb062abab18463625')
log('--------------------------------------------------------------');
}
if( Has_IsGM )
{
WeightedDice.CheckInstall();
WeightedDice.RegisterEventHandlers();
}
});
// Utility Function
var fixNewObject = fixNewObject || function(obj){
var p = obj.changed._fbpath;
var new_p = p.replace(/([^\/]*\/){4}/, "/");
obj.fbpath = new_p;
return obj;
};
@Barma500
Copy link

how does this work? like how do you use it?

@shdwjk
Copy link
Author

shdwjk commented May 17, 2021

First off, use this version, it's much newer and doesn't depend on isGM, which isn't needed anymore: https://github.com/shdwjk/Roll20API/blob/master/WeightedDice/WeightedDice.js

You call it with:

!weighted-die <sides> <min num>

and it creates a weighted table named d<sides>min<min num>. For example, this:

!weighted-die 8 4

will create an table called d8min4 which acts like an eight sided die that effectively has 4 4 4 4 5 6 7 8 on the sides. You can then roll it with something like:

[[ 3t[d8min4] ]]

to generate a properly weighted result.

@Barma500
Copy link

It did nothing, I think I put the code in wrong. Could you check?
Screenshot_42
Screenshot_41

@shdwjk
Copy link
Author

shdwjk commented May 17, 2021

This is an API script. You have to use it in the API for a game, which requires a Pro Subscription. It does not work in TamperMonkey or xjs or anything like that.

@Barma500
Copy link

damn, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment