Skip to content

Commit

Permalink
add some types for forest and make it unlockable via experience
Browse files Browse the repository at this point in the history
  • Loading branch information
lstebner committed Jan 21, 2024
1 parent 37f60ea commit 4c39401
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 23 deletions.
11 changes: 9 additions & 2 deletions src/components/Farmhand/Farmhand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* @typedef {import("../../index").farmhand.item} farmhand.item
* @typedef {import("../../index").farmhand.cow} farmhand.cow
* @typedef {import("../../index").farmhand.cowBreedingPen} farmhand.cowBreedingPen
* @typedef {import("../../index").farmhand.forestForageable} farmhand.forestForageable
* @typedef {import("../../index").farmhand.keg} farmhand.keg
* @typedef {import("../../index").farmhand.plantedTree} farmhand.plantedTree
* @typedef {import("../../index").farmhand.plotContent} farmhand.plotContent
* @typedef {import("../../index").farmhand.peerMessage} farmhand.peerMessage
* @typedef {import("../../index").farmhand.peerMetadata} farmhand.peerMetadata
Expand Down Expand Up @@ -93,6 +95,7 @@ import {
STANDARD_LOAN_AMOUNT,
Z_INDEX,
STANDARD_VIEW_LIST,
UNLOCK_FOREST_LEVEL,
} from '../../constants'
import {
HEARTBEAT_INTERVAL_PERIOD,
Expand Down Expand Up @@ -215,7 +218,7 @@ const applyPriceEvents = (valueAdjustments, priceCrashes, priceSurges) => {
* @property {number} experience
* @property {string} farmName
* @property {(?farmhand.plotContent)[][]} field
* @property {(?farmhand.plotContent)[][]} forest
* @property {(farmhand.plantedTree | farmhand.forestForageable | null)[][]} forest
* @property {farmhand.fieldMode} fieldMode
* @property {Function?} getCowAccept https://github.com/dmotz/trystero#receiver
* @property {Function?} getCowReject https://github.com/dmotz/trystero#receiver
Expand Down Expand Up @@ -374,7 +377,7 @@ export default class Farmhand extends FarmhandReducers {
viewList.unshift(HOME)
}

if (this.state.purchasedForest && features.FOREST) {
if (this.isForestUnlocked && features.FOREST) {
viewList.push(FOREST)
}

Expand Down Expand Up @@ -416,6 +419,10 @@ export default class Farmhand extends FarmhandReducers {
return isOnline && room !== DEFAULT_ROOM
}

get isForestUnlocked() {
return levelAchieved(this.state.experience) >= UNLOCK_FOREST_LEVEL
}

/**
* @returns {farmhand.state}
*/
Expand Down
12 changes: 0 additions & 12 deletions src/components/Forest/Forest.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import React from 'react'

import FarmhandContext from '../Farmhand/Farmhand.context'

export const Forest = () => {
return <div>'welcome to da forest'</div>
}

export default function Consumer(props) {
return (
<FarmhandContext.Consumer>
{({ gameState, handlers }) => (
<Forest {...{ ...gameState, ...handlers, ...props }} />
)}
</FarmhandContext.Consumer>
)
}
2 changes: 1 addition & 1 deletion src/components/Forest/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './Forest'
export { Forest } from './Forest'
9 changes: 7 additions & 2 deletions src/components/Shop/Shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Typography from '@mui/material/Typography'
import FarmhandContext from '../Farmhand/Farmhand.context'
import { features } from '../../config'
import { moneyString } from '../../utils/moneyString'
import { levelAchieved } from '../../utils/levelAchieved'
import {
dollarString,
getCostOfNextStorageExpansion,
Expand All @@ -30,6 +31,7 @@ import {
PURCHASABLE_FOREST_SIZES,
PURCHASEABLE_SMELTERS,
STORAGE_EXPANSION_AMOUNT,
UNLOCK_FOREST_LEVEL,
} from '../../constants'
import Inventory from '../Inventory'
import TierPurchase from '../TierPurchase'
Expand All @@ -56,6 +58,7 @@ const categorizeShopInventory = memoize(shopInventory =>
)

export const Shop = ({
experience,
handleCombinePurchase,
handleComposterPurchase,
handleCowPenPurchase,
Expand All @@ -82,6 +85,8 @@ export const Shop = ({

const { seeds, fieldTools } = categorizeShopInventory(shopInventory)

const isForestUnlocked = levelAchieved(experience) >= UNLOCK_FOREST_LEVEL

return (
<div className="Shop">
<Tabs
Expand Down Expand Up @@ -200,7 +205,7 @@ export const Shop = ({
/>
</li>
) : null}
{features.FOREST ? (
{features.FOREST && isForestUnlocked ? (
<li>
<TierPurchase
{...{
Expand All @@ -211,7 +216,7 @@ export const Shop = ({
renderTierLabel: ({ columns, price, rows }) =>
`${dollarString(price)}: ${columns} x ${rows}`,
tiers: PURCHASABLE_FOREST_SIZES,
title: purchasedForest ? 'Expand Forest' : 'Purchase Forest',
title: 'Expand Forest',
}}
/>
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Stage/Stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { array, arrayOf, string } from 'prop-types'

import FarmhandContext from '../Farmhand/Farmhand.context'
import Field from '../Field'
import Forest from '../Forest'
import { Forest } from '../Forest'
import Home from '../Home'
import CowPen from '../CowPen'
import Shop from '../Shop'
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,5 @@ export const STANDARD_VIEW_LIST = [stageFocusType.SHOP, stageFocusType.FIELD]
export const Z_INDEX = {
END_DAY_BUTTON: 1100,
}

export const UNLOCK_FOREST_LEVEL = 15
7 changes: 6 additions & 1 deletion src/game-logic/reducers/processLevelUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
unlockTool,
} from '../../utils'
import { getLevelEntitlements } from '../../utils/getLevelEntitlements'
import { SPRINKLER_ITEM_ID } from '../../constants'
import { SPRINKLER_ITEM_ID, UNLOCK_FOREST_LEVEL } from '../../constants'
import { LEVEL_GAINED_NOTIFICATION } from '../../templates'
import { FOREST_AVAILABLE_NOTIFICATION } from '../../strings'

import { addItemToInventory } from './addItemToInventory'
import { showNotification } from './showNotification'
Expand Down Expand Up @@ -61,6 +62,10 @@ export const processLevelUp = (state, oldLevel) => {
LEVEL_GAINED_NOTIFICATION`${i}${randomCropSeed}`,
'success'
)

if (i === UNLOCK_FOREST_LEVEL) {
state = showNotification(state, FOREST_AVAILABLE_NOTIFICATION, 'success')
}
}

return state
Expand Down
15 changes: 11 additions & 4 deletions src/game-logic/reducers/purchaseForest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { FOREST_AVAILABLE_NOTIFICATION } from '../../strings'

import { purchaseForest } from './purchaseForest'

const tree = () => {
return {
daysOld: 0,
itemId: 'test-tree',
}
}

describe('purchaseForest', () => {
test('updates purchasedForest', () => {
const { purchasedForest } = purchaseForest({ purchasedForest: 0 }, 0)
Expand Down Expand Up @@ -60,15 +67,15 @@ describe('purchaseForest', () => {
{
todaysNotifications: [],
forest: [
[testCrop(), null],
[null, testCrop()],
[tree(), null],
[null, tree()],
],
},
1
)

expectedForest[0][0] = testCrop()
expectedForest[1][1] = testCrop()
expectedForest[0][0] = tree()
expectedForest[1][1] = tree()

expect(forest).toEqual(expectedForest)
})
Expand Down
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@
* @typedef {farmhand.plotContent & farmhand.cropType} farmhand.crop
*/

/**
* Represents a tree
* @typedef farmhand.plantedTree
* @property {number} daysOld
* @property {string} itemId
*/

/**
* Represents a forageable item that grows in the forest
* @typedef farmhand.forestForageable
* @property {number} daysOld
* @property {'mushroom' | 'acorn'} forageableId
*/

/**
* Represents a shoveled plot
* @typedef farmhand.shoveledPlot
Expand Down

0 comments on commit 4c39401

Please sign in to comment.