cleaned up setupFeatures

This commit is contained in:
thepaperpilot 2021-08-18 00:18:23 -05:00
parent 6e1536930e
commit c8651adb6b
3 changed files with 43 additions and 70 deletions

View file

@ -44,7 +44,7 @@ export default defineComponent({
}; };
}, },
props: { props: {
nodes: Object as PropType<Record<string, Array<string | number>>>, nodes: Object as PropType<Record<string, Array<string>>>,
append: Boolean append: Boolean
}, },
inject: ["tab"], inject: ["tab"],
@ -55,12 +55,11 @@ export default defineComponent({
} }
return layers[this.modal].name || this.modal; return layers[this.modal].name || this.modal;
}, },
rows(): Record<string | number, Array<string | number>> { rows(): Record<string, Array<string>> {
if (this.nodes != undefined) { if (this.nodes != undefined) {
return this.nodes; return this.nodes;
} }
const rows = Object.keys(layers).reduce( const rows = Object.keys(layers).reduce((acc: Record<string, Array<string>>, curr) => {
(acc: Record<string | number, Array<string | number>>, curr) => {
if (!(layers[curr].displayRow in acc)) { if (!(layers[curr].displayRow in acc)) {
acc[layers[curr].displayRow] = []; acc[layers[curr].displayRow] = [];
} }
@ -70,16 +69,11 @@ export default defineComponent({
acc[layers[curr].displayRow].push(curr); acc[layers[curr].displayRow].push(curr);
} }
return acc; return acc;
}, }, {});
{} return Object.keys(rows).reduce((acc: Record<string, Array<string>>, curr) => {
);
return Object.keys(rows).reduce(
(acc: Record<string | number, Array<string | number>>, curr) => {
acc[curr] = rows[curr].filter(layer => layer); acc[curr] = rows[curr].filter(layer => layer);
return acc; return acc;
}, }, {});
{}
);
} }
}, },
methods: { methods: {

View file

@ -58,7 +58,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
layer = clone(layer); layer = clone(layer);
setDefault(player, "layers", {}); setDefault(player, "layers", {});
player.layers![layer.id] = applyPlayerData( player.layers[layer.id] = applyPlayerData(
{ {
points: new Decimal(0), points: new Decimal(0),
unlocked: false, unlocked: false,
@ -74,7 +74,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
confirmRespecBuyables: false, confirmRespecBuyables: false,
...(layer.startData?.() || {}) ...(layer.startData?.() || {})
}, },
player.layers![layer.id] player.layers[layer.id]
); );
// Set default property values // Set default property values
@ -92,10 +92,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
} }
if (layer.upgrades) { if (layer.upgrades) {
setupFeatures<NonNullable<RawLayer["upgrades"]>, NonNullable<Layer["upgrades"]>, Upgrade>( setupFeatures<NonNullable<RawLayer["upgrades"]>, Upgrade>(layer.id, layer.upgrades);
layer.id,
layer.upgrades
);
setRowCol(layer.upgrades); setRowCol(layer.upgrades);
for (const id in layer.upgrades.data) { for (const id in layer.upgrades.data) {
layer.upgrades.data[id].bought = function() { layer.upgrades.data[id].bought = function() {
@ -188,11 +185,10 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
} }
if (layer.achievements) { if (layer.achievements) {
setupFeatures< setupFeatures<NonNullable<RawLayer["achievements"]>, Achievement>(
NonNullable<RawLayer["achievements"]>, layer.id,
NonNullable<Layer["achievements"]>, layer.achievements
Achievement );
>(layer.id, layer.achievements);
setRowCol(layer.achievements); setRowCol(layer.achievements);
for (const id in layer.achievements.data) { for (const id in layer.achievements.data) {
layer.achievements.data[id].earned = function() { layer.achievements.data[id].earned = function() {
@ -207,11 +203,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
} }
if (layer.challenges) { if (layer.challenges) {
setupFeatures< setupFeatures<NonNullable<RawLayer["challenges"]>, Challenge>(layer.id, layer.challenges);
NonNullable<RawLayer["challenges"]>,
NonNullable<Layer["challenges"]>,
Challenge
>(layer.id, layer.challenges);
setRowCol(layer.challenges); setRowCol(layer.challenges);
layer.activeChallenge = function() { layer.activeChallenge = function() {
return Object.values(this.challenges!.data).find( return Object.values(this.challenges!.data).find(
@ -300,10 +292,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
} }
if (layer.buyables) { if (layer.buyables) {
setupFeatures<NonNullable<RawLayer["buyables"]>, NonNullable<Layer["buyables"]>, Buyable>( setupFeatures<NonNullable<RawLayer["buyables"]>, Buyable>(layer.id, layer.buyables);
layer.id,
layer.buyables
);
setRowCol(layer.buyables); setRowCol(layer.buyables);
setDefault(layer.buyables, "respec", undefined, false); setDefault(layer.buyables, "respec", undefined, false);
setDefault( setDefault(
@ -350,11 +339,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
} }
if (layer.clickables) { if (layer.clickables) {
setupFeatures< setupFeatures<NonNullable<RawLayer["clickables"]>, Clickable>(layer.id, layer.clickables);
NonNullable<RawLayer["clickables"]>,
NonNullable<Layer["clickables"]>,
Clickable
>(layer.id, layer.clickables);
setRowCol(layer.clickables); setRowCol(layer.clickables);
setDefault(layer.clickables, "masterButtonClick", undefined, false); setDefault(layer.clickables, "masterButtonClick", undefined, false);
if (layer.clickables.masterButtonDisplay != undefined) { if (layer.clickables.masterButtonDisplay != undefined) {
@ -373,11 +358,7 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
} }
if (layer.milestones) { if (layer.milestones) {
setupFeatures< setupFeatures<NonNullable<RawLayer["milestones"]>, Milestone>(layer.id, layer.milestones);
NonNullable<RawLayer["milestones"]>,
NonNullable<Layer["milestones"]>,
Milestone
>(layer.id, layer.milestones);
for (const id in layer.milestones.data) { for (const id in layer.milestones.data) {
layer.milestones.data[id].earned = function() { layer.milestones.data[id].earned = function() {
return ( return (
@ -415,12 +396,9 @@ export function addLayer(layer: RawLayer, player?: Partial<PlayerData>): void {
} }
} }
if (layer.grids) { if (layer.grids) {
setupFeatures<NonNullable<RawLayer["grids"]>, NonNullable<Layer["grids"]>, Grid>( setupFeatures<NonNullable<RawLayer["grids"]>, Grid>(layer.id, layer.grids);
layer.id,
layer.grids
);
for (const id in layer.grids.data) { for (const id in layer.grids.data) {
setDefault(player.layers![layer.id].grids, id, {}); setDefault(player.layers[layer.id].grids, id, {});
layer.grids.data[id].getData = function(cell): State { layer.grids.data[id].getData = function(cell): State {
if (playerProxy.layers[this.layer].grids[id][cell] != undefined) { if (playerProxy.layers[this.layer].grids[id][cell] != undefined) {
return playerProxy.layers[this.layer].grids[id][cell]; return playerProxy.layers[this.layer].grids[id][cell];
@ -578,17 +556,18 @@ function setRowCol<T extends GridFeatures<S>, S extends Feature>(features: RawGr
features.cols = maxCol; features.cols = maxCol;
} }
function setupFeatures<T extends RawFeatures<R, S>, R extends Features<S>, S extends Feature>( function setupFeatures<
layer: string, T extends RawFeatures<R, S, unknown>,
features: T S extends Feature,
) { R extends Features<S> = Features<S>
>(layer: string, features: T) {
features.layer = layer; features.layer = layer;
for (const id in features.data) { for (const id in features.data) {
const feature = features.data[id]; const feature = features.data[id];
(feature as Feature).id = id; (feature as S).id = id;
(feature as Feature).layer = layer; (feature as S).layer = layer;
if (feature.unlocked == undefined) { if ((feature as S).unlocked == undefined) {
(feature as Feature).unlocked = true; (feature as S).unlocked = true;
} }
} }
} }
@ -599,7 +578,7 @@ function setDefault<T, K extends keyof T>(
value: T[K], value: T[K],
forceCached?: boolean forceCached?: boolean
): asserts object is Exclude<T, K> & Required<Pick<T, K>> { ): asserts object is Exclude<T, K> & Required<Pick<T, K>> {
if (object[key] == undefined && value != undefined) { if (object[key] === undefined && value != undefined) {
object[key] = value; object[key] = value;
} }
if (object[key] != undefined && isFunction(object[key]) && forceCached != undefined) { if (object[key] != undefined && isFunction(object[key]) && forceCached != undefined) {

View file

@ -9,16 +9,16 @@ export interface Feature {
[key: string]: unknown; [key: string]: unknown;
} }
export interface RawFeatures<T extends Features<S>, S extends Feature> export interface RawFeatures<T extends Features<S>, S extends Feature, R = RawFeature<S>>
extends Partial<Omit<Computable<T>, "data">>, extends Partial<Omit<Computable<T>, "data">>,
ThisType<T> { ThisType<T> {
layer?: string; layer?: string;
data: Record<string | number, RawFeature<S>>; data: Record<string, R>;
} }
export interface Features<T extends Feature> { export interface Features<T extends Feature> {
layer: string; layer: string;
data: Record<string | number, T>; data: Record<string, T>;
[key: string]: unknown; [key: string]: unknown;
} }