
I'm looking for feedback on this sheet worker that I've written, because I've got a gut feeling it's over-complicating the process or has some glaring hole that I can't see.
The script is as follows;
on(`change:stealth_bonus_input`, function (event) {
getAttrs(["stealth_bonus_input"], function (attributes) {
// userBonus could be a number, or it could be some calculation based on the character's attributes
// such as @{ATK} + @{DEF} + @{SPD} - this is what I'm attempting to handle
let userInput = attributes["stealth_bonus_input"];
if (!isNaN(userInput)) { /* assign attribute with user input */ } else {
// This would leave us with an array such as ["ATK", "DEF", "SPD"] from the above
let attributes = userInput.replace(/[^a-zA-Z_]+/g, ' ').split(' ');
// Throw that array into the getAttr function so we get whatever attributes match that input
getAttrs(attributes, function(attributes) {
// Parse the attributes object to get access to the key-value pairs
Object.entries(attributes).forEach(([attribute, value]) => {
// Wrap the key in the attribute-accessor tags so that we replace the whole thing from the string
userInput = userInput.replace(`@{${attribute}}`, value);
});
// userBonus would now read something like 3 + 4 + 6
// evaluate that string to get a number and then assign it
const userBonus = parseInt(eval(userInput)) || 0;
setAttrs({"stealth_bonus": userBonus});
});
}
});
});
So basically, I'm looking for advice on making this more robust to whatever craziness the user might enter or for an easier way of handling it entirely.
Edit: formatting