fix: Retain compatibility with previous stored data

master
Mike Bryant 5 years ago
parent 54b8458a5f
commit b10358f13e

@ -68,9 +68,9 @@
<label>Is this your first time buying turnips from Daisy Mae on your island?<i>(This affects your
pattern)</i></label>
<div class="input__radio-buttons">
<input type="radio" id="first-time-radio-no" name="first-time" value="no" checked>
<input type="radio" id="first-time-radio-no" name="first-time" value="false" checked>
<label for="first-time-radio-no">No</label>
<input type="radio" id="first-time-radio-yes" name="first-time" value="yes">
<input type="radio" id="first-time-radio-yes" name="first-time" value="true">
<label for="first-time-radio-yes">Yes</label>
</div>
</div>
@ -81,15 +81,15 @@
<div class="input__group">
<label for="">What was last week's turnip price pattern?<i>(This affects your pattern)</i></label>
<div class="input__radio-buttons">
<input type="radio" id="pattern-radio-unknown" name="pattern" value="unknown" checked>
<input type="radio" id="pattern-radio-unknown" name="pattern" value="-1" checked>
<label for="pattern-radio-unknown">I don't know</label>
<input type="radio" id="pattern-radio-fluctuating" name="pattern" value="fluctuating">
<input type="radio" id="pattern-radio-fluctuating" name="pattern" value="0">
<label for="pattern-radio-fluctuating">Fluctuating</label>
<input type="radio" id="pattern-radio-small-spike" name="pattern" value="small-spike">
<input type="radio" id="pattern-radio-small-spike" name="pattern" value="3">
<label for="pattern-radio-small-spike">Small Spike</label>
<input type="radio" id="pattern-radio-large-spike" name="pattern" value="large-spike">
<input type="radio" id="pattern-radio-large-spike" name="pattern" value="1">
<label for="pattern-radio-large-spike">Large Spike</label>
<input type="radio" id="pattern-radio-decreasing" name="pattern" value="decreasing">
<input type="radio" id="pattern-radio-decreasing" name="pattern" value="2">
<label for="pattern-radio-decreasing">Decreasing</label>
</div>
</div>

@ -25,12 +25,15 @@ const getPreviousPatternRadios = function () {
}
const getCheckedRadio = function (radio_array) {
return radio_array.find(radio => radio.checked === true).value
return radio_array.find(radio => radio.checked === true).value;
}
const checkRadioByValue = function (radio_array, value) {
radio_array.forEach(radio => radio.checked = false)
radio_array.find(radio => radio.value == value).checked = true
if (value === null) {
return;
}
value = value.toString();
radio_array.find(radio => radio.value == value).checked = true;
}
const sell_inputs = getSellFields()
@ -40,7 +43,7 @@ const previous_pattern_radios = getPreviousPatternRadios()
//Functions
const fillFields = function (prices, first_buy, previous_pattern) {
first_buy == 'yes' ? checkRadioByValue(first_buy_radios, 'yes') : checkRadioByValue(first_buy_radios, 'no')
checkRadioByValue(first_buy_radios, first_buy);
checkRadioByValue(previous_pattern_radios, previous_pattern);
buy_input.focus();
@ -80,8 +83,6 @@ const initialize = function () {
fillFields([], false, 'unknown')
update()
})
$('select').formSelect();
}
const updateLocalStorage = function (prices, first_buy, previous_pattern) {
@ -101,11 +102,11 @@ const isEmpty = function (arr) {
}
const getFirstBuyState = function () {
return JSON.parse(localStorage.getItem('first_buy')) || 'no'
return JSON.parse(localStorage.getItem('first_buy'))
}
const getPreviousPatternState = function () {
return JSON.parse(localStorage.getItem('previous_pattern')) || 'unknown'
return JSON.parse(localStorage.getItem('previous_pattern'))
}
const getPricesFromLocalstorage = function () {
@ -157,7 +158,6 @@ const calculateOutput = function (data, first_buy, previous_pattern) {
let output_possibilities = "";
for (let poss of analyze_possibilities(data, first_buy, previous_pattern)) {
var out_line = "<tr><td class='table-pattern'>" + poss.pattern_description + "</td>"
console.log(poss.probability)
out_line += `<td>${Number.isFinite(poss.probability) ? ((poss.probability * 100).toPrecision(3) + '%') : '—'}</td>`;
for (let day of poss.prices.slice(1)) {
if (day.min !== day.max) {
@ -173,34 +173,14 @@ const calculateOutput = function (data, first_buy, previous_pattern) {
$("#output").html(output_possibilities)
}
const convertPatternToInt = function (pattern) {
switch (pattern) {
case 'unknown':
return -1;
case 'fluctuating':
return 0;
case 'large-spike':
return 1;
case 'decreasing':
return 2;
case 'small-spike':
return 3;
default:
return -1;
}
}
const update = function () {
const sell_prices = getSellPrices();
const buy_price = parseInt(buy_input.val());
const first_buy = getCheckedRadio(first_buy_radios);
const first_buy_boolean = first_buy == 'yes'
const previous_pattern = getCheckedRadio(previous_pattern_radios);
const first_buy = getCheckedRadio(first_buy_radios) == 'true';
const previous_pattern = parseInt(getCheckedRadio(previous_pattern_radios));
buy_input[0].disabled = first_buy_boolean;
buy_input[0].placeholder = first_buy_boolean ? '—' : '...'
buy_input[0].disabled = first_buy;
buy_input[0].placeholder = first_buy ? '—' : '...'
const prices = [buy_price, buy_price, ...sell_prices];
@ -208,7 +188,7 @@ const update = function () {
updateLocalStorage(prices, first_buy, previous_pattern);
}
calculateOutput(prices, first_buy_boolean, parseInt(convertPatternToInt(previous_pattern)));
calculateOutput(prices, first_buy, previous_pattern);
}
$(document).ready(initialize);

Loading…
Cancel
Save