Update Minecraft recipebook with more recipes
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
###
|
||||
# Crafting Guide - constants.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - constants.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
exports.DefaultBookUrls = DefaultBookUrls = [
|
||||
@@ -31,6 +31,9 @@ Event.load.failed = 'load:failed' # controller, error message
|
||||
Event.load.finished = 'load:finished' # controller
|
||||
Event.remove = 'remove' # collection, item...
|
||||
|
||||
exports.Key = Key = {}
|
||||
Key.Return = 13
|
||||
|
||||
exports.Opacity = Opacity = {}
|
||||
Opacity.hidden = 1e-6
|
||||
Opacity.shown = 1
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
BaseController = require './base_controller'
|
||||
{Event} = require '../constants'
|
||||
InventoryParser = require '../models/inventory_parser'
|
||||
{Key} = require '../constants'
|
||||
url = require 'url'
|
||||
{UrlParam} = require '../constants'
|
||||
|
||||
@@ -52,6 +53,10 @@ module.exports = class CraftingTableController extends BaseController
|
||||
return unless @rendered
|
||||
@$nameField.autocomplete 'search'
|
||||
|
||||
onNameFieldKeyPress: (event)->
|
||||
if event.which is Key.Return
|
||||
@$nameField.autocomplete('close')
|
||||
|
||||
onQuantityFieldChanged: ->
|
||||
return unless @rendered
|
||||
@model.quantity = parseInt @$quantityField.find(":selected").val()
|
||||
@@ -102,6 +107,7 @@ module.exports = class CraftingTableController extends BaseController
|
||||
'input textarea[name="have"]': 'onHaveFieldChanged'
|
||||
'focus input[name="name"]': 'onNameFieldFocused'
|
||||
'input input[name="name"]': 'onNameFieldChanged'
|
||||
'keypress input[name="name"]': 'onNameFieldKeyPress'
|
||||
'input select[name="quantity"]': 'onQuantityFieldChanged'
|
||||
'change input[name="including_tools"]': 'onIncludingToolsFieldChanged'
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
###
|
||||
# crafting_guide - base_model.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
crafting_guide - base_model.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -19,12 +19,12 @@ module.exports = class CraftingPlan
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
clear: ->
|
||||
@make = new Inventory
|
||||
@need = new Inventory
|
||||
@result = new Inventory
|
||||
@steps = []
|
||||
@_expected = new Inventory
|
||||
@_pending = null
|
||||
@make = new Inventory
|
||||
@need = new Inventory
|
||||
@result = new Inventory
|
||||
@steps = []
|
||||
@_expected = new Inventory
|
||||
@_pending = null
|
||||
|
||||
return this
|
||||
|
||||
@@ -49,15 +49,21 @@ module.exports = class CraftingPlan
|
||||
_processPending: ->
|
||||
targetItem = @_pending.pop()
|
||||
return unless targetItem?
|
||||
logger.debug "Processing targetItem: #{targetItem}"
|
||||
|
||||
return if @catalog.isRawMaterial targetItem.name
|
||||
|
||||
recipes = @catalog.gatherRecipes targetItem.name
|
||||
return if not recipes.length > 0
|
||||
|
||||
recipe = recipes[0]
|
||||
logger.debug "Using recipe: #{recipe}"
|
||||
|
||||
if @includingTools
|
||||
for tool in recipe.tools
|
||||
totalExpected = @result.quantityOf(tool.name) + @_expected.quantityOf(tool.name)
|
||||
if totalExpected < tool.quantity
|
||||
logger.debug "Need to build tool: #{tool}"
|
||||
@_pending.add tool.name, tool.quantity
|
||||
@_expected.add tool.name, tool.quantity
|
||||
|
||||
@@ -74,6 +80,7 @@ module.exports = class CraftingPlan
|
||||
quantityAvailable = @result.quantityOf item.name
|
||||
quantityUsed = Math.min quantityAvailable, item.quantity
|
||||
quantityNeeded = item.quantity - quantityUsed
|
||||
logger.debug "processing input item: #{item}, a:#{quantityAvailable}, u:#{quantityUsed}, n:#{quantityNeeded}"
|
||||
|
||||
@result.remove item.name, quantityUsed
|
||||
@_pending.add item.name, quantityNeeded
|
||||
@@ -83,10 +90,12 @@ module.exports = class CraftingPlan
|
||||
quantityMissing = @need.quantityOf item.name
|
||||
quantityUsed = Math.min quantityMissing, item.quantity
|
||||
quantityLeft = item.quantity - quantityUsed
|
||||
logger.debug "processing output item: #{item}, m:#{quantityMissing}, u:#{quantityUsed}, l:#{quantityLeft}"
|
||||
|
||||
@make.add item.name, item.quantity
|
||||
@need.remove item.name, quantityUsed
|
||||
@result.add item.name, quantityLeft
|
||||
@make.add item.name, item.quantity
|
||||
|
||||
|
||||
_totalQuantityOf: (name)->
|
||||
return @result.quantityOf(name) - @need.quantityOf(name)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
###
|
||||
# Crafting Guide - item.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - item.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
BaseModel = require './base_model'
|
||||
@@ -45,7 +45,7 @@ module.exports = class Item extends BaseModel
|
||||
extra -= @stackSize
|
||||
count += 1
|
||||
|
||||
return count:count extra:extra
|
||||
return count:count, extra:extra
|
||||
|
||||
# Object Overrides #############################################################################
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
###
|
||||
# Crafting Guide - recipe.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - recipe.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
BaseModel = require './base_model'
|
||||
@@ -13,11 +13,11 @@ module.exports = class Recipe extends BaseModel
|
||||
|
||||
constructor: (attributes={}, options={})->
|
||||
if not attributes.output? then throw new Error "attributes.output is required"
|
||||
if not attributes.input? then throw new Error "attributes.input is required"
|
||||
attributes.input ?= []
|
||||
attributes.tools ?= []
|
||||
super attributes, options
|
||||
|
||||
Object.defineProperty this, 'name', get:-> @output[0].name
|
||||
Object.defineProperty @prototype, 'name', get:-> @output[0].name
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
@@ -45,7 +45,7 @@ module.exports = class Recipe extends BaseModel
|
||||
for inputItem in @input
|
||||
if needsDelimiter then result.push ', '
|
||||
result.push inputItem.toString()
|
||||
needsDelimiter = false
|
||||
needsDelimiter = true
|
||||
result.push ']'
|
||||
|
||||
result.push ", output:["
|
||||
@@ -53,7 +53,7 @@ module.exports = class Recipe extends BaseModel
|
||||
for outputItem in @output
|
||||
if needsDelimiter then result.push ', '
|
||||
result.push outputItem.toString()
|
||||
needsDelimiter = false
|
||||
needsDelimiter = true
|
||||
result.push ']'
|
||||
|
||||
if @tools.length > 0
|
||||
@@ -62,7 +62,7 @@ module.exports = class Recipe extends BaseModel
|
||||
for tool in @tools
|
||||
if needsDelimiter then result.push ', '
|
||||
result.push tool.toString()
|
||||
needsDelimiter = false
|
||||
needsDelimiter = true
|
||||
result.push ']'
|
||||
|
||||
result.push '}'
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
###
|
||||
# Crafting Guide - recipe_book.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - recipe_book.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
BaseModel = require './base_model'
|
||||
@@ -16,9 +16,10 @@ module.exports = class RecipeBook extends BaseModel
|
||||
if _.isEmpty(attributes.modName) then throw new Error 'modName cannot be empty'
|
||||
if _.isEmpty(attributes.modVersion) then throw new Error 'modVersion cannot be empty'
|
||||
|
||||
attributes.description ?= ''
|
||||
attributes.recipes ?= []
|
||||
attributes.enabled ?= attributes.modName in RequiredMods
|
||||
attributes.description ?= ''
|
||||
attributes.rawMaterials ?= []
|
||||
attributes.recipes ?= []
|
||||
attributes.enabled ?= attributes.modName in RequiredMods
|
||||
super attributes, options
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
@@ -37,6 +38,9 @@ module.exports = class RecipeBook extends BaseModel
|
||||
|
||||
return result
|
||||
|
||||
isRawMaterial: (name)->
|
||||
return name in @rawMaterials
|
||||
|
||||
hasRecipe: (name)->
|
||||
for recipe in @recipes
|
||||
return true if recipe.name is name
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
###
|
||||
# Crafting Guide - recipe_book_parser.coffee
|
||||
#
|
||||
# Copyright (c) 2014 by Redwood Labs
|
||||
# All rights reserved.
|
||||
Crafting Guide - recipe_book_parser.coffee
|
||||
|
||||
Copyright (c) 2014 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
Item = require './item'
|
||||
@@ -13,6 +13,8 @@ RecipeBook = require './recipe_book'
|
||||
|
||||
module.exports = class RecipeBookParser
|
||||
|
||||
@CURRENT_VERSION = '1'
|
||||
|
||||
constructor: ->
|
||||
@_parsers =
|
||||
'1': new V1
|
||||
@@ -22,11 +24,18 @@ module.exports = class RecipeBookParser
|
||||
if not data.version? then throw new Error 'version is required'
|
||||
|
||||
parser = @_parsers["#{data.version}"]
|
||||
if not parser?
|
||||
throw new Error "cannot parse version #{data.version} recipe books"
|
||||
if not parser? then throw new Error "cannot parse version #{data.version} recipe books"
|
||||
|
||||
return parser.parse data
|
||||
|
||||
unparse: (recipeBook, version=RecipeBookParser.CURRENT_VERSION)->
|
||||
if not recipeBook? then throw new Error 'recipe book is required'
|
||||
|
||||
parser = @_parsers["#{version}"]
|
||||
if not parser? then throw new Error "version #{version} is not supported"
|
||||
|
||||
return parser.unparse recipeBook
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
module.exports.V1 = class V1
|
||||
@@ -37,6 +46,9 @@ module.exports.V1 = class V1
|
||||
parse: (data)->
|
||||
return @_parseRecipeBook data
|
||||
|
||||
unparse: (recipeBook)->
|
||||
return @_unparseRecipeBook recipeBook
|
||||
|
||||
# Private Methods ##############################################################################
|
||||
|
||||
_parseRecipeBook: (data)->
|
||||
@@ -49,24 +61,28 @@ module.exports.V1 = class V1
|
||||
book = new RecipeBook 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]
|
||||
book.recipes.push @_parseRecipe recipeData
|
||||
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"
|
||||
if not data.input? then throw new Error "#{@_errorLocation} is missing input"
|
||||
|
||||
output = @_parseItemList data.output, field:'output', canBeEmpty:false
|
||||
@_errorLocation = "recipe for output[0].name"
|
||||
@_errorLocation = "recipe for #{output[0].name}"
|
||||
|
||||
data.input ?= []
|
||||
data.tools ?= []
|
||||
input = @_parseItemList data.input, field:'input', canBeEmpty:false
|
||||
tools = @_parseItemList data.tools, field:'tools', canBeEmpty:true
|
||||
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
|
||||
|
||||
@@ -96,3 +112,97 @@ module.exports.V1 = class V1
|
||||
if not _.isNumber(data[0]) then throw new Error "#{errorBase} must start with a number"
|
||||
|
||||
return new Item quantity:data[0], name:data[1]
|
||||
|
||||
_unparseRecipeBook: (recipeBook)->
|
||||
result = []
|
||||
result.push '{\n'
|
||||
result.push ' "version": 1,\n'
|
||||
result.push ' "mod_name": "' + recipeBook.modName + '",\n'
|
||||
result.push ' "mod_version": "' + recipeBook.modVersion + '",\n'
|
||||
if recipeBook.description.length > 0
|
||||
result.push ' "description": "' + recipeBook.description + '",\n'
|
||||
|
||||
if recipeBook.rawMaterials.length > 0
|
||||
result.push ' "raw_materials": [\n'
|
||||
firstItem = true
|
||||
materials = recipeBook.rawMaterials.slice()
|
||||
materials.sort()
|
||||
for material in materials
|
||||
if not firstItem then result.push ',\n'
|
||||
result.push ' "' + material + '"'
|
||||
firstItem = false
|
||||
result.push '\n ],\n'
|
||||
|
||||
result.push ' "recipes": [\n'
|
||||
recipes = recipeBook.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
|
||||
|
||||
firstItem = true
|
||||
for recipe in recipes
|
||||
result.push if firstItem then ' {\n' else ' }, {\n'
|
||||
@_unparseRecipe recipe, result
|
||||
firstItem = false
|
||||
result.push ' }\n'
|
||||
|
||||
result.push ' ]\n'
|
||||
result.push '}'
|
||||
|
||||
return result.join ''
|
||||
|
||||
_unparseRecipe: (recipe, result=[])->
|
||||
result.push ' "output": '
|
||||
@_unparseItemList recipe.output, result, sort:false
|
||||
|
||||
if recipe.input.length > 0
|
||||
result.push ',\n'
|
||||
result.push ' "input": '
|
||||
@_unparseItemList recipe.input, result
|
||||
|
||||
if recipe.tools.length > 0
|
||||
result.push ',\n'
|
||||
result.push ' "tools": '
|
||||
@_unparseItemList recipe.tools, result
|
||||
|
||||
result.push '\n'
|
||||
return result
|
||||
|
||||
_unparseItemList: (itemList, result, options={})->
|
||||
options.sort ?= true
|
||||
|
||||
if itemList.length is 0
|
||||
result.push '[]'
|
||||
else if itemList.length is 1
|
||||
item = itemList[0]
|
||||
if item.quantity is 1
|
||||
result.push '"' + item.name + '"'
|
||||
else
|
||||
result.push '[[' + item.quantity + ', "' + item.name + '"]]'
|
||||
else
|
||||
result.push '['
|
||||
|
||||
items = itemList.slice()
|
||||
if options.sort
|
||||
items.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
|
||||
return 0
|
||||
|
||||
firstItem = true
|
||||
for item in items
|
||||
result.push ', ' if not firstItem
|
||||
if item.quantity is 1
|
||||
result.push '"' + item.name + '"'
|
||||
else
|
||||
result.push '[' + item.quantity + ', "' + item.name + '"]'
|
||||
firstItem = false
|
||||
|
||||
result.push ']'
|
||||
|
||||
return result
|
||||
|
||||
@@ -28,10 +28,12 @@ module.exports = class RecipeCatalog extends BaseModel
|
||||
if book.hasRecipe name
|
||||
book.enabled = true
|
||||
|
||||
gatherNames: ->
|
||||
gatherNames: (options={})->
|
||||
options.includeDisabled ?= false
|
||||
|
||||
nameData = {}
|
||||
for book in @books
|
||||
continue unless book.enabled
|
||||
continue unless book.enabled or options.includeDisabled
|
||||
book.gatherNames nameData
|
||||
|
||||
result = []
|
||||
@@ -40,16 +42,33 @@ module.exports = class RecipeCatalog extends BaseModel
|
||||
result.push nameData[name]
|
||||
return result
|
||||
|
||||
gatherRecipes: (name)->
|
||||
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)->
|
||||
hasRecipe: (name, options={})->
|
||||
options.includeDisabled ?= false
|
||||
|
||||
for book in @books
|
||||
return true if book.hasRecipe(name)
|
||||
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)->
|
||||
|
||||
Reference in New Issue
Block a user