Skip to content

Commit

Permalink
Change Currency and Boost code
Browse files Browse the repository at this point in the history
Add comments, CurrencyStatic.queryUpgrade, code refactors
  • Loading branch information
xShadowBlade committed Jun 11, 2024
1 parent 845ed6c commit acf029f
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 224 deletions.
79 changes: 52 additions & 27 deletions dist/game/eMath.game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5588,20 +5588,26 @@ var import_class_transformer3 = require("class-transformer");

// src/classes/Boost.ts
var BoostObject = class {
// eslint-disable-next-line jsdoc/require-returns
/** @deprecated Use {@link description} instead */
/**
* @returns The description of the boost.
* @deprecated Use {@link description} instead
*/
get desc() {
return this.description;
}
get description() {
return this.descriptionFn();
}
/**
* Constructs a new boost object.
* @param init - The initialization object.
*/
constructor(init) {
this.id = init.id;
this.name = init.name ?? "";
this.descriptionFn = init.description ? typeof init.description === "function" ? init.description : () => init.description : () => "";
this.value = init.value;
this.order = init.order ?? 99;
this.descriptionFn = init.description ? typeof init.description === "function" ? init.description : () => init.description : () => "";
}
};
var Boost = class {
Expand Down Expand Up @@ -5976,10 +5982,6 @@ var UpgradeStatic = class _UpgradeStatic {

// src/classes/Currency.ts
var Currency = class {
// public upgrades: UpgradeData<string>[];
// /** A boost object that affects the currency gain. */
// @Expose()
// public boost: boost;
/**
* Constructs a new currency object with an initial value of 0.
*/
Expand Down Expand Up @@ -6034,13 +6036,7 @@ var CurrencyStatic = class {
this.pointerFn = typeof pointer === "function" ? pointer : () => pointer;
this.boost = new Boost(this.defaultBoost);
this.pointer.value = this.defaultVal;
this.upgrades = {
// *[Symbol.iterator] () {
// for (const upgrade of Object.values(this)) {
// yield upgrade;
// }
// },
};
this.upgrades = {};
if (upgrades) this.addUpgrade(upgrades);
}
/**
Expand Down Expand Up @@ -6113,6 +6109,39 @@ var CurrencyStatic = class {
getUpgrade(id) {
return this.upgrades[id] ?? null;
}
/**
* Queries upgrades based on the provided id. Returns an array of upgrades that match the id.
* @param id - The id of the upgrade to query.
* @returns An array of upgrades that match the id.
* @example
* const currency = new CurrencyStatic(undefined, [
* { id: "healthBoostSmall", cost: (level) => level.mul(10) },
* { id: "healthBoostLarge", cost: (level) => level.mul(20) },
* { id: "damageBoostSmall", cost: (level) => level.mul(10) },
* { id: "damageBoostLarge", cost: (level) => level.mul(20) },
* ] as const satisfies UpgradeInit[]);
*
* // Get all health upgrades
* const healthUpgrades = currency.queryUpgrade(/health/); // [{ id: "healthBoostSmall", ... }, { id: "healthBoostLarge", ... }]
*
* // Get all small upgrades
* const smallUpgrades = currency.queryUpgrade(["healthBoostSmall", "damageBoostSmall"]);
* // or
* const smallUpgrades2 = currency.queryUpgrade(/.*Small/);
*/
queryUpgrade(id) {
const allUpgradeIds = Object.keys(this.upgrades);
if (id instanceof RegExp) {
const regex = id;
const matchedIds = allUpgradeIds.filter((upgrade) => regex.test(upgrade));
return matchedIds.map((matchedId) => this.upgrades[matchedId]);
}
if (typeof id === "string") {
id = [id];
}
const matchedUpgrades = allUpgradeIds.filter((upgrade) => id.includes(upgrade));
return matchedUpgrades.map((matchedId) => this.upgrades[matchedId]);
}
/**
* Creates upgrades. To update an upgrade, use {@link updateUpgrade} instead.
* @param upgrades - An array of upgrade objects.
Expand Down Expand Up @@ -6141,20 +6170,20 @@ var CurrencyStatic = class {
*/
addUpgrade(upgrades, runEffectInstantly = true) {
if (!Array.isArray(upgrades)) upgrades = [upgrades];
const addedUpgradeList = {};
const addedUpgradeList = [];
for (const upgrade of upgrades) {
const addedUpgradeData = this.pointerAddUpgrade(upgrade);
this.pointerAddUpgrade(upgrade);
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id));
if (addedUpgradeStatic.effect && runEffectInstantly) addedUpgradeStatic.effect(addedUpgradeStatic.level, addedUpgradeStatic, this);
addedUpgradeList[upgrade.id] = addedUpgradeStatic;
this.upgrades[upgrade.id] = addedUpgradeStatic;
addedUpgradeList.push(addedUpgradeStatic);
}
return Object.values(addedUpgradeList);
return addedUpgradeList;
}
/**
* Updates an upgrade. To create an upgrade, use {@link addUpgrade} instead.
* @param id - The id of the upgrade to update.
* @param upgrade - The upgrade object to update.
* @param newUpgrade - The new upgrade object.
* @example
* currency.updateUpgrade("healthBoost", {
* name: "New Health Boost".
Expand All @@ -6165,13 +6194,10 @@ var CurrencyStatic = class {
* }
* });
*/
updateUpgrade(id, upgrade) {
const upgrade1 = this.getUpgrade(id);
if (upgrade1 === null) return;
upgrade1.name = upgrade.name ?? upgrade1.name;
upgrade1.cost = upgrade.cost ?? upgrade1.cost;
upgrade1.maxLevel = upgrade.maxLevel ?? upgrade1.maxLevel;
upgrade1.effect = upgrade.effect ?? upgrade1.effect;
updateUpgrade(id, newUpgrade) {
const oldUpgrade = this.getUpgrade(id);
if (oldUpgrade === null) return;
Object.assign(oldUpgrade, newUpgrade);
}
/**
* Calculates the cost and how many upgrades you can buy.
Expand All @@ -6185,7 +6211,6 @@ var CurrencyStatic = class {
* // Calculate how many healthBoost upgrades you can buy and the cost of the upgrades
* const [amount, cost] = currency.calculateUpgrade("healthBoost", 10);
*/
// public calculateUpgrade (id: string, target: DecimalSource = 1, el: boolean = false): [amount: Decimal, cost: Decimal] {
calculateUpgrade(id, target = Infinity, mode, iterations) {
const upgrade = this.getUpgrade(id);
if (upgrade === null) {
Expand Down
2 changes: 1 addition & 1 deletion dist/game/eMath.game.min.js

Large diffs are not rendered by default.

79 changes: 52 additions & 27 deletions dist/game/eMath.game.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5554,20 +5554,26 @@ import { Type as Type2 } from "class-transformer";

// src/classes/Boost.ts
var BoostObject = class {
// eslint-disable-next-line jsdoc/require-returns
/** @deprecated Use {@link description} instead */
/**
* @returns The description of the boost.
* @deprecated Use {@link description} instead
*/
get desc() {
return this.description;
}
get description() {
return this.descriptionFn();
}
/**
* Constructs a new boost object.
* @param init - The initialization object.
*/
constructor(init) {
this.id = init.id;
this.name = init.name ?? "";
this.descriptionFn = init.description ? typeof init.description === "function" ? init.description : () => init.description : () => "";
this.value = init.value;
this.order = init.order ?? 99;
this.descriptionFn = init.description ? typeof init.description === "function" ? init.description : () => init.description : () => "";
}
};
var Boost = class {
Expand Down Expand Up @@ -5942,10 +5948,6 @@ var UpgradeStatic = class _UpgradeStatic {

// src/classes/Currency.ts
var Currency = class {
// public upgrades: UpgradeData<string>[];
// /** A boost object that affects the currency gain. */
// @Expose()
// public boost: boost;
/**
* Constructs a new currency object with an initial value of 0.
*/
Expand Down Expand Up @@ -6000,13 +6002,7 @@ var CurrencyStatic = class {
this.pointerFn = typeof pointer === "function" ? pointer : () => pointer;
this.boost = new Boost(this.defaultBoost);
this.pointer.value = this.defaultVal;
this.upgrades = {
// *[Symbol.iterator] () {
// for (const upgrade of Object.values(this)) {
// yield upgrade;
// }
// },
};
this.upgrades = {};
if (upgrades) this.addUpgrade(upgrades);
}
/**
Expand Down Expand Up @@ -6079,6 +6075,39 @@ var CurrencyStatic = class {
getUpgrade(id) {
return this.upgrades[id] ?? null;
}
/**
* Queries upgrades based on the provided id. Returns an array of upgrades that match the id.
* @param id - The id of the upgrade to query.
* @returns An array of upgrades that match the id.
* @example
* const currency = new CurrencyStatic(undefined, [
* { id: "healthBoostSmall", cost: (level) => level.mul(10) },
* { id: "healthBoostLarge", cost: (level) => level.mul(20) },
* { id: "damageBoostSmall", cost: (level) => level.mul(10) },
* { id: "damageBoostLarge", cost: (level) => level.mul(20) },
* ] as const satisfies UpgradeInit[]);
*
* // Get all health upgrades
* const healthUpgrades = currency.queryUpgrade(/health/); // [{ id: "healthBoostSmall", ... }, { id: "healthBoostLarge", ... }]
*
* // Get all small upgrades
* const smallUpgrades = currency.queryUpgrade(["healthBoostSmall", "damageBoostSmall"]);
* // or
* const smallUpgrades2 = currency.queryUpgrade(/.*Small/);
*/
queryUpgrade(id) {
const allUpgradeIds = Object.keys(this.upgrades);
if (id instanceof RegExp) {
const regex = id;
const matchedIds = allUpgradeIds.filter((upgrade) => regex.test(upgrade));
return matchedIds.map((matchedId) => this.upgrades[matchedId]);
}
if (typeof id === "string") {
id = [id];
}
const matchedUpgrades = allUpgradeIds.filter((upgrade) => id.includes(upgrade));
return matchedUpgrades.map((matchedId) => this.upgrades[matchedId]);
}
/**
* Creates upgrades. To update an upgrade, use {@link updateUpgrade} instead.
* @param upgrades - An array of upgrade objects.
Expand Down Expand Up @@ -6107,20 +6136,20 @@ var CurrencyStatic = class {
*/
addUpgrade(upgrades, runEffectInstantly = true) {
if (!Array.isArray(upgrades)) upgrades = [upgrades];
const addedUpgradeList = {};
const addedUpgradeList = [];
for (const upgrade of upgrades) {
const addedUpgradeData = this.pointerAddUpgrade(upgrade);
this.pointerAddUpgrade(upgrade);
const addedUpgradeStatic = new UpgradeStatic(upgrade, () => this.pointerGetUpgrade(upgrade.id));
if (addedUpgradeStatic.effect && runEffectInstantly) addedUpgradeStatic.effect(addedUpgradeStatic.level, addedUpgradeStatic, this);
addedUpgradeList[upgrade.id] = addedUpgradeStatic;
this.upgrades[upgrade.id] = addedUpgradeStatic;
addedUpgradeList.push(addedUpgradeStatic);
}
return Object.values(addedUpgradeList);
return addedUpgradeList;
}
/**
* Updates an upgrade. To create an upgrade, use {@link addUpgrade} instead.
* @param id - The id of the upgrade to update.
* @param upgrade - The upgrade object to update.
* @param newUpgrade - The new upgrade object.
* @example
* currency.updateUpgrade("healthBoost", {
* name: "New Health Boost".
Expand All @@ -6131,13 +6160,10 @@ var CurrencyStatic = class {
* }
* });
*/
updateUpgrade(id, upgrade) {
const upgrade1 = this.getUpgrade(id);
if (upgrade1 === null) return;
upgrade1.name = upgrade.name ?? upgrade1.name;
upgrade1.cost = upgrade.cost ?? upgrade1.cost;
upgrade1.maxLevel = upgrade.maxLevel ?? upgrade1.maxLevel;
upgrade1.effect = upgrade.effect ?? upgrade1.effect;
updateUpgrade(id, newUpgrade) {
const oldUpgrade = this.getUpgrade(id);
if (oldUpgrade === null) return;
Object.assign(oldUpgrade, newUpgrade);
}
/**
* Calculates the cost and how many upgrades you can buy.
Expand All @@ -6151,7 +6177,6 @@ var CurrencyStatic = class {
* // Calculate how many healthBoost upgrades you can buy and the cost of the upgrades
* const [amount, cost] = currency.calculateUpgrade("healthBoost", 10);
*/
// public calculateUpgrade (id: string, target: DecimalSource = 1, el: boolean = false): [amount: Decimal, cost: Decimal] {
calculateUpgrade(id, target = Infinity, mode, iterations) {
const upgrade = this.getUpgrade(id);
if (upgrade === null) {
Expand Down
Loading

0 comments on commit acf029f

Please sign in to comment.