Add CraftingPlan to compute resource requirements
This commit is contained in:
@@ -7,6 +7,7 @@ All rights reserved.
|
||||
|
||||
CraftingPlan = require '../../src/coffee/models/crafting/crafting_plan'
|
||||
fixtures = require './fixtures'
|
||||
ItemSlug = require '../../src/coffee/models/item_slug'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -20,30 +21,75 @@ describe 'crafting_plan.coffee', ->
|
||||
plan.required.unparse().should.equal 'coal'
|
||||
plan.produced.unparse().should.equal 'coal'
|
||||
|
||||
it 'can compute a single item with a single step', ->
|
||||
it 'can compute a single item with one single step plan', ->
|
||||
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'
|
||||
(s.toString() for s in plan.steps).should.eql ['1x 8 test__oak_wood,test__coal>>8 test__charcoal']
|
||||
|
||||
it 'can compute a large quantity of a single item with a single step', ->
|
||||
it 'can compute a large quantity of a single item with one single step plan', ->
|
||||
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'
|
||||
(s.toString() for s in plan.steps).should.eql ['2x 8 test__oak_wood,test__coal>>8 test__charcoal']
|
||||
|
||||
it 'can compute a single item with multiple steps', ->
|
||||
it 'can compute a single item with multiple plans', ->
|
||||
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.produced.unparse().should.equal '7.charcoal:8.iron_ingot'
|
||||
(s.toString() for s in plan.steps).should.eql [
|
||||
'1x 8 test__oak_wood,test__coal>>8 test__charcoal'
|
||||
'1x 8 test__iron_ore,test__charcoal>test__furnace>8 test__iron_ingot'
|
||||
]
|
||||
|
||||
plan = plans[1]
|
||||
plan.required.unparse().should.equal 'coal:8.iron_ore'
|
||||
plan.produced.unparse().should.equal 'iron_ingot'
|
||||
plan.produced.unparse().should.equal '8.iron_ingot'
|
||||
(s.toString() for s in plan.steps).should.eql [
|
||||
'1x 8 test__iron_ore,test__coal>test__furnace>8 test__iron_ingot'
|
||||
]
|
||||
|
||||
it 'can compute multiple items with multiple plans', ->
|
||||
plans = fixtures.makePlans [1, 'test__copper_block'], [1, 'test__iron_sword']
|
||||
plans.length.should.equal 4
|
||||
|
||||
for plan in plans
|
||||
plan.required.unparse().should.match /16.copper_ore.*8.iron_ore/
|
||||
plan.produced.unparse().should.match /copper_block.*:iron_sword/
|
||||
|
||||
plan = plans[0]
|
||||
plan.required.unparse().should.match /3.coal.*9.oak_wood/
|
||||
plan.produced.unparse().should.match /7.charcoal/
|
||||
"#{plan.steps[3]}".should.equal '1x 8 test__iron_ore,test__charcoal>test__furnace>8 test__iron_ingot'
|
||||
"#{plan.steps[4]}".should.equal '2x 8 test__copper_ore,test__coal>test__furnace>8 test__copper_ingot'
|
||||
plan.steps.length.should.equal 7
|
||||
|
||||
plan = plans[1]
|
||||
plan.required.unparse().should.match /3.coal.*:oak_wood/
|
||||
plan.produced.unparse().should.not.match /charcoal/
|
||||
"#{plan.steps[2]}".should.equal '1x 8 test__iron_ore,test__coal>test__furnace>8 test__iron_ingot'
|
||||
"#{plan.steps[3]}".should.equal '2x 8 test__copper_ore,test__coal>test__furnace>8 test__copper_ingot'
|
||||
plan.steps.length.should.equal 6
|
||||
|
||||
plan = plans[2]
|
||||
plan.required.unparse().should.match /^coal.*9.oak_wood/
|
||||
plan.produced.unparse().should.match /5.charcoal/
|
||||
"#{plan.steps[3]}".should.equal '1x 8 test__iron_ore,test__charcoal>test__furnace>8 test__iron_ingot'
|
||||
"#{plan.steps[4]}".should.equal '2x 8 test__copper_ore,test__charcoal>test__furnace>8 test__copper_ingot'
|
||||
plan.steps.length.should.equal 7
|
||||
|
||||
plan = plans[3]
|
||||
plan.required.unparse().should.match /2.coal.*9.oak_wood/
|
||||
plan.produced.unparse().should.match /6.charcoal/
|
||||
"#{plan.steps[3]}".should.equal '1x 8 test__iron_ore,test__coal>test__furnace>8 test__iron_ingot'
|
||||
"#{plan.steps[4]}".should.equal '2x 8 test__copper_ore,test__charcoal>test__furnace>8 test__copper_ingot'
|
||||
plan.steps.length.should.equal 7
|
||||
|
||||
@@ -41,16 +41,19 @@ MOD_VERSION_FILE =
|
||||
|
||||
item: Copper Ingot
|
||||
recipe:
|
||||
input: Copper Ore, Coal
|
||||
input: 8 Copper Ore, Coal
|
||||
pattern: .0. ... .1.
|
||||
quantity: 8
|
||||
tools: Furnace
|
||||
recipe:
|
||||
input: Copper Ore, Charcoal
|
||||
input: 8 Copper Ore, Charcoal
|
||||
pattern: .0. ... .1.
|
||||
quantity: 8
|
||||
tools: Furnace
|
||||
recipe:
|
||||
input: Copper Block
|
||||
pattern: ... .0. ...
|
||||
quantity: 9
|
||||
|
||||
item: Copper Ore
|
||||
|
||||
@@ -66,10 +69,12 @@ MOD_VERSION_FILE =
|
||||
recipe:
|
||||
input: 8 Iron Ore, Charcoal
|
||||
pattern: .0. ... .1.
|
||||
quantity: 8
|
||||
tools: Furnace
|
||||
recipe:
|
||||
input: 8 Iron Ore, Coal
|
||||
pattern: .0. ... .1.
|
||||
quantity: 8
|
||||
tools: Furnace
|
||||
|
||||
item: Iron Sword
|
||||
|
||||
@@ -13,27 +13,31 @@ PlanBuilder = require '../../src/coffee/models/crafting/plan_builder'
|
||||
describe 'plan_builder.coffee', ->
|
||||
|
||||
printPlan = (plan)->
|
||||
return ((s.slug.replace(/^.*>.*>/, '') for s in plan.steps;;)).join ' > '
|
||||
return '' unless plan?
|
||||
return ((s.recipe.slug.replace(/^.*>.*>/, '') for s in plan.steps;;)).join ' > '
|
||||
|
||||
it 'generates an empty plan for a gatherable item', ->
|
||||
builder = new PlanBuilder fixtures.makeTree 'test__oak_wood'
|
||||
plans = builder.producePlans 100
|
||||
plans = fixtures.makePlans [1, 'test__oak_wood']
|
||||
|
||||
plans.length.should.equal 1
|
||||
plans[0].length.should.equal 0
|
||||
builder.complete.should.be.true
|
||||
|
||||
it 'can find a multi-step plan', ->
|
||||
builder = new PlanBuilder fixtures.makeTree 'test__lever'
|
||||
plans = builder.producePlans 100
|
||||
plans = fixtures.makePlans [1, 'test__lever']
|
||||
|
||||
printPlan(plans[0]).should.equal '4 test__oak_planks > 4 test__stick > test__lever'
|
||||
plans.length.should.equal 1
|
||||
builder.complete.should.be.true
|
||||
|
||||
it 'can find multiple plans', ->
|
||||
builder = new PlanBuilder fixtures.makeTree 'test__iron_ingot'
|
||||
plans = builder.producePlans 100
|
||||
plans = fixtures.makePlans [1, 'test__iron_ingot']
|
||||
|
||||
printPlan(plans[0]).should.equal '8 test__charcoal > test__iron_ingot'
|
||||
printPlan(plans[1]).should.equal 'test__iron_ingot'
|
||||
printPlan(plans[0]).should.equal '8 test__charcoal > 8 test__iron_ingot'
|
||||
printPlan(plans[1]).should.equal '8 test__iron_ingot'
|
||||
plans.length.should.equal 2
|
||||
|
||||
it 'ignores invalid plans', ->
|
||||
plans = fixtures.makePlans [1, 'test__copper_block']
|
||||
|
||||
printPlan(plans[0]).should.equal '8 test__copper_ingot > test__copper_block'
|
||||
printPlan(plans[1]).should.equal '8 test__charcoal > 8 test__copper_ingot > test__copper_block'
|
||||
plans.length.should.equal 2
|
||||
|
||||
@@ -91,6 +91,11 @@ describe 'mod_version_parser_v1.coffee', ->
|
||||
modVersion = parser.parse baseText + 'recipe:; input:Delta, Echo, Foxtrot; pattern:...012...'
|
||||
(s.item for s in modVersion._slugs).should.eql ['charlie', 'delta', 'echo', 'foxtrot']
|
||||
|
||||
it 'correctly handles recipes which use the same input multiple times', ->
|
||||
modVersion = parser.parse baseText + 'recipe:; input:Alpha; pattern:.0..0....'
|
||||
recipe = _.values(modVersion._recipes)[0]
|
||||
recipe.getQuantityRequired(ItemSlug.slugify('alpha')).should.equal 2
|
||||
|
||||
it 'allows a quantity for each input', ->
|
||||
modVersion = parser.parse baseText + 'recipe:; input: 12 Delta, 3 Echo; pattern:... 0.1 ...'
|
||||
recipe = _.values(modVersion._recipes)[0]
|
||||
|
||||
Reference in New Issue
Block a user