forked from jch254/pokego-serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
96 lines (80 loc) · 2.8 KB
/
handler.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
const Pokeio = require('pokemon-go-node-api');
const Immutable = require('immutable');
const config = require('./config.json');
const pokedex = new Immutable.Map(Pokeio.pokemonlist.map(p => [p.id, Immutable.fromJS(p)]));
const locationType = 'coords';
module.exports.getPokegoNearby = (event, context, cb) => {
const latitude = parseFloat(event.query.lat) || -37.867877;
const longitude = parseFloat(event.query.lon) || 144.974005;
const altitude = parseFloat(event.query.alt) || 5;
const location = {
type: locationType,
coords: {
latitude: latitude,
longitude: longitude,
altitude: altitude
}
};
Pokeio.init(config.username, config.password, location, config.provider, function(err) {
if (err) {
console.log(err);
cb(err);
}
Pokeio.Heartbeat(function(err, heartbeat) {
if (err) {
console.log(err);
cb(err);
}
const mapPokemon = new Immutable.Map(
Immutable.fromJS(heartbeat.cells.map(cell => cell.MapPokemon))
.flatten()
.map(p => [p.PokedexTypeId.toString(), Immutable.fromJS(Object.assign({}, p))])
);
const mapPokemonWithPokedexData = mapPokemon.mergeDeep(pokedex.filter((p, id) => mapPokemon.has(id)));
const nearbyPokemon = new Immutable.Map(
Immutable.fromJS(heartbeat.cells.map(cell => cell.NearbyPokemon))
.flatten()
.map(p => [p.PokedexNumber.toString(), Immutable.fromJS(Object.assign({}, p))])
);
const nearbyPokemonWithPokedexData = nearbyPokemon.mergeDeep(pokedex.filter((p, id) => nearbyPokemon.has(id)));
const wildPokemon = new Immutable.Map(
Immutable.fromJS(heartbeat.cells.map(cell => cell.WildPokemon))
.flatten()
.map(p => [p.pokemon.PokemonId.toString(), Immutable.fromJS(Object.assign({}, p))])
);
const wildPokemonWithPokedexData = wildPokemon.mergeDeep(pokedex.filter((p, id) => wildPokemon.has(id)));
const response = {
mapPokemon: mapPokemonWithPokedexData,
nearbyPokemon: nearbyPokemonWithPokedexData,
wildPokemon: wildPokemonWithPokedexData
}
cb(null, response);
});
});
}
module.exports.getPokegoNearbyRaw = (event, context, cb) => {
const latitude = parseFloat(event.query.lat) || -37.867877;
const longitude = parseFloat(event.query.lon) || 144.974005;
const altitude = parseFloat(event.query.alt) || 5;
const location = {
type: locationType,
coords: {
latitude: latitude,
longitude: longitude,
altitude: altitude
}
};
Pokeio.init(username, key, location, provider, function(err) {
if (err) {
console.log(err);
cb(err);
}
Pokeio.Heartbeat(function(err, hb) {
if (err) {
console.log(err);
cb(err);
}
cb(null, hb);
});
});
}