Skip to content

Instantly share code, notes, and snippets.

@rampion
Last active March 10, 2016 02:50
Show Gist options
  • Save rampion/18ecfc6fb58162350ee3 to your computer and use it in GitHub Desktop.
Save rampion/18ecfc6fb58162350ee3 to your computer and use it in GitHub Desktop.
// !debit [field] [action] decrements [field] and runs [action] if [field] > 0
//
// examples (note the '\' to prevent the &{...} from running immediately)
//
// !debit classactionresource1 %\{Coll|classaction1}
//
// !debit repeating_classresources_-KBB1UHdMK0ygfFM3AtV_ClassResourceTotal %\{Coll|classaction2}
//
// (I don't know a better way to get repeating classaction ids than inspecting the page)
var DebitModule = DebitModule || (function(){ "use strict";
// catch any thrown warnings and print them
function catch_warnings(body){
return function(arg){
try {
body.apply( this, arguments );
} catch(err) {
if (err["!debit"]) {
log(err);
return false;
} else {
throw err;
}
}
}
};
// break the control flow and pass a warning up the stack
function warn(msg){
// XXX: Array.slice is missing
throw { "!debit": msg, context: Array.prototype.slice.call(arguments, 1) };
}
return {
version: "1.1.0",
message_re: /^!debit\b(?:\s+(\S+)\s+(\S.*)$)?/m,
onReady: catch_warnings(function(){
log('-=> Debit v'+DebitModule.version+' <=-');
on("chat:message", DebitModule.onMessage);
}),
// filter for legal !debit commands and
// attempt to perform them
onMessage: catch_warnings(function(msg){
var match = DebitModule.message_re.exec(msg.content);
if (!match) return;
(match[1] && match[2]) ||
warn("Illegal command, expecting \"!debit [field] [action]\"", msg);
var field = match[1];
var action = match[2].replace(/\\(.)/g, '$1'); // unescape backslashes
(msg.selected && msg.selected.length) ||
warn("Nothing selected", msg);
var me = msg.who.match(/.+?\b/)[0];
_.each(msg.selected, function(obj) {
var character = DebitModule.retrieveCharacter(obj);
var attribute = DebitModule.retrieveAttribute(character.get("_id"), field);
var fieldName = DebitModule.fieldName(character.get("_id"), field);
var debited = DebitModule.attemptDebit(attribute);
if (debited){
sendChat( msg.who, '/w ' + me + ' ' + character.get("name") + " uses a " + fieldName);
sendChat( character.get("name"), action)
} else {
sendChat( msg.who, '/w ' + me + ' ' + character.get("name") + " doesn't have a " + fieldName + " left to use" );
}
});
return true;
}),
fieldNamePatterns: [
{ regexp: /^(classaction)resource(\d+)$/, replacement: '$1name$2' },
{ regexp: /^(repeating_classresources_-[A-Za-z0-9]{19}_ClassResource)Total$/, replacement: '$1Name' }
],
fieldName: function(character_id, field){
var p = _.find(DebitModule.fieldNamePatterns, function(p) { return p.regexp.test(field) });
return p ? DebitModule.retrieveAttribute(character_id, field.replace(p.regexp, p.replacement)).get("current") : field;
},
retrieveCharacter: function(obj){
var graphic = getObj("graphic", obj._id) ||
warn("No graphic found", obj);
var character_id = graphic.get("represents") ||
warn("No character id found", graphic, obj);
return getObj("character", character_id) ||
warn("No character found", character_id, graphic, obj);
},
// retrieve the attribute named by the field
retrieveAttribute: function(character_id, field){
var attributes = findObjs({
_type: 'attribute',
_characterid: character_id,
name: field
});
(attributes.length <= 1) ||
warn("Too many " + field + " attributes found", attributes.length, attributes, character_id );
return (attributes.length == 1) ? attributes[0] : createObj('attribute', {
name: field,
characterid: character_id
});
},
// if the attribute has a positive value,
// decrement it and return true, otherwise return false
attemptDebit: function(attribute) {
var current = attribute.get("current");
(current || "").toString().match(/^-?\d+$/) ||
warn("Non-numeric value", current, attribute.get("name"), attribute);
if (current > 0) {
attribute.set("current", (current - 1).toString());
return true;
}
return false
}
};
})();
on("ready", DebitModule.onReady);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment