Add utility function for ETAs

This commit is contained in:
thepaperpilot 2023-02-15 15:24:36 -06:00
parent c6d2018ced
commit adb35191ea

View file

@ -22,7 +22,7 @@ import type { Persistent } from "game/persistence";
import { DefaultValue, persistent } from "game/persistence";
import player from "game/player";
import settings from "game/settings";
import Decimal, { DecimalSource, format, formatSmall } from "util/bignum";
import Decimal, { DecimalSource, format, formatSmall, formatTime } from "util/bignum";
import { formatWhole } from "util/break_eternity";
import { Direction, WithRequired } from "util/common";
import type {
@ -432,6 +432,31 @@ export function createCollapsibleMilestones(milestones: Record<string, GenericMi
};
}
/**
* Utility function for getting an ETA for when a target will be reached by a resource with a known (and assumed consistent) gain.
* @param resource The resource that will be increasing over time.
* @param rate The rate at which the resource is increasing.
* @param target The target amount of the resource to estimate the duration until.
*/
export function estimateTime(
resource: Resource,
rate: Computable<DecimalSource>,
target: Computable<DecimalSource>
) {
const processedRate = convertComputable(rate);
const processedTarget = convertComputable(target);
return computed(() => {
const currRate = unref(processedRate);
const currTarget = unref(processedTarget);
if (Decimal.gte(resource.value, currTarget)) {
return "Now";
} else if (Decimal.lt(currRate, 0)) {
return "Never";
}
return formatTime(Decimal.sub(currTarget, resource.value).div(currRate));
});
}
export function setUpDailyProgressTracker(options: {
resource: Resource;
goal: DecimalSource;