|
1 | 1 | import { fetchInventory } from './inventory.js'
|
2 | 2 |
|
| 3 | +const fishCaughtItemRE = /<br\/>(.*?)\s*$/ |
| 4 | + |
3 | 5 | const parseNetResults = (page, url) => {
|
4 | 6 | const results = {items: []}
|
5 | 7 | // Parse the ID out of the URL.
|
@@ -66,8 +68,36 @@ const visitSellAllUserFish = async (state, page, url) => {
|
66 | 68 | await fetchInventory(state)
|
67 | 69 | }
|
68 | 70 |
|
| 71 | +const parseFishCaught = (page, url) => { |
| 72 | + const results = {} |
| 73 | + // Parse the ID out of the URL. |
| 74 | + const parsedUrl = new URL(url) |
| 75 | + results.locID = parsedUrl.searchParams.get("id") |
| 76 | + // Parse the item out of the page. |
| 77 | + const match = page.match(fishCaughtItemRE) |
| 78 | + if (!match) { |
| 79 | + throw `Unable to find item in fishcaught: ${page}` |
| 80 | + } |
| 81 | + results.item = match[1] |
| 82 | + results.overflow = page.includes("filter: grayscale") |
| 83 | + return results |
| 84 | +} |
| 85 | + |
| 86 | +const visitFishCaught = async (state, page, url) => { |
| 87 | + const results = parseFishCaught(page, url) |
| 88 | + const loc = await state.locations.getByID("fishing", results.locID) |
| 89 | + if (!loc) { |
| 90 | + throw `Unknown fishing loc for results: ${results.locID}` |
| 91 | + } |
| 92 | + // Update inventory. |
| 93 | + state.player.inventory[results.item] = results.overflow ? state.player.maxInventory : ((state.player.inventory[results.item] || 0) + 1) |
| 94 | + await state.player.save(state.db) |
| 95 | + await state.log.fish({location: loc.name, item: results.item, overflow: results.overflow}) |
| 96 | +} |
| 97 | + |
69 | 98 | export const setupFishing = state => {
|
70 | 99 | state.addPageFilter("https://farmrpg.com/worker.php?go=castnet&id=*", visitNetResults)
|
71 | 100 | state.addPageFilter("https://farmrpg.com/fishing.php?*", visitFishing)
|
72 | 101 | state.addPageFilter("https://farmrpg.com/worker.php?go=sellalluserfish", visitSellAllUserFish)
|
| 102 | + state.addPageFilter("https://farmrpg.com/worker.php?go=fishcaught&*", visitFishCaught) |
73 | 103 | }
|
0 commit comments