TMT-Demo/tests/game/requirements.test.ts

283 lines
10 KiB
TypeScript
Raw Normal View History

2023-02-07 14:31:09 +00:00
import { Visibility } from "features/feature";
import { createResource, Resource } from "features/resources/resource";
import Formula from "game/formulas/formulas";
2023-02-07 14:31:09 +00:00
import {
2023-03-21 05:15:28 +00:00
CostRequirement,
2023-02-07 14:31:09 +00:00
createBooleanRequirement,
createCostRequirement,
createVisibilityRequirement,
maxRequirementsMet,
payRequirements,
Requirement,
requirementsMet
} from "game/requirements";
import Decimal from "util/bignum";
2023-02-07 14:31:09 +00:00
import { beforeAll, describe, expect, test } from "vitest";
import { isRef, ref, unref } from "vue";
import "../utils";
describe("Creating cost requirement", () => {
let resource: Resource;
beforeAll(() => {
resource = createResource(ref(10));
});
2023-02-07 14:31:09 +00:00
describe("Minimal requirement", () => {
2023-03-21 05:15:28 +00:00
let requirement: CostRequirement;
2023-02-07 14:31:09 +00:00
beforeAll(() => {
requirement = createCostRequirement(() => ({
resource,
cost: 10
2023-02-07 14:31:09 +00:00
}));
});
2023-03-21 05:15:28 +00:00
test("resource pass-through", () => expect(requirement.resource).toBe(resource));
test("cost pass-through", () => expect(requirement.cost).toBe(10));
2023-02-07 14:31:09 +00:00
test("partialDisplay exists", () =>
expect(typeof requirement.partialDisplay).toBe("function"));
test("display exists", () => expect(typeof requirement.display).toBe("function"));
test("pay exists", () => expect(typeof requirement.pay).toBe("function"));
test("requirementMet exists", () => {
expect(requirement.requirementMet).not.toBeNull();
expect(isRef(requirement.requirementMet)).toBe(true);
});
test("is visible", () => expect(requirement.visibility).toBe(Visibility.Visible));
test("requires pay", () => expect(requirement.requiresPay).toBe(true));
test("does not spend resources", () => expect(requirement.cumulativeCost).toBe(true));
test("cannot maximize", () => expect(unref(requirement.canMaximize)).toBe(false));
2023-02-07 14:31:09 +00:00
});
describe("Fully customized", () => {
2023-03-21 05:15:28 +00:00
let requirement: CostRequirement;
2023-02-07 14:31:09 +00:00
beforeAll(() => {
requirement = createCostRequirement(() => ({
resource,
2023-03-21 05:15:28 +00:00
cost: Formula.variable(resource).times(10),
2023-02-07 14:31:09 +00:00
visibility: Visibility.None,
requiresPay: false,
cumulativeCost: false,
maxBulkAmount: Decimal.dInf,
directSum: 5,
2023-02-07 14:31:09 +00:00
// eslint-disable-next-line @typescript-eslint/no-empty-function
pay() {}
}));
});
test("pay is empty function", () =>
requirement.pay != null &&
typeof requirement.pay === "function" &&
requirement.pay.length === 1);
test("is not visible", () => expect(requirement.visibility).toBe(Visibility.None));
test("does not require pay", () => expect(requirement.requiresPay).toBe(false));
test("spends resources", () => expect(requirement.cumulativeCost).toBe(false));
test("can maximize", () => expect(unref(requirement.canMaximize)).toBe(true));
test("maxBulkAmount is set", () =>
expect(unref(requirement.maxBulkAmount)).compare_tolerance(Decimal.dInf));
test("directSum is set", () => expect(unref(requirement.directSum)).toBe(5));
2023-02-07 14:31:09 +00:00
});
test("Requirement met when meeting the cost", () => {
const requirement = createCostRequirement(() => ({
resource,
2023-03-21 05:15:28 +00:00
cost: 10,
cumulativeCost: false
2023-02-07 14:31:09 +00:00
}));
2023-05-20 13:30:07 +00:00
expect(unref(requirement.requirementMet)).toBe(1);
2023-02-07 14:31:09 +00:00
});
test("Requirement not met when not meeting the cost", () => {
const requirement = createCostRequirement(() => ({
resource,
2023-03-21 05:15:28 +00:00
cost: 100,
cumulativeCost: false
2023-02-07 14:31:09 +00:00
}));
2023-05-20 13:30:07 +00:00
expect(unref(requirement.requirementMet)).toBe(0);
2023-02-07 14:31:09 +00:00
});
describe("canMaximize works correctly", () => {
test("Cost function cannot maximize", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: () => 10,
maxBulkAmount: Decimal.dInf
})).canMaximize
)
).toBe(false));
test("Integrable formula cannot maximize if maxBulkAmount is left at 1", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: () => 10
})).canMaximize
)
).toBe(false));
test("Non-invertible formula cannot maximize when max bulk amount is above direct sum", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: Formula.variable(resource).abs(),
maxBulkAmount: Decimal.dInf
})).canMaximize
)
).toBe(false));
test("Non-invertible formula can maximize when max bulk amount is lte direct sum", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: Formula.variable(resource).abs(),
maxBulkAmount: 20,
directSum: 20
})).canMaximize
)
).toBe(true));
test("Invertible formula can maximize if cumulativeCost is false", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: Formula.variable(resource).lambertw(),
cumulativeCost: false,
maxBulkAmount: Decimal.dInf
})).canMaximize
)
).toBe(true));
test("Invertible formula cannot maximize if cumulativeCost is true", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: Formula.variable(resource).lambertw(),
cumulativeCost: true,
maxBulkAmount: Decimal.dInf
})).canMaximize
)
).toBe(false));
test("Integrable formula can maximize if cumulativeCost is false", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: Formula.variable(resource).pow(2),
cumulativeCost: false,
maxBulkAmount: Decimal.dInf
})).canMaximize
)
).toBe(true));
test("Integrable formula can maximize if cumulativeCost is true", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
cost: Formula.variable(resource).pow(2),
cumulativeCost: true,
maxBulkAmount: Decimal.dInf
})).canMaximize
)
).toBe(true));
});
2023-02-07 14:31:09 +00:00
test("Requirements met capped by maxBulkAmount", () =>
expect(
unref(
createCostRequirement(() => ({
resource,
2023-05-15 12:40:00 +00:00
cost: Formula.variable(resource).times(0.0001),
maxBulkAmount: 10,
cumulativeCost: false
})).requirementMet
)
).compare_tolerance(10));
2023-02-07 14:31:09 +00:00
});
test("Creating visibility requirement", () => {
const visibility = ref<Visibility.None | Visibility.Visible | boolean>(Visibility.Visible);
Feature rewrite - Removed `jsx()` and `JSXFunction`. You can now use `JSX.Element` like any other `Computable` value - `joinJSX` now always requires a joiner. Just pass the array of elements or wrap them in `<>` and `</>` if there's no joiner - Removed `coerceComponent`, `computeComponent`, and `computeOptionalComponent`; just use the `render` function now - It's recommended to now do `<MyComponent />` instead of `<component :is="myComponent" />` - All features no longer take the options as a type parameter, and all generic forms have been removed as a result - Fixed `forceHideGoBack` not being respected - Removed `deepUnref` as now things don't get unreffed before being passed into vue components by default - Moved MarkNode to new wrapper, and removed existing `mark` properties - Moved Tooltip to new wrapper, and made it take an options function instead of raw object - VueFeature component now wraps all vue features, and applies styling, classes, and visibility in the wrapping div. It also adds the Node component so features don't need to - `mergeAdjacent` now works with grids (perhaps should've used scss to reduce the amount of css this took) - `CoercableComponent` renamed to `Renderable` since it should be used with `render` - Replaced `isCoercableComponent` with `isJSXElement` - Replaced `Computable` and `ProcessedComputable` with the vue built-ins `MaybeRefOrGetter` and `MaybeRef` - `convertComputable` renamed to `processGetter` - Also removed `GetComputableTypeWithDefault` and `GetComputableType`, which can similarly be replaced - `dontMerge` is now a property on rows and columns rather than an undocumented css class you'd have to include on every feature within the row or column - Fixed saves manager not being imported in addiction warning component - Created `vueFeatureMixin` for simplifying the vue specific parts of a feature. Passes the component's properties in explicitly and directly from the feature itself - All features should now return an object that includes props typed to omit the options object and satisfies the feature. This will ensure type correctness and pass-through custom properties. (see existing features for more thorough examples of changes) - Replaced decorators with mixins, which won't require casting. Bonus amount decorators converted into generic bonus amount mixin. Removed effect decorator - All `render` functions now return `JSX.Element`. The `JSX` variants (e.g. `renderJSX`) (except `joinJSX`) have been removed - Moved all features that use the clickable component into the clickable folder - Removed `small` property from clickable, since its a single css rule (`min-height: unset`) (you could add a small css class and pass small to any vue feature's classes property, though) - Upgrades now use the clickable component - Added ConversionType symbol - Removed setDefault, just use `??=` - Added isType function that uses a type symbol to check - General cleanup
2024-11-19 14:32:45 +00:00
const requirement = createVisibilityRequirement(visibility);
expect(unref(requirement.requirementMet)).toBe(true);
visibility.value = true;
expect(unref(requirement.requirementMet)).toBe(true);
visibility.value = Visibility.None;
expect(unref(requirement.requirementMet)).toBe(false);
visibility.value = false;
expect(unref(requirement.requirementMet)).toBe(false);
});
2023-02-07 14:31:09 +00:00
test("Creating boolean requirement", () => {
const req = ref(true);
const requirement = createBooleanRequirement(req);
expect(unref(requirement.requirementMet)).toBe(true);
req.value = false;
expect(unref(requirement.requirementMet)).toBe(false);
2023-02-07 14:31:09 +00:00
});
describe("Checking all requirements met", () => {
let metRequirement: Requirement;
let unmetRequirement: Requirement;
beforeAll(() => {
metRequirement = createBooleanRequirement(true);
unmetRequirement = createBooleanRequirement(false);
});
test("Returns true if no requirements", () => {
expect(requirementsMet([])).toBe(true);
});
test("Returns true if all requirements met", () => {
expect(requirementsMet([metRequirement, metRequirement])).toBe(true);
});
test("Returns false if any requirements unmet", () => {
expect(requirementsMet([metRequirement, unmetRequirement])).toBe(false);
});
});
describe("Checking maximum levels of requirements met", () => {
test("Returns 0 if any requirement is not met", () => {
const requirements = [
createBooleanRequirement(false),
createBooleanRequirement(true),
createCostRequirement(() => ({
resource: createResource(ref(10)),
2023-03-21 05:15:28 +00:00
cost: Formula.variable(0),
cumulativeCost: false
2023-02-07 14:31:09 +00:00
}))
];
2023-02-09 13:41:58 +00:00
expect(maxRequirementsMet(requirements)).compare_tolerance(0);
2023-02-07 14:31:09 +00:00
});
test("Returns correct number of requirements met", () => {
const requirements = [
createBooleanRequirement(true),
createCostRequirement(() => ({
resource: createResource(ref(10)),
2023-03-21 05:15:28 +00:00
cost: Formula.variable(0),
cumulativeCost: false,
maxBulkAmount: Decimal.dInf
2023-02-07 14:31:09 +00:00
}))
];
2023-02-09 13:41:58 +00:00
expect(maxRequirementsMet(requirements)).compare_tolerance(10);
2023-02-07 14:31:09 +00:00
});
});
test("Paying requirements", () => {
const resource = createResource(ref(100));
const noPayment = createCostRequirement(() => ({
resource,
cost: 10,
2023-03-21 05:15:28 +00:00
requiresPay: false,
cumulativeCost: false
2023-02-07 14:31:09 +00:00
}));
const payment = createCostRequirement(() => ({
resource,
2023-03-21 05:15:28 +00:00
cost: 10,
cumulativeCost: false
2023-02-07 14:31:09 +00:00
}));
payRequirements([noPayment, payment]);
expect(resource.value).compare_tolerance(90);
});