From 71f44da278ac16e0b50f5a1fe1cab77eff515485 Mon Sep 17 00:00:00 2001 From: Brian Hair Date: Fri, 10 Sep 2021 21:48:48 -0500 Subject: [PATCH] quickshop-price-list-site: Values updated to use EO display value. This required making functions to retrive data out of the EO structure. --- site/index.html | 2 +- site/js/main.js | 77 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/site/index.html b/site/index.html index 9b4bac6..6eb4043 100644 --- a/site/index.html +++ b/site/index.html @@ -12,7 +12,7 @@ Item - Price (M) + Price Type Enchants (If Any) Owner diff --git a/site/js/main.js b/site/js/main.js index a1d2163..6b53045 100644 --- a/site/js/main.js +++ b/site/js/main.js @@ -1,5 +1,17 @@ +/****************************************************************************** + * This file fetches the quick shop raw data from the massive link minecraft + * shops endpoint and then parses that out into objects which are then + * displayed in a table on the page. + * + * @author Brian Hair + *****************************************************************************/ -/* credit to Alec Lomax: https://lowmess.com/blog/fetch-with-timeout + +// Global defines +const DEBUG_OUTPUT = false; + +/* + * credit to Alec Lomax: https://lowmess.com/blog/fetch-with-timeout */ const fetchWithTimeout = (uri, options = {}, time = 5000) => { // Lets set up our `AbortController`, and create a request options object @@ -37,7 +49,40 @@ const fetchWithTimeout = (uri, options = {}, time = 5000) => { }) } -/* Get the raw list of shops +/* + * Get the meta value out of an EO. + * attribute is which object in the _meta, type is the sub attribute + */ +function getEOMetaValue(eo, attribute, type) { + if (eo._meta.hasOwnProperty(attribute)) { + if (eo._meta[attribute].hasOwnProperty(type)) { + return eo._meta[attribute][type].value; + } + } + return "undefined"; +} + +/* + * Get the display name label of a value if it exists, otherwise return the + * internal name. + */ +function getEOLabel(eo, attribute) { + return getEOMetaValue(eo, attribute, 'label'); +} + +/* + * Get the direct EO value. + */ +function getEOValue(eo, attribute) { + if (eo.hasOwnProperty(attribute)) { + return eo[attribute]; + } + return "undefined"; +} + + +/* + * Get the raw list of shops */ async function getShops() { let url = 'http://leigh-co.com/minecraft/neweden/link/v1/shops/read'; @@ -49,7 +94,8 @@ async function getShops() { } } -/* Parse and render the shops to the screen. +/* + * Parse and render the shops to the screen. */ async function renderShops() { // Get the list of shops from the web endpoint. @@ -65,9 +111,10 @@ async function renderShops() { return; } - // Uncomment this for debugging. Will show in the web console the raw - // shop data coming from the endpoint. - //console.trace("RAW SHOP: " + value); + if (DEBUG_OUTPUT) { + console.log("RAW SHOP: " + value); + } + // This magic line parses the plain string into a JSON object. const shop = JSON.parse(value); @@ -75,20 +122,20 @@ async function renderShops() { // Start building up the HTML table row for current shop. let htmlSegment = ` - TESTING IMAGES BEING IN PLACE ${shop.itemDisplayName} - ${shop.price} - ${shop.type}`; + ${getEOValue(shop, ${getEOLabel(shop, "item")} + ${getEOMetaValue(shop, "price", "currencyPrefix")}${getEOLabel(shop, "price")} + ${getEOValue(shop, "type")}`; // If there are enchantments on the item, add them in. // Otherwise insert an empty cell. if (shop.hasOwnProperty('enchants')) { - htmlSegment += ``; + htmlSegment += ``; $.each(shop.enchants, function(index, enchant) { if (index > 0) { htmlSegment += `
`; } htmlSegment += ` - ${enchant.name} ${enchant.level}`; + ${getEOLabel(enchant, "name")} ${getEOValue(enchant, "level")}`; }); htmlSegment += ``; } else { @@ -97,9 +144,9 @@ async function renderShops() { // Add the rest of the columns for the row. htmlSegment += ` - ${shop.ownerName} - ${shop.x} / ${shop.y} / ${shop.z} - ${shop.world}`; + ${getEOLabel(shop, "owner")} + ${getEOValue(shop, "x")} / ${getEOValue(shop, "y")} / ${getEOValue(shop, "z")} + ${getEOValue(shop, "world")}`; // Lastly, append the into the table body $('#quickShopsTableBody').append(htmlSegment); @@ -122,7 +169,7 @@ async function renderShops() { ], columnDefs: [ { orderable: false, targets: [ 3, 5 ]}, - { className: "dt-body-center", targets: "_all"} + { className: "dt-body-center", targets: [0,2,3,4,5,6]} ], colReorder: { enable: true, -- GitLab