feat: Add toggle for filtering results to first ever buy

master
Ryan Carbotte 5 years ago
parent 91e428e563
commit cb69d49113

@ -29,7 +29,7 @@
<div class="col s12 m5">
<div class="card-panel z-depth-0">
<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 />
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 />
@ -38,11 +38,30 @@
</div>
</div>
<div class="row">
<div class="col s2">
<div class="col s12">
<h5>Daisy Mae</h5>
<div class="input-field">
<label for="buy">Buy price</label>
<input type="number" id="buy">
<div class="row">
<div class="col s2">
<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>

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

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

Loading…
Cancel
Save