Added CraftingTable* components and UI

This commit is contained in:
Andrew Miner
2014-12-23 20:26:36 -08:00
parent b05fc4c5c9
commit 95c15abe10
30 changed files with 866 additions and 252 deletions
+28
View File
@@ -0,0 +1,28 @@
###
# Crafting Guide - event_recorder.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
util = require 'util'
########################################################################################################################
module.exports = class EventRecorder
constructor: (model)->
if not model? then throw new Error 'model is required'
@model = model
@events = []
Object.defineProperty this, 'names', get:-> e.event for e in @events
@model.on 'all', (event, model, args...)=>
logger.verbose "#{model?.constructor?.name}(#{model?.cid}) emitted #{event}
with args: #{util.inspect(args)}"
@events.push id:model?.cid, event:event, args:args
reset: ->
@events = []
+115
View File
@@ -0,0 +1,115 @@
###
# Crafting Guide - inventory.test.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
{Event} = require '../src/scripts/constants'
EventRecorder = require './event_recorder'
Inventory = require '../src/scripts/models/inventory'
Item = require '../src/scripts/models/item'
########################################################################################################################
inventory = null
########################################################################################################################
describe 'Inventory', ->
beforeEach ->
inventory = new Inventory
inventory.add 'wool', 4
inventory.add 'string', 20
inventory.add 'boat'
describe 'add', ->
it 'can add to an empty inventory', ->
inventory.add 'iron ingot', 4
item = inventory._items['iron ingot']
item.constructor.name.should.equal 'Item'
item.name.should.equal 'iron ingot'
item.quantity.should.equal 4
it 'can augment quantity of existing items', ->
inventory.add 'wool', 2
inventory._items.wool.quantity.should.equal 6
it 'can add zero quantity', ->
inventory.add 'wool', 0
inventory._items.wool.quantity.should.equal 4
it 'emits the proper events', ->
events = new EventRecorder inventory
inventory.add 'iron ingot', 10
events.names.should.eql [Event.add, Event.change]
describe 'each', ->
it 'works with an empty inventory', ->
inventory = new Inventory
result = []
inventory.each (item)-> result.push item.name
result.should.eql []
it 'works when items have only been added', ->
result = []
inventory.each (item)-> result.push item.name
result.should.eql ['wool', 'string', 'boat']
it 'works when items have been augmented', ->
inventory.add 'iron ingot'
inventory.add 'boat'
inventory.add 'wool', 2
result = []
inventory.each (item)-> result.push item.name
result.should.eql ['wool', 'string', 'boat', 'iron ingot']
it 'moves items to the end when removed and re-added', ->
inventory.remove 'wool', 4
inventory.add 'wool'
result = []
inventory.each (item)-> result.push item.name
result.should.eql ['string', 'boat', 'wool']
describe 'hasAtLeast', ->
it 'works when the item is completely absent', ->
answer = inventory.hasAtLeast 'chicken', 1
answer.should.be.false
it 'always returns true for zero quantity', ->
inventory.hasAtLeast('chicken', 0).should.be.true
inventory.hasAtLeast('wool', 0).should.be.true
it 'works for a quantity above 1', ->
inventory.hasAtLeast('wool', 3).should.be.true
inventory.hasAtLeast('wool', 4).should.be.true
inventory.hasAtLeast('wool', 5).should.be.false
describe 'remove', ->
it 'throws when the item is absent', ->
expect(-> inventory.remove('chicken')).to.throw Error,
'cannot remove chicken since it is not in this inventory'
it 'throws when the item has insufficient quantity', ->
expect(-> inventory.remove('wool', 10)).to.throw Error,
'cannot remove 10 wool because there is only 4 in this inventory'
it 'removes a single item by default', ->
inventory.remove 'wool'
inventory._items.wool.quantity.should.equal 3
it 'removes a quantity above 1', ->
inventory.remove 'wool', 3
inventory._items.wool.quantity.should.equal 1
it 'emits the proper events', ->
events = new EventRecorder inventory
inventory.remove 'wool'
events.names.should.eql [Event.remove, Event.change]
+46
View File
@@ -0,0 +1,46 @@
###
# Crafting Guide - inventory_parser.test.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
Inventory = require '../src/scripts/models/inventory'
InventoryParser = require '../src/scripts/models/inventory_parser'
########################################################################################################################
parser = null
########################################################################################################################
describe 'InventoryParser', ->
beforeEach -> parser = new InventoryParser
it 'returns an empty Inventory for an empty string', ->
result = parser.parse ''
result._names.length.should.equal 0
it 'can parse a single item without quantity', ->
result = parser.parse 'wool'
result._items.wool.quantity.should.equal 1
it 'can parse a single item with quantity', ->
result = parser.parse '4 wool'
result._items.wool.quantity.should.equal 4
it 'can parse multiple mixed-type items', ->
result = parser.parse '4 wool\n10 string\nboat\n\n'
result._items.wool.quantity.should.equal 4
result._items.string.quantity.should.equal 10
result._items.boat.quantity.should.equal 1
result._names.should.eql ['wool', 'string', 'boat']
it 're-uses the given inventory object', ->
inventory = new Inventory
inventory.add 'string', 8
result = parser.parse '4 wool', inventory
inventory._items.string.quantity.should.equal 8
inventory._items.wool.quantity.should.equal 4
inventory._names.should.eql ['string', 'wool']
-124
View File
@@ -1,124 +0,0 @@
###
# Crafting Guide - v1.test.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
V1 = require '../../src/scripts/models/parser_versions/v1'
########################################################################################################################
parser = null
########################################################################################################################
describe 'RecipeBookParser.V1', ->
before -> parser = new V1
describe '_parseRecipeBook', ->
it 'requires a mod_name', ->
data = version:1, mod_version:'1.0', recipes:[]
expect(-> parser._parseRecipeBook data).to.throw Error, 'mod_name is required'
it 'requires a mod_version', ->
data = version:1, mod_name:'Empty', recipes:[]
expect(-> parser._parseRecipeBook data).to.throw Error, 'mod_version is required'
it 'can parse an empty recipe book', ->
data =
version: 1
mod_name: 'Empty'
mod_version: '1.0'
recipes: []
book = parser._parseRecipeBook data
book.modName.should.equal 'Empty'
book.modVersion.should.equal '1.0'
it 'can parse a non-empty recipe book', ->
data =
version: 1
mod_name: 'Minecraft'
mod_version: '1.7.10'
recipes: [
{ input:'sugar cane', output:'sugar' }
{ input:[[3, 'wool'], [3, 'planks']], tools:'crafting table', output:'bed' }
]
book = parser._parseRecipeBook data
book.modName.should.equal 'Minecraft'
book.modVersion.should.equal '1.7.10'
(r.name for r in book.recipes).sort().should.eql ['bed', 'sugar']
describe '_parseRecipe', ->
it 'requires output to be defined', ->
parser._errorLocation = 'boat'
expect(-> parser._parseRecipe input:'wool').to.throw Error, 'boat is missing output'
it 'requires input to be defined', ->
parser._errorLocation = 'boat'
expect(-> parser._parseRecipe output:'wool').to.throw Error, 'boat is missing input'
it 'can parse a regular recipe', ->
data =
output: 'bed'
input: [[3, 'planks'], [3, 'wool']]
tools: 'crafting table'
recipe = parser._parseRecipe data
(i.name for i in recipe.output).should.eql ['bed']
(i.name for i in recipe.input).sort().should.eql ['planks', 'wool']
(i.name for i in recipe.tools).should.eql ['crafting table']
it 'can parse a recipe without tools', ->
recipe = parser._parseRecipe output:'sugar', input:'sugar cane'
(i.name for i in recipe.output).should.eql ['sugar']
(i.name for i in recipe.input).sort().should.eql ['sugar cane']
(i.name for i in recipe.tools).should.eql []
describe '_parseItemList', ->
it 'can promote a single item to a list', ->
list = parser._parseItemList 'boat'
(i.name for i in list).should.eql ['boat']
it 'can require a list to be non-empty', ->
parser._errorLocation = 'boat'
options = field:'output', canBeEmpty:false
expect(-> parser._parseItemList [], options).to.throw Error, 'output for boat cannot be empty'
it 'can allow an empty list', ->
list = parser._parseItemList [], canBeEmpty:true
list.length.should.equal 0
it 'can parse a non-empty list', ->
list = parser._parseItemList [[3, 'plank'], [3, 'wool']]
(i.name for i in list).sort().should.eql ['plank', 'wool']
describe '_parseItem', ->
it 'requires the array to have at least one element', ->
parser._errorLocation = 'boat'
options = index:1, field:'output'
expect(-> parser._parseItem([], options)).to.throw Error,
"output element 1 for boat must have at least one element"
it 'can fill in a missing number', ->
item = parser._parseItem 'boat'
item.name.should.equal 'boat'
item.quantity.should.equal 1
item2 = parser._parseItem ['boat']
item2.name.should.equal 'boat'
item2.quantity.should.equal 1
it 'requires the data to start with a number', ->
parser._errorLocation = 'boat'
options = index:1, field:'output'
expect(-> parser._parseItem(['2', 'book'], options)).to.throw Error,
"output element 1 for boat must start with a number"
it 'can parse a basic item', ->
item = parser._parseItem [2, 'book']
item.constructor.name.should.equal 'Item'
+126
View File
@@ -0,0 +1,126 @@
###
# Crafting Guide - v1.test.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
RecipeBookParser = require '../src/scripts/models/recipe_book_parser'
########################################################################################################################
parser = null
########################################################################################################################
describe 'RecipeBookParser', ->
describe "V1", ->
before -> parser = new RecipeBookParser.V1
describe '_parseRecipeBook', ->
it 'requires a mod_name', ->
data = version:1, mod_version:'1.0', recipes:[]
expect(-> parser._parseRecipeBook data).to.throw Error, 'mod_name is required'
it 'requires a mod_version', ->
data = version:1, mod_name:'Empty', recipes:[]
expect(-> parser._parseRecipeBook data).to.throw Error, 'mod_version is required'
it 'can parse an empty recipe book', ->
data =
version: 1
mod_name: 'Empty'
mod_version: '1.0'
recipes: []
book = parser._parseRecipeBook data
book.modName.should.equal 'Empty'
book.modVersion.should.equal '1.0'
it 'can parse a non-empty recipe book', ->
data =
version: 1
mod_name: 'Minecraft'
mod_version: '1.7.10'
recipes: [
{ input:'sugar cane', output:'sugar' }
{ input:[[3, 'wool'], [3, 'planks']], tools:'crafting table', output:'bed' }
]
book = parser._parseRecipeBook data
book.modName.should.equal 'Minecraft'
book.modVersion.should.equal '1.7.10'
(r.name for r in book.recipes).sort().should.eql ['bed', 'sugar']
describe '_parseRecipe', ->
it 'requires output to be defined', ->
parser._errorLocation = 'boat'
expect(-> parser._parseRecipe input:'wool').to.throw Error, 'boat is missing output'
it 'requires input to be defined', ->
parser._errorLocation = 'boat'
expect(-> parser._parseRecipe output:'wool').to.throw Error, 'boat is missing input'
it 'can parse a regular recipe', ->
data =
output: 'bed'
input: [[3, 'planks'], [3, 'wool']]
tools: 'crafting table'
recipe = parser._parseRecipe data
(i.name for i in recipe.output).should.eql ['bed']
(i.name for i in recipe.input).sort().should.eql ['planks', 'wool']
(i.name for i in recipe.tools).should.eql ['crafting table']
it 'can parse a recipe without tools', ->
recipe = parser._parseRecipe output:'sugar', input:'sugar cane'
(i.name for i in recipe.output).should.eql ['sugar']
(i.name for i in recipe.input).sort().should.eql ['sugar cane']
(i.name for i in recipe.tools).should.eql []
describe '_parseItemList', ->
it 'can promote a single item to a list', ->
list = parser._parseItemList 'boat'
(i.name for i in list).should.eql ['boat']
it 'can require a list to be non-empty', ->
parser._errorLocation = 'boat'
options = field:'output', canBeEmpty:false
expect(-> parser._parseItemList [], options).to.throw Error, 'output for boat cannot be empty'
it 'can allow an empty list', ->
list = parser._parseItemList [], canBeEmpty:true
list.length.should.equal 0
it 'can parse a non-empty list', ->
list = parser._parseItemList [[3, 'plank'], [3, 'wool']]
(i.name for i in list).sort().should.eql ['plank', 'wool']
describe '_parseItem', ->
it 'requires the array to have at least one element', ->
parser._errorLocation = 'boat'
options = index:1, field:'output'
expect(-> parser._parseItem([], options)).to.throw Error,
"output element 1 for boat must have at least one element"
it 'can fill in a missing number', ->
item = parser._parseItem 'boat'
item.name.should.equal 'boat'
item.quantity.should.equal 1
item2 = parser._parseItem ['boat']
item2.name.should.equal 'boat'
item2.quantity.should.equal 1
it 'requires the data to start with a number', ->
parser._errorLocation = 'boat'
options = index:1, field:'output'
expect(-> parser._parseItem(['2', 'book'], options)).to.throw Error,
"output element 1 for boat must start with a number"
it 'can parse a basic item', ->
item = parser._parseItem [2, 'book']
item.constructor.name.should.equal 'Item'
+6 -1
View File
@@ -17,11 +17,16 @@ global.assert = chai.assert
global.expect = chai.expect
global.should = chai.should()
Logger = require '../src/scripts/logger'
global.logger = new Logger level:Logger.TRACE
# Test Registry ########################################################################################################
mocha.setup 'bdd'
require './parser_versions/v1.test'
require './inventory.test'
require './inventory_parser.test'
require './recipe_book_parser.test'
mocha.checkLeaks()
mocha.run()