Make Recipe a child of ModVersion, not Item

This commit is contained in:
Andrew Miner
2015-02-15 19:01:16 -08:00
parent a3be372e17
commit 8804605d99
10 changed files with 146 additions and 163 deletions
+6 -4
View File
@@ -90,7 +90,7 @@ module.exports = class CraftingPlan extends BaseModel
return "#{@constructor.name} {
have:#{@have},
want:#{@want},
need:#{@_need},
need:#{@need},
result:#{@result},
steps:#{@steps}
}"
@@ -98,11 +98,13 @@ module.exports = class CraftingPlan extends BaseModel
# Private Methods ##############################################################################
_addStep: (recipe)->
logger.verbose -> "adding step: #{recipe.item.qualifiedSlug}"
@steps[recipe.item.qualifiedSlug] = recipe:recipe
logger.verbose -> "adding step: #{recipe.slug}"
@steps[recipe.slug] = recipe:recipe
_chooseRecipe: (item)->
return item.getPrimaryRecipe()
recipes = @modPack.findRecipes item.qualifiedSlug
return null unless recipes?
return recipes[0]
_findSteps: (slug)->
item = @modPack.findItem slug
+19 -34
View File
@@ -9,6 +9,7 @@ BaseCollection = require './base_collection'
BaseModel = require './base_model'
{Event} = require '../constants'
Recipe = require './recipe'
StringBuilder = require './string_builder'
########################################################################################################################
@@ -27,27 +28,14 @@ module.exports = class Item extends BaseModel
options.logEvents ?= false
super attributes, options
@_recipes = []
Object.defineProperties this,
isCraftable: { get:-> @_recipes.length > 0 }
qualifiedSlug: { get:@getQualifiedSlug }
primaryRecipe: { get:@getPrimaryRecipe }
isCraftable: {get:@getIsCraftable}
qualifiedSlug: {get:@getQualifiedSlug}
@on Event.change + ':modVersion', => @_qualifiedSlug = null
# Public Methods ###############################################################################
addRecipe: (recipe)->
[modSlug, itemSlug] = _.decomposeSlug recipe.slug
if itemSlug isnt @slug then throw new Error "cannot add a recipe for #{recipe.slug} to #{@slug}"
recipe.item = this
@_recipes.push recipe
eachRecipe: (callback)->
for recipe in @_recipes
callback recipe
compareTo: (that)->
if this.slug isnt that.slug
return if this.slug < that.slug then -1 else +1
@@ -57,6 +45,10 @@ module.exports = class Item extends BaseModel
# Property Methods #############################################################################
getIsCraftable: ->
return false unless @modVersion?
return @modVersion.hasRecipes @qualifiedSlug
getQualifiedSlug: ->
return @slug if not @modVersion?
@@ -65,25 +57,18 @@ module.exports = class Item extends BaseModel
return @_qualifiedSlug
getPrimaryRecipe: ->
return @_recipes[0]
# Object Overrides #############################################################################
toString: ->
result = []
result.push @constructor.name
result.push ' ('; result.push @cid; result.push ') { '
result.push 'name:"'; result.push @name; result.push '", '
result.push 'isGatherable:'; result.push @isGatherable
if _.slugify(@name) isnt @slug
result.push ', slug:'; result.push @slug
if @_recipes.length > 0
result.push ', recipes:«'
result.push @_recipes.length
result.push ' items»'
result.push '}'
return result.join ''
builder = new StringBuilder
return builder
.push @constructor.name, ' (', @cid, ') { '
.push 'name:"', @name, '", '
.push 'isCraftable:', @isCraftable, ', '
.push 'isGatherable:', @isGatherable, ', '
.onlyIf (@group isnt Item.Group.Other), (b)=>
b.push 'group:"', @group, '", '
.onlyIf (_.slugify(@name) isnt @slug), (b)=>
b.push 'slug:"', @slug, '", '
.push '}'
.toString()
+2 -1
View File
@@ -91,7 +91,8 @@ module.exports = class ModPack extends BaseModel
for mod in @_mods
continue unless mod.enabled
mod.findRecipes slug, result
return result
return if result.length > 0 then result else null
isGatherable: (slug)->
item = @findItem slug
+72 -46
View File
@@ -22,30 +22,14 @@ module.exports = class ModVersion extends BaseModel
attributes.mod ?= null
super attributes, options
@_groups = {}
@_items = {}
@_names = {}
@_slugs = []
@_groups = {}
@_items = {}
@_names = {}
@_recipes = {}
@_slugs = []
# Public Methods ###############################################################################
addItem: (item)->
if @_items[item.slug]? then throw new Error "duplicate item for #{item.name}"
@_items[item.slug] = item
@_groups[item.group] ?= {}
@_groups[item.group][item.slug] = item
item.modVersion = this
@registerSlug item.slug, item.name
return this
allItemsInGroup: (group)->
result = []
@eachItemInGroup group, (item)-> result.push item
return null if result.length is 0
return result
compareTo: (that)->
if this.mod? and that.mod?
return this.mod.compareTo that.mod
@@ -55,16 +39,24 @@ module.exports = class ModVersion extends BaseModel
return 0
eachGroup: (callback)->
groupNames = _.keys @_groups
groupNames.sort (a, b)->
if a is b then return 0
if a is Item.Group.Other then return -1
if b is Item.Group.Other then return +1
return if a < b then -1 else +1
# Item Methods #################################################################################
for groupName in groupNames
callback groupName
addItem: (item)->
if @_items[item.slug]? then throw new Error "duplicate item for #{item.name}"
@_items[item.slug] = item
@_groups[item.group] ?= {}
@_groups[item.group][item.slug] = item
item.modVersion = this
@registerName item.slug, item.name
return this
allItemsInGroup: (group)->
result = []
@eachItemInGroup group, (item)-> result.push item
return null if result.length is 0
return result
eachItem: (callback)->
for slug in @_slugs
@@ -80,30 +72,38 @@ module.exports = class ModVersion extends BaseModel
for slug in _.keys(group).sort()
callback group[slug]
findItem: (itemSlug)->
return @_items[itemSlug]
findItemByName: (name)->
for itemSlug, item of @_items
return item if item.name is name
return null
# Group Methods ################################################################################
eachGroup: (callback)->
groupNames = _.keys @_groups
groupNames.sort (a, b)->
if a is b then return 0
if a is Item.Group.Other then return -1
if b is Item.Group.Other then return +1
return if a < b then -1 else +1
for groupName in groupNames
callback groupName
# Name Methods #################################################################################
eachName: (callback)->
for slug in @_slugs
callback @_names[slug], slug
return this
findItem: (slug)->
return @_items[slug]
findItemByName: (name)->
return @findItem _.slugify name
findName: (slug)->
return @_names[slug]
findRecipes: (itemSlug, result=[])->
for slug in @_slugs
item = @_items[slug]
continue unless item?
item.eachRecipe (recipe)->
result.push recipe if recipe.doesProduce itemSlug
return result
registerSlug: (slug, name)->
registerName: (slug, name)->
hasSlug = @_names[slug]?
@_names[slug] = name
@@ -114,6 +114,32 @@ module.exports = class ModVersion extends BaseModel
return this
# Recipe Methods ###############################################################################
addRecipe: (recipe)->
recipe.modVersion = this
for stack in recipe.output
recipeList = @_recipes[stack.slug]
if not recipeList?
@_recipes[stack.slug] = recipeList = []
recipeList.push recipe
return this
findRecipes: (itemSlug, result=[])->
recipeList = @_recipes[itemSlug]
if recipeList?
for recipe in recipeList
result.push recipe
return result
hasRecipes: (itemSlug)->
recipeList = @_recipes[itemSlug]
return true if recipeList? and recipeList.length > 0
return false
# Backbone.Model Overrides #####################################################################
parse: (text)->
@@ -135,7 +135,7 @@ module.exports = class ModVersionParserV1 extends CommandParserVersionBase
if not recipeData.input? then throw new Error 'the "input" declaration is required'
if not recipeData.pattern? then throw new Error 'the "pattern" declaration is required'
localizeSlug = (name, slug)=>
qualifySlug = (name, slug)=>
if @_rawData.items[name]?
return _.composeSlugs modVersion.modSlug, slug
else
@@ -148,8 +148,8 @@ module.exports = class ModVersionParserV1 extends CommandParserVersionBase
inputStacks = []
for name in recipeData.input
slug = _.slugify name
modVersion.registerSlug slug, name
slug = localizeSlug name, slug
modVersion.registerName slug, name
slug = qualifySlug name, slug
inputStacks.push new Stack slug:slug, quantity:0
for c in recipeData.pattern
@@ -168,26 +168,25 @@ module.exports = class ModVersionParserV1 extends CommandParserVersionBase
outputStacks = [ new Stack slug:item.qualifiedSlug, quantity:recipeData.quantity ]
for extraData in recipeData.extras
slug = _.slugify extraData.name
modVersion.registerSlug slug, extraData.name
slug = localizeSlug extraData.name, slug
modVersion.registerName slug, extraData.name
slug = qualifySlug extraData.name, slug
outputStacks.push new Stack slug:slug, quantity:extraData.quantity
toolStacks = []
for name in recipeData.tools
slug = _.slugify name
modVersion.registerSlug slug, name
slug = localizeSlug name, slug
modVersion.registerName slug, name
slug = qualifySlug name, slug
toolStacks.push new Stack slug:slug, quantity:1
attributes =
input: inputStacks
name: item.name
pattern: recipeData.pattern
output: outputStacks
tools: toolStacks
input: inputStacks
output: outputStacks
pattern: recipeData.pattern
tools: toolStacks
recipe = new Recipe attributes
item.addRecipe recipe
modVersion.addRecipe recipe
return recipe
# Un-parsing Methods ###########################################################################
+13 -24
View File
@@ -13,23 +13,23 @@ Stack = require './stack'
module.exports = class Recipe extends BaseModel
constructor: (attributes={}, options={})->
if attributes.item?
attributes.name = attributes.item.name
attributes.output ?= [new Stack slug:attributes.item.qualifiedSlug, quantity:1]
if not attributes.name? then throw new Error 'attributes.name is required'
if not attributes.input? then throw new Error 'attributes.input is required'
if not attributes.pattern? then throw new Error 'attributes.pattern is required'
attributes.item ?= null
attributes.output ?= [new Stack slug:_.slugify(attributes.name), quantity:1]
attributes.pattern = @_parsePattern attributes.pattern
attributes.tools ?= []
options.logEvents ?= false
super attributes, options
if attributes.slug? and not attributes.output?
attributes.output = [new Stack slug:attributes.slug, quantity:1]
else if attributes.output? and not attributes.slug?
if attributes.output.length is 0 then throw new Error 'attributes.output cannot be empty'
attributes.slug = attributes.output[0].slug
else
throw new Error 'attributes.slug or attributes.output is required'
Object.defineProperties this,
slug: {get:@getSlug}
attributes.pattern = @_parsePattern attributes.pattern
attributes.modVersion ?= null
attributes.tools ?= []
options.logEvents ?= false
super attributes, options
# Public Methods ###############################################################################
@@ -44,17 +44,6 @@ module.exports = class Recipe extends BaseModel
return stack.slug
doesProduce: (itemSlug)->
for stack in @output
return true if stack.slug is itemSlug
return false
# Property Methods #############################################################################
getSlug: ->
return @item.qualifiedSlug if @item?
return @output[0].slug
# Object Overrides #############################################################################
toString: ->