Skip to content

Commit

Permalink
Change description function parameters
Browse files Browse the repository at this point in the history
Change UpgradeInit.description function parameters from (...args: any[]) to (level, upgrade, currency)
  • Loading branch information
xShadowBlade committed Jun 13, 2024
1 parent 00bd39d commit 209f475
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 49 deletions.
11 changes: 8 additions & 3 deletions dist/game/eMath.game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5901,7 +5901,10 @@ var UpgradeStatic = class _UpgradeStatic {
return this.dataPointerFn();
}
get description() {
return this.descriptionFn();
return this.descriptionFn(this.level, this, this.currencyPointerFn());
}
set description(value) {
this.descriptionFn = typeof value === "function" ? value : () => value;
}
/**
* The current level of the upgrade.
Expand All @@ -5917,11 +5920,13 @@ var UpgradeStatic = class _UpgradeStatic {
* Constructs a new static upgrade object.
* @param init - The upgrade object to initialize.
* @param dataPointer - A function or reference that returns the pointer of the data / frontend.
* @param currencyPointer - A function or reference that returns the pointer of the {@link CurrencyStatic} class.
* @param cacheSize - The size of the cache. Should be one less than a power of 2. See {@link cache}. Set to `0` to disable caching.
*/
constructor(init, dataPointer, cacheSize) {
constructor(init, dataPointer, currencyPointer, cacheSize) {
const data = typeof dataPointer === "function" ? dataPointer() : dataPointer;
this.dataPointerFn = typeof dataPointer === "function" ? dataPointer : () => data;
this.currencyPointerFn = typeof currencyPointer === "function" ? currencyPointer : () => currencyPointer;
this.cache = new LRUCache(cacheSize ?? _UpgradeStatic.cacheSize);
this.id = init.id;
this.name = init.name ?? init.id;
Expand Down Expand Up @@ -6173,7 +6178,7 @@ var CurrencyStatic = class {
const addedUpgradeList = [];
for (const upgrade of upgrades) {
this.pointerAddUpgrade(upgrade);
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id));
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id), () => this);
if (addedUpgradeStatic.effect && runEffectInstantly) addedUpgradeStatic.effect(addedUpgradeStatic.level, addedUpgradeStatic, this);
this.upgrades[upgrade.id] = addedUpgradeStatic;
addedUpgradeList.push(addedUpgradeStatic);
Expand Down
2 changes: 1 addition & 1 deletion dist/game/eMath.game.min.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions dist/game/eMath.game.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5867,7 +5867,10 @@ var UpgradeStatic = class _UpgradeStatic {
return this.dataPointerFn();
}
get description() {
return this.descriptionFn();
return this.descriptionFn(this.level, this, this.currencyPointerFn());
}
set description(value) {
this.descriptionFn = typeof value === "function" ? value : () => value;
}
/**
* The current level of the upgrade.
Expand All @@ -5883,11 +5886,13 @@ var UpgradeStatic = class _UpgradeStatic {
* Constructs a new static upgrade object.
* @param init - The upgrade object to initialize.
* @param dataPointer - A function or reference that returns the pointer of the data / frontend.
* @param currencyPointer - A function or reference that returns the pointer of the {@link CurrencyStatic} class.
* @param cacheSize - The size of the cache. Should be one less than a power of 2. See {@link cache}. Set to `0` to disable caching.
*/
constructor(init, dataPointer, cacheSize) {
constructor(init, dataPointer, currencyPointer, cacheSize) {
const data = typeof dataPointer === "function" ? dataPointer() : dataPointer;
this.dataPointerFn = typeof dataPointer === "function" ? dataPointer : () => data;
this.currencyPointerFn = typeof currencyPointer === "function" ? currencyPointer : () => currencyPointer;
this.cache = new LRUCache(cacheSize ?? _UpgradeStatic.cacheSize);
this.id = init.id;
this.name = init.name ?? init.id;
Expand Down Expand Up @@ -6139,7 +6144,7 @@ var CurrencyStatic = class {
const addedUpgradeList = [];
for (const upgrade of upgrades) {
this.pointerAddUpgrade(upgrade);
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id));
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id), () => this);
if (addedUpgradeStatic.effect && runEffectInstantly) addedUpgradeStatic.effect(addedUpgradeStatic.level, addedUpgradeStatic, this);
this.upgrades[upgrade.id] = addedUpgradeStatic;
addedUpgradeList.push(addedUpgradeStatic);
Expand Down
11 changes: 8 additions & 3 deletions dist/main/eMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -5212,7 +5212,10 @@ var UpgradeStatic = class _UpgradeStatic {
return this.dataPointerFn();
}
get description() {
return this.descriptionFn();
return this.descriptionFn(this.level, this, this.currencyPointerFn());
}
set description(value) {
this.descriptionFn = typeof value === "function" ? value : () => value;
}
/**
* The current level of the upgrade.
Expand All @@ -5228,11 +5231,13 @@ var UpgradeStatic = class _UpgradeStatic {
* Constructs a new static upgrade object.
* @param init - The upgrade object to initialize.
* @param dataPointer - A function or reference that returns the pointer of the data / frontend.
* @param currencyPointer - A function or reference that returns the pointer of the {@link CurrencyStatic} class.
* @param cacheSize - The size of the cache. Should be one less than a power of 2. See {@link cache}. Set to `0` to disable caching.
*/
constructor(init, dataPointer, cacheSize) {
constructor(init, dataPointer, currencyPointer, cacheSize) {
const data = typeof dataPointer === "function" ? dataPointer() : dataPointer;
this.dataPointerFn = typeof dataPointer === "function" ? dataPointer : () => data;
this.currencyPointerFn = typeof currencyPointer === "function" ? currencyPointer : () => currencyPointer;
this.cache = new LRUCache(cacheSize ?? _UpgradeStatic.cacheSize);
this.id = init.id;
this.name = init.name ?? init.id;
Expand Down Expand Up @@ -5486,7 +5491,7 @@ var CurrencyStatic = class {
const addedUpgradeList = [];
for (const upgrade of upgrades) {
this.pointerAddUpgrade(upgrade);
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id));
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id), () => this);
if (addedUpgradeStatic.effect && runEffectInstantly) addedUpgradeStatic.effect(addedUpgradeStatic.level, addedUpgradeStatic, this);
this.upgrades[upgrade.id] = addedUpgradeStatic;
addedUpgradeList.push(addedUpgradeStatic);
Expand Down
2 changes: 1 addition & 1 deletion dist/main/eMath.min.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions dist/main/eMath.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5151,7 +5151,10 @@ var UpgradeStatic = class _UpgradeStatic {
return this.dataPointerFn();
}
get description() {
return this.descriptionFn();
return this.descriptionFn(this.level, this, this.currencyPointerFn());
}
set description(value) {
this.descriptionFn = typeof value === "function" ? value : () => value;
}
/**
* The current level of the upgrade.
Expand All @@ -5167,11 +5170,13 @@ var UpgradeStatic = class _UpgradeStatic {
* Constructs a new static upgrade object.
* @param init - The upgrade object to initialize.
* @param dataPointer - A function or reference that returns the pointer of the data / frontend.
* @param currencyPointer - A function or reference that returns the pointer of the {@link CurrencyStatic} class.
* @param cacheSize - The size of the cache. Should be one less than a power of 2. See {@link cache}. Set to `0` to disable caching.
*/
constructor(init, dataPointer, cacheSize) {
constructor(init, dataPointer, currencyPointer, cacheSize) {
const data = typeof dataPointer === "function" ? dataPointer() : dataPointer;
this.dataPointerFn = typeof dataPointer === "function" ? dataPointer : () => data;
this.currencyPointerFn = typeof currencyPointer === "function" ? currencyPointer : () => currencyPointer;
this.cache = new LRUCache(cacheSize ?? _UpgradeStatic.cacheSize);
this.id = init.id;
this.name = init.name ?? init.id;
Expand Down Expand Up @@ -5425,7 +5430,7 @@ var CurrencyStatic = class {
const addedUpgradeList = [];
for (const upgrade of upgrades) {
this.pointerAddUpgrade(upgrade);
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id));
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id), () => this);
if (addedUpgradeStatic.effect && runEffectInstantly) addedUpgradeStatic.effect(addedUpgradeStatic.level, addedUpgradeStatic, this);
this.upgrades[upgrade.id] = addedUpgradeStatic;
addedUpgradeList.push(addedUpgradeStatic);
Expand Down
32 changes: 17 additions & 15 deletions dist/types/classes/Upgrade.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ interface UpgradeInit {
/**
* The description of the upgrade.
* Can be a string or a function that returns a string.
* Made into a getter function to allow for dynamic descriptions.
* Note: The use of a function is deprecated. Use a getter function instead.
* @param level - The current level of the upgrade.
* @param upgradeContext - The upgrade object that the description is being run on.
* @param currencyContext - The currency static class that the upgrade is being run on.
* @example
* // A dynamic description that returns a string
* const description = (a, b) => `This is a ${a} that returns a ${b}`;
Expand All @@ -53,7 +54,7 @@ interface UpgradeInit {
* // Getter function
* console.log(upgrade.descriptionFn("dynamic", "string")); // "This is a dynamic that returns a string"
*/
description?: ((...args: any[]) => string) | string;
description?: ((level: Decimal, upgradeContext: UpgradeStatic, currencyContext: CurrencyStatic) => string) | string;
/**
* The cost of upgrades at a certain level.
* This function should evaluate to a non-negative number, and should be deterministic and continuous for all levels above 0.
Expand Down Expand Up @@ -194,15 +195,8 @@ declare class UpgradeData implements IUpgradeData {
interface IUpgradeStatic extends Omit<UpgradeInit, "level"> {
maxLevel?: Decimal;
name: string;
description: string;
readonly description: string;
defaultLevel: Decimal;
/**
* A function that returns a description of the upgrade.
* @deprecated Use a getter function instead.
* @param args - Arguments to pass to the description function.
* @returns The description of the upgrade.
*/
descriptionFn: (...args: any[]) => string;
}
/**
* Represents the backend for an upgrade.
Expand All @@ -215,17 +209,24 @@ declare class UpgradeStatic implements IUpgradeStatic {
maxLevel: Decimal | undefined;
effect: ((level: Decimal, upgradeContext: UpgradeStatic, currencyContext: CurrencyStatic<[], string>) => void) | undefined;
el?: boolean | (() => boolean) | undefined;
descriptionFn: (...args: any[]) => string;
defaultLevel: Decimal;
/** The default size of the cache. Should be one less than a power of 2. */
static cacheSize: number;
/** The cache to store the values of certain upgrade levels */
/**
* The cache to store the values of certain upgrade levels.
* @deprecated Unfinished
*/
cache: LRUCache<UpgradeCachedELName | UpgradeCachedSumName, UpgradeCached>;
/** @returns The data of the upgrade. */
protected dataPointerFn: () => UpgradeData;
private dataPointerFn;
/** @returns The data of the upgrade. */
get data(): UpgradeData;
/** @returns The currency static class that the upgrade is being run on. */
protected currencyPointerFn: () => CurrencyStatic;
/** The description of the upgrade as a function. */
private descriptionFn;
get description(): string;
set description(value: Exclude<UpgradeInit["description"], undefined>);
/**
* The current level of the upgrade.
* @returns The current level of the upgrade.
Expand All @@ -236,9 +237,10 @@ declare class UpgradeStatic implements IUpgradeStatic {
* Constructs a new static upgrade object.
* @param init - The upgrade object to initialize.
* @param dataPointer - A function or reference that returns the pointer of the data / frontend.
* @param currencyPointer - A function or reference that returns the pointer of the {@link CurrencyStatic} class.
* @param cacheSize - The size of the cache. Should be one less than a power of 2. See {@link cache}. Set to `0` to disable caching.
*/
constructor(init: UpgradeInit, dataPointer: Pointer<UpgradeData>, cacheSize?: number);
constructor(init: UpgradeInit, dataPointer: Pointer<UpgradeData>, currencyPointer: Pointer<CurrencyStatic>, cacheSize?: number);
}
export type { IUpgradeStatic, IUpgradeData, UpgradeInit, UpgradeInitArrayType };
export { UpgradeData, UpgradeStatic, calculateUpgrade };
Expand Down
2 changes: 1 addition & 1 deletion src/classes/Currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class CurrencyStatic<U extends Readonly<UpgradeInit>[] = [], S extends string =

