Skip to content

Instantly share code, notes, and snippets.

@chrisforrence
Last active December 13, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisforrence/5880258 to your computer and use it in GitHub Desktop.
Save chrisforrence/5880258 to your computer and use it in GitHub Desktop.
Rolls an N-sided die M times, keeping the top P results.
var FORTITUDE = 1, WILL = 2, REFLEX = 3;
var Player = {
"player": "",
"name": "",
"race": "",
"classes": [],
"fortitude": 0,
"reflex": 0,
"will": 0,
"strength": 0,
"dexterity": 0,
"constitution": 0,
"wisdom": 0,
"intelligence": 0,
"charisma": 0,
"items": [],
"skills": [],
"spells": {
"known": []
}
};
Player.save = function(type, dc, modifiers) {
var def = roll(20);
console.log("Rolled, " + def);
if(type === FORTITUDE) def += this.fortitude;
if(type === REFLEX) def += this.reflex;
if(type === WILL) def += this.will;
console.log("After base saves, " + def);
if(modifiers !== undefined) {
for(var i = 0; i < modifiers.length; i++) {
def += modifiers[i];
}
}
console.log("After modifiers, " + def);
console.log(def + (def >= dc ? " succeeds " : " fails ") + "against DC " + dc);
return def >= dc;
};
function roll(spots, total, count)
{
if(spots === undefined || parseInt(spots) < 1) spots = 6;
if(total === undefined || parseInt(total) < 1) total = 1;
if(count === undefined || parseInt(count) < 1 || parseInt(count) > total) count = total + 0;
var res = new Array();
for(var i = 0; i < total; i++) res.push(Math.floor(Math.random() * (spots)) + 1);
res.sort(function(a,b){return b-a});
res = res.slice(0, count);
var total=0;
for(var i in res) total += res[i];
return total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment