diff --git a/src/coffee/models/crafting/crafting_node.coffee b/src/coffee/models/crafting/crafting_node.coffee index cabc03357..26fd3ace6 100644 --- a/src/coffee/models/crafting/crafting_node.coffee +++ b/src/coffee/models/crafting/crafting_node.coffee @@ -29,6 +29,24 @@ module.exports = class CraftingNode queue.push child return queue + acceptVisitor: (visitor)-> + enter = visitor[@ENTER_METHOD] + if enter? + enter.call visitor, this + else + enter = visitor['onEnterOtherNode'] + if enter? then enter.call visitor, this + + for child in @_children + child.acceptVisitor visitor + + leave = visitor[@LEAVE_METHOD] + if leave? + leave.call visitor, this + else + leave = visitor['onLeaveOtherNode'] + if leave? then leave.call visitor, this + # Property Methods ############################################################################# getChildren: -> diff --git a/src/coffee/models/crafting/inventory_node.coffee b/src/coffee/models/crafting/inventory_node.coffee index fa7614836..a29607f43 100644 --- a/src/coffee/models/crafting/inventory_node.coffee +++ b/src/coffee/models/crafting/inventory_node.coffee @@ -12,6 +12,9 @@ ItemNode = require './item_node' module.exports = class InventoryNode extends CraftingNode + @::ENTER_METHOD = 'onEnterInventoryNode' + @::LEAVE_METHOD = 'onLeaveInventoryNode' + constructor: (options={})-> if not options.inventory? then throw new Error 'options.inventory is required' super options @@ -38,10 +41,14 @@ module.exports = class InventoryNode extends CraftingNode # Object Overrides ############################################################################# - toString: (indent='')-> + toString: (options={})-> + options.indent ?= '' + options.recursive ?= true + completeText = if @complete then 'complete' else 'incomplete' - parts = ["#{indent}#{@completeText} InventoryNode for #{@inventory}"] - nextIndent = indent + ' ' - for child in @children - parts.push child.toString nextIndent + parts = ["#{options.indent}#{@completeText} InventoryNode for #{@inventory}"] + nextIndent = options.indent + ' ' + if options.recursive + for child in @children + parts.push child.toString indent:nextIndent return parts.join '\n' diff --git a/src/coffee/models/crafting/item_node.coffee b/src/coffee/models/crafting/item_node.coffee index 4c6ee324e..b886fcaa3 100644 --- a/src/coffee/models/crafting/item_node.coffee +++ b/src/coffee/models/crafting/item_node.coffee @@ -12,6 +12,9 @@ RecipeNode = require './recipe_node' module.exports = class ItemNode extends CraftingNode + @::ENTER_METHOD = 'onEnterItemNode' + @::LEAVE_METHOD = 'onLeaveItemNode' + constructor: (options={})-> if not options.item? then throw new Error 'options.item is required' super options @@ -60,10 +63,14 @@ module.exports = class ItemNode extends CraftingNode # Object Overrides ############################################################################# - toString: (indent)-> + toString: (options={})-> + options.indent ?= '' + options.recursive ?= true + completeText = if @complete then 'complete' else 'incomplete' - parts = ["#{indent}#{@completeText} ItemNode for #{@item.name}"] - nextIndent = indent + ' ' - for child in @children - parts.push child.toString nextIndent + parts = ["#{options.indent}#{@completeText} ItemNode for #{@item.name}"] + nextIndent = options.indent + ' ' + if options.recursive + for child in @children + parts.push child.toString indent:nextIndent return parts.join '\n' diff --git a/src/coffee/models/crafting/recipe_node.coffee b/src/coffee/models/crafting/recipe_node.coffee index 4fad67cc4..5eaa50d73 100644 --- a/src/coffee/models/crafting/recipe_node.coffee +++ b/src/coffee/models/crafting/recipe_node.coffee @@ -12,6 +12,9 @@ CraftingNode = require './crafting_node' module.exports = class RecipeNode extends CraftingNode + @::ENTER_METHOD = 'onEnterRecipeNode' + @::LEAVE_METHOD = 'onLeaveRecipeNode' + constructor: (options={})-> if not options.recipe? then throw new Error 'options.recipe is required' super options @@ -52,10 +55,14 @@ module.exports = class RecipeNode extends CraftingNode # Object Overrides ############################################################################ - toString: (indent)-> + toString: (options={})-> + options.indent ?= '' + options.recursive ?= true + completeText = if @complete then 'complete' else 'incomplete' - parts = ["#{indent}#{@completeText} RecipeNode for #{@recipe.slug}"] - nextIndent = indent + ' ' - for child in @children - parts.push child.toString nextIndent + parts = ["#{options.indent}#{@completeText} RecipeNode for #{@recipe.slug}"] + nextIndent = options.indent + ' ' + if options.recursive + for child in @children + parts.push child.toString indent:nextIndent return parts.join '\n' diff --git a/test/crafting/crafting_node.test.coffee b/test/crafting/crafting_node.test.coffee new file mode 100644 index 000000000..16223f587 --- /dev/null +++ b/test/crafting/crafting_node.test.coffee @@ -0,0 +1,44 @@ +### +Crafting Node - crafting_node.test.coffee + +Copyright (c) 2015 by Redwood Labs +All rights reserved. +### + +fixtures = require './fixtures' + +######################################################################################################################## + +root = null + +######################################################################################################################## + +describe 'crafting_node.coffee', -> + + describe 'acceptVisitor', -> + + it 'can provide a prefix, depth-first traversal', -> + root = fixtures.makeTree 'test__iron_ingot' + + visitor = + nodes: [] + onEnterOtherNode: (node)-> visitor.nodes.push node + + root.acceptVisitor visitor + + (v.constructor.name.replace('Node', '') for v in visitor.nodes).should.eql [ + "Inventory", "Item", "Recipe", "Item", "Item", "Recipe", "Item", "Item", "Recipe", "Item", "Item" + ] + + it 'can provide a postfix, depth-first traversal', -> + root = fixtures.makeTree 'test__iron_ingot' + + visitor = + nodes: [] + onLeaveOtherNode: (node)-> visitor.nodes.push node + + root.acceptVisitor visitor + + (n.constructor.name.replace('Node', '') for n in visitor.nodes).should.eql [ + "Item", "Item", "Item", "Recipe", "Item", "Recipe", "Item", "Item", "Recipe", "Item", "Inventory" + ] diff --git a/test/crafting/fixtures.coffee b/test/crafting/fixtures.coffee new file mode 100644 index 000000000..5f7fefc72 --- /dev/null +++ b/test/crafting/fixtures.coffee @@ -0,0 +1,102 @@ +### +Crafting Guide - sample_modpack.coffee + +Copyright (c) 2015 by Redwood Labs +All rights reserved. +### + +GraphBuilder = require '../../src/coffee/models/crafting/graph_builder' +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' + +######################################################################################################################## + +MOD_VERSION_FILE = + """ + schema: 1 + + item: Charcoal + recipe: + input: 8 Oak Wood, Coal + pattern: .0. ... .1. + + item: Crafting Table + recipe: + input: Oak Planks + pattern: .00 .00 ... + + item: Coal + + item: Cobblestone + + item: Furnace + recipe: + input: Cobblestone + pattern: 000 0.0 000 + tools: Crafting Table + + item: Iron Ore + + item: Iron Ingot + recipe: + input: 8 Iron Ore, Charcoal + pattern: .0. ... .1. + tools: Furnace + recipe: + input: 8 Iron Ore, Coal + pattern: .0. ... .1. + tools: Furnace + + item: Iron Sword + recipe: + input: Iron Ingot, Stick + pattern: .0. .0. .1. + tools: Crafting Table + + item: Lever + recipe: + input: Stick, Cobblestone + pattern: .0. .1. ... + + item: Oak Planks + recipe: + input: Oak Wood + pattern: ... .0. ... + quantity: 4 + + item: Oak Wood + + item: Stick + recipe: + input: Oak Planks + pattern: .0. .0. ... + quantity: 4 + """ + +######################################################################################################################## + +module.exports = fixtures = + + makeModPack: -> + modPack = new ModPack + + mod = new Mod name:'Test', slug:'test' + modPack.addMod mod + + modVersion = new ModVersion modSlug:'test', version:'0.0' + modVersion.parse MOD_VERSION_FILE + mod.addModVersion modVersion + + return modPack + + makeGraphBuilder: -> + return new GraphBuilder modPack:fixtures.makeModPack() + + makeTree: (itemSlug)-> + builder = fixtures.makeGraphBuilder() + builder.wanted.add ItemSlug.slugify itemSlug + builder.expandGraph() + + return builder.rootNode diff --git a/test/crafting/graph_builder.test.coffee b/test/crafting/graph_builder.test.coffee index 60b0ed86f..bd5dd1918 100644 --- a/test/crafting/graph_builder.test.coffee +++ b/test/crafting/graph_builder.test.coffee @@ -5,92 +5,19 @@ Copyright (c) 2015 by Redwood Labs All rights reserved. ### -GraphBuilder = require '../../src/coffee/models/crafting/graph_builder' +fixtures = require './fixtures' 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' ######################################################################################################################## -SAMPLE_MOD_VERSION_TEXT = """ - schema: 1 - - item: Charcoal - recipe: - input: 8 Oak Wood, Coal - pattern: .0. ... .1. - - item: Crafting Table - recipe: - input: Oak Planks - pattern: .00 .00 ... - - item: Coal - - item: Cobblestone - - item: Furnace - recipe: - input: Cobblestone - pattern: 000 0.0 000 - tools: Crafting Table - - item: Iron Ore - - item: Iron Ingot - recipe: - input: 8 Iron Ore, Charcoal - pattern: .0. ... .1. - tools: Furnace - recipe: - input: 8 Iron Ore, Coal - pattern: .0. ... .1. - tools: Furnace - - item: Iron Sword - recipe: - input: Iron Ingot, Stick - pattern: .0. .0. .1. - tools: Crafting Table - - item: Lever - recipe: - input: Stick, Cobblestone - pattern: .0. .1. ... - - item: Oak Planks - recipe: - input: Oak Wood - pattern: ... .0. ... - quantity: 4 - - item: Oak Wood - - item: Stick - recipe: - input: Oak Planks - pattern: .0. .0. ... - quantity: 4 -""" - -builder = mod = modPack = modVersion = null +builder = null ######################################################################################################################## describe 'GraphBuilder.coffee', -> beforeEach -> - modPack = new ModPack - - mod = new Mod name:'Test', slug:'test' - modPack.addMod mod - - modVersion = new ModVersion modSlug:'test', version:'0.0' - modVersion.parse SAMPLE_MOD_VERSION_TEXT - mod.addModVersion modVersion - - builder = new GraphBuilder modPack:modPack + builder = fixtures.makeGraphBuilder() describe 'expand', ->