From fbd01497e7edc2c581bb599c835dede9a206b43d Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Wed, 18 Aug 2021 00:18:23 -0500 Subject: [PATCH] cleaned up setupFeatures --- src/components/tree/Tree.vue | 40 ++++++++---------- src/game/layers.ts | 67 +++++++++++-------------------- src/typings/features/feature.d.ts | 6 +-- 3 files changed, 43 insertions(+), 70 deletions(-) diff --git a/src/components/tree/Tree.vue b/src/components/tree/Tree.vue index 3a522ef..770135f 100644 --- a/src/components/tree/Tree.vue +++ b/src/components/tree/Tree.vue @@ -44,7 +44,7 @@ export default defineComponent({ }; }, props: { - nodes: Object as PropType>>, + nodes: Object as PropType>>, append: Boolean }, inject: ["tab"], @@ -55,31 +55,25 @@ export default defineComponent({ } return layers[this.modal].name || this.modal; }, - rows(): Record> { + rows(): Record> { if (this.nodes != undefined) { return this.nodes; } - const rows = Object.keys(layers).reduce( - (acc: Record>, curr) => { - if (!(layers[curr].displayRow in acc)) { - acc[layers[curr].displayRow] = []; - } - if (layers[curr].position != undefined) { - acc[layers[curr].displayRow][layers[curr].position!] = curr; - } else { - acc[layers[curr].displayRow].push(curr); - } - return acc; - }, - {} - ); - return Object.keys(rows).reduce( - (acc: Record>, curr) => { - acc[curr] = rows[curr].filter(layer => layer); - return acc; - }, - {} - ); + const rows = Object.keys(layers).reduce((acc: Record>, curr) => { + if (!(layers[curr].displayRow in acc)) { + acc[layers[curr].displayRow] = []; + } + if (layers[curr].position != undefined) { + acc[layers[curr].displayRow][layers[curr].position!] = curr; + } else { + acc[layers[curr].displayRow].push(curr); + } + return acc; + }, {}); + return Object.keys(rows).reduce((acc: Record>, curr) => { + acc[curr] = rows[curr].filter(layer => layer); + return acc; + }, {}); } }, methods: { diff --git a/src/game/layers.ts b/src/game/layers.ts index ccf4292..41c688e 100644 --- a/src/game/layers.ts +++ b/src/game/layers.ts @@ -58,7 +58,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { layer = clone(layer); setDefault(player, "layers", {}); - player.layers![layer.id] = applyPlayerData( + player.layers[layer.id] = applyPlayerData( { points: new Decimal(0), unlocked: false, @@ -74,7 +74,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { confirmRespecBuyables: false, ...(layer.startData?.() || {}) }, - player.layers![layer.id] + player.layers[layer.id] ); // Set default property values @@ -92,10 +92,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } } if (layer.upgrades) { - setupFeatures, NonNullable, Upgrade>( - layer.id, - layer.upgrades - ); + setupFeatures, Upgrade>(layer.id, layer.upgrades); setRowCol(layer.upgrades); for (const id in layer.upgrades.data) { layer.upgrades.data[id].bought = function() { @@ -188,11 +185,10 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } } if (layer.achievements) { - setupFeatures< - NonNullable, - NonNullable, - Achievement - >(layer.id, layer.achievements); + setupFeatures, Achievement>( + layer.id, + layer.achievements + ); setRowCol(layer.achievements); for (const id in layer.achievements.data) { layer.achievements.data[id].earned = function() { @@ -207,11 +203,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } } if (layer.challenges) { - setupFeatures< - NonNullable, - NonNullable, - Challenge - >(layer.id, layer.challenges); + setupFeatures, Challenge>(layer.id, layer.challenges); setRowCol(layer.challenges); layer.activeChallenge = function() { return Object.values(this.challenges!.data).find( @@ -300,10 +292,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } } if (layer.buyables) { - setupFeatures, NonNullable, Buyable>( - layer.id, - layer.buyables - ); + setupFeatures, Buyable>(layer.id, layer.buyables); setRowCol(layer.buyables); setDefault(layer.buyables, "respec", undefined, false); setDefault( @@ -350,11 +339,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } } if (layer.clickables) { - setupFeatures< - NonNullable, - NonNullable, - Clickable - >(layer.id, layer.clickables); + setupFeatures, Clickable>(layer.id, layer.clickables); setRowCol(layer.clickables); setDefault(layer.clickables, "masterButtonClick", undefined, false); if (layer.clickables.masterButtonDisplay != undefined) { @@ -373,11 +358,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } } if (layer.milestones) { - setupFeatures< - NonNullable, - NonNullable, - Milestone - >(layer.id, layer.milestones); + setupFeatures, Milestone>(layer.id, layer.milestones); for (const id in layer.milestones.data) { layer.milestones.data[id].earned = function() { return ( @@ -415,12 +396,9 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } } if (layer.grids) { - setupFeatures, NonNullable, Grid>( - layer.id, - layer.grids - ); + setupFeatures, Grid>(layer.id, layer.grids); 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 { if (playerProxy.layers[this.layer].grids[id][cell] != undefined) { return playerProxy.layers[this.layer].grids[id][cell]; @@ -578,17 +556,18 @@ function setRowCol, S extends Feature>(features: RawGr features.cols = maxCol; } -function setupFeatures, R extends Features, S extends Feature>( - layer: string, - features: T -) { +function setupFeatures< + T extends RawFeatures, + S extends Feature, + R extends Features = Features +>(layer: string, features: T) { features.layer = layer; for (const id in features.data) { const feature = features.data[id]; - (feature as Feature).id = id; - (feature as Feature).layer = layer; - if (feature.unlocked == undefined) { - (feature as Feature).unlocked = true; + (feature as S).id = id; + (feature as S).layer = layer; + if ((feature as S).unlocked == undefined) { + (feature as S).unlocked = true; } } } @@ -599,7 +578,7 @@ function setDefault( value: T[K], forceCached?: boolean ): asserts object is Exclude & Required> { - if (object[key] == undefined && value != undefined) { + if (object[key] === undefined && value != undefined) { object[key] = value; } if (object[key] != undefined && isFunction(object[key]) && forceCached != undefined) { diff --git a/src/typings/features/feature.d.ts b/src/typings/features/feature.d.ts index 4fa74ea..d1eb6a8 100644 --- a/src/typings/features/feature.d.ts +++ b/src/typings/features/feature.d.ts @@ -9,16 +9,16 @@ export interface Feature { [key: string]: unknown; } -export interface RawFeatures, S extends Feature> +export interface RawFeatures, S extends Feature, R = RawFeature> extends Partial, "data">>, ThisType { layer?: string; - data: Record>; + data: Record; } export interface Features { layer: string; - data: Record; + data: Record; [key: string]: unknown; }