Skip to content

Instantly share code, notes, and snippets.

@BaldarSilveraxe
Created December 10, 2013 01:55
Show Gist options
  • Save BaldarSilveraxe/7884617 to your computer and use it in GitHub Desktop.
Save BaldarSilveraxe/7884617 to your computer and use it in GitHub Desktop.
Roll20API
on('chat:message', function(msg) {
//state.inventoryOfItems = undefined;
//state.inventoryOfItems = [];
if(msg.type !== "api"){return};
roll20API.contentPlayerid = getObj("player", msg.playerid);
roll20API.contentUserid = roll20API.contentPlayerid.get("d20userid");
if(allowedUser.indexOf(roll20API.contentUserid) == -1){return};
roll20API.contentGiven = msg.content.toLowerCase();
roll20API.contentGivenProper = msg.content;
if(roll20API.contentGiven.indexOf(' ') == -1){
roll20API.contentMessage = null;
roll20API.contentCommand = roll20API.contentGiven.substr(1, roll20API.contentGiven.length);
}else{
roll20API.contentMessage = roll20API.contentGiven.substr(roll20API.contentGiven.indexOf(' ') + 1);
roll20API.contentCommand = roll20API.contentGiven.substr(1, roll20API.contentGiven.indexOf(' ')-1);
};
roll20API.valid = false;
if(roll20API.contentMessage == "yes"){roll20API.valid = true;};
if(state.indexItemNumbers.length == 0){state.indexItemNumbers.push("00000-00000")};
if(findObjs({ _type: "handout", name: "API_Trader_Joe" }).length == 0){
refreshCatalog();
refreshCharacters();
resetCursorLocation();
createToFromTable();
refreshToFromTable();
};
traderJoeCommandSwitch();
});
traderJoeCommandSwitch = function() {
switch (roll20API.contentCommand){
case "traderjoeup":
moveCursorUp();
refreshToFromTable();
break;
case "traderjoedown":
moveCursorDown();
refreshToFromTable();
break;
case "traderjoeright":
cycleRight();
refreshToFromTable();
break;
case "findmarcoid":
findMacroId();
break;
case "maketrade":
maketrade();
break;
case "refresh":
refreshCatalog();
refreshCharacters();
break;
case "trade":
tradeMacro();
break;
};
};
sortPartyInventory = function () {
if(findObjs({ _type: "handout", name: "API Party Inventory" }).length == 0){createPartyInventory();};
state.PartyInventory = findObjs({ _type: "handout", name: "API Party Inventory" })[0].get("_id");
//sorting
var listArray = [];
_.each(findObjs({ _type: "character"}), function(loopCharacter) {
characterId = loopCharacter._id;if(characterId == undefined){characterId = loopCharacter.get("id");};
characterName = loopCharacter.name;if(characterName == undefined){characterName = loopCharacter.get("name");};
_.each(state.inventoryOfItems, function(inventoryItem) {
OwnerId = inventoryItem.id;if(OwnerId == undefined){OwnerId = inventoryItem.get("id");};
if(characterId == OwnerId){
ItemKey = inventoryItem.itemKey;if(ItemKey == undefined){ItemKey = inventoryItem.get("itemKey");};
descriptionItem = inventoryItem.description;if(descriptionItem == undefined){descriptionItem = inventoryItem.get("description");};
listArray.push({
characterName: characterName,
OwnerId: OwnerId,
ItemKey: ItemKey,
descriptionItem: descriptionItem
})
};
});
});
//sorting
var htmlItems = "\
<table " + roll20API.tableStyle + ">\
<tr>\
<td " + roll20API.tdStyleDbWf + "><b>Character</b></td>\
<td " + roll20API.tdStyleDbWf + "><b>Description</b></td>\
<td " + roll20API.tdStyleDbWf + "><b>Item Key</b></td>\
</tr>"
_.each(listArray, function(items) {
htmlItems += "<tr>\
<td>" + items.characterName + "</td>\
<td>" + items.descriptionItem + "</td>\
<td>" + items.ItemKey + "</td>\
</tr>"
});
htmlItems += "</table>"
findObjs({ _type: "handout", _id: state.PartyInventory })[0].set({notes: htmlItems});
};
updateInventoryOfItems = function () {
for (var i=0;i<roll20API.tradeVolume;i++){
getItemNumberUnique();
state.inventoryOfItems.push({itemKey: newUniqueItemNumber, id: roll20API.tradeToId, description: roll20API.tradeItemDesc});
};
sortPartyInventory();
};
tradeMacro = function () {
findObjs({ _type: "macro", _id: "-JAEYsIdijsELtkIeEYv"})[0].set({action: ""});
tradeArray = roll20API.contentGivenProper.split(',');
roll20API.tradeFromId = tradeArray[0]
roll20API.tradeToId = tradeArray[1]
roll20API.tradeItemDesc = roll20API.itemArray[parseInt(tradeArray[2])]
roll20API.tradeVolume = parseInt(tradeArray[3])
if(roll20API.tradeItemDesc == undefined){
sendChat("API", "Item \"" + tradeArray[2] + "\" was not found. Trade cancelled.");
return
};
if(isNaN(parseInt(tradeArray[3])) || roll20API.tradeVolume < 1){
sendChat("API", "Cannot trade \"" + tradeArray[3] + "\" number of items. Trade cancelled.");
return
};
if(roll20API.tradeToId == state.TraderJoeId){
sendChat("API", "Read the sign: \"NO REFUNDS\". Trade cancelled.");
return
};
updateInventoryOfItems();
};
getItemNumberUnique = function() {
TempItemNumberUnique = false;
while(TempItemNumberUnique == false){
getTempItemNumber();
TempItemNumberUnique = true;
if(state.indexItemNumbers.length == 0){state.indexItemNumbers.push("00000-00000")};
for (var loopIndex = 0; loopIndex < state.indexItemNumbers.length; loopIndex++) {
if(tempItemNumber == state.indexItemNumbers[loopIndex]){TempItemNumberUnique = false;};
};
state.indexItemNumbers.push(tempItemNumber);
newUniqueItemNumber = tempItemNumber;
};
};
getTempItemNumber = function(){
getItemNumberPart();
tempItemNumber = partNumber + "-";
getItemNumberPart();
tempItemNumber = tempItemNumber + partNumber;
};
getItemNumberPart = function() {
partNumber = "10010";
var zeroCheck = "0";
var oneCheck = "1";
while (partNumber.indexOf(zeroCheck) !== -1 || partNumber.indexOf(oneCheck) !== -1 || partNumber.length !== 5){
var randNumber = Math.floor(Math.random() * 99999);
partNumber = randNumber.toString();
};
return partNumber;
};
updateTradeMacro = function () {
var marcoAction = "!trade " + roll20API.playerFromId + "," + roll20API.playerToId + ","
marcoAction += "?{Which item|},?{How many|}"
findObjs({ _type: "macro", _id: "-JAEYsIdijsELtkIeEYv"})[0].set({action: marcoAction});
};
findMacroId = function () {
if(findObjs({ _type: "macro", name: "↵" }).length == 0){
sendChat("API", "/direct The _id for ↵ is not found!")
return
};
sendChat("API", "/direct The _id for ↵ is <br>" + findObjs({ _type: "macro", name:"↵"})[0].get("_id"))
};
cycleRight = function () {
if(roll20API.tableLocationRow == undefined
|| roll20API.tableLocationRow == null
|| isNaN(roll20API.tableLocationRow)){roll20API.tableLocationRow = 0;};
if(roll20API.tableLocationRow == 0){state.charactersFrom.push(state.charactersFrom.shift());};
if(roll20API.tableLocationRow == 1){state.charactersTo.push(state.charactersTo.shift());};
if(roll20API.tableLocationRow == 2){state.CatalogFamily.push(state.CatalogFamily.shift());};
};
moveCursorUp = function () {
if(roll20API.tableLocationRow == undefined
|| roll20API.tableLocationRow == null
|| isNaN(roll20API.tableLocationRow)){roll20API.tableLocationRow = 0;};
roll20API.tableLocationRow = roll20API.tableLocationRow - 1;
if(roll20API.tableLocationRow < 0){roll20API.tableLocationRow = 2;};
}
moveCursorDown = function () {
if(roll20API.tableLocationRow == undefined
|| roll20API.tableLocationRow == null
|| isNaN(roll20API.tableLocationRow)){roll20API.tableLocationRow = 0;};
roll20API.tableLocationRow = roll20API.tableLocationRow + 1;
if(roll20API.tableLocationRow > 2){roll20API.tableLocationRow = 0;};
}
refreshCatalog = function () {
state.CatalogFamily = _.chain(roll20API.TraderJoeCatalog).map(function(item) { return item.family }).uniq().value();
_.each(state.CatalogFamily, function(section) {
state.catalogSections[section] = [];
_.each(roll20API.TraderJoeCatalog, function(item) {
if(item.family == section){state.catalogSections[section].push(item);};
});
});
};
refreshCharacters = function () {
state.characters = [];
state.charactersTo = [];
state.charactersFrom = [];
_.each(findObjs({ _type: "character"}), function(loopCharacter) {
if(loopCharacter.get("archived") == false){
state.characters.push(loopCharacter);
state.charactersTo.push(loopCharacter);
state.charactersFrom.push(loopCharacter);
};
});
if(findObjs({ _type: "character", name: "Trader Joe" }).length == 0){createTraderJoe();};
state.TraderJoeId = findObjs({ _type: "character", name: "Trader Joe" })[0].get("_id");
if(findObjs({ _type: "handout", name: "API Party Inventory" }).length == 0){createPartyInventory();};
state.PartyInventory = findObjs({ _type: "handout", name: "API Party Inventory" })[0].get("_id");
};
resetCursorLocation = function () {
roll20API.tableLocationRow = 0;
};
createTraderJoe = function () {
if(findObjs({ _type: "character", name: "Trader Joe" }).length == 0){
createObj("character", {
name: "Trader Joe",
bio: "<h3>Welcome to \"Trader Joe's\"!</h3><br><br>\
<b>Return Policy details:</b><br><br>\
If the items are not as described, <i>we offer no refunds or store credit.</i><br>\
If the items are found to be cursed, <i>we offer no refunds or store credit.</i><br>\
If the items are found to be broken, <i>we offer no refunds or store credit.</i><br>\
If the items are found to be \"stolen property\", we never saw it in our life and <i>we offer no refunds or store credit.</i><br>\
If there is a problem, we can work together to resolve the problem, however <i>we offer no refunds or store credit.</i><br>\
Shipping charges may be applied and for those charges <i>we offer no refunds or store credit.</i><br>\
Items ordered but not purchased will incur a stocking fee for which <i>we offer no refunds or store credit.</i><br>\
<h2>WE DO NOT RENT PIGS!</h2>"
});
};
state.TraderJoeId = findObjs({ _type: "character", name: "Trader Joe" })[0].get("_id");
};
createPartyInventory = function () {
if(findObjs({ _type: "handout", name: "API Party Inventory" }).length == 0){
createObj("handout", {
name: "API Party Inventory",
});
};
state.PartyInventory = findObjs({ _type: "handout", name: "API Party Inventory" })[0].get("_id");
};
createToFromTable = function () {
if(findObjs({ _type: "handout", name: "API_Trader_Joe" }).length == 0){
createObj("handout", {
name: "API_Trader_Joe",
notes: "Welcome to the API Trading Post."
});
};
state.API_TraderId = findObjs({ _type: "handout", name: "API_Trader_Joe" })[0].get("_id");
createTraderJoe();
createPartyInventory();
};
refreshToFromTable = function () {
var tableStyle = roll20API.tableStyle;
var Ct1A1 = "";
var Ct1B1 = "";
var Ct1A2 = "";
var Ct1B2 = "";
var Ct1A3 = "";
var Ct1B3 = "";
if(roll20API.tableLocationRow == 0){Ct1A1 = roll20API.tdStyleDbWf;Ct1B1 = roll20API.tdStyleLbBf};
if(roll20API.tableLocationRow == 1){Ct1A2 = roll20API.tdStyleDbWf;Ct1B2 = roll20API.tdStyleLbBf};
if(roll20API.tableLocationRow == 2){Ct1A3 = roll20API.tdStyleDbWf;Ct1B3 = roll20API.tdStyleLbBf};
Ct1B1value = state.charactersFrom[0].name
if(Ct1B1value == undefined){Ct1B1value = state.charactersFrom[0].get("name");}
Ct1C1value = state.charactersFrom[0]._id
if(Ct1C1value == undefined){Ct1C1value = state.charactersFrom[0].get("_id");}
Ct1B2value = state.charactersTo[0].name
if(Ct1B2value == undefined){Ct1B2value = state.charactersTo[0].get("name");}
Ct1C2value = state.charactersTo[0]._id
if(Ct1C2value == undefined){Ct1C2value = state.charactersTo[0].get("_id");}
Ct1B3value = state.CatalogFamily[0]
var htmlToFrom = "\
<table " + tableStyle + ">\
<tr>\
<td " + Ct1A1 + "><b>From</b></td>\
<td " + Ct1B1 + ">" + Ct1B1value + "</td>\
<td>" + Ct1C1value + "</td>\
<tr>\
</tr>\
<td " + Ct1A2 + "><b>To</b></td>\
<td " + Ct1B2 + ">" + Ct1B2value + "</td>\
<td>" + Ct1C2value + "</td>\
</tr>\
<tr>\
<td " + Ct1A3 + "><b>Item Type</b></td>\
<td colspan=\"2\"" + Ct1B3 + ">" + Ct1B3value + "</td>\
</tr>\
</table>"
var htmlItems = "<br>No items to trade."
if(Ct1C1value == state.TraderJoeId){
var htmlItems = "\
<br>\
<table " + tableStyle + ">\
<tr><td><b>Item Number</b></td><td><b>Description</b></td><td><b>Cost</b></td></tr>"
roll20API.itemArray = new Array();
var itemCount = 0;
_.each(state.catalogSections[state.CatalogFamily[0]], function(item) {
roll20API.itemArray[itemCount] = item.description;
htmlItems += "<tr>"
htmlItems += "<td>(" + itemCount + ")</td>"
htmlItems += "<td>" + roll20API.itemArray[itemCount] + "</td>"
htmlItems += "<td style=\"text-align: right;\">" + item.cost + item.costunit + "</td>"
htmlItems += "</tr>"
itemCount++
});
htmlItems += "</table>"
};
var noteUpdate = htmlToFrom + htmlItems
findObjs({ _type: "handout", _id: state.API_TraderId })[0].set({notes: noteUpdate});
roll20API.playerFromName = Ct1B1value
roll20API.playerFromId = Ct1C1value
roll20API.playerToName = Ct1B2value
roll20API.playerToId = Ct1C2value
roll20API.itemCount = Ct1B3value
updateTradeMacro();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment