Massive refactoring to clean up models

* Tweak style of quantity label in output box to be more visible
* Move data files for all mods under a directory for the specific
  version of the mod
* Add a `silent` property to the base model to prevent models which
  shouldn't be observed from emitting events
* Change the term "itemSlug" to just "slug" across the board
* Change all models so that they don't require their parent in their
  constructors and so that they don't automatically add themselves to
  their parents lists
* Remove a bunch of unnecessary event triggering
* Tighten up access to various lists of children across all models
* Refactor the guts of the V2 parser into an abstract base class so
  it can be used for other parsers in the future
* Remove a number of unused methods
This commit is contained in:
Andrew Miner
2015-01-17 11:49:07 -08:00
parent bb959264ee
commit 83868e2760
34 changed files with 530 additions and 519 deletions
+14 -49
View File
@@ -13,50 +13,36 @@ Stack = require './stack'
module.exports = class Recipe extends BaseModel
constructor: (attributes={}, options={})->
if not attributes.input? then throw new Error 'attributes.input is required'
if not attributes.item? then throw new Error 'attributes.item is required'
if attributes.item?
attributes.name = attributes.item.name
attributes.slug = attributes.item.slug
attributes.output ?= [new Stack slug:item.slug, quantity:1]
attributes.output ?= [new Stack itemSlug:attributes.item.slug]
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.slug ?= attributes.output[0].slug
attributes.tools ?= []
super attributes, options
@item.addRecipe this
Object.defineProperties this,
'defaultPattern': { get: -> @_computeDefaultPattern() }
'name': { get: -> @item.name }
'slug': { get: -> @item.slug }
# Public Methods ###############################################################################
getItemSlugAt: (index)->
getItemSlugAt: (patternSlot)->
pattern = if @pattern? then @pattern else @_computeDefaultPattern()
trueIndex = 0:0, 1:1, 2:2, 3:4, 4:5, 5:6, 6:8, 7:9, 8:10
patternDigit = pattern[trueIndex[index]]
patternDigit = pattern[trueIndex[patternSlot]]
return null unless patternDigit?
return null unless patternDigit.match /[0-9]/
stack = @input[parseInt(patternDigit)]
return null unless stack?
return stack.itemSlug
make: (inventory, missing)->
for stack in @input
itemSlug = stack.itemSlug
needed = stack.quantity
while needed > 0
if inventory.hasAtLeast itemSlug
inventory.remove itemSlug
else
missing.add itemSlug
for stack in @output
inventory.add stack.itemSlug, stack.quantity
return this
return stack.slug
# Object Overrides #############################################################################
@@ -109,24 +95,3 @@ module.exports = class Recipe extends BaseModel
pattern = array.join ''
pattern = pattern.replace /(...)(...)(...)/, '$1 $2 $3'
return pattern
_computeDefaultPattern: ->
itemCount = @input.length
slotCount = _.reduce @input, ((total, stack)-> total + stack.quantity), 0
return '... .0. ...' if itemCount is 1 and slotCount is 1
return '00. 00. ...' if itemCount is 1 and slotCount is 4
return '000 000 000' if itemCount is 1 and slotCount is 9
result = ['.', '.', '.', '.', '.', '.', '.', '.', '.']
indexes = [4, 7, 1, 3, 5, 6, 8, 0, 2]
for i in [0...@input.length]
stack = @input[i]
for j in [0...stack.quantity]
index = indexes.shift()
result[index] = "#{i}"
pattern = result.join ''
pattern = pattern.replace /(...)(...)(...)/, '$1 $2 $3'
return pattern