Add printFormula util

This commit is contained in:
thepaperpilot 2023-04-02 00:48:48 -05:00
parent 6115b6687d
commit 6f9b73d0e8
2 changed files with 26 additions and 2 deletions

View file

@ -7,7 +7,8 @@ import { globalBus } from "game/events";
import Formula, {
calculateCost,
calculateMaxAffordable,
findNonInvertible
findNonInvertible,
printFormula
} from "game/formulas/formulas";
import type { BaseLayer, GenericLayer } from "game/layers";
import { createLayer } from "game/layers";
@ -23,6 +24,7 @@ window.Formula = Formula;
window.calculateMaxAffordable = calculateMaxAffordable;
window.calculateCost = calculateCost;
window.findNonInvertible = findNonInvertible;
window.printFormula = printFormula;
window.unref = unref;
window.ref = ref;
window.createResource = createResource;

View file

@ -1,5 +1,5 @@
import { Resource } from "features/resources/resource";
import Decimal, { DecimalSource } from "util/bignum";
import Decimal, { DecimalSource, format } from "util/bignum";
import { Computable, convertComputable, ProcessedComputable } from "util/computed";
import { computed, ComputedRef, ref, unref } from "vue";
import * as ops from "./operations";
@ -1334,6 +1334,28 @@ export function findNonInvertible(formula: GenericFormula): GenericFormula | nul
return null;
}
/**
* Stringifies a formula so it's more easy to read in the console
* @param formula The formula to print
*/
export function printFormula(formula: FormulaSource): string {
if (formula instanceof Formula) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return formula.internalEvaluate == null
? formula.hasVariable()
? "x"
: formula.inputs[0] ?? 0
: // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
formula.internalEvaluate.name +
"(" +
formula.inputs.map(printFormula).join(", ") +
")";
}
return format(unref(formula));
}
/**
* Utility for calculating the maximum amount of purchases possible with a given formula and resource. If {@ref spendResources} is changed to false, the calculation will be much faster with higher numbers.
* @param formula The formula to use for calculating buy max from