Skip to content

Instantly share code, notes, and snippets.

@Bastlifa
Last active August 18, 2017 17:39
Show Gist options
  • Save Bastlifa/8a8ae5eae5696b4452e0af36a171bce3 to your computer and use it in GitHub Desktop.
Save Bastlifa/8a8ae5eae5696b4452e0af36a171bce3 to your computer and use it in GitHub Desktop.
Roll20 API Script for Stress Levels
/****Stress Level Script for Matt C.****
Select a token. Click stress macro. The
macro will automatically roll some dice
and add the result to bar 3, up to 200.
If the threshold 100 is reached, a sound
will play. It will also post to chat for
any stress increase. Finally, there's a
stress reset.
You'll need to mod it to play sound.
You'll also need to add your stress table
as well as make the following macros:
!SLS Increase
!SLS Reset
!SLS Adjust ?{Adjust Value}
(those should be the macro bodies,
call the macros whatever you like)
****************************************/
on("ready", function() {
"use strict";
on("chat:message", function (msg) {
if ('api'===msg.type && msg.content.split(/\s+/)[0] == "!SLS")
{
let args;
args = msg.content.split(/\s+/);
var tableID = (findObjs({type: 'rollabletable', name: 'StressTable'}, {caseInsensitive: true})||{id:'doesnotexist'})[0].id;
if (args[1] == "Increase" && msg.selected != null)
{
var firstD20 = randomInteger(20);
sendChat("", "/w gm first d20 was a " + firstD20);
var stressToken = getObj(msg.selected[0]._type, msg.selected[0]._id);
if (parseInt(stressToken.get("bar3_value")) + firstD20 < 100 || (parseInt(stressToken.get("bar3_value")) + firstD20 < 200 && parseInt(stressToken.get("bar3_value")) > 100))
{
stressToken.set("bar3_value", parseInt(stressToken.get("bar3_value")) + firstD20);
sendChat("", "&{template:default} {{name=Stress}} {{" + stressToken.get("name") + " Stress Increased by:=" + firstD20 +"}}");
if (firstD20 <= 10) { TableRoll(tableID, stressToken); }
}
else if (parseInt(stressToken.get("bar3_value")) + firstD20 >= 100 && parseInt(stressToken.get("bar3_value")) < 100 && parseInt(stressToken.get("bar3_value")) + firstD20 < 200)
{
stressToken.set("bar3_value", parseInt(stressToken.get("bar3_value")) + firstD20);
//play sound however your sound script works here for first threshhold
sendChat("", "&{template:default} {{name=Stress}} {{" + stressToken.get("name") + " Stress Increased by:=" + firstD20 +"}} {{First Stresshhold Reached!}}");
if (firstD20 <= 10) { TableRoll(tableID, stressToken); }
}
else
{
stressToken.set("bar3_value", 200);
//play sound however your sound script works here for second threshhold.
sendChat("", "&{template:default} {{name=Stress}} {{" + stressToken.get("name") + " Stress Increased to:=" + 200 +"}} {{Second Stresshhold Reached!}}");
if (firstD20 <= 10) { TableRoll(tableID, stressToken); }
}
}
else if (args[1] == "Reset" && msg.selected != null)
{
var stressToken = getObj(msg.selected[0]._type, msg.selected[0]._id);
stressToken.set("bar3_value", 0);
}
else if (args[1] == "Adjust" && msg.selected != null)
{
if (args[2] == null) {sendChat("", "need adjustment parameter"); return;}
var stressToken = getObj(msg.selected[0]._type, msg.selected[0]._id);
if (parseInt(args[2]) < 0)
{
stressToken.set("bar3_value", Math.max(0, parseInt(stressToken.get("bar3_value") + parseInt(args[2]))));
}
else if (parseInt(args[2]) >= 0)
{
var over100 = false; var over200 = false;
if (parseInt(stressToken.get("bar3_value")) >= 100) {over100 = true;}
if (parseInt(stressToken.get("bar3_value")) >= 200) {over200 = true;}
stressToken.set("bar3_value", Math.min(200, parseInt(stressToken.get("bar3_value")) + parseInt(args[2])));
if (parseInt(stressToken.get("bar3_value")) >= 100 && over100 == false && parseInt(stressToken.get("bar3_value")) < 200)
{
sendChat("", "&{template:default} {{name=Stress}} {{" + stressToken.get("name") + " Stress Increased by:=" + args[2] +"}} {{First Stresshhold Reached!}}");
}
else if (parseInt(stressToken.get("bar3_value")) >= 100 && over100 == false && parseInt(stressToken.get("bar3_value")) == 200)
{
sendChat("", "&{template:default} {{name=Stress}} {{" + stressToken.get("name") + " Stress Increased by:=" + args[2] +"}} {{First Stresshhold Reached!}} {{Second Stresshhold Reached!}}");
}
else if (parseInt(stressToken.get("bar3_value")) >= 100 && over100 == true && parseInt(stressToken.get("bar3_value")) == 200)
{
sendChat("", "&{template:default} {{name=Stress}} {{" + stressToken.get("name") + " Stress Increased by:=" + args[2] +"}} {{Second Stresshhold Reached!}}");
}
else if (parseInt(stressToken.get("bar3_value")) >= 100 && over100 == true && parseInt(stressToken.get("bar3_value")) < 200)
{
sendChat("", "&{template:default} {{name=Stress}} {{" + stressToken.get("name") + " Stress Increased by:=" + args[2] +"}}");
}
}
else {sendChat("", "incorrect parameter for Adjustment");}
}
}
});
});
function TableRoll(tableID, stressToken)
{
if (tableID == 'doesnotexist')
{
sendChat("", "/w gm Please make a StressTable with 20 elements, or otherwise mod the script");
}
else
{
var SecondD20 = randomInteger(20);
sendChat("", "/w gm second d20 was a " + SecondD20);
var tableEntries = findObjs({type: 'tableitem', _rollabletableid: tableID});
// I don't know the roll20 sound script. I use a mangled version of someone else's.
// You'll have to mod the next line for however that script works. and uncomment it.
//sendChat("", "!roll20AMScriptCommand" + "SLS" + SecondD20);
sendChat("", "&{template:default} {{name=Extra Stress Result}} {{" + stressToken.get("name") + " Special Stress:=" + tableEntries[SecondD20 - 1].get("name") +"}}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment