Skip to content

Commit 4fc69d5

Browse files
committed
✨ Revised harvest logging code from @TheSheepGoesBa.
Will capture all harvest drops so we can get the drop rate of the rare procs like gold crops and runestones.
1 parent e614911 commit 4fc69d5

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

lib/farm.js

+73
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fetchInventory } from "./inventory.js"
2+
13
const parsePanelCrops = (page, url) => {
24
const parser = new DOMParser()
35
const dom = parser.parseFromString(page, "text/html")
@@ -39,6 +41,75 @@ const visitFarmStatus = async (state, page, url) => {
3941
await state.player.save(state.db)
4042
}
4143

44+
const beforeHarvestAll = async (state, parsedUrl) => {
45+
if (state.player.settings.harvest_logging !== "1") {
46+
// Harvest logging disabled.
47+
state.preHarvestInventory = undefined
48+
return
49+
}
50+
const now = Date.now()
51+
if (!Object.values(state.player.cropTimes).some(t => t <= now)) {
52+
// No crops pending harvest.
53+
console.debug("Skipping pre-harvest data, no crops to harvest")
54+
state.preHarvestInventory = undefined
55+
return
56+
}
57+
// Update the inventory and store a copy so it can be diff'd after a harvest.
58+
await fetchInventory(state)
59+
// Copy the inventory because we mutate it in place sometimes.
60+
state.preHarvestInventory = {}
61+
for (const item in state.player.inventory) {
62+
state.preHarvestInventory[item] = state.player.inventory[item]
63+
}
64+
}
65+
66+
const visitHarvestAll = async (state, page, url) => {
67+
if (state.player.settings.harvest_logging !== "1") {
68+
// Harvest logging disabled.
69+
return
70+
}
71+
if (!state.preHarvestInventory) {
72+
// No inventory to diff against.
73+
return
74+
}
75+
// Get the new inventory. This should be first in the method so
76+
// we don't get any background stuff happening first, as much as possible.
77+
await fetchInventory(state)
78+
// Copy it out so we have a stable value for diff.
79+
const postHarvestInventory = {}
80+
for (const item in state.player.inventory) {
81+
postHarvestInventory[item] = state.player.inventory[item]
82+
}
83+
// Work out which crops were supposed to be harvested.
84+
const results = {crops: [], items: []}
85+
const now = Date.now()
86+
for (const plot in state.player.cropTimes) {
87+
if (state.player.cropTimes[plot] > now) {
88+
// Not ready.
89+
continue
90+
}
91+
const image = state.player.cropImages[plot]
92+
const candidateItems = (await state.items.getByImage(image)).filter(it => it.name.includes("Seed") || it.name.includes("Spore"))
93+
const seed = candidateItems.length === 1 ? candidateItems[0].name : null
94+
results.crops.push({plot, image, seed})
95+
}
96+
// Diff the two inventories to find what items changed.
97+
const allKeys = new Set([...Object.keys(state.preHarvestInventory), ...Object.keys(postHarvestInventory)])
98+
for (const item of allKeys) {
99+
if(state.preHarvestInventory[item] !== postHarvestInventory[item]) {
100+
results.items.push({
101+
item,
102+
quantity: (postHarvestInventory[item] || 0) - (state.preHarvestInventory[item] || 0),
103+
})
104+
}
105+
}
106+
// Log the results.
107+
console.log("harvestall", results)
108+
await state.log.harvestall(results)
109+
// Clear the pre-harvest data for next time.
110+
state.preHarvestInventory = undefined
111+
}
112+
42113
const visitCoop = async (state, page, url) => {
43114
state.lastView = "coop"
44115
}
@@ -72,4 +143,6 @@ export const setupFarm = state => {
72143
state.addPageHandler("pen", visitPen)
73144
state.addPageHandler("hab", visitHab)
74145
state.addPageHandler("troutfarm", visitTroutFarm)
146+
state.addBeforeWorkerHandler("harvestall", beforeHarvestAll)
147+
state.addWorkerHandler("harvestall", visitHarvestAll)
75148
}

lib/settings.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const renderAdvancedSettings = state => {
7272
return renderSection(state, "Advanced Settings - Alter At Your Own Risk", [
7373
renderRowCheckbox(state, "Enable Export", "show_export"),
7474
renderRowCheckbox(state, "XP Logging", "xp_logging"),
75+
renderRowCheckbox(state, "Harvest Logging", "harvest_logging"),
7576
])
7677
}
7778

0 commit comments

Comments
 (0)