Skip to content

Commit

Permalink
Merge pull request #4 from ranisalt/spell
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Mar 10, 2015
2 parents 61efbf0 + a4e5113 commit 283af6d
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/fetcher/spell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

var util = require('../util');

/**
* Spell fetcher
*
* @param {cheerio} $
* @return this
*/

function Spell($) {

this.spell = {};

var self = this;

var getWrapper = function() {
return $('.BoxContent table');
};

var tables = getWrapper();

tables.eq(1).find('tr:nth-child(n+2)').each(function() {
var key = util.camelCase($(this).find('td:nth-child(1)').text());
var value = $(this).find('td:nth-child(2)').text().trim();

switch (key) {
case 'vocation':
case 'city':
value = value.split(', ');
break;
case 'group':
case 'type':
case 'damageType':
value = value.toLowerCase();
break;
case 'cooldown':
var match = value.match(/(\d+)/g);
value = parseInt(match[0]);
self.spell.groupCooldown = parseInt(match[1]);
break;
case 'expLvl':
key = 'minLevel';
/* falls through */
case 'soulPoints':
case 'amount':
case 'mana':
case 'price':
value = parseInt(value);
break;
case 'premium':
value = value === 'yes';
break;
}

if (self.spell.type === 'rune') {
self.rune = {};

tables.eq(2).find('tr:nth-child(n+2)').each(function() {
var key = util.camelCase($(this).find('td:nth-child(1)').text());
var value = $(this).find('td:nth-child(2)').text().trim();

switch (key) {
case 'vocation':
value = value.split(', ');
break;
case 'group':
case 'damageType':
value = value.toLowerCase();
break;
case 'expLvl':
key = 'minLevel';
value = parseInt(value);
break;
case 'magLvl':
key = 'magicLevel';
value = parseInt(value);
break;
}

self.rune[key] = value;
});
}

self.spell[key] = value;
});

return this;
}

module.exports = Spell;
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var cheerio = require('cheerio');

// Fetchers
var Character = require('./fetcher/character');
var Spell = require('./fetcher/spell');
var Spells = require('./fetcher/spells');
var World = require('./fetcher/world');
var Worlds = require('./fetcher/worlds');
Expand All @@ -23,6 +24,18 @@ var TibiaCrawler = {
});
},

spell: function(name, cb) {
var path = 'library/?subtopic=spells&spell=' + name.toLowerCase().replace(/\W/g, '');
return api.request('get', path, {}, function(err, res, body) {
var $ = cheerio.load(body);
if ($('h2:contains("' + name + '")').length === 0) {
cb();
} else {
cb(new Spell($));
}
});
},

spells: function(cb) {
var path = 'library/?subtopic=spells';
return api.request('get', path, {}, function(err, res, body) {
Expand Down
61 changes: 61 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,65 @@ describe('tibia-crawler', function() {
});
});


it('parse existing spell test', function(done) {
this.timeout(4000);
process.nextTick(function() {
crawler.spell('Rage of the Skies', function(spell) {
spell = spell.spell;

assert.equal(spell.name, 'Rage of the Skies');
assert.equal(spell.formula, 'exevo gran mas vis');
assert.deepEqual(spell.vocation, ['Sorcerer']);
assert.equal(spell.group, 'attack');
assert.equal(spell.type, 'instant');
assert.equal(spell.damageType, 'energy');
assert.equal(spell.cooldown, 40);
assert.equal(spell.groupCooldown, 4);
assert.equal(spell.minLevel, 55);
assert.equal(spell.mana, 600);
assert.equal(spell.price, 6000);
assert.deepEqual(spell.city, ['Edron']);
assert.equal(spell.premium, true);

done();
});
});
});


it('parse existing rune test', function(done) {
this.timeout(4000);
process.nextTick(function() {
crawler.spell('Great Fireball Rune', function(spell) {
var rune = spell.rune;
spell = spell.spell;

assert.equal(spell.soulPoints, 3);
assert.equal(spell.amount, 4);
assert.equal(rune.name, 'Great Fireball Rune');
assert.deepEqual(rune.vocation, ['Druid', 'Knight', 'Paladin', 'Sorcerer']);
assert.equal(rune.group, 'attack');
assert.equal(rune.damageType, 'fire');
assert.equal(rune.minLevel, 30);
assert.equal(rune.magicLevel, 4);

done();
});
});
});


it('parse non-existing spell test', function(done) {
this.timeout(4000);
process.nextTick(function() {
crawler.spell('Rage of the Skiesx', function(spell) {

assert.equal(spell, undefined);

done();
});
});
});

});

0 comments on commit 283af6d

Please sign in to comment.