forked from dwi-lanang/node-pokemon-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (102 loc) · 3.16 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// external dependencies
const bPromise = require('bluebird');
const _ = require('lodash');
// internal dependencies
const login = require('./src/login');
const location = require('./src/location');
// main functions
var fn = {
cache: {
location: {
latitude: null,
longitude: null,
value: null
},
token: null,
provider: null,
endpoint: null
},
login: function(username, password, provider) {
var promise;
provider = provider.toLowerCase();
if (provider === 'google') {
promise = login.google;
} else if (provider === 'pokemon-club') {
promise = login.pokemonClub;
}
if (!promise) {
return bPromise.reject(new Error('Invalid login provider'));
}
return promise(username, password)
.then(function(token) {
fn.cache.provider = provider;
fn.cache.token = token;
return token;
});
},
location: {
set: function(/* type, value || type, latitude, longitude */) {
var args = Array.prototype.slice.call(arguments);
var type = args.shift().toLowerCase();
var promise;
if (type === 'address') {
promise = location.coordinates.getByAddress(args.shift());
} else if (type === 'coordinates') {
promise = location.address.getByCoordinates(args.shift(), args.shift());
}
if (!promise) {
return bPromise.reject(new Error('Invalid location type'));
}
return promise
.then(function(location) {
fn.cache.location = location;
return location;
});
},
get: function() {
return bPromise.resolve(fn.cache.location);
}
},
profile: {
get: function() {
return require('./src/requests/get-profile')(fn.cache.playerEndpoint,
fn.cache.location.latitude, fn.cache.location.longitude,
fn.cache.provider, fn.cache.token);
}
},
inventory: {
get: function() {
return require('./src/requests/get-inventory')(fn.cache.playerEndpoint,
fn.cache.location.latitude, fn.cache.location.longitude,
fn.cache.provider, fn.cache.token);
}
},
mapData: {
getNearby: function() {
return require('./src/requests/get-map-data')(fn.cache.playerEndpoint,
fn.cache.location.latitude, fn.cache.location.longitude,
fn.cache.provider, fn.cache.token);
},
getByCoordinates: function(latitude, longitude) {
return require('./src/requests/get-map-data')(fn.cache.playerEndpoint,
latitude, longitude, fn.cache.provider, fn.cache.token);
},
getByAddress: function(address) {
return location.coordinates.getByAddress(address)
.then(function(location) {
return fn.mapData.getByCoordinates(location.latitude,
location.longitude);
});
}
},
getPlayerEndpoint: function() {
const request = require('./src/requests/get-player-endpoint')(fn.cache.location.latitude,
fn.cache.location.longitude, fn.cache.provider, fn.cache.token);
return request
.then(function(playerEndpoint) {
fn.cache.playerEndpoint = playerEndpoint;
return playerEndpoint;
});
}
};
exports = module.exports = fn;