wip
This commit is contained in:
@@ -764,6 +764,10 @@
|
||||
"output": [[16, "Light Gray Stained Glass Pane"]],
|
||||
"input": [[6, "Light Gray Stained Glass"]],
|
||||
"tools": "Crafting Table"
|
||||
}, {
|
||||
"output": [[8, "Light Gray Stained Glass"]],
|
||||
"input": [[8, "Glass"], "Light Gray Dye"],
|
||||
"tools": "Crafting Table"
|
||||
}, {
|
||||
"output": "Light Gray Wool",
|
||||
"input": ["Light Gray Dye", "Wool"]
|
||||
|
||||
@@ -30,7 +30,7 @@ module.exports = class CraftingTableController extends BaseController
|
||||
|
||||
onModPackChanged: ->
|
||||
return unless @rendered
|
||||
@model.modPack.enableBooksForRecipe @$nameField.val()
|
||||
@model.modPack.enableModsForItem @$nameField.val()
|
||||
@_updateNameAutocomplete()
|
||||
@_craft()
|
||||
|
||||
@@ -134,7 +134,7 @@ module.exports = class CraftingTableController extends BaseController
|
||||
onChanged = => @onNameFieldChanged()
|
||||
|
||||
@$nameField.autocomplete
|
||||
source: @model.modPack.gatherNames()
|
||||
source: @model.modPack.gatherRecipeNames()
|
||||
delay: 0
|
||||
minLength: 0
|
||||
change: onChanged
|
||||
|
||||
@@ -15,7 +15,7 @@ module.exports = class ModPackController extends BaseController
|
||||
|
||||
constructor: (options={})->
|
||||
if not options.model? then throw new Error "options.model is required"
|
||||
@_bookControllers = []
|
||||
@_modVersionControllers = []
|
||||
|
||||
options.templateName = 'mod_pack'
|
||||
super options
|
||||
@@ -23,8 +23,8 @@ module.exports = class ModPackController extends BaseController
|
||||
# BaseController Overrides #####################################################################
|
||||
|
||||
onWillRender: ->
|
||||
if @model.books.length is 0
|
||||
@model.loadAllBooks DefaultBookUrls
|
||||
if @model.modVersions.length is 0
|
||||
@model.loadAllModVersions DefaultBookUrls
|
||||
|
||||
onDidRender: ->
|
||||
@$table = @$('table')
|
||||
@@ -34,12 +34,12 @@ module.exports = class ModPackController extends BaseController
|
||||
@$('table tr:not(:last-child)').remove()
|
||||
return unless @model?
|
||||
|
||||
@_bookControllers = []
|
||||
for i in [@model.books.length-1..0] by -1
|
||||
book = @model.books[i]
|
||||
controller = new ModVersionController model:book
|
||||
@_modVersionControllers = []
|
||||
for i in [@model.modVersions.length-1..0] by -1
|
||||
modVersion = @model.modVersions[i]
|
||||
controller = new ModVersionController model:modVersion
|
||||
controller.render()
|
||||
@_bookControllers.push controller
|
||||
@_modVersionControllers.push controller
|
||||
@$table.prepend controller.$el
|
||||
|
||||
super
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
###
|
||||
# Crafting Guide - main.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - main.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
require './underscore_mixins'
|
||||
|
||||
views = require './views'
|
||||
Logger = require './logger'
|
||||
CraftingGuideRouter = require './crafting_guide_router'
|
||||
|
||||
@@ -29,11 +29,18 @@ module.exports = class CraftingPlan
|
||||
return this
|
||||
|
||||
craft: (name, quantity=1, have=null)->
|
||||
logger.trace "craft(#{name}, #{quantity}, #{have})"
|
||||
|
||||
item = @modPack.findItemByName name
|
||||
logger.debug "item: #{item}"
|
||||
if not item? then throw new Error "cannot find an item named: #{name}"
|
||||
|
||||
@clear()
|
||||
@result.addInventory(have) if have?
|
||||
|
||||
@_expected.add name, quantity
|
||||
@_expected.add item, quantity
|
||||
@_pending = @_expected.clone()
|
||||
|
||||
while not @_pending.isEmpty
|
||||
@_processPending()
|
||||
|
||||
@@ -47,50 +54,54 @@ module.exports = class CraftingPlan
|
||||
# Private Methods ##############################################################################
|
||||
|
||||
_processPending: ->
|
||||
targetItem = @_pending.pop()
|
||||
targetStack = @_pending.pop()
|
||||
targetItem = targetStack.item
|
||||
logger.verbose "processing item: #{targetItem}, craftable? #{targetItem.isCraftable}"
|
||||
return unless targetItem?
|
||||
return if (not targetItem.isCraftable) or targetItem.isGatherable
|
||||
|
||||
return if @modPack.isRawMaterial targetItem.name
|
||||
|
||||
recipes = @modPack.gatherRecipes targetItem.name
|
||||
return if not recipes.length > 0
|
||||
|
||||
recipe = recipes[0]
|
||||
recipe = targetItem.recipes[0]
|
||||
logger.verbose "recipe: #{recipe}"
|
||||
|
||||
if @includingTools
|
||||
for tool in recipe.tools
|
||||
totalExpected = @result.quantityOf(tool.name) + @_expected.quantityOf(tool.name)
|
||||
if totalExpected < tool.quantity
|
||||
@_pending.add tool.name, tool.quantity
|
||||
@_expected.add tool.name, tool.quantity
|
||||
for toolItem in recipe.tools
|
||||
totalExpected = @result.quantityOf(toolItem.slug) + @_expected.quantityOf(toolItem.slug)
|
||||
if totalExpected < 1
|
||||
@_pending.add toolItem
|
||||
@_expected.add toolItem
|
||||
|
||||
while @_totalQuantityOf(targetItem.name) < @_expected.quantityOf(targetItem.name)
|
||||
while @_totalQuantityOf(targetItem.slug) < @_expected.quantityOf(targetItem.slug)
|
||||
@steps.push recipe
|
||||
|
||||
for item in recipe.input
|
||||
@_processInputItem item
|
||||
for stack in recipe.input
|
||||
@_processInputStack stack
|
||||
|
||||
for item in recipe.output
|
||||
@_processOutputItem item
|
||||
for stack in recipe.output
|
||||
@_processOutputStack stack
|
||||
|
||||
_processInputItem: (item)->
|
||||
quantityAvailable = @result.quantityOf item.name
|
||||
quantityUsed = Math.min quantityAvailable, item.quantity
|
||||
quantityNeeded = item.quantity - quantityUsed
|
||||
_processInputStack: (stack)->
|
||||
slug = stack.item.slug
|
||||
quantityAvailable = @result.quantityOf slug
|
||||
quantityUsed = Math.min quantityAvailable, stack.quantity
|
||||
quantityNeeded = stack.quantity - quantityUsed
|
||||
logger.verbose "processing input:#{stack.name},
|
||||
a:#{quantityAvailable}, u:#{quantityUsed}, n:#{quantityNeeded}"
|
||||
|
||||
@result.remove item.name, quantityUsed
|
||||
@_pending.add item.name, quantityNeeded
|
||||
@need.add item.name, quantityNeeded
|
||||
@result.remove slug, quantityUsed
|
||||
@_pending.add stack.item, quantityNeeded
|
||||
@need.add stack.item, quantityNeeded
|
||||
|
||||
_processOutputItem: (item)->
|
||||
quantityMissing = @need.quantityOf item.name
|
||||
quantityUsed = Math.min quantityMissing, item.quantity
|
||||
quantityLeft = item.quantity - quantityUsed
|
||||
_processOutputStack: (stack)->
|
||||
slug = stack.item.slug
|
||||
quantityMissing = @need.quantityOf slug
|
||||
quantityUsed = Math.min quantityMissing, stack.quantity
|
||||
quantityLeft = stack.quantity - quantityUsed
|
||||
logger.verbose "processing output:#{stack.name},
|
||||
m:#{quantityMissing}, u:#{quantityUsed}, l:#{quantityLeft}"
|
||||
|
||||
@make.add item.name, item.quantity
|
||||
@need.remove item.name, quantityUsed
|
||||
@result.add item.name, quantityLeft
|
||||
@make.add stack.item, stack.quantity
|
||||
@need.remove slug, quantityUsed
|
||||
@result.add stack.item, quantityLeft
|
||||
|
||||
|
||||
_totalQuantityOf: (name)->
|
||||
return @result.quantityOf(name) - @need.quantityOf(name)
|
||||
_totalQuantityOf: (slug)->
|
||||
return @result.quantityOf(slug) - @need.quantityOf(slug)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
###
|
||||
# Crafting Guide - crafting_table.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - crafting_table.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
BaseModel = require './base_model'
|
||||
@@ -30,10 +30,10 @@ module.exports = class CraftingTable extends BaseModel
|
||||
@plan = null
|
||||
return
|
||||
|
||||
toolPhrase = if @includingTools then ' includingTools' else ''
|
||||
toolPhrase = if @includingTools then ' including tools' else ''
|
||||
logger.verbose "calculating build plan for #{@quantity} #{@name}#{toolPhrase} with inventory: #{@have}"
|
||||
|
||||
@modPack.enableBooksForRecipe @name
|
||||
@modPack.enableModsForItem @name
|
||||
|
||||
plan = new CraftingPlan @modPack, @includingTools
|
||||
plan.includingTools = @includingTools
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
BaseModel = require './base_model'
|
||||
{Event} = require '../constants'
|
||||
Item = require './item'
|
||||
Stack = require './stack'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -17,91 +17,91 @@ module.exports = class Inventory extends BaseModel
|
||||
super attributes, options
|
||||
@clear()
|
||||
|
||||
Object.defineProperty this, 'isEmpty', get:-> @_names.length is 0
|
||||
Object.defineProperty @prototype, 'isEmpty', get:-> @_slugs.length is 0
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
add: (name, quantity=1)->
|
||||
add: (item, quantity=1)->
|
||||
return if quantity is 0
|
||||
|
||||
item = @_items[name]
|
||||
if not item?
|
||||
item = new Item name:name, quantity:quantity
|
||||
@_items[name] = item
|
||||
@_names.push name
|
||||
@_names.sort()
|
||||
stack = @_stacks[item.slug]
|
||||
if not stack?
|
||||
stack = new Stack item:item, quantity:quantity
|
||||
@_stacks[item.slug] = stack
|
||||
@_slugs.push item.slug
|
||||
@_slugs.sort()
|
||||
else
|
||||
item.quantity += quantity
|
||||
stack.quantity += quantity
|
||||
|
||||
@trigger Event.add, this, name, quantity
|
||||
@trigger Event.add, this, item, quantity
|
||||
@trigger Event.change, this
|
||||
return this
|
||||
|
||||
addInventory: (inventory)->
|
||||
inventory.each (item)=> @add item.name, item.quantity
|
||||
inventory.each (stack)=> @add stack.item, stack.quantity
|
||||
return this
|
||||
|
||||
clear: ->
|
||||
@_items = {}
|
||||
@_names = []
|
||||
@_stacks = {}
|
||||
@_slugs = []
|
||||
|
||||
clone: ->
|
||||
inventory = new Inventory
|
||||
@each (item)-> inventory.add item.name, item.quantity
|
||||
@each (stack)-> inventory.add stack.item, stack.quantity
|
||||
return inventory
|
||||
|
||||
each: (onItem)->
|
||||
for name in @_names
|
||||
item = @_items[name]
|
||||
onItem item
|
||||
for slug in @_slugs
|
||||
stack = @_stacks[slug]
|
||||
onItem stack
|
||||
|
||||
hasAtLeast: (name, quantity=1)->
|
||||
hasAtLeast: (slug, quantity=1)->
|
||||
if quantity is 0 then return true
|
||||
|
||||
item = @_items[name]
|
||||
return false unless item?
|
||||
return item.quantity >= quantity
|
||||
stack = @_stacks[slug]
|
||||
return false unless stack?
|
||||
return stack.quantity >= quantity
|
||||
|
||||
pop: ->
|
||||
name = @_names.pop()
|
||||
return null unless name?
|
||||
slug = @_slugs.pop()
|
||||
return null unless slug?
|
||||
|
||||
item = @_items[name]
|
||||
delete @_items[name]
|
||||
stack = @_stacks[slug]
|
||||
delete @_stacks[slug]
|
||||
|
||||
@trigger Event.remove, this, item.name, item.quantity
|
||||
@trigger Event.remove, this, stack.item, stack.quantity
|
||||
@trigger Event.change, this
|
||||
return item
|
||||
return stack
|
||||
|
||||
quantityOf: (name)->
|
||||
item = @_items[name]
|
||||
return 0 unless item?
|
||||
return item.quantity
|
||||
quantityOf: (slug)->
|
||||
stack = @_stacks[slug]
|
||||
return 0 unless stack?
|
||||
return stack.quantity
|
||||
|
||||
remove: (name, quantity=1)->
|
||||
remove: (slug, quantity=1)->
|
||||
return if quantity is 0
|
||||
|
||||
item = @_items[name]
|
||||
if not item? then throw new Error "cannot remove #{name} since it is not in this inventory"
|
||||
if item.quantity < quantity
|
||||
throw new Error "cannot remove #{quantity} #{name} because there is only #{item.quantity} in this inventory"
|
||||
stack = @_stacks[slug]
|
||||
if not stack? then throw new Error "cannot remove #{slug} since it is not in this inventory"
|
||||
if stack.quantity < quantity
|
||||
throw new Error "cannot remove #{quantity} #{slug} because there is only #{stack.quantity} in this inventory"
|
||||
|
||||
item.quantity -= quantity
|
||||
if item.quantity is 0
|
||||
delete @_items[name]
|
||||
@_names = _(@_names).without name
|
||||
stack.quantity -= quantity
|
||||
if stack.quantity is 0
|
||||
delete @_stacks[slug]
|
||||
@_slugs = _(@_slugs).without slug
|
||||
|
||||
@trigger Event.remove, this, name, quantity
|
||||
@trigger Event.remove, this, slug, quantity
|
||||
@trigger Event.change, this
|
||||
return this
|
||||
|
||||
toList: ->
|
||||
result = []
|
||||
@each (item)->
|
||||
if item.quantity > 1
|
||||
result.push [item.quantity, item.name]
|
||||
@each (stack)->
|
||||
if stack.quantity > 1
|
||||
result.push [stack.quantity, stack.item.slug]
|
||||
else
|
||||
result.push item.name
|
||||
result.push stack.item.slug
|
||||
return result
|
||||
|
||||
# Object Overrides #############################################################################
|
||||
@@ -110,9 +110,9 @@ module.exports = class Inventory extends BaseModel
|
||||
result = [@constructor.name, " (", @cid, ") { items: ["]
|
||||
|
||||
needsDelimiter = false
|
||||
@each (item)->
|
||||
@each (stack)->
|
||||
if needsDelimiter then result.push ', '
|
||||
result.push item.toString()
|
||||
result.push stack.toString()
|
||||
needsDelimiter = true
|
||||
result.push ']'
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
###
|
||||
# Crafting Guide - inventory_parser.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - inventory_parser.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
Inventory = require './inventory'
|
||||
Item = require './item'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -24,6 +25,6 @@ module.exports = class InventoryParser
|
||||
quantity = if match? then parseInt(match[1]) else 1
|
||||
|
||||
if name.length > 0
|
||||
inventory.add name, quantity
|
||||
inventory.add new Item(name:name), quantity
|
||||
|
||||
return inventory
|
||||
|
||||
@@ -14,38 +14,48 @@ module.exports = class Item extends BaseModel
|
||||
@DEFAULT_STACK_SIZE = 64
|
||||
|
||||
constructor: (attributes={}, options={})->
|
||||
attributes.name ?= ''
|
||||
attributes.quantity ?= 1
|
||||
attributes.stackSize ?= Item.DEFAULT_STACK_SIZE
|
||||
if not attributes.name? then throw new Error 'attributes.name is required'
|
||||
|
||||
attributes.isGatherable ?= false
|
||||
attributes.recipes ?= []
|
||||
attributes.slug ?= _.slugify attributes.name
|
||||
attributes.stackSize ?= Item.DEFAULT_STACK_SIZE
|
||||
super attributes, options
|
||||
|
||||
Object.defineProperty this, 'stackQuantity', get:@getStackQuantity
|
||||
Object.defineProperty @prototype, 'isCraftable', get:-> @recipes.length > 0
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
canMerge: (item)->
|
||||
return item.name is @name
|
||||
addRecipe: (recipe)->
|
||||
output = recipe.output[0].item
|
||||
if output isnt this then throw new Error "invalid recipe for #{@name} because it makes a #{output.name}"
|
||||
|
||||
merge: (item)->
|
||||
if not @canMerge(item) then throw new Error "cannot merge #{item} into #{this}"
|
||||
@quantity += item.quantity
|
||||
@recipes.push recipe
|
||||
|
||||
# Property Methods #############################################################################
|
||||
|
||||
getStackQuantity: ->
|
||||
count = 0
|
||||
extra = @quantity
|
||||
while extra > @stackSize
|
||||
extra -= @stackSize
|
||||
count += 1
|
||||
|
||||
return count:count, extra:extra
|
||||
compareTo: (that)->
|
||||
if this.name isnt that.name
|
||||
return if this.name < that.name then -1 else +1
|
||||
return 0
|
||||
|
||||
# Object Overrides #############################################################################
|
||||
|
||||
toString: ->
|
||||
result = "#{@constructor.name} (#{@cid}) { name:\"#{@name}\", quantity:#{@quantity}"
|
||||
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 @stackSize isnt Item.DEFAULT_STACK_SIZE
|
||||
result += ", stackSize:#{@stackSize}"
|
||||
result += ' }'
|
||||
return result
|
||||
result.push ', stackSize:'; result.push @stackSize
|
||||
|
||||
if @recipes.length > 0
|
||||
result.push ', recipes:'
|
||||
result.push @recipes.length
|
||||
result.push ' items'
|
||||
|
||||
result.push '}'
|
||||
return result.join ''
|
||||
|
||||
@@ -9,32 +9,43 @@ BaseModel = require './base_model'
|
||||
{Event} = require '../constants'
|
||||
ModVersionParser = require './mod_version_parser'
|
||||
{RequiredMods} = require '../constants'
|
||||
util = require 'util'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
module.exports = class ModPack extends BaseModel
|
||||
|
||||
constructor: (attributes={}, options={})->
|
||||
attributes.books ?= []
|
||||
attributes.modVersions ?= []
|
||||
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
|
||||
enableModsForItem: (name)->
|
||||
for modVersion in @modVersions
|
||||
continue if modVersion.enabled
|
||||
if modVersion.hasRecipe name
|
||||
modVersion.enabled = true
|
||||
|
||||
gatherNames: (options={})->
|
||||
findItemByName: (name, options={})->
|
||||
options.includeDisabled ?= false
|
||||
|
||||
for modVersion in @modVersions
|
||||
continue unless modVersion.enabled or options.includeDisabled
|
||||
item = modVersion.findItemByName name
|
||||
return item if item?
|
||||
|
||||
return null
|
||||
|
||||
gatherRecipeNames: (options={})->
|
||||
options.includeDisabled ?= false
|
||||
|
||||
nameData = {}
|
||||
for book in @books
|
||||
continue unless book.enabled or options.includeDisabled
|
||||
book.gatherNames nameData
|
||||
for modVersion in @modVersions
|
||||
continue unless modVersion.enabled or options.includeDisabled
|
||||
modVersion.gatherRecipeNames nameData
|
||||
|
||||
result = []
|
||||
names = _.keys(nameData).sort()
|
||||
@@ -42,76 +53,56 @@ module.exports = class ModPack extends BaseModel
|
||||
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
|
||||
for modVersion in @modVersions
|
||||
continue unless modVersion.enabled or options.includeDisabled
|
||||
return true if modVersion.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)->
|
||||
loadModVersion: (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)
|
||||
resolve @onModVersionLoaded(url, data, status, xhr)
|
||||
error: (xhr, status, error)=>
|
||||
reject @onBookLoadFailed(url, error, status, xhr)
|
||||
reject @onModVersionLoadFailed(url, error, status, xhr)
|
||||
|
||||
loadBookData: (data)->
|
||||
book = @_parser.parse data
|
||||
@books.push book
|
||||
@_sortBooks()
|
||||
book.on Event.change, => @trigger Event.change, this
|
||||
loadModVersionData: (data)->
|
||||
modVersion = @_parser.parse data
|
||||
@modVersions.push modVersion
|
||||
@modVersions.sort (a, b)-> a.compareTo b
|
||||
modVersion.on Event.change, => @trigger Event.change, this
|
||||
|
||||
return book
|
||||
return modVersion
|
||||
|
||||
loadAllBooks: (urlList)->
|
||||
promises = (@loadBook(url) for url in urlList)
|
||||
loadAllModVersions: (urlList)->
|
||||
promises = (@loadModVersion(url) for url in urlList)
|
||||
return w.settle promises
|
||||
|
||||
# Event Methods ################################################################################
|
||||
|
||||
onBookLoaded: (url, data, status, xhr)->
|
||||
onModVersionLoaded: (url, data, status, xhr)->
|
||||
try
|
||||
book = @loadBookData data
|
||||
modVersion = @loadModVersionData data
|
||||
|
||||
logger.info "loaded recipe book from #{url}: #{book}"
|
||||
@trigger Event.load.succeeded, this, book
|
||||
logger.info "loaded ModVersion from #{url}: #{modVersion}"
|
||||
@trigger Event.load.succeeded, this, modVersion
|
||||
@trigger Event.load.finished, this
|
||||
@trigger Event.change, this
|
||||
|
||||
return book
|
||||
return modVersion
|
||||
catch e
|
||||
@onBookLoadFailed url, e, status, xhr
|
||||
@onModVersionLoadFailed url, e, status, xhr
|
||||
|
||||
onBookLoadFailed: (url, error, status, xhr)->
|
||||
onModVersionLoadFailed: (url, error, status, xhr)->
|
||||
message = if error.stack? then error.stack else error
|
||||
logger.error "failed to load recipe book from #{url}: #{message}"
|
||||
logger.error "failed to load ModVersion from #{url}: #{message}"
|
||||
@trigger Event.load.failed, this, error.message
|
||||
@trigger Event.load.finished, this
|
||||
return error
|
||||
@@ -119,20 +110,4 @@ module.exports = class ModPack extends BaseModel
|
||||
# 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
|
||||
return "ModPack (#{@cid}) {modVersions:#{@modVersions.length} items}"
|
||||
|
||||
@@ -17,34 +17,48 @@ module.exports = class ModVersion extends BaseModel
|
||||
if _.isEmpty(attributes.modVersion) then throw new Error 'modVersion cannot be empty'
|
||||
|
||||
attributes.description ?= ''
|
||||
attributes.rawMaterials ?= []
|
||||
attributes.recipes ?= []
|
||||
attributes.items ?= {}
|
||||
attributes.enabled ?= attributes.modName in RequiredMods
|
||||
super attributes, options
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
gatherNames: (result)->
|
||||
for recipe in @recipes
|
||||
continue if result[recipe.name]
|
||||
result[recipe.name] = value:recipe.name, label:"#{recipe.name} (from #{@modName} #{@modVersion})"
|
||||
addItem: (item)->
|
||||
if @items[item.slug]? then throw new Error "duplicate item for #{item.slug}"
|
||||
@items[item.slug] = item
|
||||
return this
|
||||
|
||||
compareTo: (that)->
|
||||
if this.modName is that.modName then return 0
|
||||
|
||||
thisRequired = this.modName in RequiredMods
|
||||
thatRequired = that.modName in RequiredMods
|
||||
|
||||
if thisRequired and thatRequired
|
||||
return if this.modName < that.modName then -1 else +1
|
||||
else if thisRequired
|
||||
return -1
|
||||
else if thatRequired
|
||||
return +1
|
||||
else
|
||||
return if this.modName < that.modName then -1 else +1
|
||||
|
||||
findItemByName: (name)->
|
||||
slug = _.slugify name
|
||||
return @items[slug]
|
||||
|
||||
gatherRecipeNames: (result={})->
|
||||
for slug, item of @items
|
||||
continue if result[item.slug]
|
||||
continue unless item.isCraftable
|
||||
result[item.slug] = value:item.name, label:"#{item.name} (from #{@modName} #{@modVersion})"
|
||||
|
||||
return result
|
||||
|
||||
gatherRecipes: (name, result)->
|
||||
for recipe in @recipes
|
||||
if recipe.name is name
|
||||
result.push recipe
|
||||
|
||||
return result
|
||||
|
||||
isRawMaterial: (name)->
|
||||
return name in @rawMaterials
|
||||
|
||||
hasRecipe: (name)->
|
||||
for recipe in @recipes
|
||||
return true if recipe.name is name
|
||||
return false
|
||||
item = @findItemByName name
|
||||
return false unless item?
|
||||
return item.recipes.length > 0
|
||||
|
||||
# Object Overrides #############################################################################
|
||||
|
||||
@@ -53,4 +67,4 @@ module.exports = class ModVersion extends BaseModel
|
||||
enabled:#{@enabled},
|
||||
modName:#{@modName},
|
||||
modVersion:#{@modVersion},
|
||||
recipes:#{@recipes.length} items}"
|
||||
items:#{_.keys(@items).length} items}"
|
||||
|
||||
@@ -6,8 +6,10 @@ All rights reserved.
|
||||
###
|
||||
|
||||
Item = require './item'
|
||||
Recipe = require './recipe'
|
||||
ModVersion = require './mod_version'
|
||||
Recipe = require './recipe'
|
||||
Stack = require './stack'
|
||||
util = require 'util'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -20,21 +22,21 @@ module.exports = class ModVersionParser
|
||||
'1': new V1
|
||||
|
||||
parse: (data)->
|
||||
if not data? then throw new Error 'recipe book data is missing'
|
||||
if not data? then throw new Error 'mod description data is missing'
|
||||
if not data.version? then throw new Error 'version is required'
|
||||
|
||||
parser = @_parsers["#{data.version}"]
|
||||
if not parser? then throw new Error "cannot parse version #{data.version} recipe books"
|
||||
if not parser? then throw new Error "cannot parse version #{data.version} mod descriptions"
|
||||
|
||||
return parser.parse data
|
||||
|
||||
unparse: (ModVersion, version=ModVersionParser.CURRENT_VERSION)->
|
||||
if not ModVersion? then throw new Error 'recipe book is required'
|
||||
unparse: (modVersion, version=ModVersionParser.CURRENT_VERSION)->
|
||||
if not modVersion? then throw new Error 'modVersion is required'
|
||||
|
||||
parser = @_parsers["#{version}"]
|
||||
if not parser? then throw new Error "version #{version} is not supported"
|
||||
|
||||
return parser.unparse ModVersion
|
||||
return parser.unparse modVersion
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -46,62 +48,77 @@ module.exports.V1 = class V1
|
||||
parse: (data)->
|
||||
return @_parseModVersion data
|
||||
|
||||
unparse: (ModVersion)->
|
||||
return @_unparseModVersion ModVersion
|
||||
unparse: (modVersion)->
|
||||
return @_unparseModVersion modVersion
|
||||
|
||||
# Private Methods ##############################################################################
|
||||
|
||||
_parseModVersion: (data)->
|
||||
if not data? then throw new Error 'recipe book data is missing'
|
||||
if not data.version? then throw new Error 'version is required'
|
||||
if not data.mod_name? then throw new Error 'mod_name is required'
|
||||
if not data.mod_version? then throw new Error 'mod_version is required'
|
||||
if not _.isArray(data.recipes) then throw new Error 'recipes must be an array'
|
||||
|
||||
book = new ModVersion version:data.version, modName:data.mod_name, modVersion:data.mod_version
|
||||
book.description = data.description or ''
|
||||
|
||||
book.rawMaterials = data.raw_materials or []
|
||||
|
||||
for index in [0...data.recipes.length]
|
||||
@_errorLocation = "recipe #{index + 1}"
|
||||
recipeData = data.recipes[index]
|
||||
recipe = @_parseRecipe recipeData
|
||||
recipe._originalIndex = index
|
||||
book.recipes.push recipe
|
||||
|
||||
return book
|
||||
|
||||
_parseRecipe: (data, options={})->
|
||||
if not data? then throw new Error "recipe data is missing for #{@_errorLocation}"
|
||||
if not data.output? then throw new Error "#{@_errorLocation} is missing output"
|
||||
|
||||
output = @_parseItemList data.output, field:'output', canBeEmpty:false
|
||||
@_errorLocation = "recipe for #{output[0].name}"
|
||||
|
||||
if not data.input? then throw new Error "#{@_errorLocation} is missing input"
|
||||
data.tools ?= []
|
||||
|
||||
input = @_parseItemList data.input, field:'input', canBeEmpty:true
|
||||
tools = @_parseItemList data.tools, field:'tools', canBeEmpty:true
|
||||
|
||||
return new Recipe input:input, output:output, tools:tools
|
||||
_findOrCreateItem: (name)->
|
||||
item = @modVersion.findItemByName name
|
||||
if not item?
|
||||
item = new Item name:name
|
||||
@modVersion.addItem item
|
||||
return item
|
||||
|
||||
_parseItemList: (data, options={})->
|
||||
if not data? then throw new Error "#{@_errorLocation} must have an #{options.field} field"
|
||||
|
||||
if not _.isArray(data) then data = [data]
|
||||
if data.length is 0 and not options.canBeEmpty
|
||||
throw new Error "#{options.field} for #{@_errorLocation} cannot be empty"
|
||||
|
||||
result = []
|
||||
for index in [0...data.length]
|
||||
itemData = data[index]
|
||||
result.push @_parseItem itemData, field:options.field, index:index
|
||||
|
||||
for name in data
|
||||
item = @_findOrCreateItem name
|
||||
result.push item
|
||||
return result
|
||||
|
||||
_parseItem: (data, options={})->
|
||||
_parseModVersion: (data)->
|
||||
if not data? then throw new Error 'mod description data is missing'
|
||||
if not data.version? then throw new Error 'version is required'
|
||||
if not data.mod_name? then throw new Error 'mod_name is required'
|
||||
if not data.mod_version? then throw new Error 'mod_version is required'
|
||||
if not _.isArray(data.recipes) then throw new Error 'recipes must be an array'
|
||||
|
||||
@modVersion = modVersion = new ModVersion modName:data.mod_name, modVersion:data.mod_version
|
||||
modVersion.description = data.description or ''
|
||||
|
||||
@_parseRawMaterials data.raw_materials
|
||||
|
||||
for index in [0...data.recipes.length]
|
||||
@_errorLocation = "recipe #{index + 1}"
|
||||
recipeData = data.recipes[index]
|
||||
@_parseRecipe recipeData
|
||||
|
||||
@modVersion = null
|
||||
return modVersion
|
||||
|
||||
_parseRawMaterials: (data, options={})->
|
||||
return unless data? and data.length > 0
|
||||
|
||||
results = []
|
||||
for name in data
|
||||
item = @_findOrCreateItem name
|
||||
item.isGatherable = true
|
||||
results.push item
|
||||
|
||||
_parseRecipe: (data, options={})->
|
||||
if not data? then throw new Error "recipe data is missing for #{@_errorLocation}"
|
||||
if not data.output? then throw new Error "#{@_errorLocation} is missing output"
|
||||
|
||||
output = @_parseStackList data.output, field:'output', canBeEmpty:false
|
||||
item = output[0].item
|
||||
@_errorLocation = "recipe for #{item.name}"
|
||||
|
||||
if not data.input? then throw new Error "#{@_errorLocation} is missing input"
|
||||
data.tools ?= []
|
||||
|
||||
input = @_parseStackList data.input, field:'input', canBeEmpty:true
|
||||
tools = @_parseItemList data.tools, field:'tools'
|
||||
|
||||
recipe = new Recipe input:input, output:output, tools:tools
|
||||
item.addRecipe recipe
|
||||
return recipe
|
||||
|
||||
_parseStack: (data, options={})->
|
||||
errorBase = "#{options.field} element #{options.index} for #{@_errorLocation}"
|
||||
if not data? then throw new Error "#{errorBase} is missing"
|
||||
|
||||
@@ -112,42 +129,67 @@ module.exports.V1 = class V1
|
||||
if data.length isnt 2 then throw new Error "#{errorBase} must have at least one element"
|
||||
if not _.isNumber(data[0]) then throw new Error "#{errorBase} must start with a number"
|
||||
|
||||
return new Item quantity:data[0], name:data[1]
|
||||
item = @_findOrCreateItem data[1]
|
||||
return new Stack item:item, quantity:data[0]
|
||||
|
||||
_unparseModVersion: (ModVersion)->
|
||||
_parseStackList: (data, options={})->
|
||||
if not data? then throw new Error "#{@_errorLocation} must have an #{options.field} field"
|
||||
|
||||
if not _.isArray(data) then data = [data]
|
||||
if data.length is 0 and not options.canBeEmpty
|
||||
throw new Error "#{options.field} for #{@_errorLocation} cannot be empty"
|
||||
|
||||
result = []
|
||||
for index in [0...data.length]
|
||||
stackData = data[index]
|
||||
result.push @_parseStack stackData, field:options.field, index:index
|
||||
|
||||
return result
|
||||
|
||||
_parseItemList: (data, options={})->
|
||||
if not data? then throw new Error "#{@_errorLocation} must have an #{options.field} field"
|
||||
|
||||
if not _.isArray(data) then data = [data]
|
||||
|
||||
result = []
|
||||
for name in data
|
||||
item = @_findOrCreateItem name
|
||||
result.push item
|
||||
return result
|
||||
|
||||
# Un-parsing Methods ###########################################################################
|
||||
|
||||
_unparseModVersion: (modVersion)->
|
||||
result = []
|
||||
result.push '{\n'
|
||||
result.push ' "version": 1,\n'
|
||||
result.push ' "mod_name": "' + ModVersion.modName + '",\n'
|
||||
result.push ' "mod_version": "' + ModVersion.modVersion + '",\n'
|
||||
if ModVersion.description.length > 0
|
||||
result.push ' "description": "' + ModVersion.description + '",\n'
|
||||
result.push ' "mod_name": "' + modVersion.modName + '",\n'
|
||||
result.push ' "mod_version": "' + modVersion.modVersion + '",\n'
|
||||
if modVersion.description.length > 0
|
||||
result.push ' "description": "' + modVersion.description + '",\n'
|
||||
|
||||
if ModVersion.rawMaterials.length > 0
|
||||
rawMaterials = (item.name for slug, item of modVersion.items when item.isGatherable)
|
||||
rawMaterials.sort()
|
||||
if rawMaterials.length > 0
|
||||
result.push ' "raw_materials": [\n'
|
||||
firstItem = true
|
||||
materials = ModVersion.rawMaterials.slice()
|
||||
materials.sort()
|
||||
for material in materials
|
||||
for material in rawMaterials
|
||||
if not firstItem then result.push ',\n'
|
||||
result.push ' "' + material + '"'
|
||||
firstItem = false
|
||||
result.push '\n ],\n'
|
||||
|
||||
result.push ' "recipes": [\n'
|
||||
recipes = ModVersion.recipes.slice()
|
||||
recipes.sort (a, b)->
|
||||
if a.name isnt b.name
|
||||
return if a.name < b.name then -1 else +1
|
||||
if a._originalIndex isnt b._originalIndex
|
||||
return if a._originalIndex < b._originalIndex then -1 else +1
|
||||
return 0
|
||||
|
||||
items = (item for slug, item of modVersion.items when item.isCraftable)
|
||||
items.sort (a, b)-> a.compareTo b
|
||||
|
||||
firstItem = true
|
||||
for recipe in recipes
|
||||
result.push if firstItem then ' {\n' else ' }, {\n'
|
||||
@_unparseRecipe recipe, result
|
||||
firstItem = false
|
||||
for item in items
|
||||
for recipe in item.recipes
|
||||
result.push if firstItem then ' {\n' else ' }, {\n'
|
||||
@_unparseRecipe recipe, result
|
||||
firstItem = false
|
||||
result.push ' }\n'
|
||||
|
||||
result.push ' ]\n'
|
||||
@@ -157,12 +199,12 @@ module.exports.V1 = class V1
|
||||
|
||||
_unparseRecipe: (recipe, result=[])->
|
||||
result.push ' "output": '
|
||||
@_unparseItemList recipe.output, result, sort:false
|
||||
@_unparseStackList recipe.output, result, sort:false
|
||||
|
||||
if recipe.input.length > 0
|
||||
result.push ',\n'
|
||||
result.push ' "input": '
|
||||
@_unparseItemList recipe.input, result
|
||||
@_unparseStackList recipe.input, result
|
||||
|
||||
if recipe.tools.length > 0
|
||||
result.push ',\n'
|
||||
@@ -172,38 +214,51 @@ module.exports.V1 = class V1
|
||||
result.push '\n'
|
||||
return result
|
||||
|
||||
_unparseItemList: (itemList, result, options={})->
|
||||
_unparseStackList: (stackList, result, options={})->
|
||||
options.sort ?= true
|
||||
|
||||
if itemList.length is 0
|
||||
if stackList.length is 0
|
||||
result.push '[]'
|
||||
else if itemList.length is 1
|
||||
item = itemList[0]
|
||||
if item.quantity is 1
|
||||
result.push '"' + item.name + '"'
|
||||
else if stackList.length is 1
|
||||
stack = stackList[0]
|
||||
if stack.quantity is 1
|
||||
result.push '"' + stack.name + '"'
|
||||
else
|
||||
result.push '[[' + item.quantity + ', "' + item.name + '"]]'
|
||||
result.push '[[' + stack.quantity + ', "' + stack.name + '"]]'
|
||||
else
|
||||
result.push '['
|
||||
|
||||
items = itemList.slice()
|
||||
stacks = stackList.slice()
|
||||
if options.sort
|
||||
items.sort (a, b)->
|
||||
stacks.sort (a, b)->
|
||||
if a.quantity isnt b.quantity
|
||||
return if a.quantity > b.quantity then -1 else +1
|
||||
if a.name isnt b.name
|
||||
return if a.name < b.name then -1 else +1
|
||||
if a.item.name isnt b.item.name
|
||||
return if a.item.name < b.item.name then -1 else +1
|
||||
return 0
|
||||
|
||||
firstItem = true
|
||||
for item in items
|
||||
for stack in stacks
|
||||
result.push ', ' if not firstItem
|
||||
if item.quantity is 1
|
||||
result.push '"' + item.name + '"'
|
||||
if stack.quantity is 1
|
||||
result.push '"' + stack.name + '"'
|
||||
else
|
||||
result.push '[' + item.quantity + ', "' + item.name + '"]'
|
||||
result.push '[' + stack.quantity + ', "' + stack.name + '"]'
|
||||
firstItem = false
|
||||
|
||||
result.push ']'
|
||||
|
||||
return result
|
||||
|
||||
_unparseItemList: (itemList, result)->
|
||||
if itemList.length is 1
|
||||
result.push '"'; result.push itemList[0].name; result.push '"'
|
||||
else
|
||||
firstItem = true
|
||||
for item in itemList
|
||||
result.push if firstItem then '["' else '", "'
|
||||
result.push item.name
|
||||
firstItem = false
|
||||
result.push '"]'
|
||||
|
||||
return result
|
||||
|
||||
@@ -12,26 +12,29 @@ BaseModel = require './base_model'
|
||||
module.exports = class Recipe extends BaseModel
|
||||
|
||||
constructor: (attributes={}, options={})->
|
||||
if not attributes.output? then throw new Error "attributes.output is required"
|
||||
attributes.input ?= []
|
||||
attributes.tools ?= []
|
||||
if not attributes.input? then throw new Error 'attributes.input is required'
|
||||
if not attributes.output? then throw new Error 'attributes.output is required'
|
||||
|
||||
attributes.pattern ?= (i for i in [0...attributes.input.length]).join('')
|
||||
attributes.tools ?= []
|
||||
super attributes, options
|
||||
|
||||
Object.defineProperty @prototype, 'name', get:-> @output[0].name
|
||||
Object.defineProperty @prototype, 'name', get:-> @output[0].item.name
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
make: (inventory, missing)->
|
||||
for item in @input
|
||||
needed = item.quantity
|
||||
for stack in @input
|
||||
item = stack.item
|
||||
needed = stack.quantity
|
||||
while needed > 0
|
||||
if inventory.hasAtLeast item.name
|
||||
inventory.remove item.name
|
||||
if inventory.hasAtLeast item.slug
|
||||
inventory.remove item.slug
|
||||
else
|
||||
missing.add item.name
|
||||
missing.add item.slug
|
||||
|
||||
for item in @output
|
||||
inventory.add item.name, item.quantity
|
||||
for stack in @output
|
||||
inventory.add stack.item, stack.quantity
|
||||
|
||||
return this
|
||||
|
||||
@@ -42,26 +45,26 @@ module.exports = class Recipe extends BaseModel
|
||||
|
||||
result.push ", input:["
|
||||
needsDelimiter = false
|
||||
for inputItem in @input
|
||||
for stack in @input
|
||||
if needsDelimiter then result.push ', '
|
||||
result.push inputItem.toString()
|
||||
result.push stack.toString()
|
||||
needsDelimiter = true
|
||||
result.push ']'
|
||||
|
||||
result.push ", output:["
|
||||
needsDelimiter = false
|
||||
for outputItem in @output
|
||||
for stack in @output
|
||||
if needsDelimiter then result.push ', '
|
||||
result.push outputItem.toString()
|
||||
result.push stack.toString()
|
||||
needsDelimiter = true
|
||||
result.push ']'
|
||||
|
||||
if @tools.length > 0
|
||||
result.push ", tools:["
|
||||
needsDelimiter = false
|
||||
for tool in @tools
|
||||
for stack in @tools
|
||||
if needsDelimiter then result.push ', '
|
||||
result.push tool.toString()
|
||||
result.push stack.toString()
|
||||
needsDelimiter = true
|
||||
result.push ']'
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
###
|
||||
Crafting Guide - stack.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
module.exports = class Stack
|
||||
|
||||
constructor: (attributes={})->
|
||||
if not attributes.item? then throw new Error 'item is required'
|
||||
attributes.quantity ?= 1
|
||||
|
||||
@item = attributes.item
|
||||
@quantity = attributes.quantity
|
||||
|
||||
Object.defineProperty @prototype, 'name', get:-> @item?.name
|
||||
|
||||
Object.defineProperty @prototype, 'stackQuantity', get:@getStackQuantity
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
canMerge: (stack)->
|
||||
return @item.slug is stack.item.slug
|
||||
|
||||
merge: (stack)->
|
||||
if not @canMerge stack
|
||||
throw new Error "this stack of #{@item.name} cannot merge a stack of #{@stack.name}"
|
||||
|
||||
@quantity += stack.quantity
|
||||
return this
|
||||
|
||||
# Property Methods #############################################################################
|
||||
|
||||
getStackQuantity: ->
|
||||
count = 0
|
||||
extra = @quantity
|
||||
while extra > @item.stackSize
|
||||
extra -= @item.stackSize
|
||||
count += 1
|
||||
|
||||
return count:count, extra:extra
|
||||
|
||||
# Object Overrides #############################################################################
|
||||
|
||||
toString: ->
|
||||
return "#{@quantity} #{@item.name}"
|
||||
@@ -0,0 +1,18 @@
|
||||
###
|
||||
Crafting Guide - underscore.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
_.mixin
|
||||
|
||||
slugify: (text)->
|
||||
return null unless text?
|
||||
|
||||
result = text.toLowerCase()
|
||||
result = result.replace /[^a-zA-Z0-9_]/g, '_'
|
||||
result = result.replace /_+/, '_'
|
||||
result = result.replace /_$/, ''
|
||||
result = result.replace /^_/, ''
|
||||
return result
|
||||
Reference in New Issue
Block a user