Added a !wattrib that whispers to the character instead of saying it as a description: function getAttributeObjects(characterObj,attributeArray) {
"use strict";
var i,
j = 0,
attributeObjArray = [],
attributeValue = [] ;
// can pass array of attribute strings or a single attribute string along with an associated character
// returns those attributes as an object array or returns false if they do not exist on the passed character.
// get the passed attribute name array from the character object and test if they are defined
if (characterObj) {
if (!(attributeArray instanceof Array)) {
attributeArray = attributeArray.split();
}
for (i = 0; i < attributeArray.length; i++) {
attributeObjArray[i] = findObjs({_type: "attribute", name: attributeArray[i], _characterid: characterObj.id})[0];
if (attributeObjArray[i] === undefined) {
sendChat("API","Selected character requires attribute: " + attributeArray[i] + " ");
}
}
}
if (attributeObjArray.indexOf(undefined) !== -1) {
return false;
}
//loop through attributeArray and names of attributes to make sure they all match and get their values if they are valid.
//make sure none of the values are empty
for (i = 0; i < attributeArray.length; i++) {
attributeValue[i] = attributeObjArray[i].get("current");
if (attributeValue[i] === "") {
sendChat("API"," " + attributeArray[i] + " is empty.");
j++;
}
}
if (j !== 0) {
return false;
}
return attributeObjArray;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
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 false;
}
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 false;
}
}
if (objType === "character") {
characterObj = getObj("character", obj._id);
}
return characterObj;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
function attrib(characterObj,attributeObjArray,newValue,isWhisper) {
"use strict";
var attributeName = attributeObjArray[0].get("name"),
attributeValue = attributeObjArray[0].get("current"),
characterName = characterObj.get("name");
// change character attribute
attributeObjArray[0].set("current", newValue);
//output
sendChat("", ( isWhisper ? ("/w "+characterName.split(/\s+/)[0] +" ") : "/desc ") + characterName + " has changed " + attributeName + " from " + attributeValue + " to " + newValue + ".");
}
on("chat:message", function(msg_orig) {
"use strict";
var msg,
selected,
isWhisper,
Parameters,
ids,
attributeName,
newValue,
characterObj,
attributeObjArray;
if (msg_orig.type === "api"
&& (
msg_orig.content.indexOf("!attrib ") !== -1
|| msg_orig.content.indexOf("!wattrib ") !== -1
)
){
// TheAaron's Inline Roll Expansion Code
msg = _.clone(msg_orig);
if(_.has(msg,'inlinerolls')){
msg.content = _.chain(msg.inlinerolls)
.reduce(function(m,v,k){
m['$[['+k+']]']=v.results.total || 0;
return m;
},{})
.reduce(function(m,v,k){
return m.replace(k,v);
},msg.content)
.value();
}
//parse the input into two variables, attribute and newValue
selected = msg.selected;
isWhisper = !!msg.content.match(/^!w/);
Parameters = msg.content.split(/\s+--\s+/)[0].split(/![w]?attrib /)[1];
ids = (msg.content.split(/\s+--\s+/)[1] ||'').split(/\s+/);
attributeName = Parameters.split("|")[0];
newValue = Parameters.split("|")[1];
// Add selected as IDs following other parameters
if(!selected) {
selected = _.chain(ids)
.uniq()
.filter(function(s){return s.length>0;})
.map(function(t){
return {
_type: 'graphic',
_id: t
};
})
.value();
}
if(!selected) {
sendChat("", "/desc Select token and try again.");
return; //quit if nothing selected
}
//loop through selected tokens
_.each(selected, function(obj) {
characterObj = getCharacterObj(obj);
if ( ! characterObj) {
return;
}
attributeObjArray = getAttributeObjects(characterObj, attributeName);
if ( ! attributeObjArray) {
return;
}
attrib(characterObj,attributeObjArray,newValue,isWhisper);
});
}
});