Created
October 20, 2018 01:24
-
-
Save oukag/b0b1f3df5ea0dbe362c03d9c3c7e5356 to your computer and use it in GitHub Desktop.
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
var handleammo = function (msg,character,player) { | |
if(msg.content.indexOf("ammo=") === -1) { | |
// UNABLE TO FIND AMMO | |
return; | |
} | |
var ammostr; | |
if(msg.content.indexOf("{{charname=") > -1) { | |
ammostr = (msg.content.split("ammo=")[1]||'').split(" {{charname=")[0]; | |
} | |
else { | |
ammostr = (msg.content.split("ammo=")[1]||'').split(" charname")[0]; | |
} | |
_.each(ammostr.split(","), function(ammofull) { | |
ammofull = ammofull.trim(); | |
var ammoid = ""; | |
var ammoname = ""; | |
// check to see if more than one ammo is used | |
var ammouseregex = /[[].*]/; | |
var ammousestr = ammouseregex.exec(ammofull) ? ammofull.match(ammouseregex)[0] : null; | |
ammofull = ammofull.replace(ammousestr,""); | |
var ammouses = ammousestr ? parseInt(ammousestr.replace("[","").replace("]")) : 1; | |
// get the name and id for the ammo resource | |
if(ammofull.substring(0,1) === "-") { | |
// ammofull was just the id for the resource | |
ammoid = ammofull; | |
var ammoresource = findObjs({type: 'attribute', characterid: character.id, id: ammoid}, {caseInsensitive: true})[0]; | |
if(ammoresource) { | |
ammoname = getAttrByName(character.id, ammoresource.get("name") + "_name"); | |
} | |
} | |
else if(ammofull.indexOf("|") > -1) { | |
// ammofull contained the id for the resource | |
var temp = ammofull.split("|"); | |
ammoid = temp[1]; | |
ammoname = temp[0]; | |
} | |
else { | |
// no id was found for the resource | |
ammoname = ammofull; | |
var resources = filterObjs(function(obj) { | |
return obj.get("type") === "attribute" | |
&& obj.get("characterid") === character.id | |
&& (obj.get("current") + "").toLowerCase() === ammofull.toLowerCase() | |
&& obj.get("name").indexOf("resource") > -1; | |
}); | |
if(!resources[0]) { | |
log("UNABLE TO FIND RESOURCE"); | |
return; | |
} | |
var resname = resources[0].get("name").replace("_name",""); | |
ammoid = findObjs({type: 'attribute', characterid: character.id, name: resname}, {caseInsensitive: true})[0].id; | |
var atkid = null; | |
var reg = new RegExp("repeating_attack_(.+)_attack_dmg"); | |
var match = reg.exec(msg.content); | |
if(match) { | |
atkid = match[1]; | |
} | |
else { | |
var atkname = (msg.content.split("{{rname=")[1]||'').split("}}")[0]; | |
var attacks = filterObjs(function(obj) { | |
return obj.get("type") === "attribute" | |
&& obj.get("characterid") === character.id | |
&& obj.get("current") === atkname; | |
}); | |
reg = new RegExp("repeating_attack_(.+)_atkname"); | |
_.each(attacks, function(atk) { | |
match = reg.exec(atk.get("name")); | |
if(match) { | |
atkid = match[1]; | |
} | |
}); | |
} | |
if(!atkid) { | |
log("UNABLE TO FIND ATTACK ID"); | |
return; | |
} | |
var atkammo = findObjs({type: 'attribute', characterid: character.id, name: "repeating_attack_" + atkid + "_ammo"}, {caseInsensitive: true})[0]; | |
var value = ammousestr | |
? atkammo.get("current").replace(ammoname+ammousestr, ammoname+ammousestr+"|"+ammoid) | |
: atkammo.get("current").replace(ammoname, ammoname + "|" + ammoid); | |
atkammo.set({current: value}); | |
} | |
ammoresource = findObjs({type: 'attribute', characterid: character.id, id: ammoid}, {caseInsensitive: true})[0]; | |
if(ammoresource) { | |
ammoresource.set({current: ammoresource.get("current") - ammouses}); | |
var ammoitemid = getAttrByName(character.id, ammoresource.get("name") + "_itemid"); | |
if(ammoitemid) { | |
var ammoitem = findObjs({type: 'attribute', characterid: character.id, name: "repeating_inventory_" + ammoitemid + "_itemcount"}, {caseInsensitive: true})[0]; | |
if(ammoitem) { | |
ammoitem.set({current: ammoitem.get("current") - 1}); | |
} | |
var ammoweight = findObjs({type: 'attribute', characterid: character.id, name: "repeating_inventory_" + ammoitemid + "_itemweight"}, {caseInsensitive: true})[0]; | |
var totalweight = findObjs({type: 'attribute', characterid: character.id, name: "weighttotal"}, {caseInsensitive: true})[0]; | |
if(ammoweight && totalweight) { | |
totalweight.set({current: totalweight.get("current") - ammoweight.get("current")}); | |
} | |
} | |
var output = ammoname + ":\n" + ((ammoresource.get("current") >= 0) ? ammoresource.get("current") + " LEFT" : "OUT OF AMMO"); | |
var formattedOutput = (getAttrByName(character.id, "wtype") === "" ? "" : "/w gm ") + "&{template:desc} {{desc=" + output + "}}"; | |
if(ammoresource.get("current") <= 0 || state.FifthEditionOGLbyRoll20.ammotracking !== "quiet") { | |
sendChat(msg.who, formattedOutput); | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment