Skip to content

Instantly share code, notes, and snippets.

@BaldarSilveraxe
Last active December 30, 2015 10:09
Show Gist options
  • Save BaldarSilveraxe/7813764 to your computer and use it in GitHub Desktop.
Save BaldarSilveraxe/7813764 to your computer and use it in GitHub Desktop.
Roll20API Joe Y Round Tracker
//Roll20 UserIDs that can use this command (replace "999999" another player allowed and as more as needed.)
var allowedUser = ["999999","999999"];
var GM_CombatMarkerDefaultTokenExist = false;
//dice used for combat
var rollValue = 10;
on('chat:message', function(msg) {
//Return if its not an API command
if(msg.type !== "api"){return};
//Get Player ID and Roll20 User Id
contentPlayerid = getObj("player", msg.playerid);
contentUserid = contentPlayerid.get("d20userid");
//Allowed to use
if(allowedUser.indexOf(contentUserid) == -1){return};
//grab the message
var contentGiven = msg.content.toLowerCase();
//parse the message and command
if(contentGiven.indexOf(' ') == -1){
contentMessage = null;
contentCommand = contentGiven.substr(1, contentGiven.length);
}else{
contentMessage = contentGiven.substr(contentGiven.indexOf(' ') + 1);
contentCommand = contentGiven.substr(1, contentGiven.indexOf(' ')-1);
};
CreateMarker();
//act on valid commands
switch (contentCommand){
case "newcombat":
NewCombat();
break;
case "newround":
NewRound();
break;
case "endcombat":
EndCombat();
break;
};
if(GM_CombatMarkerDefaultTokenExist == false){GM_CombatMarkerDefaultTokenExistFalse();};
});
TurnOrderManagement = function() {
var currentRound = parseInt(findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid})[0].get("current"));
//clean array for turn order
turnorder = [];
Campaign().set("turnorder", JSON.stringify(turnorder))
Campaign().set("initiativepage", false );
sendChat("API", "/direct <br><u>NEW ROUND ("+ currentRound + ")</U>");
//Provide party and GM dice rolls
var partyInt = Math.floor(Math.random() * rollValue) + 1
var MonsterInt = Math.floor(Math.random() * rollValue) + 1
sendChat("API", "/direct \
<table>\
<tr><td>Party:</td><td> 1d" + rollValue + " = <b>" + partyInt + "</b></td></tr>\
<tr><td>GM:</td><td> 1d" + rollValue + " = <b>" + MonsterInt + "</td></tr>\
</table>");
//find all the graphics on the page the players are on
var currentPageGraphics = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
});
//start of a string to output a table in chat with results
var outputTable = "<table>"
//Loop through all the graphics
_.each(currentPageGraphics, function(loopGraphic) {
//if the graphic represents a character sheet... then
if(loopGraphic.get("represents").length !== 0){
//grab the bar1 value (green bar) and sum with party initiative roll (var partyInt)
var greenBar = loopGraphic.get("bar1_value");
if(loopGraphic.get("bar1_value").length == 0){
greenBar = 9;
};
var intvalue = +partyInt + +greenBar;
//Skip over "Round*"
if(loopGraphic.get("name").indexOf('Round') !== 0){
//push the graphic id and player intiative result into the turn order array
turnorder.push({
id: loopGraphic.get("_id"),
pr: intvalue,
});
//builds on astring to output a table in chat with results
outputTable = outputTable + "<tr>\
<td>" + loopGraphic.get("name") + "</td>\
<td>" + partyInt + " + " + greenBar + " = <b>" + intvalue + "</b></td>\
</tr>"
};
};
});
//addss the GM intiative result into the turn order array
turnorder.push({
id: "-1",
pr: MonsterInt,
custom: "GM Initiative"
});
//finishes and sends on astring to output a table in chat with results
var outputTable = outputTable + "</table>"
sendChat("API", "/direct " + outputTable)
//sort the orrder
turnorder.sort(function(a,b) {
first = a.pr;
second = b.pr;
//return second - first;
return first - second;
});
//Set the results in the turnorder pane
Campaign().set("turnorder", JSON.stringify(turnorder))
};
NewCombat = function() {
GM_CombatMarkerDefaultTokenExist = false;
GM_CombatMarkerObject = findObjs({ _type: "character", name: "GM_CombatMarker" });
GM_CombatMarkerid = GM_CombatMarkerObject[0].id
if(findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid}).length == 0){
GM_CombatMarkerDefaultTokenExist = undefined;
sendChat("API", "Error with the GM_CombatMarke sheet. Please delete and try again.")
return
};
var currentRound = parseInt(findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid})[0].get("current"));
findObjs({ _type: "attribute", name: "LastRound", _characterid: GM_CombatMarkerid})[0].set({current: currentRound});
findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid})[0].set({current: 1});
currentRound = 1
var currentPageGraphics = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
});
_.each(currentPageGraphics, function(loopGraphic) {
if(loopGraphic.get("represents") == GM_CombatMarkerid) {
GM_CombatMarkerDefaultTokenExist = true;
loopGraphic.set("name", "Round " + currentRound);
};
if(loopGraphic.get("represents").length !== 0){
};
});
if(GM_CombatMarkerDefaultTokenExist == false){return};
/*
Extra Start of Combat stuff goes here....
Extra Start of Combat stuff goes here....
Extra Start of Combat stuff goes here....
*/
TurnOrderManagement();
};
NewRound = function() {
GM_CombatMarkerDefaultTokenExist = false;
GM_CombatMarkerObject = findObjs({ _type: "character", name: "GM_CombatMarker" });
GM_CombatMarkerid = GM_CombatMarkerObject[0].id
if(findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid}).length == 0){
GM_CombatMarkerDefaultTokenExist = undefined;
sendChat("API", "Error with the GM_CombatMarke sheet. Please delete and try again.")
return
};
var currentRound = parseInt(findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid})[0].get("current"));
findObjs({ _type: "attribute", name: "LastRound", _characterid: GM_CombatMarkerid})[0].set({current: currentRound});
currentRound = currentRound + 1;
findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid})[0].set({current: currentRound});
var currentPageGraphics = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
});
_.each(currentPageGraphics, function(loopGraphic) {
if(loopGraphic.get("represents") == GM_CombatMarkerid) {
GM_CombatMarkerDefaultTokenExist = true;
loopGraphic.set("name", "Round " + currentRound);
};
if(loopGraphic.get("represents").length !== 0){
};
});
if(GM_CombatMarkerDefaultTokenExist == false){return};
/*
Extra Next Round stuff goes here....
Extra Next Round stuff goes here....
Extra Next Round stuff goes here....
*/
TurnOrderManagement();
};
EndCombat = function() {
GM_CombatMarkerDefaultTokenExist = false;
GM_CombatMarkerObject = findObjs({ _type: "character", name: "GM_CombatMarker" });
GM_CombatMarkerid = GM_CombatMarkerObject[0].id
if(findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid}).length == 0){
GM_CombatMarkerDefaultTokenExist = undefined;
sendChat("API", "Error with the GM_CombatMarke sheet. Please delete and try again.")
return
};
var currentRound = parseInt(findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid})[0].get("current"));
findObjs({ _type: "attribute", name: "LastRound", _characterid: GM_CombatMarkerid})[0].set({current: currentRound});
findObjs({ _type: "attribute", name: "Round", _characterid: GM_CombatMarkerid})[0].set({current: 0});
var currentPageGraphics = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
});
_.each(currentPageGraphics, function(loopGraphic) {
if(loopGraphic.get("represents") == GM_CombatMarkerid) {
GM_CombatMarkerDefaultTokenExist = true;
loopGraphic.set("name", "!NewCombat");
};
if(loopGraphic.get("represents").length !== 0){
};
});
if(GM_CombatMarkerDefaultTokenExist == false){return};
/*
Extra End of Combat stuff goes here....
Extra End of Combat stuff goes here....
Extra End of Combat stuff goes here....
*/
};
GM_CombatMarkerDefaultTokenExistFalse = function() {
sendChat("API", "/direct GM_CombatMarker does not have a <b>'Default Token'</b> on the board, please place and \
relate on to it. <br><a href='https://wiki.roll20.net/Journal' target='_blank'><u>The Default Token field (8.)\
</u></a>");
};
CreateMarker = function() {
if(findObjs({ _type: "character", name: "GM_CombatMarker" }).length !== 0){return};
//Make GM_CombatMarker sheeet
createObj("character", {
name: "GM_CombatMarker",
});
GM_CombatMarkerObject = findObjs({ _type: "character", name: "GM_CombatMarker" });
GM_CombatMarkerid = GM_CombatMarkerObject[0].id
attributeGM_CombatMarker = [];
attributeGM_CombatMarker["Round"] = { name: "Round", current: 1, max: null};
attributeGM_CombatMarker["LastRound"] = { name: "LastRound", current: 1, max: null};
abilitiesGM_CombatMarker = [
{ name: "New Combat", description: "", action: "!NewCombat" },
{ name: "New Round", description: "", action: "!NewRound" },
{ name: "End Combat", description: "", action: "!EndCombat" }
];
for (var index in attributeGM_CombatMarker) {
createObj("attribute", {
name: attributeGM_CombatMarker[index].name,
current: attributeGM_CombatMarker[index].current,
max: attributeGM_CombatMarker[index].max,
characterid: GM_CombatMarkerid
});
};
for (var ability in abilitiesGM_CombatMarker) {
createObj("ability", {
name: abilitiesGM_CombatMarker[ability].name,
description: "",
action: abilitiesGM_CombatMarker[ability].action,
characterid: GM_CombatMarkerid
});
};
sendChat("API", "/direct GM_CombatMarker newly created. Please relate a <b>'Default Token'</b> to it. <br>\
<a href='https://wiki.roll20.net/Journal' target='_blank'><u>The Default Token field (8.)</u></a>");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment