I am trying to recreate complex paper based roll tables using API scripts which will be called from macros attached to the character sheet. The code I am using is as follows:
on("chat:message", function(msg) {
//This allows players to enter !sr <number> to roll a number of d6 dice with a target of 4.
if(msg.type == "api" && msg.content.indexOf("!roll1 ") !== -1) {
var numdice = msg.content.replace("!roll1 ", "");
var splited = numdice.split(' ');
sendChat(msg.who, splited[0]);
sendChat(msg.who, splited[1]);
sendChat(msg.who, splited[2]);
var damage = AT1(splited[0], splited[2]);
sendChat(msg.who, damage);
};
});
// ---------------------------------
function AT_weapon_stats(weapon) {
var fumble=0;
var pri_crit=0;
var sec_crit=0;
// Calculate Fumble/Critical Chances/Types
if (weapon == 1) {fumble=3; pri_crit=2; sec_crit=0;}
else if (weapon == 2) {fumble=1; pri_crit=3; sec_crit=0;}
else if (weapon == 3) {fumble=4; pri_crit=2; sec_crit=0;}
else if (weapon == 4) {fumble=4; pri_crit=2; sec_crit=0;}
else if (weapon == 5) {fumble=3; pri_crit=2; sec_crit=0;}
else if (weapon == 6) {fumble=4; pri_crit=1; sec_crit=0;}
else if (weapon == 7) {fumble=3; pri_crit=1; sec_crit=0;}
else if (weapon == 8) {fumble=8; pri_crit=1; sec_crit=3;}
else if (weapon == 9) {fumble=6; pri_crit=5; sec_crit=0;}
else if (weapon == 10) {fumble=4; pri_crit=1; sec_crit=0;}
else if (weapon == 11) {fumble=6; pri_crit=5; sec_crit=2;}
else if (weapon == 12) {fumble=4; pri_crit=3; sec_crit=0;}
else if (weapon == 13) {fumble=5; pri_crit=3; sec_crit=2;}
else if (weapon == 14) {fumble=75; pri_crit=2; sec_crit=1;}
else if (weapon == 15) {fumble=8; pri_crit=1; sec_crit=3;}
else if (weapon == 16) {fumble=3; pri_crit=1; sec_crit=0;}
else if (weapon == 17) {fumble=5; pri_crit=2; sec_crit=1;}
else if (weapon == 18) {fumble=3; pri_crit=5; sec_crit=1;}
else if (weapon == 19) {fumble=5; pri_crit=3; sec_crit=0;}
else if (weapon == 20) {fumble=7; pri_crit=3; sec_crit=0;}
else if (weapon == 21) {fumble=4; pri_crit=3; sec_crit=0;}
else if (weapon == 22) {fumble=5; pri_crit=3; sec_crit=0;}
else if (weapon == 23) {fumble=5; pri_crit=1; sec_crit=0;}
else if (weapon == 24) {fumble=4; pri_crit=2; sec_crit=0;}
else if (weapon == 25) {fumble=6; pri_crit=2; sec_crit=0;}
var return_value = [fumble, pri_crit, sec_crit];
return return_value;
};
function AT1(weapon, target_armour) {
var weapon_details = AT_weapon_stats(weapon);
var return_value;
let fumble=weapon_details[0];
roll_val = Math.floor(Math.random() * Math.floor(100));
if (roll_val < fumble) {return_value="F";}
else {return_value = roll_val;}
sendChat('API',"weapon_details[0]: " + weapon_details[0])
sendChat('API',"weapon_details[1]: " + weapon_details[1])
sendChat('API',"weapon_details[2]: " + weapon_details[2])
sendChat('API',"roll_val: " + roll_val)
sendChat('API',"weapon: " + weapon)
sendChat('API',"target_armour: " + target_armour)
sendChat('API',"return_value: " + return_value)
return roll_val;
};
I am calling the function using the following command which is located inside of a fieldset <td class="sheet-table-data-center-stats"><textarea rows="1" style="width: 45px; height: 19px; margin-bottom: 0;" name="attr_wpn-attack-macro">!roll1 @{wpn_name} @{wpn_total} @{target|main_armour}</textarea></textarea><td> If line 11 (sendChat(msg.who, damage);) is uncommented I get the following error in the API Output Console: Your scripts are currently disabled due to an error that was detected. Please make appropriate changes to your scripts and click the "Save Script" button and we'll attempt to start running them again. More info... For reference, the error message generated was: TypeError: op.content.replace is not a function
TypeError: op.content.replace is not a function
at Object.d20.textchat.doChatInput (eval at <anonymous> (/home/node/d20-api-server/api.js:155:1), <anonymous>:368:29)
at sendChat (/home/node/d20-api-server/api.js:1788:16)
at apiscript.js:11:5
at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:65:16)
at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:70:8)
at /home/node/d20-api-server/api.js:1634:12
at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560
at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147)
at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546)
at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) If line 11 is commented out all sendChat calls are successfully made. Please can you advise why line 11 is failing and what I need to do to rectify?