-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
200 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** @typedef {import("../../components/Farmhand/Farmhand").farmhand.state} state */ | ||
|
||
import { randomNumberService } from '../../common/services/randomNumber' | ||
import { KEG_SPOILED_MESSAGE } from '../../templates' | ||
import { getKegSpoilageRate } from '../../utils/getKegSpoilageRate' | ||
|
||
import { removeKegFromCellar } from './removeKegFromCellar' | ||
|
||
/** | ||
* @param {state} state | ||
* @returns state | ||
*/ | ||
export const processCellarSpoilage = state => { | ||
const { cellarInventory } = state | ||
|
||
const newCellarInventory = [...cellarInventory] | ||
|
||
for (let i = newCellarInventory.length - 1; i > -1; i--) { | ||
const keg = newCellarInventory[i] | ||
const spoilageRate = getKegSpoilageRate(keg) | ||
|
||
if (randomNumberService.isRandomNumberLessThan(spoilageRate)) { | ||
state = removeKegFromCellar(state, keg.id) | ||
state = { | ||
...state, | ||
newDayNotifications: [ | ||
...state.newDayNotifications, | ||
{ | ||
message: KEG_SPOILED_MESSAGE`${keg}`, | ||
severity: 'error', | ||
}, | ||
], | ||
} | ||
} | ||
} | ||
|
||
state = { ...state } | ||
|
||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { randomNumberService } from '../../common/services/randomNumber' | ||
import { KEG_SPOILED_MESSAGE } from '../../templates' | ||
import { getKegStub } from '../../test-utils/stubs/getKegStub' | ||
|
||
import { processCellarSpoilage } from './processCellarSpoilage' | ||
|
||
describe('processCellarSpoilage', () => { | ||
test('does not remove kegs that have not spoiled', () => { | ||
jest | ||
.spyOn(randomNumberService, 'isRandomNumberLessThan') | ||
.mockReturnValueOnce(false) | ||
|
||
const keg = getKegStub() | ||
const cellarInventory = [keg] | ||
const newDayNotifications = [] | ||
const expectedState = processCellarSpoilage({ | ||
cellarInventory, | ||
newDayNotifications, | ||
}) | ||
|
||
expect(expectedState.cellarInventory).toHaveLength(1) | ||
}) | ||
|
||
test('removes kegs that have spoiled', () => { | ||
jest | ||
.spyOn(randomNumberService, 'isRandomNumberLessThan') | ||
.mockReturnValueOnce(true) | ||
|
||
const keg = getKegStub() | ||
const cellarInventory = [keg] | ||
const newDayNotifications = [] | ||
const expectedState = processCellarSpoilage({ | ||
cellarInventory, | ||
newDayNotifications, | ||
}) | ||
|
||
expect(expectedState.cellarInventory).toHaveLength(0) | ||
}) | ||
|
||
test('shows notification for kegs that have spoiled', () => { | ||
jest | ||
.spyOn(randomNumberService, 'isRandomNumberLessThan') | ||
.mockReturnValueOnce(true) | ||
|
||
const keg = getKegStub() | ||
const cellarInventory = [keg] | ||
const newDayNotifications = [] | ||
const expectedState = processCellarSpoilage({ | ||
cellarInventory, | ||
newDayNotifications, | ||
}) | ||
|
||
expect(expectedState.newDayNotifications).toEqual([ | ||
{ | ||
message: KEG_SPOILED_MESSAGE`${keg}`, | ||
severity: 'error', | ||
}, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @typedef {import("../../index").farmhand.keg} keg */ | ||
|
||
import { v4 as uuid } from 'uuid' | ||
|
||
import { carrot } from '../../data/crops' | ||
|
||
/** | ||
* @param {Partial<keg>} overrides | ||
* @returns keg | ||
*/ | ||
export const getKegStub = overrides => { | ||
return { | ||
id: uuid(), | ||
itemId: carrot.id, | ||
daysUntilMature: carrot.daysToFerment, | ||
...overrides, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @typedef {import("../index").farmhand.keg} keg */ | ||
|
||
import { KEG_SPOILAGE_RATE_MULTIPLIER } from '../constants' | ||
|
||
/** | ||
* @param {keg} keg | ||
* @returns number | ||
*/ | ||
export const getKegSpoilageRate = keg => { | ||
if (keg.daysUntilMature > 0) { | ||
return 0 | ||
} | ||
|
||
const spoilageRate = | ||
Math.abs(keg.daysUntilMature) * KEG_SPOILAGE_RATE_MULTIPLIER | ||
|
||
return spoilageRate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getKegStub } from '../test-utils/stubs/getKegStub' | ||
|
||
import { getKegSpoilageRate } from './getKegSpoilageRate' | ||
|
||
describe('getKegSpoilageRate', () => { | ||
test('handles kegs that are not mature', () => { | ||
const keg = getKegStub() | ||
expect(getKegSpoilageRate(keg)).toEqual(0) | ||
}) | ||
|
||
test('handles kegs that are mature', () => { | ||
const keg = getKegStub({ daysUntilMature: 0 }) | ||
expect(getKegSpoilageRate(keg)).toEqual(0) | ||
}) | ||
|
||
test.each([ | ||
[-1, 0.001], | ||
[-10, 0.01], | ||
[-100, 0.1], | ||
])( | ||
'handles kegs that are beyond mature', | ||
(daysUntilMature, expectedSpoilageRate) => { | ||
const keg = getKegStub({ daysUntilMature }) | ||
expect(getKegSpoilageRate(keg)).toEqual(expectedSpoilageRate) | ||
} | ||
) | ||
}) |
bf81e73
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
farmhand – ./
farmhand-git-main-jeremyckahn.vercel.app
farmhand.vercel.app
farmhand.life
farmhand-jeremyckahn.vercel.app
www.farmhand.life