Adjust fate calculator calculation

pull/1/head
Made Baruna 2022-01-16 00:11:59 +07:00
parent 813f87372a
commit 0b4a9102ae
2 changed files with 79 additions and 35 deletions

View File

@ -1,5 +1,8 @@
<script> <script>
import Icon from './Icon.svelte'; import Icon from './Icon.svelte';
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
export let className = ''; export let className = '';
export let icon = null; export let icon = null;
@ -18,13 +21,15 @@
} else { } else {
value = event.target.value; value = event.target.value;
} }
dispatch('input');
}; };
</script> </script>
<div <div
class="flex flex-1 relative items-center bg-background rounded-2xl h-14 class="flex flex-1 relative items-center bg-background rounded-2xl h-14
focus-within:border-primary border-2 border-transparent ease-in duration-100 {className}" focus-within:border-primary border-2 border-transparent ease-in duration-100 {className}"
style="min-height: 3.5rem;" style="min-height: 3.5rem;"
> >
{#if icon} {#if icon}
<Icon path={icon} color="white" className="absolute ml-4 w-6 h-6" /> <Icon path={icon} color="white" className="absolute ml-4 w-6 h-6" />

View File

@ -92,24 +92,27 @@
} }
function onChange() { function onChange() {
result = [];
result = null; result = null;
} }
function calculateUsable() { function calculateUsable(sortOrder = true) {
usable = usable usable = usable
.slice() .slice()
.map((e) => { .map((e) => {
const total = e.amount + (e.firstTime ? e.amount : e.bonus); const total = e.amount + (e.firstTime ? e.amount : e.bonus);
return { ...e, total, perItem: e.price / total }; return { ...e, total, perItem: e.price / (e.firstTime ? total * 2 : total) };
}) })
.sort((a, b) => a.perItem - b.perItem); .sort(sortOrder ? (a, b) => a.perItem - b.perItem : (a, b) => b.total - a.total);
} }
async function calculate() { function calculate(sortOrder) {
onChange(); onChange();
usable = values.slice().map((e, i) => ({ ...e, price: usedPrices[i] })); usable = values.slice().map((e, i) => ({ ...e, price: usedPrices[i] }));
calculateUsable(); calculateUsable(sortOrder);
console.log(usable.slice());
let currentMoney = money; let currentMoney = money;
const used = []; const used = [];
@ -132,23 +135,25 @@
if (currentUsable.firstTime) { if (currentUsable.firstTime) {
currentUsable.firstTime = false; currentUsable.firstTime = false;
calculateUsable(); calculateUsable(sortOrder);
} }
} else { } else {
usable.shift(); usable.shift();
} }
} }
result = used; return {
resultTotal = total; used,
resultTotalPrice = totalPrice; total,
totalPrice,
};
} }
async function calculateFate() { function calculateFate(sortOrder) {
onChange(); onChange();
usable = values.slice().map((e, i) => ({ ...e, price: usedPrices[i] })); usable = values.slice().map((e, i) => ({ ...e, price: usedPrices[i] }));
calculateUsable(); calculateUsable(sortOrder);
const usableTemp = usable.slice(); const usableTemp = usable.slice();
let currentGenesis = fate * 160; let currentGenesis = fate * 160;
@ -175,34 +180,68 @@
if (currentUsable.firstTime) { if (currentUsable.firstTime) {
currentUsable.firstTime = false; currentUsable.firstTime = false;
usableTemp.find((e) => e.amount === currentUsable.amount).firstTime = false; usableTemp.find((e) => e.amount === currentUsable.amount).firstTime = false;
calculateUsable(); calculateUsable(sortOrder);
} }
} else { } else {
usable.shift(); usable.shift();
} }
} }
let min = Number.MAX_SAFE_INTEGER; if (currentGenesis > 0) {
let current = null; let min = Number.MAX_SAFE_INTEGER;
for (const u of usableTemp) { let current = null;
const usageAmount = u.amount + (u.firstTime ? u.amount : u.bonus); for (const u of usableTemp) {
if (Math.abs(currentGenesis - usageAmount) < min) { const usageAmount = u.amount + (u.firstTime ? u.amount : u.bonus);
current = u; if (Math.abs(currentGenesis - usageAmount) < min) {
min = Math.abs(currentGenesis - usageAmount); current = u;
min = Math.abs(currentGenesis - usageAmount);
}
}
total += current.amount + (current.firstTime ? current.amount : current.bonus);
totalPrice += current.price;
const usedItem = used.find((e) => e.amount === current.amount && e.firstTime === current.firstTime);
if (usedItem) {
usedItem.qty++;
} else {
used.push({ ...current, qty: 1 });
} }
} }
total += current.amount + (current.firstTime ? current.amount : current.bonus);
totalPrice += current.price;
const usedItem = used.find((e) => e.amount === current.amount && e.firstTime === current.firstTime);
if (usedItem) {
usedItem.qty++;
} else {
used.push({ ...current, qty: 1 });
}
result = used; return {
resultTotal = total; used,
resultTotalPrice = totalPrice; total,
totalPrice,
};
}
function calculateByMoney() {
const res1 = calculate(true);
const res2 = calculate(false);
result = null;
if (res1.total > res2.total) {
result = res1.used;
resultTotal = res1.total;
resultTotalPrice = res1.totalPrice;
} else {
result = res2.used;
resultTotal = res2.total;
resultTotalPrice = res2.totalPrice;
}
}
function calculateByFate() {
const res1 = calculateFate(true);
const res2 = calculateFate(false);
result = null;
if (res1.total > res2.total) {
result = res1.used;
resultTotal = res1.total;
resultTotalPrice = res1.totalPrice;
} else {
result = res2.used;
resultTotal = res2.total;
resultTotalPrice = res2.totalPrice;
}
} }
</script> </script>
@ -243,15 +282,15 @@
</div> </div>
{#if selected !== null} {#if selected !== null}
<div> <div>
<Input placeholder="Total Money" bind:value={money} type="number" on:change={onChange} /> <Input placeholder="Total Money" bind:value={money} type="number" on:input={onChange} />
<div class="mb-1" /> <div class="mb-1" />
<Button className="w-full" on:click={calculate}> <Button className="w-full" on:click={calculateByMoney}>
{$t('calculator.fate.calculate', { values: { currency: currencyLabel, value: numberFormat.format(money) } })} {$t('calculator.fate.calculate', { values: { currency: currencyLabel, value: numberFormat.format(money) } })}
</Button> </Button>
<p class="text-white my-6 text-center">OR</p> <p class="text-white my-6 text-center">OR</p>
<Input placeholder="Total Fate" bind:value={fate} type="number" on:change={onChange} /> <Input placeholder="Total Fate" bind:value={fate} type="number" on:input={onChange} />
<div class="mb-1" /> <div class="mb-1" />
<Button className="w-full" on:click={calculateFate}> <Button className="w-full" on:click={calculateByFate}>
{$t('calculator.fate.calculateFate', { values: { value: fate } })} {$t('calculator.fate.calculateFate', { values: { value: fate } })}
<img class="ml-1 w-6 inline" src="/images/intertwined_fate.png" alt="Fate" /> <img class="ml-1 w-6 inline" src="/images/intertwined_fate.png" alt="Fate" />
</Button> </Button>