Fix bug with recipe books table

This commit is contained in:
Andrew Miner
2014-12-29 09:07:39 -08:00
parent 372a1e0063
commit 004320d052
9 changed files with 61 additions and 23 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ module.exports = class CraftingPlan
targetItem = @_pending.pop()
return unless targetItem?
recipes = @catalog.findRecipes targetItem.name
recipes = @catalog.gatherRecipes targetItem.name
return if not recipes.length > 0
recipe = recipes[0]
+19 -2
View File
@@ -17,22 +17,39 @@ module.exports = class RecipeBook extends BaseModel
attributes.description ?= ''
attributes.recipes ?= []
attributes.enabled ?= true
super attributes, options
# Public Methods ###############################################################################
findRecipes: (name)->
result = []
gatherNames: (result)->
return unless @enabled
for recipe in @recipes
continue if result[recipe.name]
result[recipe.name] = value:recipe.name, label:"#{recipe.name} (from #{@modName} #{@modVersion})"
return result
gatherRecipes: (name, result)->
return unless @enabled
for recipe in @recipes
if recipe.name is name
result.push recipe
return result
hasRecipe: (name)->
for recipe in @recipes
return true if recipe.name is name
return false
# Object Overrides #############################################################################
toString: ->
return "RecipeBook (#{@cid}) {
enabled:#{@enabled},
modName:#{@modName},
modVersion:#{@modVersion},
recipes:#{@recipes.length} items}"
+17 -14
View File
@@ -21,27 +21,29 @@ module.exports = class RecipeCatalog extends BaseModel
# Public Methods ###############################################################################
findRecipes: (name)->
result = []
gatherNames: ->
nameData = {}
for book in @books
for recipe in book.findRecipes name
result.push recipe
book.gatherNames nameData
return result
getRecipeNames: ->
nameHash = {}
for book in @books
for recipe in book.recipes
nameHash[recipe.name] = "#{recipe.name} (from #{book.modName} #{book.modVersion})"
names = (k for k, v of nameHash).sort()
result = []
names = _.keys(nameData).sort()
for name in names
result.push value:name, label:nameHash[name]
result.push nameData[name]
return result
gatherRecipes: (name)->
result = []
for book in @books
book.gatherRecipes name, result
return result
hasRecipe: (name)->
for book in @books
return true if book.hasRecipe(name)
return false
loadBook: (url)->
w.promise (resolve, reject)=>
@trigger Event.load.started, this, url
@@ -59,6 +61,7 @@ module.exports = class RecipeCatalog extends BaseModel
@books.sort (a, b)->
return 0 if a.modName is b.modName
return if a.modName < b.modName then -1 else +1
book.on Event.change, => @trigger Event.change, this
return book