Skip to content

Instantly share code, notes, and snippets.

@Bastlifa
Last active November 21, 2018 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bastlifa/c2afa72a7b07a8b903d00566cfaa5cae to your computer and use it in GitHub Desktop.
Save Bastlifa/c2afa72a7b07a8b903d00566cfaa5cae to your computer and use it in GitHub Desktop.
Add Weapon to FFG SW Sheets on Roll20
on("ready", function() {
on('chat:message', function(msg) {
if (msg.type === "api" && msg.content.split(' ')[0] === "!AddWep")
{
// strips the first bit off of the api command
let weaponName = msg.content.replace("!AddWep ", "");
AddWep(msg, weaponName);
}
});
});
function AddWep(msg, weaponName)
{
//update this array with your weapons detailed in the WeaponObj function
let aWeapons = ["Bast's Gun", "Other Gun"];
//Make or update macro for your weapon adds
if (msg.content.split(' ')[1] == "MacroGen" && playerIsGM(msg.playerid))
{
MacroGen(aWeapons, msg);
return;
}
// have to select a character's token.
if (!msg.selected || !getObj(msg.selected[0]._type, msg.selected[0]._id).get('represents'))
{
sendChat('', "Please select a representative token");
return;
}
// gets the token's character's ID
let charID = getObj(msg.selected[0]._type, msg.selected[0]._id).get('represents');
//make sure you passed an allowed weapon
if (!aWeapons.includes(weaponName)) {sendChat("","Not a defined weapon"); return; }
//get weapon obj from function
let wepObj = WeaponObj(weaponName);
//return if function didn't get a weapon object
if (!wepObj) {return;}
const addWepForCharacter = function (charID)
{
// make new row from item data.
const data = {};
const repString = `repeating_weapons_${generateRowID()})`;
Object.keys(wepObj).forEach(field => {
data[`${repString}_${field}`] = wepObj[field];
});
// set attributes. I think this is a sheetworkers command. I'm not sure about any of this.
setAttrs(charID, data);
};
addWepForCharacter(charID);
}
// Complicated stuff that I have no idea on. I copied from this thread:
// https://app.roll20.net/forum/post/6847210/5e-shaped-sheet-adding-equipment-through-the-api
var generateUUID = (function() {
"use strict";
var a = 0, b = [];
return function() {
var c = (new Date()).getTime() + 0, d = c === a;
a = c;
for (var e = new Array(8), f = 7; 0 <= f; f--) {
e[f] = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".charAt(c % 64);
c = Math.floor(c / 64);
}
c = e.join("");
if (d) {
for (f = 11; 0 <= f && 63 === b[f]; f--) {
b[f] = 0;
}
b[f]++;
} else {
for (f = 0; 12 > f; f++) {
b[f] = Math.floor(64 * Math.random());
}
}
for (f = 0; 12 > f; f++){
c += "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".charAt(b[f]);
}
return c;
};
}()),
generateRowID = function () {
"use strict";
return generateUUID().replace(/_/g, "Z");
};
//Macro Generator
function MacroGen(aWeapons, msg)
{
let macroAction = "!AddWep ?{Weapon Make";
for (let i=0; i<aWeapons.length; i++)
{
macroAction += "|" + aWeapons[i];
}
macroAction += "}";
if (!findObjs({_type: 'macro', _playerid: msg.playerid, name: 'AddWeapon'})[0])
{
createObj('macro', {
name: 'AddWeapon',
playerid: msg.playerid,
istokenaction: false,
visibleto: "all",
action: macroAction
});
}
else
{
findObjs({_type: 'macro', _playerid: msg.playerid, name: 'AddWeapon'})[0].set("action", macroAction);
}
}
// put your weapons in here. Follow the format used in example weapons.
function WeaponObj(weaponName)
{
let aWeaponObjects =
[
{
weaponmake: "Bast's Gun",
damage: 5,
critical: 4,
//js doesn't like dashes in object keys.
['weapon-encum']: 1,
['weapon-hp']: 2,
weapontype: "Ranged (Light)",
weapondice: "1g",
weapon_bookpage: "The Book of Bast, 173",
weapon_restrict: "no",
weaponrange: "Short", //options are: "Engaged", "Short", "Medium", "Long", "Extreme"
weaponskill: "skill(@{rankLight}|@{agility})", //too much to list. If using chrome, hit f12, little arrow thing to select elem
//then select weapon skill. click dropdown, look at options.
weaponcondition: "1blk", //options include "new", "1blk", "1p", "unusableWeapon"
weapon_rarity: 7, //integer between 1 and 10
weapon_value: "5000 credits",
weaponspecial: "Stun Setting, Bulky",
weaponattchments: true, //view weapon attachments. Doesn't seem to work.
weaponattach1: "Scope",
weaponattachHP1: 2, //integer maybe
weaponattachbase1: "1b", // no idea what to put in here. This may add a blue die.
weaponattachmod1: "red dot added to scope"
},
{
weaponmake: "Other Gun",
damage: 11, //integer
critical: 3, //integer
//js doesn't like dashes in object keys.
['weapon-encum']: 7, //integer, probably
['weapon-hp']: 4, //same
weapontype: "Ranged (Heavy)", //just text, I think
weapondice: "",
weapon_bookpage: "EotE 1", //just text
weapon_restrict: "yes", //yes if R, no if not R
weaponrange: "Long",
weaponskill: "skill(@{rankMedicine}|@{intellect})", //Heal Gun
weaponcondition: "new", //options include "new", "1blk", "1p", "unusableWeapon"
weapon_rarity: 7, //integer between 1 and 10
weapon_value: "2600 (R) credits",
weaponspecial: "Auto-Fire, Cumbersome (3), Pierce (2)",
weaponattchments: false,
}
]
//go through the array, looking for weaponmake matching the name of the weapon in function parameter
//if found, return the weapon object.
for (let i=0; i<aWeaponObjects.length; i++)
{
if (aWeaponObjects[i].weaponmake == weaponName)
{
sendChat("", aWeaponObjects[i].weaponmake + " found");
return (aWeaponObjects[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment