Skip to content

Commit

Permalink
docs(types): define remaining FarmhandContext props
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Apr 19, 2024
1 parent 418ab5c commit 2e83fa9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
16 changes: 15 additions & 1 deletion src/components/Farmhand/Farmhand.context.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
/**
* @typedef {import('../../').farmhand.item} farmhand.item
* @typedef {import('../../').farmhand.levelEntitlements} farmhand.levelEntitlements
* @typedef {import('./Farmhand').farmhand.state} farmhand.state
*/
import { createContext } from 'react'

/**
* @type {import('react').Context<{
* gameState: farmhand.state
* gameState: farmhand.state & {
* blockInput: boolean,
* features: Record<string, boolean>,
* fieldToolInventory: farmhand.item[],
* isChatAvailable: boolean,
* levelEntitlements: farmhand.levelEntitlements,
* plantableCropInventory: farmhand.item[],
* playerInventory: farmhand.item[],
* playerInventoryQuantities: Record<string, number>,
* shopInventory: farmhand.item[],
* viewList: string[],
* viewTitle: string,
* }
* handlers: {}
* }>}
*/
Expand Down
25 changes: 16 additions & 9 deletions src/components/Farmhand/Farmhand.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@ const { CLEANUP, HARVEST, MINE, OBSERVE, WATER, PLANT } = fieldMode
const emptyObject = Object.freeze({})

/*!
* @param {Array.<{ id: farmhand.item, quantity: number }>} inventory
* @param {Object.<number>} valueAdjustments
* @returns {Array.<farmhand.item>}
* @param {{ id: farmhand.item['id'], quantity: number }} inventory
* @param {Record<string, number>} valueAdjustments
* @returns {farmhand.item[]}
*/
export const computePlayerInventory = memoize((inventory, valueAdjustments) =>
export const computePlayerInventory = memoize((
/** @type {{ id: farmhand.item['id'], quantity: number }[]} */ inventory,
/** @type {Record<string, number>} */ valueAdjustments
) =>
inventory.map(({ quantity, id }) => ({
quantity,
...itemsMap[id],
Expand All @@ -148,10 +151,12 @@ export const computePlayerInventory = memoize((inventory, valueAdjustments) =>
)

/*!
* @param {Array.<{ id: farmhand.item }>} inventory
* @returns {Array.<{ id: farmhand.item }>}
* @param {farmhand.item[]} inventory
* @returns {{ id: farmhand.item }[]}
*/
export const getFieldToolInventory = memoize(inventory =>
export const getFieldToolInventory = memoize((
/** @type {Array.<farmhand.item>} */ inventory
) =>
inventory
.filter(({ id }) => {
const { enablesFieldMode } = itemsMap[id]
Expand All @@ -162,10 +167,12 @@ export const getFieldToolInventory = memoize(inventory =>
)

/*!
* @param {Array.<{ id: farmhand.item }>} inventory
* @param {farmhand.item[]} inventory
* @returns {Array.<{ id: farmhand.item }>}
*/
export const getPlantableCropInventory = memoize(inventory =>
export const getPlantableCropInventory = memoize((
/** @type {farmhand.item[]} */ inventory
) =>
inventory
.filter(({ id }) => itemsMap[id].isPlantableCrop)
.map(({ id }) => itemsMap[id])
Expand Down
2 changes: 1 addition & 1 deletion src/components/Farmhand/helpers/getInventoryQuantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const itemIds = Object.keys(itemsMap)

/**
* @param {Array.<{ id: farmhand.item, quantity: number }>} inventory
* @returns {Object.<string, number>}
* @returns {Record<string, number>}
*/
export const getInventoryQuantities = inventory => {
const quantities = {}
Expand Down
5 changes: 5 additions & 0 deletions src/data/shop-inventory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('../index').farmhand.item} farmhand.item
*/

import {
// Plantable crops
asparagusSeed,
Expand Down Expand Up @@ -27,6 +31,7 @@ import {

import { fertilizer } from './recipes'

/** @type {farmhand.item[]} */
const inventory = [
// Plantable crops
asparagusSeed,
Expand Down
7 changes: 5 additions & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/** @typedef {import("../index").farmhand.cowBreedingPen} farmhand.cowBreedingPen */
/** @typedef {import("../enums").cropLifeStage} farmhand.cropLifeStage */
/** @typedef {import("../components/Farmhand/Farmhand").farmhand.state} farmhand.state */
/** @typedef {import("../index").farmhand.levelEntitlements} farmhand.levelEntitlements */

/**
* @module farmhand.utils
Expand Down Expand Up @@ -824,10 +825,12 @@ export const experienceNeededForLevel = targetLevel =>
((targetLevel - 1) * 10) ** 2

/**
* @param {Object} levelEntitlements
* @param {farmhand.levelEntitlements} levelEntitlements
* @returns {Array.<{ item: farmhand.item }>}
*/
export const getAvailableShopInventory = memoize(levelEntitlements =>
export const getAvailableShopInventory = memoize((
/** @type {farmhand.levelEntitlements} */ levelEntitlements
) =>
shopInventory.filter(
({ id }) =>
!(
Expand Down

0 comments on commit 2e83fa9

Please sign in to comment.