Rename RecipeCatalog to ModPack

This commit is contained in:
Andrew Miner
2015-01-01 20:16:38 -08:00
parent f28a763b13
commit e14a4d0810
9 changed files with 17 additions and 17 deletions
+138
View File
@@ -0,0 +1,138 @@
###
# Crafting Guide - mod_pack.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
BaseModel = require './base_model'
{Event} = require '../constants'
ModVersionParser = require './mod_version_parser'
{RequiredMods} = require '../constants'
########################################################################################################################
module.exports = class ModPack extends BaseModel
constructor: (attributes={}, options={})->
attributes.books ?= []
super attributes, options
@_parser = new ModVersionParser
# Public Methods ###############################################################################
enableBooksForRecipe: (name)->
for book in @books
continue if book.enabled
if book.hasRecipe name
book.enabled = true
gatherNames: (options={})->
options.includeDisabled ?= false
nameData = {}
for book in @books
continue unless book.enabled or options.includeDisabled
book.gatherNames nameData
result = []
names = _.keys(nameData).sort()
for name in names
result.push nameData[name]
return result
gatherRecipes: (name, options={})->
options.includeDisabled ?= false
result = []
for book in @books
continue unless book.enabled or options.includeDisabled
book.gatherRecipes name, result
return result
hasRecipe: (name, options={})->
options.includeDisabled ?= false
for book in @books
continue unless book.enabled or options.includeDisabled
return true if book.hasRecipe name
return false
isRawMaterial: (name, options={})->
options.includeDisabled ?= false
for book in @books
continue unless book.enabled or options.includeDisabled
return true if book.isRawMaterial name
return false
loadBook: (url)->
w.promise (resolve, reject)=>
@trigger Event.load.started, this, url
$.ajax
url: url
dataType: 'json'
success: (data, status, xhr)=>
resolve @onBookLoaded(url, data, status, xhr)
error: (xhr, status, error)=>
reject @onBookLoadFailed(url, error, status, xhr)
loadBookData: (data)->
book = @_parser.parse data
@books.push book
@_sortBooks()
book.on Event.change, => @trigger Event.change, this
return book
loadAllBooks: (urlList)->
promises = (@loadBook(url) for url in urlList)
return w.settle promises
# Event Methods ################################################################################
onBookLoaded: (url, data, status, xhr)->
try
book = @loadBookData data
logger.info "loaded recipe book from #{url}: #{book}"
@trigger Event.load.succeeded, this, book
@trigger Event.load.finished, this
@trigger Event.change, this
return book
catch e
@onBookLoadFailed url, e, status, xhr
onBookLoadFailed: (url, error, status, xhr)->
message = if error.stack? then error.stack else error
logger.error "failed to load recipe book from #{url}: #{message}"
@trigger Event.load.failed, this, error.message
@trigger Event.load.finished, this
return error
# Object Overrides #############################################################################
toString: ->
return "ModPack (#{@cid}) {books:#{@books.length} items}"
_sortBooks:->
@books.sort (a, b)->
if a.modName is b.modName then return 0
aRequired = a.modName in RequiredMods
bRequired = b.modName in RequiredMods
if aRequired and bRequired
return if a.modName < b.modName then -1 else +1
else if aRequired
return -1
else if bRequired
return +1
else
return if a.modName < b.modName then -1 else +1