From a62a86235ccfd536374e2484de7a70d061eed433 Mon Sep 17 00:00:00 2001 From: Blake Felt Date: Thu, 9 Apr 2020 22:32:01 -0600 Subject: [PATCH 1/2] Added parameters first-time and previous-pattern. Added text box to notify that values will not be saved. Valid first-time options are: yes/true/1 no/false/0. Valid first-time options are: 0/fluctuating 1/large-spike 2/decreasing 3/small-spike anything else defaults to unknown. Test with: https://turnipprophet.io/?pattern=large-spike&prices=90.91..93.94..&first=true --- index.html | 7 ++++ js/scripts.js | 113 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 108 insertions(+), 12 deletions(-) diff --git a/index.html b/index.html index 00b94c6..8521e34 100644 --- a/index.html +++ b/index.html @@ -273,6 +273,13 @@

This app is still in development, but will improve over time!

+ +

Daisy Mae

diff --git a/js/scripts.js b/js/scripts.js index 24f1376..d1f5234 100644 --- a/js/scripts.js +++ b/js/scripts.js @@ -65,9 +65,10 @@ const fillFields = function (prices, first_buy, previous_pattern) { const initialize = function () { try { - const prices = getPrices() - const first_buy = getFirstBuyState(); - const previous_pattern = getPreviousPatternState(); + const previous = getPrevious(); + const first_buy = previous[0]; + const previous_pattern = previous[1]; + const prices = previous[2]; if (prices === null) { fillFields([], first_buy, previous_pattern) } else { @@ -101,14 +102,65 @@ const isEmpty = function (arr) { return filtered.length == 0 } -const getFirstBuyState = function () { +const getFirstBuyStateFromQuery = function (param) { + try { + const params = new URLSearchParams(window.location.search.substr(1)); + const firstbuy_str = params.get(param); + + if (firstbuy_str == null) { + return null; + } + + firstbuy = null; + if (firstbuy_str == "1" || firstbuy_str == "yes" || firstbuy_str == "true") { + firstbuy = true; + } else if (firstbuy_str == "0" || firstbuy_str == "no" || firstbuy_str == "false") { + firstbuy = false; + } + + return firstbuy; + + } catch (e) { + return null; + } +} + +const getFirstBuyStateFromLocalstorage = function () { return JSON.parse(localStorage.getItem('first_buy')) } -const getPreviousPatternState = function () { +const getPreviousPatternStateFromLocalstorage = function () { return JSON.parse(localStorage.getItem('previous_pattern')) } +const getPreviousPatternStateFromQuery = function (param) { + try { + const params = new URLSearchParams(window.location.search.substr(1)); + const pattern_str = params.get(param); + + if (pattern_str == null) { + return null; + } + + if (pattern_str == "0" || pattern_str == "fluctuating") { + pattern = 0; + } else if (pattern_str == "1" || pattern_str == "large-spike") { + pattern = 1; + } else if (pattern_str == "2" || pattern_str == "decreasing") { + pattern = 2; + } else if (pattern_str == "3" || pattern_str == "small-spike") { + pattern = 3; + } else { + pattern = -1; + } + + return pattern; + + } catch (e) { + return null; + } +} + const getPricesFromLocalstorage = function () { try { const sell_prices = JSON.parse(localStorage.getItem("sell_prices")); @@ -123,10 +175,10 @@ const getPricesFromLocalstorage = function () { } }; -const getPricesFromQuery = function () { +const getPricesFromQuery = function (param) { try { const params = new URLSearchParams(window.location.search.substr(1)); - const sell_prices = params.get("prices").split(".").map((x) => parseInt(x, 10)); + const sell_prices = params.get(param).split(".").map((x) => parseInt(x, 10)); if (!Array.isArray(sell_prices)) { return null; @@ -141,15 +193,52 @@ const getPricesFromQuery = function () { sell_prices.push(0); } - window.price_from_query = true; return sell_prices; } catch (e) { return null; } }; -const getPrices = function () { - return getPricesFromQuery() || getPricesFromLocalstorage(); +const getPreviousFromQuery = function () { + const queries = [ + ["first", getFirstBuyStateFromQuery], + ["pattern", getPreviousPatternStateFromQuery], + ["prices", getPricesFromQuery] + ]; + + + found = null; /* value to save if we found any parameters */ + ret = []; + for (q of queries) { + val = q[1](q[0]); /* run the function with the parameter */ + found = found || val; + ret.push(val); + } + + if (found != null) { /* if we found any parameter */ + window.from_query = true; + document.getElementById("from_query").style.visibility = "visible"; + return ret; + } + return null; +}; + +const getPreviousFromLocalstorage = function () { + return [ + getFirstBuyStateFromLocalstorage(), + getPreviousPatternStateFromQuery(), + getPricesFromLocalstorage() + ]; +}; + + +/** + * Gets previous values. First tries to parse parameters, + * if none of them match then it looks in local storage. + * @return {[first time, previous pattern, prices]} + */ +const getPrevious = function () { + return getPreviousFromQuery() || getPreviousFromLocalstorage(); }; const getSellPrices = function () { @@ -187,13 +276,13 @@ const update = function () { const buy_price = parseInt(buy_input.val()); const first_buy = getCheckedRadio(first_buy_radios) == 'true'; const previous_pattern = parseInt(getCheckedRadio(previous_pattern_radios)); - + buy_input[0].disabled = first_buy; buy_input[0].placeholder = first_buy ? '—' : '...' const prices = [buy_price, buy_price, ...sell_prices]; - if (!window.price_from_query) { + if (!window.from_query) { updateLocalStorage(prices, first_buy, previous_pattern); } From a893a5617b553cf9e33a4176b391c306d3d1cd49 Mon Sep 17 00:00:00 2001 From: Blake Felt Date: Tue, 14 Apr 2020 17:20:04 -0600 Subject: [PATCH 2/2] Removed UI notification when query is used. A console log shows instead. Cleaned up getPreviousFromQuery. It now returns null when the "prices" parameter is not used. Fixed getPreviousFromLocalStorage to only look at local storage. Changed from_query to populated_from_query to be more descriptive. --- index.html | 7 ------- js/scripts.js | 33 +++++++++++++-------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/index.html b/index.html index 8521e34..00b94c6 100644 --- a/index.html +++ b/index.html @@ -273,13 +273,6 @@

This app is still in development, but will improve over time!

- -

Daisy Mae

diff --git a/js/scripts.js b/js/scripts.js index d1f5234..1fe174b 100644 --- a/js/scripts.js +++ b/js/scripts.js @@ -200,33 +200,26 @@ const getPricesFromQuery = function (param) { }; const getPreviousFromQuery = function () { - const queries = [ - ["first", getFirstBuyStateFromQuery], - ["pattern", getPreviousPatternStateFromQuery], - ["prices", getPricesFromQuery] - ]; - - found = null; /* value to save if we found any parameters */ - ret = []; - for (q of queries) { - val = q[1](q[0]); /* run the function with the parameter */ - found = found || val; - ret.push(val); + /* Check if valid prices are entered. Exit immediately if not. */ + prices = getPricesFromQuery("prices"); + if (prices == null) { + return null; } - if (found != null) { /* if we found any parameter */ - window.from_query = true; - document.getElementById("from_query").style.visibility = "visible"; - return ret; - } - return null; + console.log("Using data from query."); + window.populated_from_query = true; + return [ + getFirstBuyStateFromQuery("first"), + getPreviousPatternStateFromQuery("pattern"), + prices + ]; }; const getPreviousFromLocalstorage = function () { return [ getFirstBuyStateFromLocalstorage(), - getPreviousPatternStateFromQuery(), + getPreviousPatternStateFromLocalstorage(), getPricesFromLocalstorage() ]; }; @@ -282,7 +275,7 @@ const update = function () { const prices = [buy_price, buy_price, ...sell_prices]; - if (!window.from_query) { + if (!window.populated_from_query) { updateLocalStorage(prices, first_buy, previous_pattern); }