Skip to content

Commit

Permalink
Add roundBase, Attribute boost, resetLayer, code optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
xShadowBlade committed Apr 21, 2024
1 parent 3de42f4 commit a575fb3
Show file tree
Hide file tree
Showing 22 changed files with 231 additions and 363 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dist/
bin/build/umdPlugin.js
documentation/production/
build/

# Logs
logs
Expand Down
97 changes: 29 additions & 68 deletions dist/game/eMath.game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5656,12 +5656,11 @@ var AttributeStatic = class {
* @param useBoost - Indicates whether to use boost for the attribute.
* @param initial - The initial value of the attribute.
*/
constructor(pointer, useBoost = true, initial = 0) {
constructor(pointer, useBoost, initial = 0) {
this.initial = E(initial);
pointer = pointer ? typeof pointer === "function" ? pointer : () => pointer : () => new Attribute(this.initial);
this.pointerFn = pointer;
if (useBoost)
this.boost = new Boost(this.initial);
this.boost = useBoost ? new Boost(this.initial) : void 0;
}
/**
* Updates the value of the attribute.
Expand Down Expand Up @@ -6175,7 +6174,7 @@ var DataManager = class {
* @param data - The data to decompile. If not provided, it will be fetched from localStorage using the key `${game.config.name.id}-data`.
* @returns The decompiled object, or null if the data is empty or invalid.
*/
decompileData(data = localStorage.getItem(`${this.gameRef.config.name.id}-data`)) {
decompileData(data = window?.localStorage.getItem(`${this.gameRef.config.name.id}-data`)) {
if (!data)
return null;
let parsedData;
Expand Down Expand Up @@ -6215,15 +6214,19 @@ var DataManager = class {
this.data = this.normalData;
this.saveData();
if (reload)
window.location.reload();
window?.location.reload();
}
/**
* Saves the game data to local storage under the key `${game.config.name.id}-data`.
* If you dont want to save to local storage, use {@link compileData} instead.
* @param dataToSave - The data to save. If not provided, it will be fetched from localStorage using {@link compileData}.
*/
saveData(dataToSave = this.compileData()) {
localStorage.setItem(`${this.gameRef.config.name.id}-data`, dataToSave);
if (!dataToSave)
throw new Error("dataManager.saveData(): Data to save is empty.");
if (!window.localStorage)
throw new Error("dataManager.saveData(): Local storage is not supported. You can use compileData() instead to implement a custom save system.");
window.localStorage.setItem(`${this.gameRef.config.name.id}-data`, dataToSave);
}
/**
* Compiles the game data and prompts the user to download it as a text file using {@link window.prompt}.
Expand Down Expand Up @@ -6284,52 +6287,9 @@ var DataManager = class {
return out;
}
let loadedDataProcessed = !mergeData ? loadedData : deepMerge(this.normalDataPlain, this.normalData, loadedData);
const templateClasses = function(templateClassesInit) {
const out = [];
for (const templateClassConvert of templateClassesInit) {
out.push({
class: templateClassConvert.class,
name: templateClassConvert.name,
// subclasses: templateClassConvert.subclasses,
properties: Object.getOwnPropertyNames((0, import_class_transformer5.instanceToPlain)(new templateClassConvert.class()))
});
}
return out;
}([
{
class: Attribute,
name: "attribute"
// subclasses: {
// value: Decimal,
// },
},
// {
// class: boost,
// subclasses: {
// baseEffect: Decimal,
// boostArray: [boostObject],
// },
// },
{
class: Currency,
name: "currency"
// subclasses: {
// // boost: boost,
// upgrades: [upgradeData],
// value: Decimal,
// },
},
{
class: Decimal,
name: "Decimal"
}
]);
function compareArrays(arr1, arr2) {
return arr1.length === arr2.length && arr1.every((val) => arr2.includes(val));
}
const upgradeDataProperties = Object.getOwnPropertyNames(new UpgradeData({ id: "", level: E(0) }));
function convertTemplateClass(templateClassToConvert, plain) {
const out = (0, import_class_transformer5.plainToInstance)(templateClassToConvert.class, plain);
const out = (0, import_class_transformer5.plainToInstance)(templateClassToConvert, plain);
if (out instanceof Currency) {
for (const upgradeName in out.upgrades) {
const upgrade = out.upgrades[upgradeName];
Expand All @@ -6344,26 +6304,25 @@ var DataManager = class {
throw new Error(`Failed to convert ${templateClassToConvert.name} to class instance.`);
return out;
}
function plainToInstanceRecursive(plain) {
function plainToInstanceRecursive(normal, plain) {
const out = plain;
for (const key in plain) {
if (!(plain[key] instanceof Object && plain[key].constructor === Object))
for (const key in normal) {
if (!plain[key]) {
console.warn(`Missing property "${key}" in loaded data.`);
continue;
}
if (!isPlainObject(plain[key]))
continue;
const normalDataClass = normal[key].constructor;
if (normalDataClass === Object) {
out[key] = plainToInstanceRecursive(normal[key], plain[key]);
continue;
if ((() => {
for (const templateClassR of templateClasses) {
if (compareArrays(Object.getOwnPropertyNames(plain[key]), templateClassR.properties)) {
out[key] = convertTemplateClass(templateClassR, plain[key]);
return false;
}
}
return true;
})()) {
out[key] = plainToInstanceRecursive(plain[key]);
}
out[key] = convertTemplateClass(normalDataClass, plain[key]);
}
return out;
}
loadedDataProcessed = plainToInstanceRecursive(loadedDataProcessed);
loadedDataProcessed = plainToInstanceRecursive(this.normalData, loadedDataProcessed);
return loadedDataProcessed;
}
/**
Expand Down Expand Up @@ -6470,7 +6429,7 @@ var GameReset = class {
*/
constructor(currenciesToReset, extender) {
this.currenciesToReset = Array.isArray(currenciesToReset) ? currenciesToReset : [currenciesToReset];
this.extender = extender;
this.extender = Array.isArray(extender) ? extender : extender ? [extender] : [];
this.id = Symbol();
}
/**
Expand All @@ -6481,9 +6440,11 @@ var GameReset = class {
this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
if (this.extender && this.extender.id !== this.id) {
this.extender.reset();
}
this.extender.forEach((extender) => {
if (extender && extender.id !== this.id) {
extender.reset();
}
});
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/game/eMath.game.min.js

Large diffs are not rendered by default.

97 changes: 29 additions & 68 deletions dist/game/eMath.game.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5623,12 +5623,11 @@ var AttributeStatic = class {
* @param useBoost - Indicates whether to use boost for the attribute.
* @param initial - The initial value of the attribute.
*/
constructor(pointer, useBoost = true, initial = 0) {
constructor(pointer, useBoost, initial = 0) {
this.initial = E(initial);
pointer = pointer ? typeof pointer === "function" ? pointer : () => pointer : () => new Attribute(this.initial);
this.pointerFn = pointer;
if (useBoost)
this.boost = new Boost(this.initial);
this.boost = useBoost ? new Boost(this.initial) : void 0;
}
/**
* Updates the value of the attribute.
Expand Down Expand Up @@ -6142,7 +6141,7 @@ var DataManager = class {
* @param data - The data to decompile. If not provided, it will be fetched from localStorage using the key `${game.config.name.id}-data`.
* @returns The decompiled object, or null if the data is empty or invalid.
*/
decompileData(data = localStorage.getItem(`${this.gameRef.config.name.id}-data`)) {
decompileData(data = window?.localStorage.getItem(`${this.gameRef.config.name.id}-data`)) {
if (!data)
return null;
let parsedData;
Expand Down Expand Up @@ -6182,15 +6181,19 @@ var DataManager = class {
this.data = this.normalData;
this.saveData();
if (reload)
window.location.reload();
window?.location.reload();
}
/**
* Saves the game data to local storage under the key `${game.config.name.id}-data`.
* If you dont want to save to local storage, use {@link compileData} instead.
* @param dataToSave - The data to save. If not provided, it will be fetched from localStorage using {@link compileData}.
*/
saveData(dataToSave = this.compileData()) {
localStorage.setItem(`${this.gameRef.config.name.id}-data`, dataToSave);
if (!dataToSave)
throw new Error("dataManager.saveData(): Data to save is empty.");
if (!window.localStorage)
throw new Error("dataManager.saveData(): Local storage is not supported. You can use compileData() instead to implement a custom save system.");
window.localStorage.setItem(`${this.gameRef.config.name.id}-data`, dataToSave);
}
/**
* Compiles the game data and prompts the user to download it as a text file using {@link window.prompt}.
Expand Down Expand Up @@ -6251,52 +6254,9 @@ var DataManager = class {
return out;
}
let loadedDataProcessed = !mergeData ? loadedData : deepMerge(this.normalDataPlain, this.normalData, loadedData);
const templateClasses = function(templateClassesInit) {
const out = [];
for (const templateClassConvert of templateClassesInit) {
out.push({
class: templateClassConvert.class,
name: templateClassConvert.name,
// subclasses: templateClassConvert.subclasses,
properties: Object.getOwnPropertyNames(instanceToPlain(new templateClassConvert.class()))
});
}
return out;
}([
{
class: Attribute,
name: "attribute"
// subclasses: {
// value: Decimal,
// },
},
// {
// class: boost,
// subclasses: {
// baseEffect: Decimal,
// boostArray: [boostObject],
// },
// },
{
class: Currency,
name: "currency"
// subclasses: {
// // boost: boost,
// upgrades: [upgradeData],
// value: Decimal,
// },
},
{
class: Decimal,
name: "Decimal"
}
]);
function compareArrays(arr1, arr2) {
return arr1.length === arr2.length && arr1.every((val) => arr2.includes(val));
}
const upgradeDataProperties = Object.getOwnPropertyNames(new UpgradeData({ id: "", level: E(0) }));
function convertTemplateClass(templateClassToConvert, plain) {
const out = plainToInstance(templateClassToConvert.class, plain);
const out = plainToInstance(templateClassToConvert, plain);
if (out instanceof Currency) {
for (const upgradeName in out.upgrades) {
const upgrade = out.upgrades[upgradeName];
Expand All @@ -6311,26 +6271,25 @@ var DataManager = class {
throw new Error(`Failed to convert ${templateClassToConvert.name} to class instance.`);
return out;
}
function plainToInstanceRecursive(plain) {
function plainToInstanceRecursive(normal, plain) {
const out = plain;
for (const key in plain) {
if (!(plain[key] instanceof Object && plain[key].constructor === Object))
for (const key in normal) {
if (!plain[key]) {
console.warn(`Missing property "${key}" in loaded data.`);
continue;
}
if (!isPlainObject(plain[key]))
continue;
const normalDataClass = normal[key].constructor;
if (normalDataClass === Object) {
out[key] = plainToInstanceRecursive(normal[key], plain[key]);
continue;
if ((() => {
for (const templateClassR of templateClasses) {
if (compareArrays(Object.getOwnPropertyNames(plain[key]), templateClassR.properties)) {
out[key] = convertTemplateClass(templateClassR, plain[key]);
return false;
}
}
return true;
})()) {
out[key] = plainToInstanceRecursive(plain[key]);
}
out[key] = convertTemplateClass(normalDataClass, plain[key]);
}
return out;
}
loadedDataProcessed = plainToInstanceRecursive(loadedDataProcessed);
loadedDataProcessed = plainToInstanceRecursive(this.normalData, loadedDataProcessed);
return loadedDataProcessed;
}
/**
Expand Down Expand Up @@ -6437,7 +6396,7 @@ var GameReset = class {
*/
constructor(currenciesToReset, extender) {
this.currenciesToReset = Array.isArray(currenciesToReset) ? currenciesToReset : [currenciesToReset];
this.extender = extender;
this.extender = Array.isArray(extender) ? extender : extender ? [extender] : [];
this.id = Symbol();
}
/**
Expand All @@ -6448,9 +6407,11 @@ var GameReset = class {
this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
if (this.extender && this.extender.id !== this.id) {
this.extender.reset();
}
this.extender.forEach((extender) => {
if (extender && extender.id !== this.id) {
extender.reset();
}
});
}
};

Expand Down
17 changes: 14 additions & 3 deletions dist/main/eMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ __export(src_exports, {
calculateUpgrade: () => calculateUpgrade,
decimalToJSONString: () => decimalToJSONString,
inverseFunctionApprox: () => inverseFunctionApprox,
roundingBase: () => roundingBase,
upgradeToCacheNameEL: () => upgradeToCacheNameEL
});
module.exports = __toCommonJS(src_exports);
Expand Down Expand Up @@ -4967,6 +4968,17 @@ function calculateSum(f, b, a = 0, epsilon, iterations) {
return calculateSumApprox(f, b, a, iterations);
}
}
function roundingBase(x, acc = 10, sig = 0, max = 1e3) {
x = E(x);
if (x.gte(E.pow(acc, max)))
return x;
const powerN = E.floor(E.log(x, acc));
let out = x.div(E.pow(acc, powerN));
out = out.mul(E.pow(acc, sig)).round();
out = out.div(E.pow(acc, sig));
out = out.mul(E.pow(acc, powerN));
return out;
}

// src/classes/Upgrade.ts
function calculateUpgrade(value, upgrade, start, end, mode, iterations, el = false) {
Expand Down Expand Up @@ -5401,12 +5413,11 @@ var AttributeStatic = class {
* @param useBoost - Indicates whether to use boost for the attribute.
* @param initial - The initial value of the attribute.
*/
constructor(pointer, useBoost = true, initial = 0) {
constructor(pointer, useBoost, initial = 0) {
this.initial = E(initial);
pointer = pointer ? typeof pointer === "function" ? pointer : () => pointer : () => new Attribute(this.initial);
this.pointerFn = pointer;
if (useBoost)
this.boost = new Boost(this.initial);
this.boost = useBoost ? new Boost(this.initial) : void 0;
}
/**
* Updates the value of the attribute.
Expand Down
Loading

0 comments on commit a575fb3

Please sign in to comment.