src/craftingSteps.js
function craftingSteps (tree, steps = [], index = 0) {
  // Skip any tree parts where nothing needs to be crafted
  if (!tree.components || tree.craft === false) {
    return steps
  }

  // Go through the existing steps, and if we already have a step
  // with this id, just add up the quantities
  let stepIndex = steps.findIndex(step => step.id === tree.id)
  if (stepIndex !== -1) {
    steps[stepIndex].quantity += tree.usedQuantity
    steps[stepIndex].components = steps[stepIndex].components
      .map(component => {
        let treeComponent = tree.components.find(tC => tC.id === component.id)
        component.quantity += treeComponent.totalQuantity
        return component
      })
    index = stepIndex
  }

  // We don't have a step like this yet, push a new one at the given index
  if (stepIndex === -1) {
    steps.splice(index, 0, {
      id: tree.id,
      output: tree.output,
      quantity: tree.usedQuantity,
      components: tree.components.map(component => {
        return {id: component.id, quantity: component.totalQuantity}
      })
    })
  }

  // Go through the components and push them after the index of their parent
  tree.components.map(component => craftingSteps(component, steps, index + 1))
  return steps
}
function craftingSteps (tree, steps = [], index = 0) {
  // Skip any tree parts where nothing needs to be crafted
  if (!tree.components || tree.craft === false) {
    return steps
  }

  // Go through the existing steps, and if we already have a step
  // with this id, just add up the quantities
  let stepIndex = steps.findIndex(step => step.id === tree.id)
  if (stepIndex !== -1) {
    steps[stepIndex].quantity += tree.usedQuantity
    steps[stepIndex].components = steps[stepIndex].components
      .map(component => {
        let treeComponent = tree.components.find(tC => tC.id === component.id)
        component.quantity += treeComponent.totalQuantity
        return component
      })
    index = stepIndex
  }

  // We don't have a step like this yet, push a new one at the given index
  if (stepIndex === -1) {
    steps.splice(index, 0, {
      id: tree.id,
      output: tree.output,
      quantity: tree.usedQuantity,
      components: tree.components.map(component => {
        return {id: component.id, quantity: component.totalQuantity}
      })
    })
  }

  // Go through the components and push them after the index of their parent
  tree.components.map(component => craftingSteps(component, steps, index + 1))
  return steps
}
function craftingSteps ( tree , steps = [ ] , index = 0 ) { // Skip any tree parts where nothing needs to be crafted if ( ! tree . components || tree . craft === false ) { return steps } // Go through the existing steps, and if we already have a step // with this id, just add up the quantities let stepIndex = steps . findIndex ( step => step . id === tree . id ) if ( stepIndex !== - 1 ) { steps [ stepIndex ] . quantity += tree . usedQuantity steps [ stepIndex ] . components = steps [ stepIndex ] . components . map ( component => { let treeComponent = tree . components . find ( tC => tC . id === component . id ) component . quantity += treeComponent . totalQuantity return component } ) index = stepIndex } // We don't have a step like this yet, push a new one at the given index if ( stepIndex === - 1 ) { steps . splice ( index , 0 , { id : tree . id , output : tree . output , quantity : tree . usedQuantity , components : tree . components . map ( component => { return { id : component . id , quantity : component . totalQuantity } } ) } ) } // Go through the components and push them after the index of their parent tree . components . map ( component => craftingSteps ( component , steps , index + 1 ) ) return steps }
Generate an ordered list of crafting steps
Generate an ordered list of crafting steps