Merge branch 'master' into upstream_master

master
Peter Shih 4 years ago committed by GitHub
commit fb7fbe55e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 />
@ -37,92 +37,109 @@
</span>
</div>
</div>
<form>
<div class="row">
<div class="col s2">
<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 s12">
<h5>Daisy Mae</h5>
<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>
</div>
<div class="row">
<div class="col s2">
<h6>Monday</h6>
<div class="input-field col s6">
<label for="sell_2">AM</label>
<input type="number" id="sell_2">
<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="input-field col s6">
<label for="sell_3">PM</label>
<input type="number" id="sell_3">
<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 class="col s2">
<h6>Tuesday</h6>
<div class="input-field col s6">
<label for="sell_4">AM</label>
<input type="number" id="sell_4">
</div>
<div class="input-field col s6">
<label for="sell_5">PM</label>
<input type="number" id="sell_5">
</div>
</div>
</div>
<div class="row">
<div class="col s2">
<h6>Monday</h6>
<div class="input-field col s6">
<label for="sell_2">AM</label>
<input type="number" id="sell_2">
</div>
<div class="col s2">
<h6>Wednesday</h6>
<div class="input-field col s6">
<label for="sell_6">AM</label>
<input type="number" id="sell_6">
</div>
<div class="input-field col s6">
<label for="sell_7">PM</label>
<input type="number" id="sell_7">
</div>
<div class="input-field col s6">
<label for="sell_3">PM</label>
<input type="number" id="sell_3">
</div>
<div class="col s2">
<h6>Thursday</h6>
<div class="input-field col s6">
<label for="sell_8">AM</label>
<input type="number" id="sell_8">
</div>
<div class="input-field col s6">
<label for="sell_9">PM</label>
<input type="number" id="sell_9">
</div>
</div>
<div class="col s2">
<h6>Tuesday</h6>
<div class="input-field col s6">
<label for="sell_4">AM</label>
<input type="number" id="sell_4">
</div>
<div class="col s2">
<h6>Friday</h6>
<div class="input-field col s6">
<label for="sell_10">AM</label>
<input type="number" id="sell_10">
</div>
<div class="input-field col s6">
<label for="sell_11">PM</label>
<input type="number" id="sell_11">
</div>
<div class="input-field col s6">
<label for="sell_5">PM</label>
<input type="number" id="sell_5">
</div>
<div class="col s2">
<h6>Saturday</h6>
<div class="input-field col s6">
<label for="sell_12">AM</label>
<input type="number" id="sell_12">
</div>
<div class="input-field col s6">
<label for="sell_13">PM</label>
<input type="number" id="sell_13">
</div>
</div>
<div class="col s2">
<h6>Wednesday</h6>
<div class="input-field col s6">
<label for="sell_6">AM</label>
<input type="number" id="sell_6">
</div>
<div class="input-field col s6">
<label for="sell_7">PM</label>
<input type="number" id="sell_7">
</div>
</div>
<div class="row">
<div class="col s12">
<button id="reset" class="btn waves-effect waves-light" type="submit" name="action">
Reset
</button>
<div class="col s2">
<h6>Thursday</h6>
<div class="input-field col s6">
<label for="sell_8">AM</label>
<input type="number" id="sell_8">
</div>
<div class="input-field col s6">
<label for="sell_9">PM</label>
<input type="number" id="sell_9">
</div>
</div>
<div class="col s2">
<h6>Friday</h6>
<div class="input-field col s6">
<label for="sell_10">AM</label>
<input type="number" id="sell_10">
</div>
<div class="input-field col s6">
<label for="sell_11">PM</label>
<input type="number" id="sell_11">
</div>
</div>
</form>
<div class="col s2">
<h6>Saturday</h6>
<div class="input-field col s6">
<label for="sell_12">AM</label>
<input type="number" id="sell_12">
</div>
<div class="input-field col s6">
<label for="sell_13">PM</label>
<input type="number" id="sell_13">
</div>
</div>
</div>
<div class="row">
<div class="col s12">
<button id="reset" class="btn waves-effect waves-light" name="action">
Reset
</button>
</div>
</div>
<div class="divider"></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++) {

@ -1,4 +1,77 @@
function get_prices_from_localstorage() {
//Reusable Fields
const getSellFields = function () {
let fields = [];
for (var i = 2; i < 14; i++) {
fields.push($("#sell_" + i)[0]);
}
return fields;
};
const sell_inputs = getSellFields();
const buy_input = $("#buy");
const first_buy_field = $("#first_buy");
//Functions
const fillFields = function (prices, first_buy) {
first_buy_field.prop("checked", first_buy);
buy_input.focus();
buy_input.val(prices[0] || '');
buy_input.blur();
const sell_prices = prices.slice(2);
sell_prices.forEach((price, index) => {
if (!price) {
return
} else {
const element = $("#sell_" + (index + 2));
element.focus();
element.val(price);
element.blur();
}
})
};
const initialize = function () {
try {
const prices = getPrices();
const first_buy = getFirstBuyState();
if (prices === null) {
fillFields([], first_buy);
} else {
fillFields(prices, first_buy);
}
$(document).trigger("input");
} catch (e) {
console.error(e);
}
$("#reset").on("click", function () {
first_buy_field.prop('checked', false);
$("input").val(null).trigger("input");
})
};
const updateLocalStorage = function (prices, first_buy) {
try {
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)
}
};
const isEmpty = function (arr) {
const filtered = arr.filter(value => value !== null && value !== '' && !isNaN(value));
return filtered.length == 0;
};
const getFirstBuyState = function () {
return JSON.parse(localStorage.getItem('first_buy'));
};
const getPricesFromLocalstorage = function () {
try {
const sell_prices = JSON.parse(localStorage.getItem("sell_prices"));
@ -10,9 +83,9 @@ function get_prices_from_localstorage() {
} catch (e) {
return null;
}
}
};
function get_prices_from_query() {
const getPricesFromQuery = function () {
try {
const params = new URLSearchParams(window.location.search.substr(1));
const sell_prices = params.get("prices").split(".").map((x) => parseInt(x, 10));
@ -26,70 +99,26 @@ function get_prices_from_query() {
} catch (e) {
return null;
}
}
$(document).ready(function () {
try {
// load sell_prices from URL hash first, then local storage
const sell_prices = get_prices_from_query() || get_prices_from_localstorage();
if (sell_prices == null) {
return;
}
sell_prices.forEach((sell_price, index) => {
if (!sell_price) {
return;
}
const buyInput = $("#buy");
if (index === 0) {
buyInput.focus();
buyInput.val(sell_price);
buyInput.blur();
return;
}
const element = $("#sell_" + index);
if (element.length) {
element.focus();
element.val(sell_price);
element.blur();
}
});
$(document).trigger("input");
} catch (e) {
console.error(e);
}
$("#reset").on("click", function() {
$("input").val(null).trigger("input");
})
});
};
$(document).on("input", function() {
// Update output on any input change
const getPrices = function () {
return getPricesFromQuery() || getPricesFromLocalstorage();
};
var buy_price = parseInt($("#buy").val());
const getSellPrices = function () {
//Checks all sell inputs and returns an array with their values
return res = sell_inputs.map(function (input) {
return parseInt(input.value || '');
});
};
var sell_prices = [buy_price, buy_price];
for (var i = 2; i < 14; i++) {
sell_prices.push(parseInt($("#sell_" + i).val()));
}
if (!window.price_from_query) {
localStorage.setItem("sell_prices", JSON.stringify(sell_prices));
}
const is_empty = sell_prices.every(sell_price => !sell_price);
if (is_empty) {
const calculateOutput = function (data, first_buy) {
if (isEmpty(data)) {
$("#output").html("");
return;
}
let output_possibilities = "";
for (let poss of analyze_possibilities(sell_prices)) {
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) {
@ -99,8 +128,26 @@ $(document).on("input", function() {
}
}
out_line += `<td class="one">${poss.weekMin}</td><td class="one">${poss.weekMax}</td></tr>`;
output_possibilities += out_line
output_possibilities += out_line;
}
$("#output").html(output_possibilities);
};
const update = function () {
const sell_prices = getSellPrices();
const buy_price = parseInt(buy_input.val());
const first_buy = first_buy_field.is(":checked");
buy_input.prop('disabled', first_buy);
const prices = [buy_price, buy_price, ...sell_prices];
if (!window.price_from_query) {
updateLocalStorage(prices, first_buy);
}
calculateOutput(prices, first_buy);
};
$("#output").html(output_possibilities)
});
$(document).ready(initialize);
$(document).on("input", update);

Loading…
Cancel
Save