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 Aug 24, 2023
2 parents 3b58a14 + 81e1c52 commit 588744d
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 31 deletions.
15 changes: 9 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jeremyckahn/farmhand",
"version": "1.17.4",
"version": "1.17.5",
"publishConfig": {
"access": "public"
},
Expand Down Expand Up @@ -132,7 +132,7 @@
"shifty": "^3.0.1",
"source-map-explorer": "^2.3.1",
"stream": "npm:stream-browserify@^3.0.0",
"trystero": "^0.11.8",
"trystero": "^0.13.0",
"typeface-francois-one": "0.0.71",
"typeface-public-sans": "^1.1.4",
"url": "^0.11.0",
Expand Down
11 changes: 10 additions & 1 deletion src/components/Farmhand/Farmhand.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @typedef {import("../../index").farmhand.keg} farmhand.keg
* @typedef {import("../../index").farmhand.plotContent} farmhand.plotContent
* @typedef {import("../../index").farmhand.peerMessage} farmhand.peerMessage
* @typedef {import("../../index").farmhand.peerMetadata} farmhand.peerMetadata
* @typedef {import("../../index").farmhand.priceEvent} farmhand.priceEvent
* @typedef {import("../../index").farmhand.notification} farmhand.notification
* @typedef {import("../../enums").cowColors} farmhand.cowColors
Expand Down Expand Up @@ -247,7 +248,8 @@ const applyPriceEvents = (valueAdjustments, priceCrashes, priceSurges) => {
* @property {farmhand.notification?} latestNotification
* @property {Array.<farmhand.notification>} newDayNotifications
* @property {Array.<farmhand.notification>} notificationLog
* @property {Object} peers Keys are (Trystero) peer ids, values are their respective metadata or null.
* @property {Record<string, farmhand.peerMetadata?>} peers Keys are (Trystero)
* peer ids, values are their respective metadata or null.
* @property {Object?} peerRoom See https://github.com/dmotz/trystero
* @property {farmhand.peerMessage[]} pendingPeerMessages An array of messages
* to be sent to the Trystero peer room upon the next broadcast.
Expand Down Expand Up @@ -707,6 +709,7 @@ export default class Farmhand extends FarmhandReducers {
const [sendPeerMetadata, getPeerMetadata] = peerRoom.makeAction(
'peerMetadata'
)

getPeerMetadata((
/** @type {[object, string]} */
...args
Expand All @@ -715,18 +718,21 @@ export default class Farmhand extends FarmhandReducers {
const [sendCowTradeRequest, getCowTradeRequest] = peerRoom.makeAction(
'cowTrade'
)

getCowTradeRequest((
/** @type {[object, string]} */
...args
) => handleCowTradeRequest(this, ...args))

const [sendCowAccept, getCowAccept] = peerRoom.makeAction('cowAccept')

getCowAccept((
/** @type {[object, string]} */
...args
) => handleCowTradeRequestAccept(this, ...args))

const [sendCowReject, getCowReject] = peerRoom.makeAction('cowReject')

getCowReject((
/** @type {[object]} */
...args
Expand Down Expand Up @@ -892,12 +898,15 @@ export default class Farmhand extends FarmhandReducers {

this.scheduleHeartbeat()

const trackerRedundancy = 4

this.setState({
activePlayers,
peerRoom: joinRoom(
{
appId: process.env.REACT_APP_NAME,
trackerUrls,
trackerRedundancy,
rtcConfig,
},
room
Expand Down
11 changes: 10 additions & 1 deletion src/components/LogView/LogView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ export const LogView = ({ notificationLog, todaysNotifications }) => (
<ul>
{todaysNotifications.map(({ message, onClick, severity }) => (
<li {...{ key: message }}>
<Alert {...{ elevation: 3, onClick, severity }}>
<Alert
{...{
elevation: 3,
onClick,
severity,
style: {
cursor: onClick ? 'pointer' : 'default',
},
}}
>
<ReactMarkdown {...{ source: message }} />
</Alert>
</li>
Expand Down
3 changes: 3 additions & 0 deletions src/components/NotificationSystem/NotificationSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const snackbarProviderContentCallback = (
key,
onClick,
severity,
style: {
cursor: onClick ? 'pointer' : 'default',
},
}}
>
<ReactMarkdown {...{ source: message }} />
Expand Down
4 changes: 3 additions & 1 deletion src/game-logic/reducers/showNotification.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @typedef {import("../../components/Farmhand/Farmhand").farmhand.state} state
* @typedef {import("@material-ui/lab/Alert").Color} alertSeverity
* @typedef {import('@material-ui/lab/Alert').Color} alertSeverity
* @typedef {import('@material-ui/lab/Alert').AlertProps} AlertProps
*/

// TODO: Change showNotification to accept a configuration object instead of so
Expand All @@ -10,6 +11,7 @@
* @param {string} message
* @param {alertSeverity} [severity] Corresponds to the `severity` prop here:
* https://material-ui.com/api/alert/
* @param {AlertProps['onClick']} onClick
* @returns {state}
* @see https://material-ui.com/api/alert/
*/
Expand Down
37 changes: 33 additions & 4 deletions src/game-logic/reducers/updatePeer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
/**
* @typedef {import('../../components/Farmhand/Farmhand').default} Farmhand
* @typedef {import('../../components/Farmhand/Farmhand').farmhand.state} farmhand.state
* @typedef {import('../../index').farmhand.peerMetadata} farmhand.peerMetadata
*/
import { MAX_LATEST_PEER_MESSAGES } from '../../constants'
import { NEW_COW_OFFERED_FOR_TRADE } from '../../templates'
import { dialogView } from '../../enums'

import { showNotification } from './showNotification'

/**
* @param {farmhand.state} state
* @param {Farmhand} farmhand
* @param {farmhand.peerMetadata} peerMetadata
* @param {string} peerId The peer to update
* @param {Object} state
* @returns {farmhand.state}
*/
export const updatePeer = (state, peerId, peerState) => {
export const updatePeer = (state, farmhand, peerMetadata, peerId) => {
const peers = { ...state.peers }
peers[peerId] = peerState

const previousPeerMetadata = peers[peerId]

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

const isNewTrade = newCowOfferedId && previousCowOfferedId !== newCowOfferedId

peers[peerId] = peerMetadata

// Out of date peer clients may not provide pendingPeerMessages, so default
// it here.
const { pendingPeerMessages = [] } = peerState
const { pendingPeerMessages = [] } = peerMetadata

if (isNewTrade) {
state = showNotification(
state,
NEW_COW_OFFERED_FOR_TRADE`${peerMetadata}`,
'info',
() => {
farmhand.openDialogView(dialogView.ONLINE_PEERS)
}
)
}

return {
...state,
Expand Down
61 changes: 55 additions & 6 deletions src/game-logic/reducers/updatePeer.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
/**
* @typedef {import('../../index').farmhand.peerMetadata} farmhand.peerMetadata
*/
import Farmhand from '../../components/Farmhand'
import { MAX_LATEST_PEER_MESSAGES } from '../../constants'
import { NEW_COW_OFFERED_FOR_TRADE } from '../../templates'
import { getCowStub } from '../../test-utils/stubs/cowStub'
import { getPeerMetadataStub } from '../../test-utils/stubs/peerMetadataStub'

import { updatePeer } from './updatePeer'

const stubPeerMetadata = getPeerMetadataStub()

describe('updatePeer', () => {
test('updates peer data', () => {
const { latestPeerMessages, peers } = updatePeer(
{
latestPeerMessages: [],
peers: { abc123: { foo: true } },
peers: { abc123: stubPeerMetadata },
},
'abc123',
{ foo: false }
Farmhand,
{ foo: false },
'abc123'
)

expect(latestPeerMessages).toEqual([])
Expand All @@ -21,12 +31,51 @@ describe('updatePeer', () => {
const { latestPeerMessages } = updatePeer(
{
latestPeerMessages: new Array(50).fill('message'),
peers: { abc123: { foo: true } },
peers: { abc123: stubPeerMetadata },
},
'abc123',
{ foo: false }
Farmhand,
{ foo: false },
'abc123'
)

expect(latestPeerMessages).toHaveLength(MAX_LATEST_PEER_MESSAGES)
})

test('shows a notification when a new cow is offered', () => {
const { todaysNotifications } = updatePeer(
{
latestPeerMessages: [],
todaysNotifications: [],
peers: {
abc123: stubPeerMetadata,
},
},
Farmhand,
{ ...stubPeerMetadata, cowOfferedForTrade: getCowStub() },
'abc123'
)

expect(todaysNotifications[0]).toEqual(
expect.objectContaining({
message: NEW_COW_OFFERED_FOR_TRADE`${stubPeerMetadata}`,
})
)
})

test('does not show a notification when a cow is rescinded', () => {
const { todaysNotifications } = updatePeer(
{
latestPeerMessages: [],
todaysNotifications: [],
peers: {
abc123: { ...stubPeerMetadata, cowOfferedForTrade: getCowStub() },
},
},
Farmhand,
stubPeerMetadata,
'abc123'
)

expect(todaysNotifications).toEqual([])
})
})
8 changes: 5 additions & 3 deletions src/handlers/peer-events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** @typedef {import('../components/Farmhand/Farmhand').default} Farmhand */
/** @typedef {import('../index').farmhand.peerMetadata} farmhand.peerMetadata */
import { cowTradeRejectionReason } from '../enums'
import { COW_TRADED_NOTIFICATION } from '../templates'
import {
Expand All @@ -16,11 +18,11 @@ import {

/**
* @param {Farmhand} farmhand
* @param {Object} peerState
* @param {farmhand.peerMetadata} peerMetadata
* @param {string} peerId
*/
export const handlePeerMetadataRequest = (farmhand, peerState, peerId) => {
farmhand.updatePeer(peerId, peerState)
export const handlePeerMetadataRequest = (farmhand, peerMetadata, peerId) => {
farmhand.updatePeer(farmhand, peerMetadata, peerId)
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* @namespace farmhand
*/

/**
* @typedef {import("./components/Farmhand/Farmhand").farmhand.state} farmhand.state
*/

/**
* @typedef {import("./enums").cropType} cropType
* @typedef {import("./enums").cowColors} cowColors
Expand Down Expand Up @@ -194,6 +198,17 @@
* @property {string} message
*/

/**
* @typedef farmhand.offeredCow
* @type {farmhand.cow}
* @property {string} ownerId
*/

/**
* @typedef farmhand.peerMetadata
* @type {Pick<farmhand.state, 'cowsSold' | 'cropsHarvested' | 'dayCount' | 'id' | 'itemsSold' | 'money' | 'pendingPeerMessages' | 'version'> & { cowOfferedForTrade?: farmhand.offeredCow }}
*/

/**
* @typedef farmhand.upgradesMetadatum
* @type {Object}
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 {TemplateStringsArray} _
* @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)}!`
Loading

1 comment on commit 588744d

@vercel
Copy link

@vercel vercel bot commented on 588744d Aug 24, 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.