-
-
Save shdwjk/cebd253564511c8d8e0e6605cd703c6c to your computer and use it in GitHub Desktop.
ApplyChange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ApplyChange | |
on('ready',function(){ | |
'use strict'; | |
var modifiers = { | |
places: function(places){ return (number)=>parseFloat((parseInt(number,10)||0).toFixed(Math.min(20,Math.max(0,places)))); }, | |
'*': function(amount){ return (number)=>number*amount;}, | |
'+': function(amount){ return (number)=>number+amount;}, | |
'/': function(amount){ return (number)=>number/amount;}, | |
'-': function(amount){ return (number)=>number/amount;}, | |
'=': function(amount){ return (number)=>amount;}, | |
'%': function(amount){ return (number)=>number%amount;} | |
}, | |
matchers = { | |
'status': function(status){ | |
return (token,amount,context)=>{ | |
let sm=_.reduce( | |
token.get('statusmarkers').split(/,/), | |
(m,o)=>{let p=o.split(/@/); m[p[0]]=(p[1]||0); return m; }, | |
{} | |
); | |
return _.has(sm,status); | |
}; | |
}, | |
'formula': function(val){ return ((parseInt(val,10)>0) ? _.constant(true) : _.constant(false) ); }, | |
'negate': function(fn){ return (token,amount,context)=>!fn(context,token); }, | |
'tag': function(tag){ return (token,amount,context)=>_.contains(amount.tags,tag); } | |
}, | |
buildMatcher = function(params){ | |
return (function(matchers){ | |
return function(token,amount,context){ | |
return !_.find(matchers,(m)=>!m(token,amount,context)); | |
}; | |
}(_.chain(params) | |
.reduce((m,o)=>{ | |
let p=_.rest(o.match(/^(!)?([^:]*):(.*)$/)||[]); | |
p[0]=!!p[0]; | |
m.push({ | |
negate: p[0], | |
type: p[1]||'negate', | |
value: p[2]||0 | |
}); | |
return m; | |
}, []) | |
.map((o)=>{ | |
if('negate'===o.type || !_.has(matchers,o.type)){ | |
return _.constant(false); | |
} | |
return (o.negate ? | |
matchers.negate(matchers[o.type](o.value)) : | |
matchers[o.type](o.value) ); | |
}) | |
.value())); | |
}, | |
buildModifier = function(op,amount,places){ | |
return (_.isUndefined(places) ? | |
modifiers[op](amount) : | |
_.compose(modifiers.places(amount) , modifiers[op](amount)) ); | |
}; | |
on('chat:message',function(msg){ | |
if('api' === msg.type && msg.content.match(/^!apply-change/) && playerIsGM(msg.playerid) ){ | |
if(_.has(msg,'inlinerolls')){ | |
msg.content = _.chain(msg.inlinerolls) | |
.reduce(function(m,v,k){ | |
var ti=_.reduce(v.results.rolls,function(m2,v2){ | |
if(_.has(v2,'table')){ | |
m2.push(_.reduce(v2.results,function(m3,v3){ | |
m3.push(v3.tableItem.name); | |
return m3; | |
},[]).join(', ')); | |
} | |
return m2; | |
},[]).join(', '); | |
m['$[['+k+']]']= (ti.length && ti) || v.results.total || 0; | |
return m; | |
},{}) | |
.reduce(function(m,v,k){ | |
return m.replace(k,v); | |
},msg.content) | |
.value(); | |
} | |
let args = _.rest(msg.content | |
.replace(/<br\/>\n/g, ' ') | |
.replace(/(\{\{(.*?)\}\})/g," $2 ") | |
.split(/\s+--/)), | |
ids = [], | |
ignoreSelected = false, | |
context = { | |
bar: 1, | |
amount: [], | |
mods: [] | |
}; | |
// !apply-change --bar|1 --amount|-[[xdy]] --mod[status:red,!status:green,tag:fire]|*.5 --ids @{target|token_id} | |
// !apply-change --bar|1 --damage|[[xdy]] --mod[status:green]|*.5 --ids @{target|token_id} | |
_.map(args,function(arg){ | |
let cmds=((arg.match(/([^\s]+[\|#]'[^']+'|[^\s]+[\|#]"[^"]+"|[^\s]+)/)||[])[0]||'').split(/[\|#]/), | |
mult=1, | |
cmdparts=((_.isArray(cmds) ? cmds : []).shift()||'').match(/^([^\[{]*)(?:\[([^\]]*)\]|{([^}]*)})?$/), | |
cmd=cmdparts[1], | |
params=((cmdparts[2]||cmdparts[3]||'').length ? ((cmdparts[2]||cmdparts[3]||'').trim().split(/\s*,\s*/)) : []); | |
switch(cmd){ | |
case 'ids': | |
ids=_.union(_.rest(arg.split(/\s+/)),ids); | |
break; | |
case 'ignore-selected': | |
ignoreSelected=true; | |
break; | |
case 'bar': | |
context.bar = (parseInt(cmds[0])||1); | |
break; | |
case 'damage': | |
mult=-1; | |
/* falls through */ | |
case 'amount': | |
context.amount.push({ | |
amount: mult*parseInt(cmds[0]||0), | |
tags: params | |
}); | |
mult=1; | |
break; | |
case 'mod': | |
let parts=cmds[0].match(/^([\*\+\-\/=%](?:\[\d\])?)?([\d\.]*)/), | |
amount=parseFloat(parts[2]), | |
opParts=parts[1].match(/^(.)(?:\[(\d*)\])?$/), | |
op=opParts[1], | |
places=opParts[2]||0; | |
context.mods.push({ | |
matcher: buildMatcher(params), | |
modifier: buildModifier(op,amount,places) | |
}); | |
break; | |
} | |
}); | |
if(!ignoreSelected){ | |
ids=_.union(_.pluck(msg.selected,'_id'),ids); | |
} | |
// get tokens | |
if(ids.length){ | |
_.chain(ids) | |
.uniq() | |
.map(function(t){ | |
return getObj('graphic',t); | |
}) | |
.reject(_.isUndefined) | |
.each(function(token) { | |
let bar=`bar${context.bar}_value`, | |
change=0; | |
_.map(context.amount, (amount)=>{ | |
let value=amount.amount; | |
if(context.mods.length){ | |
_.each(context.mods,(mod)=>{ | |
if(mod.matcher(token,amount,context)){ | |
value=mod.modifier(value); | |
} | |
}); | |
} | |
change+=(value||0); | |
}); | |
token.set(bar,(parseFloat(token.get(bar))||0)+change); | |
}); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment