Skip to content

Commit

Permalink
Merge patch commit
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jun 23, 2023
2 parents bf81e73 + d1510c8 commit 0265594
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 82 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jeremyckahn/farmhand",
"version": "1.17.1",
"version": "1.17.2",
"publishConfig": {
"access": "public"
},
Expand Down
41 changes: 12 additions & 29 deletions src/components/Farmhand/Farmhand.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @typedef {import("../../index").farmhand.item} farmhand.item */
/** @typedef {import("../../index").farmhand.cow} farmhand.cow */
/** @typedef {import("../../index").farmhand.keg} farmhand.keg */
/**
* @typedef {import("../../index").farmhand.item} farmhand.item
* @typedef {import("../../index").farmhand.cow} farmhand.cow
* @typedef {import("../../index").farmhand.keg} farmhand.keg
* @typedef {import("../../index").farmhand.notification} notification
*/
import React, { Component } from 'react'
import window from 'global/window'
import { Redirect } from 'react-router-dom'
Expand Down Expand Up @@ -74,7 +77,6 @@ import {
COW_TRADE_TIMEOUT,
DEFAULT_ROOM,
INITIAL_STORAGE_LIMIT,
PURCHASEABLE_COW_PENS,
STAGE_TITLE_MAP,
STANDARD_LOAN_AMOUNT,
} from '../../constants'
Expand All @@ -84,7 +86,6 @@ import {
} from '../../common/constants'
import {
CONNECTED_TO_ROOM,
COW_PEN_PURCHASED,
LOAN_INCREASED,
POSITIONS_POSTED_NOTIFICATION,
RECIPE_LEARNED,
Expand Down Expand Up @@ -230,9 +231,9 @@ const applyPriceEvents = (valueAdjustments, priceCrashes, priceSurges) => {
* @property {number} loanBalance
* @property {number} loansTakenOut
* @property {number} money
* @property {farmhand.notification} latestNotification
* @property {Array.<farmhand.notification>} newDayNotifications
* @property {Array.<farmhand.notification>} notificationLog
* @property {notification} latestNotification
* @property {Array.<notification>} newDayNotifications
* @property {Array.<notification>} notificationLog
* @property {Object} peers Keys are (Trystero) peer ids, values are their respective metadata or null.
* @property {Object?} peerRoom See https://github.com/dmotz/trystero
* @property {Array.<farmhand.peerMessage>} pendingPeerMessages An array of
Expand Down Expand Up @@ -264,7 +265,7 @@ const applyPriceEvents = (valueAdjustments, priceCrashes, priceSurges) => {
* @property {boolean} showHomeScreen Option to show the Home Screen
* @property {boolean} showNotifications
* @property {farmhand.module:enums.stageFocusType} stageFocus
* @property {Array.<farmhand.notification>} todaysNotifications
* @property {Array.<notification>} todaysNotifications
* @property {number} todaysLosses Should always be a negative number.
* @property {Object} todaysPurchases Keys are item names, values are their
* respective quantities.
Expand Down Expand Up @@ -635,7 +636,7 @@ export default class Farmhand extends Component {
this.setState({ hasBooted: true })
}

componentDidUpdate(prevProps, prevState) {
componentDidUpdate(_prevProps, prevState) {
const {
hasBooted,
heartbeatTimeoutId,
Expand Down Expand Up @@ -766,7 +767,6 @@ export default class Farmhand extends Component {
}

;[
'showCowPenPurchasedNotifications',
'showInventoryFullNotifications',
'showRecipeLearnedNotifications',
].forEach(fn => this[fn](prevState))
Expand Down Expand Up @@ -813,9 +813,7 @@ export default class Farmhand extends Component {
const { ownerId } = peerPlayerCow

const [peerId] =
Object.entries(peers).find(
([peerId, peer]) => peer?.id === ownerId
) ?? []
Object.entries(peers).find(([, peer]) => peer?.id === ownerId) ?? []

if (!peerId) {
console.error(
Expand Down Expand Up @@ -1035,21 +1033,6 @@ export default class Farmhand extends Component {
}))
}

/*!
* @param {farmhand.state} prevState
*/
showCowPenPurchasedNotifications(prevState) {
const {
state: { purchasedCowPen },
} = this

if (purchasedCowPen !== prevState.purchasedCowPen) {
const { cows } = PURCHASEABLE_COW_PENS.get(purchasedCowPen)

this.showNotification(COW_PEN_PURCHASED`${cows}`)
}
}

/*!
* @param {farmhand.state} prevState
*/
Expand Down
11 changes: 10 additions & 1 deletion src/game-logic/reducers/purchaseCellar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { moneyTotal } from '../../utils'
import { PURCHASEABLE_CELLARS } from '../../constants'

import { CELLAR_PURCHASED } from '../../templates'

import { showNotification } from './showNotification'

/**
* @param {farmhand.state} state
* @param {number} cellarId
Expand All @@ -13,8 +17,13 @@ export const purchaseCellar = (state, cellarId) => {
return state
}

const { price, space } = PURCHASEABLE_CELLARS.get(cellarId)

state = showNotification(state, CELLAR_PURCHASED`${space}`, 'success')

return {
...state,
purchasedCellar: cellarId,
money: moneyTotal(money, -PURCHASEABLE_CELLARS.get(cellarId).price),
money: moneyTotal(money, -price),
}
}
22 changes: 19 additions & 3 deletions src/game-logic/reducers/purchaseCellar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import { purchaseCellar } from './purchaseCellar'

describe('purchaseCellar', () => {
test('updates purchasedCellar', () => {
const { purchasedCellar } = purchaseCellar({}, 1)
const { purchasedCellar } = purchaseCellar({ todaysNotifications: [] }, 1)
expect(purchasedCellar).toEqual(1)
})

test('prevents repurchasing options', () => {
const { purchasedCellar } = purchaseCellar({ purchasedCellar: 2 }, 1)
const { purchasedCellar } = purchaseCellar(
{ todaysNotifications: [], purchasedCellar: 2 },
1
)
expect(purchasedCellar).toEqual(2)
})

test('deducts money', () => {
const { money } = purchaseCellar({ money: 1_000_000 }, 1)
const { money } = purchaseCellar(
{ todaysNotifications: [], money: 1_000_000 },
1
)
expect(money).toEqual(1_000_000 - PURCHASEABLE_CELLARS.get(1).price)
})

test('shows notification of purchase', () => {
const { todaysNotifications } = purchaseCellar(
{ todaysNotifications: [] },
1
)
expect(todaysNotifications[0].message).toEqual(
'Purchased a cellar with capacity for 10 kegs! View your keg inventory by going to the "Cellar" page.'
)
})
})
17 changes: 14 additions & 3 deletions src/game-logic/reducers/purchaseCowPen.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/**
* @typedef {import("../../components/Farmhand/Farmhand").farmhand.state} state
*/
import { moneyTotal } from '../../utils'
import { PURCHASEABLE_COW_PENS } from '../../constants'
import { COW_PEN_PURCHASED } from '../../templates'

import { showNotification } from './showNotification'

/**
* @param {farmhand.state} state
* @param {state} state
* @param {number} cowPenId
* @returns {farmhand.state}
* @returns {state}
*/
export const purchaseCowPen = (state, cowPenId) => {
const { money, purchasedCowPen } = state
Expand All @@ -13,8 +19,13 @@ export const purchaseCowPen = (state, cowPenId) => {
return state
}

const { cows, price } = PURCHASEABLE_COW_PENS.get(cowPenId)

state = showNotification(state, COW_PEN_PURCHASED`${cows}`, 'success')

return {
...state,
purchasedCowPen: cowPenId,
money: moneyTotal(money, -PURCHASEABLE_COW_PENS.get(cowPenId).price),
money: moneyTotal(money, -price),
}
}
22 changes: 19 additions & 3 deletions src/game-logic/reducers/purchaseCowPen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import { purchaseCowPen } from './purchaseCowPen'

describe('purchaseCowPen', () => {
test('updates purchasedCowPen', () => {
const { purchasedCowPen } = purchaseCowPen({}, 1)
const { purchasedCowPen } = purchaseCowPen({ todaysNotifications: [] }, 1)
expect(purchasedCowPen).toEqual(1)
})

test('prevents repurchasing options', () => {
const { purchasedCowPen } = purchaseCowPen({ purchasedCowPen: 2 }, 1)
const { purchasedCowPen } = purchaseCowPen(
{ todaysNotifications: [], purchasedCowPen: 2 },
1
)
expect(purchasedCowPen).toEqual(2)
})

test('deducts money', () => {
const { money } = purchaseCowPen({ money: 1500 }, 1)
const { money } = purchaseCowPen(
{ todaysNotifications: [], money: 1500 },
1
)
expect(money).toEqual(1500 - PURCHASEABLE_COW_PENS.get(1).price)
})

test('shows notification of purchase', () => {
const { todaysNotifications } = purchaseCowPen(
{ todaysNotifications: [] },
1
)
expect(todaysNotifications[0].message).toEqual(
'Purchased a cow pen with capacity for 10 cows! You can visit your cow pen by going to the "Cows" page.'
)
})
})
11 changes: 8 additions & 3 deletions src/game-logic/reducers/showNotification.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* @typedef {import("../../components/Farmhand/Farmhand").farmhand.state} state
* @typedef {import("@material-ui/lab/Alert").Color} alertSeverity
*/

// TODO: Change showNotification to accept a configuration object instead of so
// many formal parameters.
/**
* @param {farmhand.state} state
* @param {state} state
* @param {string} message
* @param {string} [severity] Corresponds to the `severity` prop here:
* @param {alertSeverity} [severity] Corresponds to the `severity` prop here:
* https://material-ui.com/api/alert/
* @returns {farmhand.state}
* @returns {state}
* @see https://material-ui.com/api/alert/
*/
export const showNotification = (
Expand Down
38 changes: 1 addition & 37 deletions src/shell/notifications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { screen } from '@testing-library/react'
import { within } from '@testing-library/dom'
import userEvent from '@testing-library/user-event'

import { getItemByName, nextView } from '../test-utils/ui'
import { getItemByName } from '../test-utils/ui'
import { farmhandStub } from '../test-utils/stubs/farmhandStub'
import { saveDataStubFactory } from '../test-utils/stubs/saveDataStubFactory'

Expand All @@ -29,42 +29,6 @@ describe('notifications', () => {
expect(within(notification).getByText('Carrot Soup')).toBeInTheDocument()
})

test('notification is shown when cow pen is purchased', async () => {
const loadedState = saveDataStubFactory({
money: 1500,
})

await farmhandStub({
localforage: {
getItem: () => Promise.resolve(loadedState),
setItem: (_key, data) => Promise.resolve(data),
},
})

await nextView()
const upgradesTab = screen.getByText('Upgrades')
userEvent.click(upgradesTab)

const cowPenContainer = screen
.getByText('Buy cow pen')
.closest('.TierPurchase')

// Open the list of cow pen options
userEvent.click(within(cowPenContainer).getAllByRole('button')[1])

userEvent.click(screen.getByRole('option', { name: '$1,500: 10 cow pen' }))

// Make the purchase
userEvent.click(within(cowPenContainer).getAllByRole('button')[0])

const notification = await screen.findByRole('alert')
expect(
within(notification).getByText(
'Purchased a cow pen with capacity for 10 cows! You can visit your cow pen by going to the "Cows" page.'
)
).toBeInTheDocument()
})

test('multiple notifications are shown when multiple recipes are learned', async () => {
const loadedState = saveDataStubFactory({
inventory: [
Expand Down
9 changes: 9 additions & 0 deletions src/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ export const CROWS_DESTROYED = (_, numCropsDestroyed) =>
}!`

/**
* @param {string} _
* @param {number} cows
* @returns {string}
*/
export const COW_PEN_PURCHASED = (_, cows) =>
`Purchased a cow pen with capacity for ${cows} cows! You can visit your cow pen by going to the "Cows" page.`

/**
* @param {string} _
* @param {number} kegCapacity
* @returns {string}
*/
export const CELLAR_PURCHASED = (_, kegCapacity) =>
`Purchased a cellar with capacity for ${kegCapacity} kegs! View your keg inventory by going to the "Cellar" page.`

/**
* @param {Object.<string, number>} milksProduced
* @returns {string}
Expand Down

1 comment on commit 0265594

@vercel
Copy link

@vercel vercel bot commented on 0265594 Jun 23, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.