Last active
February 24, 2020 17:19
-
-
Save andreberg/5110212 to your computer and use it in GitHub Desktop.
[Guild Wars 2 Spidy functions] Used in my Google Docs spreadsheets for price fetches and conversions. #guildwars2 #spidy #googledocs #spreadsheets
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
/** | |
* Guild Wars 2 Spidy Functions | |
* | |
* for use in Google Docs spreadsheets | |
* via the Script Editor. | |
* | |
* After an idea and first concept of | |
* Valaadus.5012 on Northern Shiverpeaks | |
* | |
* Additional functions by Cauldron.1653 | |
* on Riverside. | |
*/ | |
// ======= GW2 Spidy ======= | |
/** | |
* Builds a url that can be used to query the gw2spidy API. | |
*/ | |
function buildSpidyAPIUrl(itemID, format, version) { | |
if (typeof version === "undefined") { | |
version = "v0.9"; | |
} | |
if (typeof format === "undefined") { | |
format = "json"; | |
} | |
var url = "http://www.gw2spidy.com/api/" + escape(version) + "/" + escape(format) + "/item/" + escape(itemID); | |
return url; | |
} | |
/** | |
* Builds a url that can be used to link to the gw2spidy page for an item. | |
*/ | |
function buildSpidyItemUrl(itemID) { | |
var url = "http://www.gw2spidy.com/item/" + escape(itemID); | |
return url; | |
} | |
/** | |
* Queries gw2spidy and returns a data object for an item. | |
* | |
* "format" can be "json" (the default if undefined) or "csv". | |
*/ | |
function getDataFromSpidy(itemID, format) { | |
if ((typeof format === "undefined") || (format !== "json" && format !== "csv")) { | |
format = "json"; | |
} | |
var url = buildSpidyAPIUrl(itemID, format); | |
var data = UrlFetchApp.fetch(url); | |
var dataString = data.getContentText(); | |
var result = dataString; | |
if (format === "json") { | |
result = JSON.parse(dataString).result; | |
} | |
return result; | |
} | |
/** | |
* Returns the value of a property "propertyName" of an item with ID "itemID". | |
* | |
* This can be used as a generic function to query anything there is to know about | |
* an item. Currently the GW2Spidy API supports the following properties: | |
* | |
* data_id : 23654, | |
* name : "Fake Item", | |
* rarity : 3, | |
* restriction_level : 72, | |
* img : "http://www.url-to-offical-gw2-site.com/img.png", | |
* type_id : 1, | |
* sub_type_id : 2, | |
* price_last_changed : "YYYY-MM-DD HH:II:SS UTC", | |
* max_offer_unit_price : 6523, | |
* min_sale_unit_price : 9345, | |
* offer_availability : 1235232, | |
* sale_availability : 203203, | |
* gw2db_external_id : 2836, # the ID they use in their URLs (for tooltips etc) | |
* sale_price_change_last_hour : 40, # this is the percentage the item price changed since the last hour | |
* offer_price_change_last_hour : 70 # same --^ I know it ain't pretty but have to do with this for now ;) | |
*/ | |
function getItemProperty(itemID, propertyName) { | |
var jsonObject = getDataFromSpidy(itemID, "json"); | |
return jsonObject[propertyName]; | |
} | |
/** | |
* Returns the current sell value in silver for an item. | |
*/ | |
function getItemSellValue(itemID) { | |
var minSaleUnitPrice = getItemProperty(itemID, 'min_sale_unit_price'); | |
return minSaleUnitPrice; | |
} | |
/** | |
* Returns the name of an item. | |
*/ | |
function getItemName(itemID) { | |
var itemName = getItemProperty(itemID, 'name'); | |
return itemName; | |
} | |
/** | |
* Returns the UTC time of the change in price. | |
*/ | |
function getItemLastPriceChange(itemID) { | |
var lastUpdate = getItemProperty(itemID, 'price_last_changed'); | |
return lastUpdate.substr(0,19); // shave off " UTC" part | |
} | |
// ======= Utility Functions ======= | |
/** | |
* Formats the sell value as "Ng Ns Nc" to match the ingame display string. | |
*/ | |
function formatAsGold(sellValue) { | |
var n = sellValue; | |
var s = ""; | |
if (sellValue < 0) { | |
s = "-"; | |
n = Math.abs(n); | |
} | |
var gold = Math.floor(((n / 10000) % 100)); | |
var silver = Math.floor(((n / 100) % 100)); | |
var copper = Math.floor((n % 100)) + "c"; | |
if (gold == 0) { | |
gold = ""; | |
} else { | |
gold += "g "; | |
} | |
if (silver == 0) { | |
silver = ""; | |
} else { | |
silver += "s "; | |
} | |
return s + gold + silver + copper; | |
} | |
/** | |
* Returns a display string indicating the status of supply versus demand. | |
*/ | |
function calcSDStatus(supply, demand, threshold) { | |
if (typeof threshold === "undefined") { | |
threshold = 0.05; | |
} | |
//supply = 212; | |
//demand = 541; | |
var ratio = supply / demand; | |
var lowerBound = (ratio - threshold); | |
var upperBound = (ratio + threshold); | |
var status; | |
if (ratio == 1.0) { | |
status = "Equilibrium"; | |
} else if (lowerBound < 1.0) { | |
status = "In Demand"; | |
} else { // upperBound > 1.0 | |
status = "Saturated"; | |
} | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment