feat: Add toggle for filtering results to first ever buy

master
Ryan Carbotte 4 years ago
parent 91e428e563
commit cb69d49113

@ -29,7 +29,7 @@
<div class="col s12 m5"> <div class="col s12 m5">
<div class="card-panel z-depth-0"> <div class="card-panel z-depth-0">
<span> <span>
To use, enter as much data as you can from <b>your own island</b>. If you bought from Daisy on your own island <b>for the first time this week</b> leave the buy price blank. The tool will figure out all possible patterns and predict the lowest and highest prices for each day. To use, enter as much data as you can from <b>your own island</b>. The tool will figure out all possible patterns and predict the lowest and highest prices for each day.
<p /> <p />
I couldn't have done this without <a href="https://twitter.com/_Ninji/status/1244818665851289602?s=20">Ninji's work extracting the code</a> I couldn't have done this without <a href="https://twitter.com/_Ninji/status/1244818665851289602?s=20">Ninji's work extracting the code</a>
<p /> <p />
@ -38,11 +38,30 @@
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col s2"> <div class="col s12">
<h5>Daisy Mae</h5> <h5>Daisy Mae</h5>
<div class="input-field"> <div class="row">
<label for="buy">Buy price</label> <div class="col s2">
<input type="number" id="buy"> <div class="input-field">
<label for="buy">Buy price</label>
<input type="number" id="buy">
</div>
</div>
<div class="col s2">
<div class="input-field">
<label>
<input id="first_buy" type="checkbox" class="filled-in" />
<span>First time buyer</span>
</label>
</div>
</div>
<div class="col s6">
<div class="input-field">
<span>
Check this box if you bought from Daisy on your own island <b>for the first time ever</b>. If you bought from her on your own island in a previous week, leave this box unchecked.
</span>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

