Created
January 21, 2015 01:53
-
-
Save rileydutton/f4323222539c952fde10 to your computer and use it in GitHub Desktop.
Mirrorshades Auto-Init Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 | |
| // | |
| 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."); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment