Files
roll20-character-sheets/rapidfire_system/Javascript/triggeredfunctions.js
T

95 lines
3.5 KiB
JavaScript

/*jshint esversion: 11, laxcomma:true, eqeqeq:true*/
/*jshint -W014,-W084,-W030,-W033*/
// Triggered Functions
const setActionCalls = function(trigger,attributes,sections){
debug('setting action calls');
actionAttributes.forEach((base)=>{
let [section,,field] = parseTriggerName(base);
let fieldAction = field.replace(/_/g,'-');
if(section){
sections[section].forEach((id)=>{
attributes[`${section}_${id}_${field}`] = `%{${attributes.character_name}|${section}_${id}_${fieldAction}}`;
});
}else{
attributes[`${field}`] = `%{${attributes.character_name}|${fieldAction}}`;
}
});
debug({updates:attributes.updates});
};
const checkHealth = function(trigger,attributes,sections){
debug('checking current health trackers')
let [section,rowID] = parseRepeatName(trigger.name);
let currDamage = sections[section].indexOf(rowID);
sections[section].forEach((id,index)=>{
if(index !== currDamage){
attributes[`${section}_${id}_damaged`] = 0;
}
attributes[`${section}_${id}_fill`] = index < currDamage ? 1 : 0;
});
};
const updateTrack = function(section,attributes,sections){
let newTracks = attributes[`${section}_max`] - sections[`repeating_${section}`].length;
if(newTracks > 0){
_.range(newTracks).forEach((n)=>{
let newID = generateRowID();
let priorPenalties = sections[`repeating_${section}`].slice(Math.max(0,sections[`repeating_${section}`].length - 2))
.map((id)=>{
return attributes[`repeating_${section}_${id}_penalty`];
});
let newPenalty;
if(priorPenalties[0] !== priorPenalties[1]){
newPenalty = priorPenalties[1] || priorPenalties[0];
}else{
let penStart = priorPenalties[1] || priorPenalties[0];
newPenalty = Math.max(Math.min(-3,penStart),penStart - 1);
}
attributes[`repeating_${section}_${newID}_damaged`] = 0;
attributes[`repeating_${section}_${newID}_penalty`] = newPenalty;
sections[`repeating_${section}`].push(newID);
});
}else if(newTracks < 0){
let slicePoint = Math.max(0,sections[`repeating_${section}`].length + newTracks);
let extraTracks = sections[`repeating_${section}`].slice(slicePoint);
extraTracks.forEach((id)=>{
_removeRepeatingRow(`repeating_${section}_${id}`,attributes,sections);
});
}
};
const maxHealth = function(type,attributes){
const typeSwitch = {
structure:()=> attributes.structure_base,
health:()=> 3 + attributes.body + attributes.spirit + attributes.health_mod
}
return Math.max(typeSwitch[type](),1);
};
const calcHealth = function(trigger,attributes,sections){
const type = trigger.name.replace(/_max/,'');
debug(`calculating ${type}`);
attributes[`${type}_max`] = maxHealth(type,attributes);
let healthDiff = attributes[`${type}_max`] - value(attributes.attributes[`${type}_max`]);
updateTrack(type,attributes,sections);
attributes[type] = attributes[type] + healthDiff;
syncHealth(trigger,attributes,sections);
};
const skillEffects = function(trigger,attributes,sections){
let [section,rowID,field] = parseRepeatName(trigger.name);
let skillName = attributes[`${section}_${rowID}_name`];
const skillSwitch = {
dodge:['defence'],
awareness:['initiative']
};
if(skillSwitch[skillName]){
trigger.affects = [...trigger.affects,...skillSwitch[skillName]];
}
};
const validateActionPenalty = function(trigger,attributes,sections){
if(attributes.action_penalty > 0){
attributes.action_penalty = attributes.action_penalty * -1;
}
};