Skip to content

Commit

Permalink
feat(online): improve cow offered message
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Aug 20, 2023
1 parent b821eb8 commit aced9ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/game-logic/reducers/updatePeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @typedef {import('../../index').farmhand.peerMetadata} farmhand.peerMetadata
*/
import { MAX_LATEST_PEER_MESSAGES } from '../../constants'
import { NEW_COW_OFFERED_FOR_TRADE } from '../../templates'

import { showNotification } from './showNotification'

Expand All @@ -17,9 +18,12 @@ export const updatePeer = (state, peerId, peerMetadata) => {

const previousPeerMetadata = peers[peerId]

const previousCowOfferedId = previousPeerMetadata?.cowOfferedForTrade?.id
const newCowOfferedId = peerMetadata.cowOfferedForTrade?.id

const isCowNewlyBeingOfferedForTrade =
previousPeerMetadata?.cowOfferedForTrade?.id !==
peerMetadata.cowOfferedForTrade?.id
newCowOfferedId &&
previousCowOfferedId !== peerMetadata.cowOfferedForTrade?.id

peers[peerId] = peerMetadata

Expand All @@ -28,8 +32,7 @@ export const updatePeer = (state, peerId, peerMetadata) => {
const { pendingPeerMessages = [] } = peerMetadata

if (isCowNewlyBeingOfferedForTrade) {
// FIXME: Improve this message
state = showNotification(state, `A new cow is being offered for trade!`)
state = showNotification(state, NEW_COW_OFFERED_FOR_TRADE`${peerMetadata}`)
}

return {
Expand Down
11 changes: 11 additions & 0 deletions src/templates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* @typedef {import("./index").farmhand.item} farmhand.item
* @typedef {import("./index").farmhand.crop} farmhand.crop
* @typedef {import("./index").farmhand.keg} keg
* @typedef {import("./index").farmhand.peerMetadata} farmhand.peerMetadata
*/

/**
Expand All @@ -13,6 +15,7 @@ import { itemsMap } from './data/maps'
import { moneyString } from './utils/moneyString'
import {
getCowDisplayName,
getPlayerName,
getRandomLevelUpRewardQuantity,
integerString,
} from './utils'
Expand Down Expand Up @@ -355,3 +358,11 @@ export const FERMENTED_CROP_NAME = (_, item) => `Fermented ${item.name}`
*/
export const KEG_SPOILED_MESSAGE = (_, keg) =>
`Oh no! Your ${FERMENTED_CROP_NAME`${itemsMap[keg.itemId]}`} has spoiled!`

/**
* @param {string} _
* @param {farmhand.peerMetadata} peerMetadata
* @returns {string}
*/
export const NEW_COW_OFFERED_FOR_TRADE = (_, peerMetadata) =>
`A new cow is being offered for trade by ${getPlayerName(peerMetadata.id)}!`
16 changes: 9 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,13 +989,15 @@ export const transformStateDataForImport = state => {
return sanitizedState
}

/**
* @param {string} playerId
* @returns {string}
*/
export const getPlayerName = memoize(playerId => {
return funAnimalName(playerId)
})
export const getPlayerName = memoize(
/**
* @param {string} playerId
* @returns {string}
*/
playerId => {
return funAnimalName(playerId)
}
)

/**
* @param {number} currentInventoryLimit
Expand Down

0 comments on commit aced9ae

Please sign in to comment.