Skip to content

Instantly share code, notes, and snippets.

@SteveKoontz
Last active August 29, 2015 14:14
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 SteveKoontz/8cba7605820529ddeea0 to your computer and use it in GitHub Desktop.
Save SteveKoontz/8cba7605820529ddeea0 to your computer and use it in GitHub Desktop.
Shadow Run 1st Edition Initiative API
//TO USE THIS:
// To make an initiative roll, have the player do their normal intiiative roll, and add "!init" on the end. For example
// /roll 3d6+5 !init
// This will show the roll like normal, plus auto-add turns to the turn tracker for them for each multiple of 7.
//
// The GM can also easy clear the turn tracker by doing:
// !clearinit
//
// You can also apply a damage penalty to your current initiative by using the "!dmg" command followed by a nymber.
// For example: "!dmg 1" would subtract 1 from all of your character's initiative scores.
on("chat:message", function(op) {
//If this is a roll, and we had the "!init" command in the roll text
//then auto-add the initiative to the turn order list using our special formula.
if(op.type === "rollresult" && op.origRoll.indexOf("!init") !== -1) {
//What they rolled.
var currentTurnOrder;
try {
var diceinfo = JSON.parse(op.content)
if(Campaign().get("turnorder") === "" || Campaign().get("turnorder") === "[]") {
currentTurnOrder = [];
}
else {
currentTurnOrder = JSON.parse(Campaign().get("turnorder"));
}
}
catch(e) {
log("ERROR parsing what I thought was an init roll");
log(e+"");
return;
}
//What they rolled.
var initrolled = diceinfo.total;
//Starting with what they rolled, subtract 7 each time and add that to the turn order,
//Until we get below 1.
for(var i=initrolled; i > 0; i=i-7) {
log("Adding turn for " + i);
currentTurnOrder.push({
custom: op.who,
pr: i,
id: "-1"
});
}
//Sort the turn order.
currentTurnOrder = _.sortBy(currentTurnOrder, function(turnitem) {
if(typeof turnitem.pr === "string") {
return -(parseInt(turnitem.pr, 10));
}
else {
return -(turnitem.pr);
}
});
//Save the turn order.
Campaign().set({
turnorder: JSON.stringify(currentTurnOrder)
});
//Done!
log("Added turns for " + op.who);
}
else if(op.type === "api" && op.content.indexOf("!clearinit") !== -1) {
Campaign().set({
turnorder: ""
});
log("Cleared turn order.");
}
else if(op.type === "api" && op.content.indexOf("!dmg") !== -1) {
var currentTurnOrder;
var oparray = op.content.split(" ");
var dmgamount = oparray[1];
if(!dmgamount || isNaN(dmgamount) || dmgamount === "" || dmgamount <1) {
log("Not a valid damage amount.");
return;
}
try {
if(Campaign().get("turnorder") === "" || Campaign().get("turnorder") === "[]") {
currentTurnOrder = [];
}
else {
currentTurnOrder = JSON.parse(Campaign().get("turnorder"));
}
}
catch(e) {
log("ERROR parsing what I thought was an init roll");
log(e+"");
return;
}
for(var i=(currentTurnOrder.length -1); i > -1; i--) {
if(currentTurnOrder[i].custom == op.who) {
currentTurnOrder[i].pr -= dmgamount
if(currentTurnOrder[i].pr < 1){
currentTurnOrder.splice(i, 1);
}
};
};
currentTurnOrder = _.sortBy(currentTurnOrder, function(turnitem) {
if(typeof turnitem.pr === "string") {
return -(parseInt(turnitem.pr, 10));
}
else {
return -(turnitem.pr);
}
});
//Save the turn order.
Campaign().set({
turnorder: JSON.stringify(currentTurnOrder)
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment