From f0faa0f48678a60a45e5dba60954106196a588c6 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Wed, 16 Aug 2023 12:29:48 -0500 Subject: [PATCH 1/3] fix(utils): [closes #430] Make `findInField` return the correct plot (#431) * fix(utils): make findInField return correct plot * refactor(utils): move findInField to its own file * test(utils): validate findInField * refactor(utils): simplify findInField --- src/data/achievements.js | 2 +- src/game-logic/reducers/applyCrows.test.js | 2 +- src/game-logic/reducers/helpers.js | 3 +- src/utils/findInField.js | 26 +++++++++++++++++ src/utils/findInField.test.js | 33 ++++++++++++++++++++++ src/utils/index.js | 21 +------------- 6 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 src/utils/findInField.js create mode 100644 src/utils/findInField.test.js diff --git a/src/data/achievements.js b/src/data/achievements.js index 1b19c7d88..1e4eb45a0 100644 --- a/src/data/achievements.js +++ b/src/data/achievements.js @@ -2,7 +2,6 @@ import { addItemToInventory } from '../game-logic/reducers/' import { doesPlotContainCrop, dollarString, - findInField, getCropLifeStage, getProfitRecord, integerString, @@ -11,6 +10,7 @@ import { percentageString, } from '../utils' import { memoize } from '../utils/memoize' +import { findInField } from '../utils/findInField' import { cropLifeStage, standardCowColors } from '../enums' import { COW_FEED_ITEM_ID, I_AM_RICH_BONUSES } from '../constants' diff --git a/src/game-logic/reducers/applyCrows.test.js b/src/game-logic/reducers/applyCrows.test.js index aef142e36..80a93f10f 100644 --- a/src/game-logic/reducers/applyCrows.test.js +++ b/src/game-logic/reducers/applyCrows.test.js @@ -1,5 +1,5 @@ -import { findInField } from '../../utils' import { MAX_CROWS, SCARECROW_ITEM_ID } from '../../constants' +import { findInField } from '../../utils/findInField' import { randomNumberService } from '../../common/services/randomNumber' diff --git a/src/game-logic/reducers/helpers.js b/src/game-logic/reducers/helpers.js index 47022f39f..845f6cafc 100644 --- a/src/game-logic/reducers/helpers.js +++ b/src/game-logic/reducers/helpers.js @@ -1,7 +1,8 @@ import { itemType } from '../../enums' import { SCARECROW_ITEM_ID } from '../../constants' -import { findInField, getPlotContentType } from '../../utils' +import { getPlotContentType } from '../../utils' +import { findInField } from '../../utils/findInField' // This file is designed to contain common logic that is needed across multiple // reducers. diff --git a/src/utils/findInField.js b/src/utils/findInField.js new file mode 100644 index 000000000..7cdeb6943 --- /dev/null +++ b/src/utils/findInField.js @@ -0,0 +1,26 @@ +/** @typedef {import("../index").farmhand.plotContent} farmhand.plotContent */ +import { memoize } from './memoize' + +import { memoizationSerializer } from './' + +export const findInField = memoize( + /** + * @param {(?farmhand.plotContent)[][]} field + * @param {function(?farmhand.plotContent): boolean} condition + * @returns {?farmhand.plotContent} + */ + (field, condition) => { + for (const row of field) { + for (const plot of row) { + if (condition(plot)) { + return plot + } + } + } + + return null + }, + { + serializer: memoizationSerializer, + } +) diff --git a/src/utils/findInField.test.js b/src/utils/findInField.test.js new file mode 100644 index 000000000..2bab29cdb --- /dev/null +++ b/src/utils/findInField.test.js @@ -0,0 +1,33 @@ +/** + * @typedef {import("../components/Farmhand/Farmhand").farmhand.state['field']} farmhand.state.field + */ + +import { carrot, pumpkin } from '../data/crops' + +import { findInField } from './findInField' + +const carrotPlot = { + itemId: carrot.id, + fertilizerType: 'NONE', +} + +describe('findInField', () => { + /** @type farmhand.state.field} */ + const field = [[null, carrotPlot, null]] + + test('returns the desired plot from the field', () => { + const foundPlot = findInField(field, plot => { + return plot?.itemId === carrot.id + }) + + expect(foundPlot).toEqual(carrotPlot) + }) + + test('yields null if desired plot is not in the field', () => { + const foundPlot = findInField(field, plot => { + return plot?.itemId === pumpkin.id + }) + + expect(foundPlot).toEqual(null) + }) +}) diff --git a/src/utils/index.js b/src/utils/index.js index 8fa17ebbb..ed7d3e570 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -138,7 +138,7 @@ export const chooseRandom = list => list[chooseRandomIndex(list)] * accept function arguments. * @param {any[]} args */ -const memoizationSerializer = args => +export const memoizationSerializer = args => JSON.stringify( [...args].map(arg => (typeof arg === 'function' ? arg.toString() : arg)) ) @@ -714,25 +714,6 @@ export const getPriceEventForCrop = cropItem => ({ getCropLifecycleDuration(cropItem) - PRICE_EVENT_STANDARD_DURATION_DECREASE, }) -export const findInField = memoize( - /** - * @param {(?farmhand.plotContent)[][]} field - * @param {function(?farmhand.plotContent): boolean} condition - * @returns {?farmhand.plotContent} - */ - (field, condition) => { - const [plot = null] = - field.find(row => { - return row.find(condition) - }) ?? [] - - return plot - }, - { - serializer: memoizationSerializer, - } -) - export const doesMenuObstructStage = () => window.innerWidth < BREAKPOINTS.MD const itemTypesToShowInReverse = new Set([itemType.MILK]) From 67853ecbcf6ca76e88f5799ebffcbdfe2ae582ee Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:30:44 -0500 Subject: [PATCH 2/3] chore(deps): bump protobufjs from 6.11.3 to 6.11.4 (#429) Bumps [protobufjs](https://github.com/protobufjs/protobuf.js) from 6.11.3 to 6.11.4. - [Release notes](https://github.com/protobufjs/protobuf.js/releases) - [Changelog](https://github.com/protobufjs/protobuf.js/blob/master/CHANGELOG.md) - [Commits](https://github.com/protobufjs/protobuf.js/commits) --- updated-dependencies: - dependency-name: protobufjs dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 47faf8f5a..2c7a71107 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3589,9 +3589,10 @@ } }, "node_modules/@grpc/grpc-js/node_modules/protobufjs": { - "version": "7.1.2", + "version": "7.2.4", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.2.4.tgz", + "integrity": "sha512-AT+RJgD2sH8phPmCf7OUZR8xGdcJRga4+1cOaXJ64hvcSkVhNcRHOwIxUatPH15+nj59WAGTDv3LSGZPEQbJaQ==", "hasInstallScript": true, - "license": "BSD-3-Clause", "dependencies": { "@protobufjs/aspromise": "^1.1.2", "@protobufjs/base64": "^1.1.2", @@ -27313,9 +27314,10 @@ } }, "node_modules/protobufjs": { - "version": "6.11.3", + "version": "6.11.4", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.4.tgz", + "integrity": "sha512-5kQWPaJHi1WoCpjTGszzQ32PG2F4+wRY6BmAT4Vfw56Q2FZ4YZzK20xUYQH4YkfehY1e6QSICrJquM6xXZNcrw==", "hasInstallScript": true, - "license": "BSD-3-Clause", "dependencies": { "@protobufjs/aspromise": "^1.1.2", "@protobufjs/base64": "^1.1.2", @@ -36443,7 +36445,9 @@ } }, "protobufjs": { - "version": "7.1.2", + "version": "7.2.4", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.2.4.tgz", + "integrity": "sha512-AT+RJgD2sH8phPmCf7OUZR8xGdcJRga4+1cOaXJ64hvcSkVhNcRHOwIxUatPH15+nj59WAGTDv3LSGZPEQbJaQ==", "requires": { "@protobufjs/aspromise": "^1.1.2", "@protobufjs/base64": "^1.1.2", @@ -52719,7 +52723,9 @@ } }, "protobufjs": { - "version": "6.11.3", + "version": "6.11.4", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.4.tgz", + "integrity": "sha512-5kQWPaJHi1WoCpjTGszzQ32PG2F4+wRY6BmAT4Vfw56Q2FZ4YZzK20xUYQH4YkfehY1e6QSICrJquM6xXZNcrw==", "requires": { "@protobufjs/aspromise": "^1.1.2", "@protobufjs/base64": "^1.1.2", From 7323dfd9a5c7e34ec2eeef3ce012a37f4a39faec Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 16 Aug 2023 17:43:08 +0000 Subject: [PATCH 3/3] 1.17.4 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c7a71107..5d9f66fdf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@jeremyckahn/farmhand", - "version": "1.17.3", + "version": "1.17.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@jeremyckahn/farmhand", - "version": "1.17.3", + "version": "1.17.4", "license": "GPL-2.0-or-later", "dependencies": { "@fortawesome/fontawesome-svg-core": "^1.2.27", diff --git a/package.json b/package.json index a2f237889..21c83e1f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jeremyckahn/farmhand", - "version": "1.17.3", + "version": "1.17.4", "publishConfig": { "access": "public" },