Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
- changed findOne and find to findItem and findItems
Browse files Browse the repository at this point in the history
- added ID support to both functions. You may now invoke them with string names or numerical itemIds
  • Loading branch information
xenolf committed Feb 3, 2013
1 parent 5a2322f commit ed1b4c7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 23 deletions.
50 changes: 37 additions & 13 deletions src/eveStatic.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ exports.EvEStatic = class EveStatic
callback()

###
Looks up an array of names and returns an object with name : id : slot (if applicable)
Looks up an array of names or ids and returns an object with name : id : slot (if applicable)
###
find: (nameArr, callback) ->
findItems: (nameArr, callback) ->
if not callback?
throw new Error 'Must be called with a callback!'

Expand All @@ -53,17 +53,30 @@ exports.EvEStatic = class EveStatic
inStr += '?,' for num in [0 ... nameArr.length]
inStr = inStr.slice 0, -1

@db.all "SELECT i.typeName, i.typeID, e.effectID FROM invTypes as i LEFT JOIN dgmTypeEffects e ON i.typeID = e.typeID AND e.effectID IN (11,12,13,2663) WHERE i.typeName IN (#{inStr})", nameArr, (err, rows) ->
if err
callback err, null
return
if _.isNaN(parseInt nameArr[0])

callback null, rows
@db.all "SELECT i.typeName, i.typeID, e.effectID FROM invTypes as i LEFT JOIN dgmTypeEffects e ON i.typeID = e.typeID AND e.effectID IN (11,12,13,2663) WHERE i.typeName IN (#{inStr})", nameArr, (err, rows) ->
if err
callback err, null
return

callback null, rows

else

@db.all "SELECT i.typeName, i.typeID, e.effectID FROM invTypes as i LEFT JOIN dgmTypeEffects e ON i.typeID = e.typeID AND e.effectID IN (11,12,13,2663) WHERE i.typeID IN (#{inStr})", nameArr, (err, rows) ->
if err
callback err, null
return

callback null, rows



###
Looks for the supplied name in the db and returns the typeID
###
findOne: (name, callback) ->
findItem: (name, callback) ->
if not callback?
throw new Error 'Must be called with a callback!'

Expand All @@ -75,9 +88,20 @@ exports.EvEStatic = class EveStatic
callback new Error 'Must call init() first!', null
return

@db.get "SELECT i.typeName, i.typeID , e.effectID FROM invTypes as i LEFT JOIN dgmTypeEffects as e ON i.typeID = e.typeID AND e.effectID IN (11,12,13,2663) WHERE typeName = '#{name}'", (err, row) ->
if err
callback err, null
return
if _.isNaN(parseInt name)

@db.get "SELECT i.typeName, i.typeID , e.effectID FROM invTypes as i LEFT JOIN dgmTypeEffects as e ON i.typeID = e.typeID AND e.effectID IN (11,12,13,2663) WHERE i.typeName = '#{name}'", (err, row) ->
if err
callback err, null
return

callback null, row

else

@db.get "SELECT i.typeName, i.typeID, e.effectID FROM invTypes as i LEFT JOIN dgmTypeEffects as e ON i.typeID = e.typeID AND e.effectID IN (11,12,13,2663) WHERE i.typeID = '#{name}'", (err, row) ->
if err
callback err, null
return

callback null, row
callback null, row
59 changes: 49 additions & 10 deletions test/evestatic-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ describe 'EvEStatic', ->
should.not.exist error
done()

describe 'findOne', ->
describe 'findItem', ->
it 'should return an error callback if called before init', (done) ->
stat = new EvEStatic
stat.findOne 'test', (error, data) ->
stat.findItem 'test', (error, data) ->
should.exist error
should.not.exist data
error.message.should.eql 'Must call init() first!'
Expand All @@ -37,7 +37,7 @@ describe 'EvEStatic', ->
stat.init (error) ->
should.not.exist error

stat.findOne '', (error, result) ->
stat.findItem '', (error, result) ->
should.exist error
error.message.should.eql 'Pass a valid item name!'
should.not.exist result
Expand All @@ -48,7 +48,7 @@ describe 'EvEStatic', ->
stat.init (error) ->
should.not.exist error
(->
stat.findOne 'Tyrfing'
stat.findItem 'Tyrfing'
).should.throw 'Must be called with a callback!'
done()

Expand All @@ -57,18 +57,33 @@ describe 'EvEStatic', ->
stat.init (error) ->
should.not.exist error

stat.findOne 'Tyrfing', (error, result) ->
stat.findItem 'Tyrfing', (error, result) ->
should.not.exist error
should.exist result
result.typeID.should.eql 32342
should.not.exist result.effectID
done()

describe 'find', ->
it 'should also work with IDs', (done) ->
stat = new EvEStatic
stat.init (error) ->
should.not.exist error

stat.findItem '32342', (error, result) ->
should.not.exist error
should.exist result
result.typeID.should.eql 32342
result.typeName.should.eql 'Tyrfing'
should.not.exist result.effectID
done()



describe 'findItems', ->

it 'should return an error callback if called before init', (done) ->
stat = new EvEStatic
stat.findOne 'test', (error, data) ->
stat.findItems 'test', (error, data) ->
should.exist error
should.not.exist data
error.message.should.eql 'Must call init() first!'
Expand All @@ -79,7 +94,7 @@ describe 'EvEStatic', ->
stat.init (error) ->
should.not.exist error

stat.findOne '', (error, result) ->
stat.findItems '', (error, result) ->
should.exist error
error.message.should.eql 'Pass a valid item name!'
should.not.exist result
Expand All @@ -90,7 +105,7 @@ describe 'EvEStatic', ->
stat.init (error) ->
should.not.exist error
(->
stat.findOne 'Tyrfing'
stat.findItems 'Tyrfing'
).should.throw 'Must be called with a callback!'
done()

Expand All @@ -99,7 +114,31 @@ describe 'EvEStatic', ->
stat.init (error) ->
should.not.exist error

stat.find ['Tyrfing', 'Erebus', 'Quad 3500mm Gallium Cannon'], (error, result) ->
stat.findItems ['Tyrfing', 'Erebus', 'Quad 3500mm Gallium Cannon'], (error, result) ->
should.not.exist error
should.exist result

result.should.be.instanceOf Array
result[0].typeName.should.eql 'Erebus'
result[0].typeID.should.eql 671
should.not.exist result[0].effectID

result[1].typeName.should.eql 'Quad 3500mm Gallium Cannon'
result[1].typeID.should.eql 3571
result[1].effectID.should.eql 12

result[2].typeName.should.eql 'Tyrfing'
result[2].typeID.should.eql 32342
should.not.exist result[2].effectID

done()

it 'should also work with item IDs', (done) ->
stat = new EvEStatic
stat.init (error) ->
should.not.exist error

stat.findItems ['32342', '671', '3571'], (error, result) ->
should.not.exist error
should.exist result

Expand Down

0 comments on commit ed1b4c7

Please sign in to comment.