pull current changes #1

Merged
nif merged 8 commits from profectus/Profectus:main into main 2024-03-02 19:40:14 +00:00
3 changed files with 37 additions and 38 deletions
Showing only changes of commit 2e0e221010 - Show all commits

View file

@ -4,7 +4,7 @@ import { jsx } from "features/feature";
import settings from "game/settings";
import type { DecimalSource } from "util/bignum";
import Decimal, { formatSmall } from "util/bignum";
import type { WithRequired } from "util/common";
import type { OmitOptional, OptionalKeys, RequiredKeys, WithRequired } from "util/common";
import type { Computable, ProcessedComputable } from "util/computed";
import { convertComputable } from "util/computed";
import { createLazyProxy } from "util/proxies";
@ -38,16 +38,11 @@ export interface Modifier {
description?: ProcessedComputable<CoercableComponent>;
}
/**
* Utility type used to narrow down a modifier type that will have a description and/or enabled property based on optional parameters, T and S (respectively).
*/
export type ModifierFromOptionalParams<T, S> = undefined extends T
? undefined extends S
? Omit<WithRequired<Modifier, "invert" | "getFormula">, "description" | "enabled">
: Omit<WithRequired<Modifier, "invert" | "enabled" | "getFormula">, "description">
: undefined extends S
? Omit<WithRequired<Modifier, "invert" | "description" | "getFormula">, "enabled">
: WithRequired<Modifier, "invert" | "enabled" | "description" | "getFormula">;
/** Utility type that represents the output of all modifiers that represent a single operation. */
export type OperationModifier<T> = WithRequired<
Modifier,
"invert" | "getFormula" | Extract<RequiredKeys<T>, keyof Modifier>
>;
/** An object that configures an additive modifier via {@link createAdditiveModifier}. */
export interface AdditiveModifierOptions {
@ -65,9 +60,9 @@ export interface AdditiveModifierOptions {
* Create a modifier that adds some value to the input value.
* @param optionsFunc Additive modifier options.
*/
export function createAdditiveModifier<T extends AdditiveModifierOptions>(
export function createAdditiveModifier<T extends AdditiveModifierOptions, S = OperationModifier<T>>(
optionsFunc: OptionsFunc<T>
): ModifierFromOptionalParams<T["description"], T["enabled"]> {
) {
return createLazyProxy(feature => {
const { addend, description, enabled, smallerIsBetter } = optionsFunc.call(
feature,
@ -111,7 +106,7 @@ export function createAdditiveModifier<T extends AdditiveModifierOptions>(
</div>
))
};
}) as unknown as ModifierFromOptionalParams<T["description"], T["enabled"]>;
}) as S;
}
/** An object that configures an multiplicative modifier via {@link createMultiplicativeModifier}. */
@ -130,9 +125,10 @@ export interface MultiplicativeModifierOptions {
* Create a modifier that multiplies the input value by some value.
* @param optionsFunc Multiplicative modifier options.
*/
export function createMultiplicativeModifier<T extends MultiplicativeModifierOptions>(
optionsFunc: OptionsFunc<T>
): ModifierFromOptionalParams<T["description"], T["enabled"]> {
export function createMultiplicativeModifier<
T extends MultiplicativeModifierOptions,
S = OperationModifier<T>
>(optionsFunc: OptionsFunc<T>) {
return createLazyProxy(feature => {
const { multiplier, description, enabled, smallerIsBetter } = optionsFunc.call(
feature,
@ -175,7 +171,7 @@ export function createMultiplicativeModifier<T extends MultiplicativeModifierOpt
</div>
))
};
}) as unknown as ModifierFromOptionalParams<T["description"], T["enabled"]>;
}) as S;
}
/** An object that configures an exponential modifier via {@link createExponentialModifier}. */
@ -196,9 +192,10 @@ export interface ExponentialModifierOptions {
* Create a modifier that raises the input value to the power of some value.
* @param optionsFunc Exponential modifier options.
*/
export function createExponentialModifier<T extends ExponentialModifierOptions>(
optionsFunc: OptionsFunc<T>
): ModifierFromOptionalParams<T["description"], T["enabled"]> {
export function createExponentialModifier<
T extends ExponentialModifierOptions,
S = OperationModifier<T>
>(optionsFunc: OptionsFunc<T>) {
return createLazyProxy(feature => {
const { exponent, description, enabled, supportLowNumbers, smallerIsBetter } =
optionsFunc.call(feature, feature);
@ -263,7 +260,7 @@ export function createExponentialModifier<T extends ExponentialModifierOptions>(
</div>
))
};
}) as unknown as ModifierFromOptionalParams<T["description"], T["enabled"]>;
}) as S;
}
/**
@ -274,15 +271,9 @@ export function createExponentialModifier<T extends ExponentialModifierOptions>(
* @see {@link createModifierSection}.
*/
export function createSequentialModifier<
T extends Modifier[],
S = T extends WithRequired<Modifier, "invert">[]
? T extends WithRequired<Modifier, "getFormula">[]
? WithRequired<Modifier, "description" | "invert" | "getFormula">
: Omit<WithRequired<Modifier, "description" | "getFormula">, "invert">
: T extends WithRequired<Modifier, "invert">[]
? WithRequired<Modifier, "getFormula" | "invert">
: Omit<WithRequired<Modifier, "getFormula">, "invert">
>(modifiersFunc: () => T): S {
T extends Modifier,
S = WithRequired<Modifier, Extract<RequiredKeys<T>, keyof Modifier>>
>(modifiersFunc: () => T[]) {
return createLazyProxy(() => {
const modifiers = modifiersFunc();
@ -325,7 +316,7 @@ export function createSequentialModifier<
))
: undefined
};
}) as unknown as S;
}) as S;
}
/** An object that configures a modifier section via {@link createModifierSection}. */

View file

@ -1,3 +1,11 @@
export type RequiredKeys<T> = {
[K in keyof T]-?: NonNullable<unknown> extends Pick<T, K> ? never : K;
}[keyof T];
export type OptionalKeys<T> = {
[K in keyof T]-?: NonNullable<unknown> extends Pick<T, K> ? K : never;
}[keyof T];
export type OmitOptional<T> = Pick<T, RequiredKeys<T>>;
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
export type ArrayElements<T extends ReadonlyArray<unknown>> = T extends ReadonlyArray<infer S>

View file

@ -133,14 +133,14 @@ describe("Exponential Modifiers", () =>
testModifiers(createExponentialModifier, "exponent", Decimal.pow));
describe("Sequential Modifiers", () => {
function createModifier(
function createModifier<T extends Partial<ModifierConstructorOptions>>(
value: Computable<DecimalSource>,
options: Partial<ModifierConstructorOptions> = {}
): WithRequired<Modifier, "invert" | "getFormula"> {
options?: T
) {
return createSequentialModifier(() => [
createAdditiveModifier(() => ({ ...options, addend: value })),
createMultiplicativeModifier(() => ({ ...options, multiplier: value })),
createExponentialModifier(() => ({ ...options, exponent: value }))
createAdditiveModifier(() => ({ ...(options ?? {}), addend: value })),
createMultiplicativeModifier(() => ({ ...(options ?? {}), multiplier: value })),
createExponentialModifier(() => ({ ...(options ?? {}), exponent: value }))
]);
}