|
| 1 | +import { fetchInventory } from "./inventory.js" |
| 2 | + |
1 | 3 | const parsePanelCrops = (page, url) => {
|
2 | 4 | const parser = new DOMParser()
|
3 | 5 | const dom = parser.parseFromString(page, "text/html")
|
@@ -39,6 +41,75 @@ const visitFarmStatus = async (state, page, url) => {
|
39 | 41 | await state.player.save(state.db)
|
40 | 42 | }
|
41 | 43 |
|
| 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 | + |
42 | 113 | const visitCoop = async (state, page, url) => {
|
43 | 114 | state.lastView = "coop"
|
44 | 115 | }
|
@@ -72,4 +143,6 @@ export const setupFarm = state => {
|
72 | 143 | state.addPageHandler("pen", visitPen)
|
73 | 144 | state.addPageHandler("hab", visitHab)
|
74 | 145 | state.addPageHandler("troutfarm", visitTroutFarm)
|
| 146 | + state.addBeforeWorkerHandler("harvestall", beforeHarvestAll) |
| 147 | + state.addWorkerHandler("harvestall", visitHarvestAll) |
75 | 148 | }
|
0 commit comments