Ok, so what I mean is, it seems to work at random. It always works right after restarting the sandbox, but while it stays on, it seems to randomly not respect the tested value, and always default to the first item in the list. The script below is the whole script, normally wouldn't post the whole thing, but something is making this act wonky. I can change out to three separate if/then statements if I have to... I am calling the API from a macro that is - !cncAttackV2 @{selected|token_ID} 1 ?{Enter Advantage/Disadvantage/Standard|Standard,S|Advantage,A|Disadvantage,D} @{target|token_id} Here is the script - on('ready',()=>{
on('chat:message', async function(msg) {//the async keyword denotes this function as an asynchronous function. This is necessary to use the await keyword later
// Syntax: !cncAttackV2 <attacker> <weapon #> <advantage> <target>
//
// <attacker> - The token id of the attacking character
// <weapon #> - Number of the weapon slot
// <advntage> -
// S - Standard attack
// A - Advantage attack
// D - Disadvantage attack
//
// Example: !cncAttackV2 @{selected|token_id} 1 S @{target|token_id}
var args = msg.content.split(' ');
if('!cncAttackV2' !== args.shift() ) {
return;
}
let attackerTokenID = args[0];
let attackerWeaponNumber = args[1];
let attackerAdvantage = args[2];
let attackerTokenObj = getObj("graphic", attackerTokenID);
let attackerObj = getObj('character',(attackerTokenObj||{get:()=>{}}).get('represents'));
let attackerName = getAttrByName(attackerObj.id,'CharacterName','current');
let attackerWeaponName = getAttrByName(attackerObj.id,'WeaponName_' + attackerWeaponNumber);
let attackerWeaponDamage = getAttrByName(attackerObj.id,'WeaponDmg_' + attackerWeaponNumber);
let attackerTHBth = parseInt(getAttrByName(attackerObj.id,'WeaponBth_' + attackerWeaponNumber),10);
let attackerTHMod = parseInt(getAttrByName(attackerObj.id,'WeaponHitMod_' + attackerWeaponNumber),10);
let attackerTHMagic = parseInt(getAttrByName(attackerObj.id,'WeaponHitMagic_' + attackerWeaponNumber),10);
let attackerTHMisc = parseInt(getAttrByName(attackerObj.id,'WeaponHitMisc_' + attackerWeaponNumber),10);
let attackerDamMod = parseInt(getAttrByName(attackerObj.id,'WeaponDmgMod_' + attackerWeaponNumber),10);
let attackerDamMagic = parseInt(getAttrByName(attackerObj.id,'WeaponDmgMagic_' + attackerWeaponNumber),10);
let attackerDamMisc = parseInt(getAttrByName(attackerObj.id,'WeaponDmgMisc_' + attackerWeaponNumber),10);
let attackerToHitBonus = attackerTHBth + " + " + attackerTHMod + " +" + attackerTHMisc + " + " + attackerTHMagic;
let attackerToHitBonusTotal = attackerTHBth + attackerTHMod + attackerTHMagic + attackerTHMisc;
let attackerDamageBonus = attackerDamMod + " + " + attackerDamMisc + " + " + attackerDamMagic;
let toHitRoll = 0;
let dieResult1 = await new Promise((resolve,reject)=>{
sendChat(msg.who, '/roll 1d20', function(ops) {
let rollResult = JSON.parse(ops[0].content);
resolve(rollResult.total);
});
});
let dieResult2 = await new Promise((resolve,reject)=>{
sendChat(msg.who, '/roll 1d20', function(ops) {
let rollResult = JSON.parse(ops[0].content);
resolve(rollResult.total);
});
});
sendChat("Combat Computer", "Ad - " + attackerAdvantage)
switch(attackerAdvantage) {
case 'S':
toHitRoll = dieResult1;
break;
case 'A':
if (dieResult1 >= dieResult2) {
toHitRoll = dieResult1;
sendChat("Combat Computer","Advantage Die Roll: " + dieResult1 + " / " + dieResult2);
} else {
toHitRoll = dieResult2;
}
break;
case 'D':
if (dieResult1 <= dieResult2) {
toHitRoll = dieResult1;
sendChat("Combat Computer","Disadvantage Die Roll: " + dieResult1 + " / " + dieResult2);
} else {
toHitRoll = dieResult2;
}
break;
};
let toHitTotal = toHitRoll + attackerToHitBonusTotal;
let ToHitResult = attackerName + " is attacking with a " + attackerWeaponName + ". To hit roll: " + toHitRoll + " + " + attackerToHitBonus + " Total: " + toHitTotal;
sendChat("Combat Computer", ToHitResult);
let targetTokenID = args[3];
let targetTokenObj = getObj("graphic", targetTokenID);
let targetObj = getObj('character',(targetTokenObj||{get:()=>{}}).get('represents'));
let targetArmorAC = parseInt(getAttrByName(targetObj.id,'ArmorAC','current'),10);
let targetShieldAC = parseInt(getAttrByName(targetObj.id,'ShieldAC','current'),10);
let targetModAC = parseInt(getAttrByName(targetObj.id,'ModAC','current'),10);
let targetMagicAC = parseInt(getAttrByName(targetObj.id,'MagicAC','current'),10);
let targetMiscAC = parseInt(getAttrByName(targetObj.id,'MiscAC','current'),10);
let targetAC = 10 + (targetArmorAC + targetShieldAC + targetModAC + targetMagicAC + targetMiscAC);
let targetHP = parseInt(targetTokenObj.get("bar1_value"));
let tokenAC = parseInt(targetTokenObj.get("bar2_value"));
//toHitRoll = 20;
if (toHitRoll == 20) {
sendChat("Combat Computer", attackerName + " scored a critical hit!");
let baseDamage = await new Promise((resolve,reject)=>{
sendChat(msg.who, '/roll ' + attackerWeaponDamage, function(ops) {
let rollResult = JSON.parse(ops[0].content);
resolve(rollResult.total);
});
});
let criticalDamage = await new Promise((resolve,reject)=>{
sendChat(msg.who, '/roll ' + attackerWeaponDamage, function(ops) {
let rollResult = JSON.parse(ops[0].content);
resolve(rollResult.total);
});
});
baseDamage += attackerDamMod + attackerDamMagic + attackerDamMisc
criticalDamage += attackerDamMagic
let totalDamage = baseDamage + criticalDamage
sendChat("Combat Computer","Damage - Base: " + baseDamage + " Critical: " + criticalDamage + " Total: " + totalDamage)
let targetHP = parseInt(targetTokenObj.get("bar1_value"))
targetTokenObj.set("bar1_value", targetHP - totalDamage)
} else {
if (toHitTotal >= tokenAC){
sendChat(msg.who, "/roll "+attackerWeaponDamage, function(ops) {
let rollResult = JSON.parse(ops[0].content);
let dieResult = rollResult.total
sendChat("Combat Computer", attackerName + " scored a hit!!")
let totalDamage = dieResult + attackerDamMod + attackerDamMagic + attackerDamMisc
sendChat("Combat Computer", "Base Damage: " + dieResult + " + " + attackerDamageBonus + " Total: " + totalDamage)
let targetHP = parseInt(targetTokenObj.get("bar1_value"))
targetTokenObj.set("bar1_value", targetHP - totalDamage)
});
} else {
sendChat("Combat Computer", attackerName + " failed to score a hit.")
}
}
});
});