diff --git a/src/game-logic/reducers/updatePeer.js b/src/game-logic/reducers/updatePeer.js index 1aeaf44a7..d2229d967 100644 --- a/src/game-logic/reducers/updatePeer.js +++ b/src/game-logic/reducers/updatePeer.js @@ -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' @@ -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 @@ -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 { diff --git a/src/templates.js b/src/templates.js index 02fbefed1..fb2483152 100644 --- a/src/templates.js +++ b/src/templates.js @@ -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 */ /** @@ -13,6 +15,7 @@ import { itemsMap } from './data/maps' import { moneyString } from './utils/moneyString' import { getCowDisplayName, + getPlayerName, getRandomLevelUpRewardQuantity, integerString, } from './utils' @@ -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)}!` diff --git a/src/utils/index.js b/src/utils/index.js index ed7d3e570..966db1c07 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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