From 6f9b73d0e87b96fc7314c3ddd5e8d96b88fdb606 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Sun, 2 Apr 2023 00:48:48 -0500 Subject: [PATCH] Add printFormula util --- src/data/projEntry.tsx | 4 +++- src/game/formulas/formulas.ts | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/data/projEntry.tsx b/src/data/projEntry.tsx index 55e8500..5c8d456 100644 --- a/src/data/projEntry.tsx +++ b/src/data/projEntry.tsx @@ -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; diff --git a/src/game/formulas/formulas.ts b/src/game/formulas/formulas.ts index 8161903..030e604 100644 --- a/src/game/formulas/formulas.ts +++ b/src/game/formulas/formulas.ts @@ -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