// Create the upgrade object
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id)!);
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id)!, () => this as CurrencyStatic);

// Run the effect instantly if needed
if (addedUpgradeStatic.effect && runEffectInstantly) addedUpgradeStatic.effect(addedUpgradeStatic.level, addedUpgradeStatic, this as CurrencyStatic);
Expand Down
43 changes: 24 additions & 19 deletions src/classes/Upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ interface UpgradeInit {
/**
* The description of the upgrade.
* Can be a string or a function that returns a string.
* Made into a getter function to allow for dynamic descriptions.
* Note: The use of a function is deprecated. Use a getter function instead.
* @param level - The current level of the upgrade.
* @param upgradeContext - The upgrade object that the description is being run on.
* @param currencyContext - The currency static class that the upgrade is being run on.
* @example
* // A dynamic description that returns a string
* const description = (a, b) => `This is a ${a} that returns a ${b}`;
Expand All @@ -163,9 +164,7 @@ interface UpgradeInit {
* // Getter function
* console.log(upgrade.descriptionFn("dynamic", "string")); // "This is a dynamic that returns a string"
*/
// description?: Pointer<string>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
description?: ((...args: any[]) => string) | string;
description?: ((level: Decimal, upgradeContext: UpgradeStatic, currencyContext: CurrencyStatic) => string) | string;

/**
* The cost of upgrades at a certain level.
Expand Down Expand Up @@ -349,40 +348,44 @@ class UpgradeData implements IUpgradeData {
interface IUpgradeStatic extends Omit<UpgradeInit, "level"> {
maxLevel?: Decimal;
name: string;
description: string;
readonly description: string;
defaultLevel: Decimal;

/**
* A function that returns a description of the upgrade.
* @deprecated Use a getter function instead.
* @param args - Arguments to pass to the description function.
* @returns The description of the upgrade.
*/
descriptionFn: (...args: any[]) => string;
}

