Skip to content

Instantly share code, notes, and snippets.

@SCPRedMage
Last active March 15, 2016 04:44
Show Gist options
  • Select an option

  • Save SCPRedMage/3fa037c791e8579d365f to your computer and use it in GitHub Desktop.

Select an option

Save SCPRedMage/3fa037c791e8579d365f to your computer and use it in GitHub Desktop.
Updated to only grab spellclass if we're filtering for it.
function getCharacterObj(obj) {
"use strict";
//send any object and returns the associated character object
//returns character object for attribute, token/graphic, and ability, and... character
var objType = obj._type,
att, characterObj, tok;
if ((objType !== "attribute") && (objType !== "graphic") && (objType !== "character")) {
sendChat("API"," cannot be associated with a character.");
return;
}
if ((objType === "attribute") || (objType === "ability")) {
att = getObj(objType, obj._id);
if (att.get("_characterid") !== "") {
characterObj = getObj("character", att.get("_characterid"));
}
}
if (objType === "graphic") {
tok = getObj("graphic", obj._id);
if (tok.get("represents") !== "") {
characterObj = getObj("character", tok.get("represents"));
} else {
sendChat("API"," Selected token does not represent a character.");
return;
}
}
if (objType === "character") {
characterObj = getObj("character", obj._id);
}
return characterObj;
}
on("chat:message", function(msg) {
"use strict";
var msg,
selected,
Parameters,
statusName,
aName,
ids,
attributeName,
newValue,
characterObj,
attributeObjArray,
tok;
if (msg.type === "api" && msg.content.indexOf("!generate-spellbook") !== -1 )
{
selected = msg.selected;
var parameters = msg.content.split(" ");
// Set filter for what spell class to generate a spellbook for
// Defaults to -1 for no filter
var spellClass = -1;
if (parameters.length > 1)
spellClass = parameters[1];
// Set the name of the ability the macro will be put into
// Defaults to "Spells"
var abilityName = "Spells";
if (parameters.length > 2)
abilityName = parameters[2];
// Set the title to use in the template header
// Defaults to "Spells"
var title = "Spells";
if (parameters.length > 3)
title = parameters[3];
//loop through selected tokens
_.each(selected, function(obj) {
tok = getObj("graphic", obj._id);
// Get the character token represents
characterObj = getCharacterObj(obj);
if ( ! characterObj) {
return;
}
var characterName = characterObj.get("name");
var rowIDs = new Array(); // Create array for repeating spell attribute names
getAllObjs().forEach(function(obj) {
// Filter down to just attribute objects belonging to the character, whose name indicates it's a spell
if (obj.get("type") !== "attribute")
return;
if (obj._characterid !== characterObj._id)
return;
if (obj.get("name").indexOf("repeating_lvl-") === -1)
return;
// If this is a name attribute, use that to add the base repeating attribute name to the array
if (obj.get("name").indexOf("_name") !== -1)
rowIDs.push(obj.get("name").substr(0,obj.get("name").indexOf("_name")));
});
// Create arrays for individual spell levels
var lvl0 = new Array();
var lvl1 = new Array();
var lvl2 = new Array();
var lvl3 = new Array();
var lvl4 = new Array();
var lvl5 = new Array();
var lvl6 = new Array();
var lvl7 = new Array();
var lvl8 = new Array();
var lvl9 = new Array();
// Sort rowIDs into spell levels
rowIDs.forEach(function(entry){
// Filter out spells from other spell classes, unless filter is -1
if (spellclass !== -1)
{
var thisClass = getAttrByName(characterObj.get("_id"), entry + "_spellclass");
if (_.isUndefined(thisClass))
thisClass = "@{spellclass-0-name}";
if (thisClass.substr(thisClass.indexOf("-name")-1, 1) !== spellClass)
return;
}
// Grab the spell level from attribute name
var lvl = entry.substr(14,1);
// Put spell into the correct spell level
switch (lvl) {
case "0":
lvl0.push(entry);
break;
case "1":
lvl1.push(entry);
break;
case "2":
lvl2.push(entry);
break;
case "3":
lvl3.push(entry);
break;
case "4":
lvl4.push(entry);
break;
case "5":
lvl5.push(entry);
break;
case "6":
lvl6.push(entry);
break;
case "7":
lvl7.push(entry);
break;
case "8":
lvl8.push(entry);
break;
case "9":
lvl9.push(entry);
break;
}
});
var macroText = "/w @{character_name} &{template:pf_generic} {{character_name}} {{character_id=@{character_id}}} {{name="+title+"}}"; // Base macro text
// Add spells to macro by spell level
if (lvl0.length > 0)
{
var lvlText = "";
lvl0.forEach(function(entry) {
// Check for spell name attribute; was having orphaned row_id attributes creating buttons for repeating rows that didn't actually exist
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 0}} {{"+ lvlText + "}}";
}
if (lvl1.length > 0)
{
var lvlText = "";
lvl1.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 1}} {{"+ lvlText + "}}";
}
if (lvl2.length > 0)
{
var lvlText = "";
lvl2.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 2}} {{"+ lvlText + "}}";
}
if (lvl3.length > 0)
{
var lvlText = "";
lvl3.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 3}} {{"+ lvlText + "}}";
}
if (lvl4.length > 0)
{
var lvlText = "";
lvl4.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 4}} {{"+ lvlText + "}}";
}
if (lvl5.length > 0)
{
var lvlText = "";
lvl5.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 5}} {{"+ lvlText + "}}";
}
if (lvl6.length > 0)
{
var lvlText = "";
lvl6.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 6}} {{"+ lvlText + "}}";
}
if (lvl7.length > 0)
{
var lvlText = "";
lvl7.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 7}} {{"+ lvlText + "}}";
}
if (lvl8.length > 0)
{
var lvlText = "";
lvl8.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 8}} {{"+ lvlText + "}}";
}
if (lvl9.length > 9)
{
var lvlText = "";
lvl9.forEach(function(entry) {
if (_.isUndefined(getAttrByName(characterObj.get("_id"), entry + "_name")))
return;
lvlText = lvlText + "[@{"+entry+"_name}](!
%{"+characterName+"|"+entry+"_roll}) ";
});
if (lvlText !== "")
macroText = macroText + " {{Level 9}} {{"+ lvlText + "}}";
}
// Check for existing ability with chosen ability name
var abil = findObjs({ _type: "ability", _characterid: characterObj.get("_id"), name: abilityName });
if (abil.length === 0) // If you don't find one...
{
// ...make one
createObj("ability", {
name: abilityName,
characterid: characterObj.get("_id"),
action: macroText,
istokenaction: true
});
}
else // If you DO find one...
{
// Just use that one
abil[0].set("action", macroText);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment