npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gw2efficiency/recipe-calculation

v3.2.1

Published

Calculate the cheapest tree traversal, price and used items of crafting recipes

Downloads

6

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