Files
website/src/scripts/models/recipe.coffee
T
Andrew Miner 83868e2760 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
2015-01-17 11:49:07 -08:00

98 lines
3.2 KiB
CoffeeScript

###
Crafting Guide - recipe.coffee
Copyright (c) 2014-2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
Stack = require './stack'
########################################################################################################################
module.exports = class Recipe extends BaseModel
constructor: (attributes={}, options={})->
if attributes.item?
attributes.name = attributes.item.name
attributes.slug = attributes.item.slug
attributes.output ?= [new Stack slug:item.slug, 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.slug ?= attributes.output[0].slug
attributes.tools ?= []
super attributes, options
# Public Methods ###############################################################################
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[patternSlot]]
return null unless patternDigit?
return null unless patternDigit.match /[0-9]/
stack = @input[parseInt(patternDigit)]
return null unless stack?
return stack.slug
# Object Overrides #############################################################################
toString: ->
result = [@constructor.name, " (", @cid, ") { name:", @name]
result.push ", input:["
needsDelimiter = false
for stack in @input
if needsDelimiter then result.push ', '
result.push stack.toString()
needsDelimiter = true
result.push ']'
result.push ", output:["
needsDelimiter = false
for stack in @output
if needsDelimiter then result.push ', '
result.push stack.toString()
needsDelimiter = true
result.push ']'
if @tools.length > 0
result.push ", tools:["
needsDelimiter = false
for stack in @tools
if needsDelimiter then result.push ', '
result.push stack.toString()
needsDelimiter = true
result.push ']'
result.push '}'
return result.join ''
# Private Methods ##############################################################################
_parsePattern: (pattern)->
return unless pattern?
pattern = pattern.replace /\ /g, ''
return if pattern.length is 0
pattern = pattern.replace /[^0-9]/g, '.'
array = pattern.split ''
array = array[0...9]
while array.length isnt 9
array.push '.'
pattern = array.join ''
pattern = pattern.replace /(...)(...)(...)/, '$1 $2 $3'
return pattern