Add new CraftingPlan class

This commit is contained in:
Andrew Miner
2015-09-22 19:54:31 -07:00
parent 6ea97ac3aa
commit de04c79d17
9 changed files with 187 additions and 109 deletions
+49
View File
@@ -0,0 +1,49 @@
###
Crafting Guide - crafting_plan.test.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
CraftingPlan = require '../../src/coffee/models/crafting/crafting_plan'
fixtures = require './fixtures'
########################################################################################################################
describe 'crafting_plan.coffee', ->
it 'requires wanted item if gatherable', ->
plans = fixtures.makePlans [1, 'test__coal']
plans.length.should.equal 1
plan = plans[0]
plan.required.unparse().should.equal 'coal'
plan.produced.unparse().should.equal 'coal'
it 'can compute a single item with a single step', ->
plans = fixtures.makePlans [1, 'test__charcoal']
plans.length.should.equal 1
plan = plans[0]
plan.required.unparse().should.equal 'coal:8.oak_wood'
plan.produced.unparse().should.equal '8.charcoal'
it 'can compute a large quantity of a single item with a single step', ->
plans = fixtures.makePlans [15, 'test__charcoal']
plans.length.should.equal 1
plan = plans[0]
plan.required.unparse().should.equal '2.coal:16.oak_wood'
plan.produced.unparse().should.equal '16.charcoal'
it 'can compute a single item with multiple steps', ->
plans = fixtures.makePlans [1, 'test__iron_ingot']
plans.length.should.equal 2
plan = plans[0]
plan.required.unparse().should.equal 'coal:8.iron_ore:8.oak_wood'
plan.produced.unparse().should.equal '7.charcoal:iron_ingot'
plan = plans[1]
plan.required.unparse().should.equal 'coal:8.iron_ore'
plan.produced.unparse().should.equal 'iron_ingot'
+16 -3
View File
@@ -10,6 +10,7 @@ ItemSlug = require '../../src/coffee/models/item_slug'
Mod = require '../../src/coffee/models/mod'
ModPack = require '../../src/coffee/models/mod_pack'
ModVersion = require '../../src/coffee/models/mod_version'
PlanBuilder = require '../../src/coffee/models/crafting/plan_builder'
########################################################################################################################
@@ -21,6 +22,7 @@ MOD_VERSION_FILE =
recipe:
input: 8 Oak Wood, Coal
pattern: .0. ... .1.
quantity: 8
item: Crafting Table
recipe:
@@ -100,6 +102,9 @@ MOD_VERSION_FILE =
module.exports = fixtures =
makeGraphBuilder: ->
return new GraphBuilder modPack:fixtures.makeModPack()
makeModPack: ->
modPack = new ModPack
@@ -112,10 +117,18 @@ module.exports = fixtures =
return modPack
makeGraphBuilder: ->
return new GraphBuilder modPack:fixtures.makeModPack()
makePlans: (stacks...)->
modPack = fixtures.makeModPack()
makeTree: (itemSlug)->
graphBuilder = fixtures.makeGraphBuilder()
for stack in stacks
graphBuilder.wanted.add ItemSlug.slugify(stack[1]), stack[0]
graphBuilder.expandGraph()
planBuilder = new PlanBuilder graphBuilder.rootNode, wanted:graphBuilder.wanted
return planBuilder.producePlans()
makeTree: (itemSlug, quantity=1)->
builder = fixtures.makeGraphBuilder()
builder.wanted.add ItemSlug.slugify itemSlug
builder.expandGraph()
+5 -4
View File
@@ -13,13 +13,14 @@ PlanBuilder = require '../../src/coffee/models/crafting/plan_builder'
describe 'plan_builder.coffee', ->
printPlan = (plan)->
return ((s.slug.replace(/^.*>.*>/, '') for s in plan)).join ' > '
return ((s.slug.replace(/^.*>.*>/, '') for s in plan.steps;;)).join ' > '
it 'generates no plans for a gatherable item', ->
it 'generates an empty plan for a gatherable item', ->
builder = new PlanBuilder fixtures.makeTree 'test__oak_wood'
plans = builder.producePlans 100
plans.length.should.equal 0
plans.length.should.equal 1
plans[0].length.should.equal 0
builder.complete.should.be.true
it 'can find a multi-step plan', ->
@@ -34,5 +35,5 @@ describe 'plan_builder.coffee', ->
builder = new PlanBuilder fixtures.makeTree 'test__iron_ingot'
plans = builder.producePlans 100
printPlan(plans[0]).should.equal 'test__charcoal > test__iron_ingot'
printPlan(plans[0]).should.equal '8 test__charcoal > test__iron_ingot'
printPlan(plans[1]).should.equal 'test__iron_ingot'