@ -285,7 +285,7 @@ function* generate_pattern_1_with_peak(given_prices, peak_start) {
}); });
} }
yield { yield {
pattern_description: "decreasing, high spike, random lows", pattern_description: "decreasing middle, high spike, random low",
pattern_number: 1, pattern_number: 1,
prices: predicted_prices prices: predicted_prices
}; };
@ -351,7 +351,7 @@ function* generate_pattern_2(given_prices) {
max_rate -= 300; max_rate -= 300;
} }
yield { yield {
pattern_description: "always decreasing", pattern_description: "consistently decreasing",
pattern_number: 2, pattern_number: 2,
prices: predicted_prices prices: predicted_prices
}; };
@ -543,26 +543,29 @@ function* generate_pattern_3(given_prices) {
} }
} }
function* generate_possibilities(sell_prices, first_buy) {
function* generate_possibilities(sell_prices) { if (first_buy || isNaN(sell_prices[0])) {
if (!isNaN(sell_prices[0])) { for (var buy_price = 90; buy_price <= 110; buy_price++) {
sell_prices[0] = sell_prices[1] = buy_price;
if (first_buy) {
yield* generate_pattern_3(sell_prices);
} else {
yield* generate_pattern_0(sell_prices);
yield* generate_pattern_1(sell_prices);
yield* generate_pattern_2(sell_prices);
yield* generate_pattern_3(sell_prices);
}
}
} else {
yield* generate_pattern_0(sell_prices); yield* generate_pattern_0(sell_prices);
yield* generate_pattern_1(sell_prices); yield* generate_pattern_1(sell_prices);
yield* generate_pattern_2(sell_prices); yield* generate_pattern_2(sell_prices);
yield* generate_pattern_3(sell_prices); yield* generate_pattern_3(sell_prices);
} else {
for (var buy_price = 90; buy_price <= 110; buy_price++) {
sell_prices[0] = sell_prices[1] = buy_price;
yield* generate_pattern_0(sell_prices);
yield* generate_pattern_1(sell_prices);
yield* generate_pattern_2(sell_prices);
yield* generate_pattern_3(sell_prices);
}
} }
} }
function analyze_possibilities(sell_prices) { function analyze_possibilities(sell_prices, first_buy) {
generated_possibilities = Array.from(generate_possibilities(sell_prices)); generated_possibilities = Array.from(generate_possibilities(sell_prices, first_buy));
global_min_max = []; global_min_max = [];
for (var day = 0; day < 14; day++) { for (var day = 0; day < 14; day++) {

@ -9,10 +9,12 @@ const getSellFields = function () {
const sell_inputs = getSellFields() const sell_inputs = getSellFields()
const buy_input = $("#buy") const buy_input = $("#buy")
const first_buy_field = $("#first_buy");
//Functions //Functions
const fillFields = function (prices) { const fillFields = function (prices, first_buy) {
first_buy_field.prop("checked", first_buy);
buy_input.focus(); buy_input.focus();
buy_input.val(prices[0] || '') buy_input.val(prices[0] || '')
buy_input.blur(); buy_input.blur();
@ -33,11 +35,11 @@ const fillFields = function (prices) {
const initialize = function () { const initialize = function () {
try { try {
const prices = getPrices() const prices = getPrices()
console.log("Prices from Storage", prices) const first_buy = getFirstBuyState();
if (prices === null) { if (prices === null) {
return fillFields([], first_buy)
} else { } else {
fillFields(prices) fillFields(prices, first_buy)
} }
$(document).trigger("input"); $(document).trigger("input");
} catch (e) { } catch (e) {
@ -45,14 +47,16 @@ const initialize = function () {
} }
$("#reset").on("click", function () { $("#reset").on("click", function () {
first_buy_field.prop('checked', false);
$("input").val(null).trigger("input"); $("input").val(null).trigger("input");
}) })
} }
const updateLocalStorage = function (data) { const updateLocalStorage = function (prices, first_buy) {
try { try {
if (data.length !== 14) throw "The data array needs exactly 14 elements to be valid" if (prices.length !== 14) throw "The data array needs exactly 14 elements to be valid"
localStorage.setItem("sell_prices", JSON.stringify(data)) localStorage.setItem("sell_prices", JSON.stringify(prices))
localStorage.setItem("first_buy", JSON.stringify(first_buy));
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -63,6 +67,10 @@ const isEmpty = function (arr) {
return filtered.length == 0 return filtered.length == 0
} }
const getFirstBuyState = function () {
return JSON.parse(localStorage.getItem('first_buy'))
}
const getPrices = function () { const getPrices = function () {
let prices = JSON.parse(localStorage.getItem("sell_prices")) let prices = JSON.parse(localStorage.getItem("sell_prices"))
if (!prices || isEmpty(prices) || prices.length !== 14) { if (!prices || isEmpty(prices) || prices.length !== 14) {
@ -79,13 +87,13 @@ const getSellPrices = function () {
}) })
} }
const calculateOutput = function (data) { const calculateOutput = function (data, first_buy) {
if (isEmpty(data)) { if (isEmpty(data)) {
$("#output").html(""); $("#output").html("");
return; return;
} }
let output_possibilities = ""; let output_possibilities = "";
for (let poss of analyze_possibilities(data)) { for (let poss of analyze_possibilities(data, first_buy)) {
var out_line = "<tr><td>" + poss.pattern_description + "</td>" var out_line = "<tr><td>" + poss.pattern_description + "</td>"
for (let day of poss.prices.slice(1)) { for (let day of poss.prices.slice(1)) {
if (day.min !== day.max) { if (day.min !== day.max) {
@ -104,9 +112,13 @@ const calculateOutput = function (data) {
const update = function () { const update = function () {
const sell_prices = getSellPrices(); const sell_prices = getSellPrices();
const buy_price = parseInt(buy_input.val()); const buy_price = parseInt(buy_input.val());
const data = [buy_price, buy_price, ...sell_prices]; const first_buy = first_buy_field.is(":checked");
updateLocalStorage(data);
calculateOutput(data); buy_input.prop('disabled', first_buy);
const prices = [buy_price, buy_price, ...sell_prices];
updateLocalStorage(prices, first_buy);
calculateOutput(prices, first_buy);
} }
$(document).ready(initialize); $(document).ready(initialize);

Loading…
Cancel
Save