/**
* Represents the backend for an upgrade.
*/
class UpgradeStatic implements IUpgradeStatic {
public id; name; cost; costBulk; maxLevel; effect; el?; descriptionFn; defaultLevel: Decimal;
public id; name; cost; costBulk; maxLevel; effect; el?; defaultLevel: Decimal;

/** The default size of the cache. Should be one less than a power of 2. */
public static cacheSize = 15;

/** The cache to store the values of certain upgrade levels */
/**
* The cache to store the values of certain upgrade levels.
* @deprecated Unfinished
*/
public cache: LRUCache<UpgradeCachedELName | UpgradeCachedSumName, UpgradeCached>;

/** @returns The data of the upgrade. */
protected dataPointerFn: () => UpgradeData;
private dataPointerFn: () => UpgradeData;

/** @returns The data of the upgrade. */
public get data (): UpgradeData {
return this.dataPointerFn();
}

/** @returns The currency static class that the upgrade is being run on. */
protected currencyPointerFn: () => CurrencyStatic;

/** The description of the upgrade as a function. */
private descriptionFn: Exclude<UpgradeInit["description"], string | undefined>;

public get description (): string {
return this.descriptionFn();
return this.descriptionFn(this.level, this, this.currencyPointerFn());
}
public set description (value: Exclude<UpgradeInit["description"], undefined>) {
this.descriptionFn = typeof value === "function" ? value : (): string => value;
}

/**
Expand All @@ -402,11 +405,13 @@ class UpgradeStatic implements IUpgradeStatic {
* Constructs a new static upgrade object.
* @param init - The upgrade object to initialize.
* @param dataPointer - A function or reference that returns the pointer of the data / frontend.
* @param currencyPointer - A function or reference that returns the pointer of the {@link CurrencyStatic} class.
* @param cacheSize - The size of the cache. Should be one less than a power of 2. See {@link cache}. Set to `0` to disable caching.
*/
constructor (init: UpgradeInit, dataPointer: Pointer<UpgradeData>, cacheSize?: number) {
constructor (init: UpgradeInit, dataPointer: Pointer<UpgradeData>, currencyPointer: Pointer<CurrencyStatic>, cacheSize?: number) {
const data = (typeof dataPointer === "function" ? dataPointer() : dataPointer);
this.dataPointerFn = typeof dataPointer === "function" ? dataPointer : (): UpgradeData => data;
this.currencyPointerFn = typeof currencyPointer === "function" ? currencyPointer : (): CurrencyStatic => currencyPointer;

this.cache = new LRUCache(cacheSize ?? UpgradeStatic.cacheSize);
this.id = init.id;
Expand Down

0 comments on commit 209f475

Please sign in to comment.