Cleaned up store/layers.js

This commit is contained in:
thepaperpilot 2021-06-28 22:34:02 -05:00
parent 465d15df15
commit f50d634812

View file

@ -73,8 +73,7 @@ export function addLayer(layer, player = null) {
layer.upgrades[id].bought = function() { layer.upgrades[id].bought = function() {
return !layer.deactivated && playerProxy[layer.id].upgrades.some(upgrade => upgrade == id); return !layer.deactivated && playerProxy[layer.id].upgrades.some(upgrade => upgrade == id);
} }
if (layer.upgrades[id].canAfford == undefined) { setDefault(layer.upgrades[id], 'canAfford', function() {
layer.upgrades[id].canAfford = function() {
if (this.currencyInternalName) { if (this.currencyInternalName) {
let name = this.currencyInternalName; let name = this.currencyInternalName;
if (this.currencyLocation) { if (this.currencyLocation) {
@ -88,10 +87,8 @@ export function addLayer(layer, player = null) {
} else { } else {
return !(playerProxy[this.layer].points.lt(this.cost)) return !(playerProxy[this.layer].points.lt(this.cost))
} }
} });
} setDefault(layer.upgrades[id], 'pay', function() {
if (layer.upgrades[id].pay == undefined) {
layer.upgrades[id].pay = noCache(function() {
if (this.bought || !this.canAfford) { if (this.bought || !this.canAfford) {
return; return;
} }
@ -120,22 +117,15 @@ export function addLayer(layer, player = null) {
} }
playerProxy[this.layer].points = playerProxy[this.layer].points.sub(this.cost); playerProxy[this.layer].points = playerProxy[this.layer].points.sub(this.cost);
} }
}); }, false);
} else { setDefault(layer.upgrades[id], 'buy', function() {
layer.upgrades[id].pay.forceCached = false;
}
if (layer.upgrades[id].buy == undefined) {
layer.upgrades[id].buy = noCache(function() {
if (this.bought || !this.canAfford) { if (this.bought || !this.canAfford) {
return; return;
} }
this.pay(); this.pay();
playerProxy[this.layer].upgrades.push(this.id); playerProxy[this.layer].upgrades.push(this.id);
this.onPurchase?.(); this.onPurchase?.();
}); }, false);
} else {
layer.upgrades[id].buy.forceCached = false;
}
} }
} }
} }
@ -145,24 +135,16 @@ export function addLayer(layer, player = null) {
layer.achievements[id].earned = function() { layer.achievements[id].earned = function() {
return !layer.deactivated && playerProxy[layer.id].achievements.some(achievement => achievement == id); return !layer.deactivated && playerProxy[layer.id].achievements.some(achievement => achievement == id);
} }
if (layer.achievements[id].onComplete != undefined) { setDefault(layer.achievements[id], 'onComplete', null, false);
layer.achievements[id].onComplete.forceCached = false;
}
} }
} }
} }
if (layer.challenges) { if (layer.challenges) {
layer.activeChallenge = function() {
return Object.values(this.challenges).find(challenge => challenge.active);
}
for (let id in layer.challenges) { for (let id in layer.challenges) {
if (isPlainObject(layer.challenges[id])) { if (isPlainObject(layer.challenges[id])) {
if (layer.challenges[id].onComplete != undefined) {
layer.challenges[id].onComplete.forceCached = false;
}
if (layer.challenges[id].onEnter != undefined) {
layer.challenges[id].onEnter.forceCached = false;
}
if (layer.challenges[id].onExit != undefined) {
layer.challenges[id].onExit.forceCached = false;
}
layer.challenges[id].shown = function() { layer.challenges[id].shown = function() {
return this.unlocked !== false && (playerProxy.hideChallenges === false || !this.maxed); return this.unlocked !== false && (playerProxy.hideChallenges === false || !this.maxed);
} }
@ -175,38 +157,9 @@ export function addLayer(layer, player = null) {
layer.challenges[id].maxed = function() { layer.challenges[id].maxed = function() {
return !layer.deactivated && Decimal.gte(playerProxy[layer.id].challenges[id], this.completionLimit); return !layer.deactivated && Decimal.gte(playerProxy[layer.id].challenges[id], this.completionLimit);
} }
if (layer.challenges[id].mark == undefined) {
layer.challenges[id].mark = function() {
return this.maxed;
}
}
layer.challenges[id].active = function() { layer.challenges[id].active = function() {
return !layer.deactivated && playerProxy[layer.id].activeChallenge === id; return !layer.deactivated && playerProxy[layer.id].activeChallenge === id;
} }
if (layer.challenges[id].canComplete == undefined) {
layer.challenges[id].canComplete = function() {
if (this.active) {
return false;
}
if (this.currencyInternalName) {
let name = this.currencyInternalName;
if (this.currencyLocation) {
return !(this.currencyLocation[name].lt(this.goal));
} else if (this.currencyLayer) {
let lr = this.currencyLayer;
return !(playerProxy[lr][name].lt(this.goal));
} else {
return !(playerProxy[name].lt(this.goal));
}
} else {
return !(playerProxy.points.lt(this.goal));
}
}
}
if (layer.challenges[id].completionLimit == undefined) {
layer.challenges[id].completionLimit = new Decimal(1);
}
layer.challenges[id].toggle = noCache(function() { layer.challenges[id].toggle = noCache(function() {
let exiting = playerProxy[layer.id].activeChallenge === id; let exiting = playerProxy[layer.id].activeChallenge === id;
if (exiting) { if (exiting) {
@ -228,26 +181,40 @@ export function addLayer(layer, player = null) {
this.onEnter?.(); this.onEnter?.();
} }
}); });
if (layer.challenges[id].canStart == undefined) { setDefault(layer.challenges[id], 'onComplete', null, false);
layer.challenges[id].canStart = true; setDefault(layer.challenges[id], 'onEnter', null, false);
setDefault(layer.challenges[id], 'onExit', null, false);
setDefault(layer.challenges[id], 'canStart', true);
setDefault(layer.challenges[id], 'completionLimit', new Decimal(1));
setDefault(layer.challenges[id], 'mark', function() {
return Decimal.gt(this.completionLimit, 1) && this.maxed;
});
setDefault(layer.challenges[id], 'canComplete', function() {
if (this.active) {
return false;
} }
if (this.currencyInternalName) {
let name = this.currencyInternalName;
if (this.currencyLocation) {
return !(this.currencyLocation[name].lt(this.goal));
} else if (this.currencyLayer) {
let lr = this.currencyLayer;
return !(playerProxy[lr][name].lt(this.goal));
} else {
return !(playerProxy[name].lt(this.goal));
} }
} else {
return !(playerProxy.points.lt(this.goal));
}
});
} }
layer.activeChallenge = function() {
return Object.values(this.challenges).find(challenge => challenge.active);
} }
} }
if (layer.buyables) { if (layer.buyables) {
if (layer.buyables.reset == undefined) { setDefault(layer.buyables, 'respec', null, false);
layer.buyables.reset = noCache(function() { setDefault(layer.buyables, 'reset', function() {
playerProxy[this.layer].buyables = getStartingBuyables(layer); playerProxy[this.layer].buyables = getStartingBuyables(layer);
}); }, false);
} else {
layer.buyables.reset.forceCached = false;
}
if (layer.buyables.respec != undefined) {
layer.buyables.respec.forceCached = false;
}
for (let id in layer.buyables) { for (let id in layer.buyables) {
if (isPlainObject(layer.buyables[id])) { if (isPlainObject(layer.buyables[id])) {
layer.buyables[id].amount = function() { layer.buyables[id].amount = function() {
@ -260,27 +227,24 @@ export function addLayer(layer, player = null) {
return !layer.deactivated && this.unlocked !== false && this.canAfford !== false && return !layer.deactivated && this.unlocked !== false && this.canAfford !== false &&
Decimal.lt(playerProxy[layer.id].buyables[id], this.purchaseLimit); Decimal.lt(playerProxy[layer.id].buyables[id], this.purchaseLimit);
} }
if (layer.buyables[id].purchaseLimit == undefined) { setDefault(layer.buyables[id], 'purchaseLimit', new Decimal(Infinity));
layer.buyables[id].purchaseLimit = new Decimal(Infinity); setDefault(layer.buyables[id], 'sellOne', null, false);
} setDefault(layer.buyables[id], 'sellAll', null, false);
if (layer.buyables[id].cost != undefined && layer.buyables[id].buy == undefined) { if (layer.buyables[id].cost != undefined) {
layer.buyables[id].buy = noCache(function() { setDefault(layer.buyables[id], 'buy', function() {
playerProxy[this.layer].points = playerProxy[this.layer].points.sub(this.cost()); playerProxy[this.layer].points = playerProxy[this.layer].points.sub(this.cost());
this.amount = this.amount.add(1); this.amount = this.amount.add(1);
}); }, false);
} else {
layer.buyables[id].buy.forceCached = false;
}
if (layer.buyables[id].sellOne != undefined) {
layer.buyables[id].sellOne.forceCached = false;
}
if (layer.buyables[id].sellAll != undefined) {
layer.buyables[id].sellAll.forceCached = false;
} }
} }
} }
} }
if (layer.clickables) { if (layer.clickables) {
layer.clickables.layer = layer.id;
setDefault(layer.clickables, 'masterButtonClick', null, false);
if (layer.clickables.masterButtonDisplay != undefined) {
setDefault(layer.clickables, 'showMaster', true);
}
for (let id in layer.clickables) { for (let id in layer.clickables) {
if (isPlainObject(layer.clickables[id])) { if (isPlainObject(layer.clickables[id])) {
layer.clickables[id].state = function() { layer.clickables[id].state = function() {
@ -289,30 +253,21 @@ export function addLayer(layer, player = null) {
layer.clickables[id].stateSet = function(state) { layer.clickables[id].stateSet = function(state) {
playerProxy[layer.id].clickables[id] = state; playerProxy[layer.id].clickables[id] = state;
} }
if (layer.clickables[id].click != undefined) { setDefault(layer.clickables[id], 'click', null, false);
layer.clickables[id].click.forceCached = false; setDefault(layer.clickables[id], 'hold', null, false);
} }
if (layer.clickables[id].hold != undefined) {
layer.clickables[id].hold.forceCached = false;
}
}
}
layer.clickables.layer = layer.id;
if (layer.clickables.masterButtonClick != undefined) {
layer.clickables.masterButtonClick.forceCached = false;
}
if (layer.clickables.showMaster == undefined && layer.clickables.masterButtonDisplay != undefined) {
layer.clickables.showMaster = true;
} }
} }
if (layer.milestones) { if (layer.milestones) {
for (let id in layer.milestones) { for (let id in layer.milestones) {
if (isPlainObject(layer.milestones[id])) { if (isPlainObject(layer.milestones[id])) {
layer.milestones[id].earned = function() {
return !layer.deactivated && playerProxy[layer.id].milestones.some(milestone => milestone == id);
}
layer.milestones[id].shown = function() { layer.milestones[id].shown = function() {
if (!this.unlocked) { if (!this.unlocked) {
return false; return false;
} }
switch (playerProxy.msDisplay) { switch (playerProxy.msDisplay) {
default: default:
case "all": case "all":
@ -328,51 +283,13 @@ export function addLayer(layer, player = null) {
return false; return false;
} }
} }
layer.milestones[id].earned = function() {
return !layer.deactivated && playerProxy[layer.id].milestones.some(milestone => milestone == id);
}
} }
} }
} }
if (layer.grids) { if (layer.grids) {
for (let id in layer.grids) { for (let id in layer.grids) {
if (player[layer.id].grids[id] == undefined) {
player[layer.id].grids[id] = {};
}
if (isPlainObject(layer.grids[id])) { if (isPlainObject(layer.grids[id])) {
if (player[layer.id].grids[id] == undefined) { setDefault(player[layer.id].grids, id, {});
player[layer.id].grids[id] = {};
}
if (layer.grids[id].getUnlocked == undefined) {
layer.grids[id].getUnlocked = true;
} else {
layer.grids[id].getUnlocked.forceCached = false;
}
if (layer.grids[id].getCanClick == undefined) {
layer.grids[id].getCanClick = true;
} else {
layer.grids[id].getCanClick.forceCached = false;
}
if (layer.grids[id].getStartData == undefined) {
layer.grids[id].getStartData = "";
} else {
layer.grids[id].getStartData.forceCached = false;
}
if (layer.grids[id].getStyle != undefined) {
layer.grids[id].getStyle.forceCached = false;
}
if (layer.grids[id].click != undefined) {
layer.grids[id].click.forceCached = false;
}
if (layer.grids[id].hold != undefined) {
layer.grids[id].hold.forceCached = false;
}
if (layer.grids[id].getTitle != undefined) {
layer.grids[id].getTitle.forceCached = false;
}
if (layer.grids[id].getDisplay != undefined) {
layer.grids[id].getDisplay.forceCached = false;
}
layer.grids[id].getData = function(cell) { layer.grids[id].getData = function(cell) {
if (playerProxy[layer.id].grids[id][cell] != undefined) { if (playerProxy[layer.id].grids[id][cell] != undefined) {
return playerProxy[layer.id].grids[id][cell]; return playerProxy[layer.id].grids[id][cell];
@ -385,24 +302,19 @@ export function addLayer(layer, player = null) {
layer.grids[id].dataSet = function(cell, data) { layer.grids[id].dataSet = function(cell, data) {
playerProxy[layer.id].grids[id][cell] = data; playerProxy[layer.id].grids[id][cell] = data;
} }
setDefault(layer.grids[id], 'getUnlocked', true, false);
setDefault(layer.grids[id], 'getCanClick', true, false);
setDefault(layer.grids[id], 'getStartData', "", false);
setDefault(layer.grids[id], 'getStyle', null, false);
setDefault(layer.grids[id], 'click', null, false);
setDefault(layer.grids[id], 'hold', null, false);
setDefault(layer.grids[id], 'getTitle', null, false);
setDefault(layer.grids[id], 'getDisplay', null, false);
layer.grids[id] = createGridProxy(layer.grids[id], getters, `${layer.id}/grids-${id}-`); layer.grids[id] = createGridProxy(layer.grids[id], getters, `${layer.id}/grids-${id}-`);
} }
} }
} }
if (layer.subtabs) { if (layer.subtabs) {
if (player.subtabs[layer.id] == undefined) {
player.subtabs[layer.id] = {};
}
if (player.subtabs[layer.id].mainTabs == undefined) {
player.subtabs[layer.id].mainTabs = Object.keys(layer.subtabs)[0];
}
for (let id in layer.subtabs) {
if (isPlainObject(layer.subtabs[id])) {
layer.subtabs[id].active = function() {
return playerProxy.subtabs[this.layer].mainTabs === this.id;
}
}
}
layer.activeSubtab = function() { layer.activeSubtab = function() {
if (this.subtabs != undefined) { if (this.subtabs != undefined) {
if (this.subtabs[player.subtabs[layer.id].mainTabs] && if (this.subtabs[player.subtabs[layer.id].mainTabs] &&
@ -413,15 +325,29 @@ export function addLayer(layer, player = null) {
return Object.values(this.subtabs).find(subtab => subtab.unlocked !== false); return Object.values(this.subtabs).find(subtab => subtab.unlocked !== false);
} }
} }
setDefault(player.subtabs, layer.id, {});
setDefault(player.subtabs[layer.id], 'mainTabs', Object.keys(layer.subtabs)[0]);
for (let id in layer.subtabs) {
if (isPlainObject(layer.subtabs[id])) {
layer.subtabs[id].active = function() {
return playerProxy.subtabs[this.layer].mainTabs === this.id;
}
}
}
} }
if (layer.microtabs) { if (layer.microtabs) {
if (player.subtabs[layer.id] == undefined) { setDefault(player.subtabs, layer.id, {});
player.subtabs[layer.id] = {};
}
for (let family in layer.microtabs) { for (let family in layer.microtabs) {
if (player.subtabs[layer.id][family] == undefined) { layer.microtabs[family].activeMicrotab = function() {
player.subtabs[layer.id][family] = Object.keys(layer.microtabs[family])[0]; if (this[player.subtabs[this.layer]?.[family]] && this[player.subtabs[this.layer][family]].unlocked !== false) {
return this[player.subtabs[this.layer][family]];
} }
// Default to first unlocked tab
return this[Object.keys(this).find(microtab => microtab !== 'activeMicrotab' && this[microtab].unlocked !== false)];
}
setDefault(player.subtabs[layer.id], family, Object.keys(layer.microtabs[family])[0]);
layer.microtabs[family].layer = layer.id;
layer.microtabs[family].family = family;
for (let id in layer.microtabs[family]) { for (let id in layer.microtabs[family]) {
if (isPlainObject(layer.microtabs[family][id])) { if (isPlainObject(layer.microtabs[family][id])) {
layer.microtabs[family][id].layer = layer.id; layer.microtabs[family][id].layer = layer.id;
@ -432,28 +358,15 @@ export function addLayer(layer, player = null) {
} }
} }
} }
layer.microtabs[family].layer = layer.id;
layer.microtabs[family].family = family;
layer.microtabs[family].activeMicrotab = function() {
if (this[player.subtabs[this.layer]?.[family]] && this[player.subtabs[this.layer][family]].unlocked !== false) {
return this[player.subtabs[this.layer][family]];
}
// Default to first unlocked tab
return this[Object.keys(this).find(microtab => microtab !== 'activeMicrotab' && this[microtab].unlocked !== false)];
}
} }
} }
if (layer.hotkeys) { if (layer.hotkeys) {
for (let id in layer.hotkeys) { for (let id in layer.hotkeys) {
if (isPlainObject(layer.hotkeys[id])) { if (isPlainObject(layer.hotkeys[id])) {
if (layer.hotkeys[id].press) { setDefault(layer.hotkeys[id], 'press', null, false);
layer.hotkeys[id].press.forceCached = false; setDefault(layer.hotkeys[id], 'unlocked', function() {
}
if (layer.hotkeys[id].unlocked == undefined) {
layer.hotkeys[id].unlocked = function() {
return layer.unlocked; return layer.unlocked;
} });
}
} }
} }
} }
@ -530,3 +443,12 @@ function setupFeature(layer, features) {
} }
} }
} }
function setDefault(object, key, value, forceCached) {
if (object[key] == undefined && value != undefined) {
object[key] = value;
}
if (object[key] != undefined && isFunction(object[key]) && forceCached != undefined) {
object[key].forceCached = forceCached;
}
}