mirror of
https://github.com/thepaperpilot/Planar-Pioneers.git
synced 2024-11-25 01:41:44 +00:00
Fix calculate max affordable edge case
This commit is contained in:
parent
4160986a4d
commit
b6317a47e8
2 changed files with 20 additions and 2 deletions
|
@ -1426,14 +1426,14 @@ export function calculateMaxAffordable(
|
|||
summedPurchases = 0;
|
||||
}
|
||||
}
|
||||
if (summedPurchases > 0) {
|
||||
if (summedPurchases > 0 && Decimal.lt(calculateCost(formula, affordable, true, 0), 1e308)) {
|
||||
affordable = affordable.sub(summedPurchases).clampMin(0);
|
||||
let summedCost = calculateCost(formula, affordable, true, 0);
|
||||
while (true) {
|
||||
const nextCost = formula.evaluate(
|
||||
affordable.add(unref(formula.innermostVariable) ?? 0)
|
||||
);
|
||||
if (Decimal.add(summedCost, nextCost).lt(resource.value)) {
|
||||
if (Decimal.add(summedCost, nextCost).lte(resource.value)) {
|
||||
affordable = affordable.add(1);
|
||||
summedCost = Decimal.add(summedCost, nextCost);
|
||||
} else {
|
||||
|
|
|
@ -1174,6 +1174,24 @@ describe("Buy Max", () => {
|
|||
// Since we're summing all the purchases this should be equivalent
|
||||
expect(calculatedCost).compare_tolerance(actualCost);
|
||||
});
|
||||
test("Handles summing purchases when making very few purchases", () => {
|
||||
const purchases = ref(0);
|
||||
const variable = Formula.variable(purchases);
|
||||
const formula = variable.add(1);
|
||||
const resource = createResource(ref(3));
|
||||
const maxAffordable = calculateMaxAffordable(formula, resource, true);
|
||||
expect(maxAffordable.value).compare_tolerance(2);
|
||||
|
||||
const actualCost = new Array(2)
|
||||
.fill(null)
|
||||
.reduce(
|
||||
(acc, _, i) => acc.add(formula.evaluate(i + purchases.value)),
|
||||
new Decimal(0)
|
||||
);
|
||||
const calculatedCost = calculateCost(formula, maxAffordable.value, true);
|
||||
// Since we're summing all the purchases this should be equivalent
|
||||
expect(calculatedCost).compare_tolerance(actualCost);
|
||||
});
|
||||
test("Handles summing purchases when over e308 purchases", () => {
|
||||
resource.value = "1ee308";
|
||||
const purchases = ref(0);
|
||||
|
|
Loading…
Reference in a new issue