Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): [closes #430] Make findInField return the correct plot #431

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data/achievements.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { addItemToInventory } from '../game-logic/reducers/'
import {
doesPlotContainCrop,
dollarString,
findInField,
getCropLifeStage,
getProfitRecord,
integerString,
Expand All @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion src/game-logic/reducers/applyCrows.test.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
3 changes: 2 additions & 1 deletion src/game-logic/reducers/helpers.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
26 changes: 26 additions & 0 deletions src/utils/findInField.js
Original file line number Diff line number Diff line change
@@ -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,
}
)
33 changes: 33 additions & 0 deletions src/utils/findInField.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
21 changes: 1 addition & 20 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
Expand Down Expand Up @@ -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] =
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause of the bug is that this is always returning the first plot in the row.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh, wow good find. i didn't even notice this syntax when i was looking yesterday

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... that's what I get for being clever. 🤦

The new version is much less clever and more straightforward, I think!

field.find(row => {
return row.find(condition)
}) ?? []

return plot
},
{
serializer: memoizationSerializer,
}
)

export const doesMenuObstructStage = () => window.innerWidth < BREAKPOINTS.MD

const itemTypesToShowInReverse = new Set([itemType.MILK])
Expand Down