Samuel T. said: Hey Invincible Spleen nice job on this api. Any chance you would be willing to modify it to accept a !setAttribute command were we could tell it on the fly a new attribute to create if it doesn't exists or update if it does? I would love to use this to keep track of my burn and nonlethal values in a game and I've been looking for something to do just that. I would also love an ability to take the value of the attribute if it exists and then add a new value to it. For example this would be my macro !setAttribute burn +1
!setAttribute nonlethal-damage +[[@{level}]] This is starting to get pretty robust, but there are likely still ways you can creatively break it. var DefaultAttributes = DefaultAttributes || (function () {
'use strict';
// Attributes all characters should have
var attributeHash = {
"Attribute1": {
"current": "Some text"
},
"Attribute2": {
"current": "Blah"
},
"Attribute3": {
"current": "Words words words"
},
"Attribute4": {
"current": "asdasdf"
}
},
// Set an attribute's value, or create it if it does not exist
setAttribute = function(characterID, attributeName, newValue, operator) {
var mod_newValue = {
"+": function (num) { return num; },
"-": function (num) { return -num; }
},
foundAttribute = findObjs({
_characterid: characterID,
_type: "attribute",
name: attributeName
})[0];
if (!foundAttribute) {
if (typeof operator !== 'undefined' && !isNaN(newValue)) {
log (newValue + " is a number.");
newValue = mod_newValue[operator](newValue);
}
log("DefaultAttributes: Initializing " + attributeName + " on character ID " + characterID + " with a value of " + newValue + ".");
sendChat("DefaultAttributes:", "/w GM Initializing " + attributeName + " on character ID " + characterID + " with a value of " + newValue + ".");
createObj("attribute", {
name: attributeName,
current: newValue,
characterid: characterID
});
}
else {
if (typeof operator !== 'undefined' && !isNaN(newValue) && !isNaN(foundAttribute.get("current"))) {
newValue = parseFloat(foundAttribute.get("current")) + parseFloat(mod_newValue[operator](newValue));
}
log("DefaultAttributes: Setting " + attributeName + " on character ID " + characterID + " to a value of " + newValue + ".");
sendChat("DefaultAttributes:", "/w GM Setting " + attributeName + " on character ID " + characterID + " to a value of " + newValue + ".");
foundAttribute.set("current", newValue);
}
},
// Add missing attributes and restore exiting ones to their default values
initCharacterAttributes = function(char){
log("DefaultAttributes: Initializing default attributes for character ID " + char.id + ".");
sendChat("DefaultAttributes:", "/w GM Initializing default attributes for character ID " + char.id + ".");
for (var key in attributeHash) {
if (attributeHash.hasOwnProperty(key)) {
setAttribute(char.id, key, attributeHash[key]["current"]);
}
}
},
showHelp = function () {
sendChat("DefaultAttributes:", "/w GM Syntax is !setattribute <i>Attribute</i> [+/-] <i>Value</i>");
},
handleInput = function(msg) {
if(msg.type == "api" && isGM(msg.playerid)) {
var args = msg.content.split(/\s+/);
switch(args[0]) {
case '!initattributes':
log("DefaultAttributes: Initializing default attributes for all existing characters.");
sendChat("DefaultAttributes:", "/w GM Initializing default attributes for all existing characters.");
var allCharacters = findObjs({
_type: "character"
});
_.each(allCharacters, function(char) {
initCharacterAttributes(char);
});
break;
case '!setattribute':
if (args.length < 3 || args.length > 4) {
return showHelp();
}
var foundCharacter = findObjs({
_type: "character",
name: msg.who
})[0];
if (foundCharacter) {
if (args[2] == "+" || args[2] == "-") {
setAttribute(foundCharacter.id, args[1], args[3], args[2]);
}
else {
setAttribute(foundCharacter.id, args[1], args[2]);
}
}
else {
log("DefaultAttributes: No character associated with " + msg.who);
sendChat("DefaultAttributes:", "/w GM No character associated with " + msg.who);
}
break;
}
}
},
// Event triggers
registerEventHandlers = function() {
on("add:character", initCharacterAttributes);
on("chat:message", handleInput);
};
return {
RegisterEventHandlers: registerEventHandlers
};
})();
on("ready", function() {
'use strict';
DefaultAttributes.RegisterEventHandlers();
}); Note that the IsGM script is required now. We probably don't want players resetting every character's attributes to their default values...