-
Notifications
You must be signed in to change notification settings - Fork 46
/
poke-api.js
92 lines (86 loc) · 2.79 KB
/
poke-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var request = require('request'),
Q = require('q');
module.exports = {}
module.exports.getPokemon = function(name) {
var deferred = Q.defer();
request("http://pokeapi.co/api/v1/pokemon/"+name, function (error, response, body) {
if (response.statusCode == 200) {
deferred.resolve(JSON.parse(body));
} else {
deferred.reject(new Error("Error Getting Pokemon"));
}
})
return deferred.promise;
}
module.exports.getSprite = function(url) {
var deferred = Q.defer();
request(url, function (error, response, body) {
if (response.statusCode == 200) {
deferred.resolve(JSON.parse(body));
} else {
deferred.reject(new Error("Error Getting Sprite"));
}
})
return deferred.promise;
}
module.exports.getMove = function(url) {
var deferred = Q.defer();
request(url, function (error, response, body) {
if (response.statusCode == 200) {
deferred.resolve(JSON.parse(body));
} else {
deferred.reject(new Error("Error Getting Move"));
}
})
return deferred.promise;
}
/*
* Calculates the effectiveness of one move on a pokemon with 1 or 2 types.
* When accessing a move from the API, it will return with three arrays that look like this:
* "super_effective": [{name:"fairy", resource_uri:"/api/v1/type/18"} ... ]
* We only care about the name, so we map these arrays to something like:
* supereffective = ["fairy", "ice", ...]
* then we can go through and calculate the damage multiplier based on the three arrays.
*/
module.exports.getAttackMultiplier = function(offensive, defensive1, defensive2) {
var multiplier = 1,
typesArray = [
"normal", //1
"fighting",
"flying",
"poison",
"ground",
"rock",
"bug",
"ghost",
"steel",
"fire",
"water",
"grass",
"electric",
"psychic",
"ice",
"dragon",
"dark",
"fairy" //18
],
typeID = typesArray.indexOf(offensive.toLowerCase()) + 1,
deferred = Q.defer();
request("http://pokeapi.co/api/v1/type/"+typeID, function(error, response, body){
if(response.statusCode == 200) {
var d = JSON.parse(body),
ineffective = d.ineffective.map(function(val){return val.name}),
noeffect = d.no_effect.map(function(val){return val.name}),
supereffective = d.super_effective.map(function(val){return val.name});
[defensive1, defensive2].forEach(function(type){
if(ineffective.indexOf(type) !== -1) { multiplier *= 0.5; }
if(noeffect.indexOf(type) !== -1) { multiplier *= 0; }
if(supereffective.indexOf(type) !== -1) { multiplier *= 2; }
});
deferred.resolve(multiplier);
} else {
deferred.reject(new Error("Error accessing API while getting type."));
}
})
return deferred.promise;
}