@gw2efficiency/recipe-calculation
v3.5.6
Published
Calculate the cheapest tree traversal, price and used items of crafting recipes
Downloads
476
Readme
Installation
yarn add @gw2efficiency/recipe-calculation
The recipe trees this package consumes are generated via
@gw2efficiency/recipe-nesting
.
Usage
Calculate the cheapest tree
import {cheapestTree} from '@gw2efficiency/recipe-calculation'
// How many times do we want to craft this item
// Note: If you want to craft a item 5 times and the output of the
// recipe is 5, it will calculate 1 craft if you pass in amount = 5
const amount = 1
// A nested recipe tree, as generated from "@gw2efficiency/recipe-nesting"
const recipeTree = {
id: 13243,
quantity: 5,
output: 1,
components: [/* ... */]
}
// The item prices, as a map of item id -> price
const itemPrices = {1: 123, 2: 42, 3: 1337}
// (Optional) The available items, e.g. from the material storage, bank and characters,
// as a map of item id -> amount
const availableItems = {1: 1, 2: 250, 3: 5}
// (Optional) A list of item ids for which crafting is *disabled* when generating the
// cheapest tree (e.g. for excluding precursor crafting or daily cooldowns)
const craftingDisabled = [1337, 42]
// Calculate the tree
const calculatedTree = cheapestTree(amount, recipeTree, itemPrices, availableItems, craftingDisabled)
// The result looks like this:
{
id: 13243,
quantity: 5,
output: 1,
components: [/* ... */],
// (The following keys get set for the top level and all sub-components)
// The total quantity of this component
totalQuantity: 5,
// The total used quantity of this component. This is after
// subtracting the available items of the user. If this is 0
// then the user owns all items already.
usedQuantity: 5,
// The flag if the component should be crafted (true) or bought (false)
craft: true,
// Total buy price of the component
buyPrice: 50,
// Buy price for one of the components
buyPriceEach: 10,
// Total price to craft this component
craftPrice: 42
}
Update tree quantities & prices
If you want to update the tree, because the amount
, availableItems
or itemPrices
changed or
the user flipped a craft
flag, you should use this method. This updates the following keys:
totalQuantity
, usedQuantity
, buyPrice
, buyPriceEach
and craftPrice
This method does not change any craft
flags (= uses the pre-calculated best tree). If you want
to recalculate the cheapest tree, just use cheapestTree
again!
import { updateTree } from '@gw2efficiency/recipe-calculation'
// How many times do we want to craft this item
const amount = 1
// The already calculated tree (from "cheapestTree") that got changed
const calculatedTree = {
/* ... */
}
// The item prices, as a map of item id -> price
const itemPrices = { 1: 123, 2: 42, 3: 1337 }
// (Optional) The available items, e.g. from the material storage, bank and characters,
// as a map of item id -> amount
const availableItems = { 1: 1, 2: 250, 3: 5 }
// Update the tree
const updatedTree = updateTree(amount, calculatedTree, itemPrices, availableItems)
Generate list of items to buy & used available items
import {usedItems} from '@gw2efficiency/recipe-calculation'
// Get all item ids of a calculated recipe tree (after "cheapestTree")
const calculatedTree = {/* ... */}
const usedItemsMap = usedItems(calculatedTree)
// Generates a object with maps of item id -> amount
{
buy: {1: 5, 3: 10, /* ... */},
available: {1: 10, 2: 5, /* ... */}
}
Generate list of crafting steps
import {craftingSteps} from '@gw2efficiency/recipe-calculation'
// Get the crafting steps of a calculated recipe tree (after "cheapestTree")
const calculatedTree = {/* ... */}
const craftingStepsArray = craftingSteps(calculatedTree)
// Generates an array with the crafting steps in correct order
[
{
id: 1,
quantity: 10,
components: [
{id: 2, quantity: 20},
{id: 3, quantity: 10}
]
},
// ...
]
Static content
import {staticItems} from '@gw2efficiency/recipe-calculation'
// Get all item ids of items that can only be crafted once a day
const dailyCooldowns = staticItems.dailyCooldowns
// -> [1, 2, 3, 4]
// Get all item ids of items that can be bought, where the item or the immediate component
// (e.g. Deldrimor Steel Ingot-> Lump of Mithrillium) is a daily cooldown
const buyableDailyCooldowns = staticItems.buyableDailyCooldowns
// -> [1, 2, 3, 4]
// Get an object with item ids as keys of all vendor-buyable items
const vendorItems = staticItems.vendorItems
// Returns an object like this:
{
20798: {
type: 'spirit-shard', // can be gold, spirit shards, karma or dungeon currency
quantity: 1, // quantity the vendor sells
cost: 1, // copper the vendor sells the quantity for
npcs: [
{name: 'Miyani / Mystic Forge Attendant', position: 'Mystic Forge'}
]
},
// ...
}
Helpers
import { recipeItems, dailyCooldowns, useVendorPrices } from '@gw2efficiency/recipe-calculation'
// Get all item ids of a recipe tree (before or after "cheapestTree")
const recipeTree = {
/* ... */
}
const itemIds = recipeItems(recipeTree)
// -> [1, 2, 3, 4]
// Get a map of item id -> count of all needed daily cooldowns (after "cheapestTree")
const calculatedTree = {
/* ... */
}
const cooldownItemsMap = dailyCooldowns(calculatedTree)
// -> {46740: 3, 66913: 4}
// Overwrite and add all vendor prices to a price array
// To show the users more information afterwards use "staticItems.vendorItems"
const prices = useVendorPrices({ 1: 1233, 19750: 50000 })
// -> {1: 1233, 19750: 16, 19924: 48, /* ... */}
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
License
MIT