-
Notifications
You must be signed in to change notification settings - Fork 198
/
poke.io.js
939 lines (775 loc) · 27.8 KB
/
poke.io.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
'use strict';
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
var crypto = require('crypto');
var request = require('request');
var geocoder = require('geocoder');
var events = require('events');
var ProtoBuf = require('protobufjs');
var GoogleOAuth = require('gpsoauthnode');
var fs = require('fs');
var S2 = require('s2-geometry').S2;
var Logins = require('./logins');
var pogoSignature = require('node-pogo-signature');
var builder = ProtoBuf.loadProtoFile('pokemon.proto');
if (builder === null) {
builder = ProtoBuf.loadProtoFile(__dirname + '/pokemon.proto');
}
var pokemonProto = builder.build();
var RequestEnvelop = pokemonProto.RequestEnvelop;
var ResponseEnvelop = pokemonProto.ResponseEnvelop;
var Signature = pokemonProto.Signature;
var pokemonlist = JSON.parse(fs.readFileSync(__dirname + '/pokemons.json', 'utf8'));
var itemlist = JSON.parse(fs.readFileSync(__dirname + '/items.json', 'utf8'));
var EventEmitter = events.EventEmitter;
var api_url = 'https://pgorelease.nianticlabs.com/plfe/rpc';
function GetCoords(self) {
var _self$playerInfo = self.playerInfo;
var latitude = _self$playerInfo.latitude;
var longitude = _self$playerInfo.longitude;
return [latitude, longitude];
}
function getNeighbors(lat, lng) {
var level = 15;
var origin = S2.latLngToKey(lat, lng, level);
var walk = [ S2.keyToId(origin) ];
// 10 before and 10 after
var next = S2.nextKey(origin);
var prev = S2.prevKey(origin);
for (var i = 0; i < 10; i++) {
// in range(10):
walk.push(S2.toId(prev));
walk.push(S2.toId(next));
next = S2.nextKey(next);
prev = S2.prevKey(prev);
}
return walk;
}
function Pokeio() {
var self = this;
self.events = new EventEmitter();
self.j = request.jar();
self.request = request.defaults({
jar: self.j
});
self.google = new GoogleOAuth();
self.pokemonlist = pokemonlist.pokemon;
self.itemlist = itemlist.items;
self.playerInfo = {
accessToken: '',
debug: true,
latitude: 0,
longitude: 0,
altitude: 0,
locationName: '',
provider: '',
apiEndpoint: '',
device_info: null
};
self.queue = {
line: [],
timeouts: {
heartbeat: -1,
normal: -1
},
in_progress: false,
add: function(options, requestType, callback) {
var t = requestType == 'heartbeat'? self.queue.timeouts.heartbeat: self.queue.timeouts.normal;
// check if queue has been disabled
if (t < 0) {
self.request.post(options, callback);
return;
}
// if in progress add request to queue
if (self.queue.in_progress) {
self.queue.line.push({
options: options,
callback: callback,
type: requestType
});
return;
}
// startup queue
self.queue.in_progress = true;
var doNext = function (opt, type, cb) {
self.request.post(opt, function (err, response, body) {
cb(err, response, body);
var timeout = type == 'heartbeat'? self.queue.timeouts.heartbeat: self.queue.timeouts.normal;
setTimeout(function() {
if (self.queue.line.length == 0) {
self.queue.in_progress = false;
return;
}
var next = self.queue.line.shift();
doNext(next.options, next.type, next.callback);
}, timeout);
});
};
doNext(options, requestType, callback);
}
};
self.SetThrottle = function(normal, heartbeat, cb) {
if (self.queue.in_progress) {
if (typeof cb === 'function') cb('cant set throttle, queue is busy');
return;
}
self.queue.timeouts.normal = normal;
self.queue.timeouts.heartbeat = heartbeat;
if (typeof cb === 'function') cb();
};
self.DebugPrint = function (str) {
if (self.playerInfo.debug === true) {
//self.events.emit('debug',str)
console.log(str);
}
};
function api_req(api_endpoint, access_token, req, callback, requestType) {
if (!requestType) requestType = 'normal';
// Auth
var authInfo = new RequestEnvelop.AuthInfo({
provider: self.playerInfo.provider,
token: new RequestEnvelop.AuthInfo.JWT(access_token, 59)
});
//console.log(req);
var f_req = new RequestEnvelop({
unknown1: 2,
rpc_id: 1469378659230941192,
requests: req,
latitude: self.playerInfo.latitude,
longitude: self.playerInfo.longitude,
altitude: self.playerInfo.altitude,
unknown12: 989
});
if (self.playerInfo.authTicket) {
f_req.auth_ticket = self.playerInfo.authTicket;
var lat = self.playerInfo.latitude, lng = self.playerInfo.longitude, alt = self.playerInfo.altitude;
var authTicketEncoded = self.playerInfo.authTicket.encode().toBuffer();
var signature = new Signature({
location_hash1: pogoSignature.utils.hashLocation1(authTicketEncoded, lat, lng, alt).toNumber(),
location_hash2: pogoSignature.utils.hashLocation2(lat, lng, alt).toNumber(),
unk22: crypto.randomBytes(32),
timestamp: new Date().getTime(),
timestamp_since_start: (new Date().getTime() - self.playerInfo.initTime),
});
if (!Array.isArray(req)) {
req = [req];
}
req.forEach(function(request) {
var reqHash = pogoSignature.utils.hashRequest(authTicketEncoded, request.encode().toBuffer()).toString();
var hash = require('long').fromString(reqHash, true, 10);
signature.request_hash.push(hash);
});
// Simulate real device
// add condition
if( self.playerInfo.device_info !== null ) {
signature.device_info = new Signature.DeviceInfo({
device_id: self.playerInfo.device_info.device_id,
android_board_name: self.playerInfo.device_info.android_board_name,
android_bootloader: self.playerInfo.device_info.android_bootloader,
device_brand: self.playerInfo.device_info.device_brand,
device_model: self.playerInfo.device_info.device_model,
device_model_identifier: self.playerInfo.device_info.device_model_identifier,
device_model_boot: self.playerInfo.device_info.device_model_boot,
hardware_manufacturer: self.playerInfo.device_info.hardware_manufacturer,
hardware_model: self.playerInfo.device_info.hardware_model,
firmware_brand: self.playerInfo.device_info.firmware_brand,
firmware_tags: self.playerInfo.device_info.firmware_tags,
firmware_type: self.playerInfo.device_info.firmware_type,
firmware_fingerprint: self.playerInfo.device_info.firmware_fingerprint
});
}
signature.location_fix = new Signature.LocationFix({
provider: "network",
timestamp_since_start: (new Date().getTime() - self.playerInfo.initTime),
provider_status: 3,
location_type: 1
});
var iv = crypto.randomBytes(32);
pogoSignature.encrypt(signature.encode().toBuffer(), iv, function(err, signatureEnc) {
f_req.unknown6 = new RequestEnvelop.Unknown6({
unknown1: 6,
unknown2: new RequestEnvelop.Unknown6.Unknown2({
unknown1: signatureEnc
})
});
compiledProtobuf(f_req);
});
} else {
f_req.auth = authInfo;
compiledProtobuf(f_req);
}
function compiledProtobuf(protobuf) {
//console.log(JSON.stringify(protobuf))
protobuf = f_req.encode().toBuffer();
var options = {
url: api_endpoint,
body: protobuf,
encoding: null,
headers: {
'User-Agent': 'Niantic App'
}
};
self.queue.add(options, requestType, function (err, response, body) {
if (err) {
return callback(new Error('Error'));
}
if (response === undefined || body === undefined) {
console.error('[!] RPC Server offline');
return callback(new Error('RPC Server offline'));
}
var f_ret;
try {
f_ret = ResponseEnvelop.decode(body);
} catch (e) {
if (e.decoded) {
// Truncated
console.warn(e);
f_ret = e.decoded; // Decoded message with missing required fields
}
}
if (f_ret) {
if (f_ret.auth_ticket) {
self.playerInfo.authTicket = f_ret.auth_ticket;
}
return callback(null, f_ret);
} else {
api_req(api_endpoint, access_token, req, callback);
}
});
}
}
self.init = function (username, password, location, provider, callback) {
if (provider !== 'ptc' && provider !== 'google') {
return callback(new Error('Invalid provider'));
}
self.playerInfo.initTime = new Date().getTime();
// set provider
self.playerInfo.provider = provider;
// Updating location
self.SetLocation(location, function (err, loc) {
if (err) {
return callback(err);
}
// Getting access token
self.GetAccessToken(username, password, function (err, token) {
if (err) {
return callback(err);
}
// Getting api endpoint
self.GetApiEndpoint(function (err, api_endpoint) {
if (err) {
return callback(err);
}
callback(null);
});
});
});
};
self.GetAccessToken = function (user, pass, callback) {
self.DebugPrint('[i] Logging with user: ' + user);
if (self.playerInfo.provider === 'ptc') {
Logins.PokemonClub(user, pass, self, function (err, token) {
if (err) {
return callback(err);
}
self.playerInfo.accessToken = token;
self.DebugPrint('[i] Received PTC access token!');
callback(null, token);
});
} else {
Logins.GoogleAccount(user, pass, self, function (err, token) {
if (err) {
return callback(err);
}
self.playerInfo.accessToken = token;
self.DebugPrint('[i] Received Google access token!');
callback(null, token);
});
}
};
self.GetApiEndpoint = function (callback) {
var req = [new RequestEnvelop.Requests(2), new RequestEnvelop.Requests(126), new RequestEnvelop.Requests(4), new RequestEnvelop.Requests(129), new RequestEnvelop.Requests(5)];
api_req(api_url, self.playerInfo.accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
}
var api_endpoint = 'https://' + f_ret.api_url + '/rpc';
self.playerInfo.apiEndpoint = api_endpoint;
self.DebugPrint('[i] Received API Endpoint: ' + api_endpoint);
return callback(null, api_endpoint);
});
};
self.GetInventory = function (callback) {
var req = new RequestEnvelop.Requests(4);
api_req(self.playerInfo.apiEndpoint, self.playerInfo.accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
}
var dErr, inventory;
try {
inventory = ResponseEnvelop.GetInventoryResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, inventory);
});
};
self.GetProfile = function (callback) {
var req = new RequestEnvelop.Requests(2);
api_req(self.playerInfo.apiEndpoint, self.playerInfo.accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
}
var dErr, response;
try {
response = ResponseEnvelop.ProfilePayload.decode(f_ret.payload[0]).profile;
} catch (err) {
dErr = err;
}
callback(dErr, response);
if (response && response.username) {
self.DebugPrint('[i] Logged in!');
}
});
};
self.GetJournal = function (callback) {
var req = new RequestEnvelop.Requests(801);
var _self$playerInfo6 = self.playerInfo;
var apiEndpoint = _self$playerInfo6.apiEndpoint;
var accessToken = _self$playerInfo6.accessToken;
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, journal;
try {
journal = ResponseEnvelop.ActionLogResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, journal);
});
};
// IN DEVELPOMENT, YES WE KNOW IS NOT WORKING ATM
self.Heartbeat = function (callback) {
var _self$playerInfo2 = self.playerInfo;
var apiEndpoint = _self$playerInfo2.apiEndpoint;
var accessToken = _self$playerInfo2.accessToken;
var nullbytes = new Array(21);
nullbytes.fill(0);
// Generating walk data using s2 geometry
var walk = getNeighbors(self.playerInfo.latitude, self.playerInfo.longitude).sort(function (a, b) {
return a > b;
});
// Creating MessageQuad for Requests type=106
var walkData = new RequestEnvelop.MessageQuad({
'f1': walk,
'f2': nullbytes,
'lat': self.playerInfo.latitude,
'long': self.playerInfo.longitude
});
var req = [new RequestEnvelop.Requests(106, walkData.encode().toBuffer()), new RequestEnvelop.Requests(126), new RequestEnvelop.Requests(4, new RequestEnvelop.Unknown3(Date.now().toString()).encode().toBuffer()), new RequestEnvelop.Requests(129), new RequestEnvelop.Requests(5, new RequestEnvelop.Unknown3('54b359c97e46900f87211ef6e6dd0b7f2a3ea1f5').encode().toBuffer())];
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, heartbeat;
try {
heartbeat = ResponseEnvelop.HeartbeatPayload.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, heartbeat);
}, 'heartbeat');
};
self.GetLocation = function (callback) {
geocoder.reverseGeocode.apply(geocoder, _toConsumableArray(GetCoords(self)).concat([function (err, data) {
if (data.status === 'ZERO_RESULTS') {
return callback(new Error('location not found'));
}
callback(null, data.results[0].formatted_address);
}]));
};
// Still WIP
self.GetFortDetails = function (fortid, fortlat, fortlong, callback) {
var fortDetailsMessage = new RequestEnvelop.FortDetailsRequest({
'fort_id': fortid,
'fort_latitude': fortlat,
'fort_longitude': fortlong
});
var req = new RequestEnvelop.Requests(104, fortDetailsMessage.encode().toBuffer());
api_req(self.playerInfo.apiEndpoint, self.playerInfo.accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.FortDetailsResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
// Still WIP
self.GetFort = function (fortid, fortlat, fortlong, callback) {
var fortSearchMessage = new RequestEnvelop.FortSearchMessage({
'fort_id': fortid,
'player_latitude': self.playerInfo.latitude,
'player_longitude': self.playerInfo.longitude,
'fort_latitude': fortlat,
'fort_longitude': fortlong
});
var req = new RequestEnvelop.Requests(101, fortSearchMessage.encode().toBuffer());
api_req(self.playerInfo.apiEndpoint, self.playerInfo.accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.FortSearchResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.EvolvePokemon = function (pokemonId, callback) {
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
var evolvePokemon = new RequestEnvelop.EvolvePokemonMessage({
'PokemonId': pokemonId
});
var req = new RequestEnvelop.Requests(125, evolvePokemon.encode().toBuffer());
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.EvolvePokemonResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.TransferPokemon = function (pokemonId, callback) {
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
var transferPokemon = new RequestEnvelop.TransferPokemonMessage({
'PokemonId': pokemonId
});
var req = new RequestEnvelop.Requests(112, transferPokemon.encode().toBuffer());
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.TransferPokemonResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
//still WIP
self.CatchPokemon = function (mapPokemon, normalizedHitPosition, normalizedReticleSize, spinModifier, pokeball, callback) {
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
var catchPokemon = new RequestEnvelop.CatchPokemonMessage({
'encounter_id': mapPokemon.EncounterId,
'pokeball': pokeball,
'normalized_reticle_size': normalizedReticleSize,
'spawnpoint_id': mapPokemon.SpawnPointId,
'hit_pokemon': true,
'spin_modifier': spinModifier,
'normalized_hit_position': normalizedHitPosition
});
var req = new RequestEnvelop.Requests(103, catchPokemon.encode().toBuffer());
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.CatchPokemonResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.RenamePokemon = function(pokemonId, nickname, callback) {
var renamePokemonMessage = new RequestEnvelop.NicknamePokemonMessage({
'pokemon_id': pokemonId,
'nickname': nickname,
});
var req = new RequestEnvelop.Requests(149, renamePokemonMessage.encode().toBuffer());
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.NicknamePokemonResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.EncounterPokemon = function (catchablePokemon, callback) {
var _self$playerInfo4 = self.playerInfo;
var apiEndpoint = _self$playerInfo4.apiEndpoint;
var accessToken = _self$playerInfo4.accessToken;
var latitude = _self$playerInfo4.latitude;
var longitude = _self$playerInfo4.longitude;
var encounterPokemon = new RequestEnvelop.EncounterMessage({
'encounter_id': catchablePokemon.EncounterId,
'spawnpoint_id': catchablePokemon.SpawnPointId,
'player_latitude': latitude,
'player_longitude': longitude
});
var req = new RequestEnvelop.Requests(102, encounterPokemon.encode().toBuffer());
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.EncounterResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.DropItem = function (itemId, count, callback) {
var _self$playerInfo4 = self.playerInfo;
var apiEndpoint = _self$playerInfo4.apiEndpoint;
var accessToken = _self$playerInfo4.accessToken;
var latitude = _self$playerInfo4.latitude;
var longitude = _self$playerInfo4.longitude;
var dropItemMessage = new RequestEnvelop.RecycleInventoryItemMessage({
'item_id': itemId,
'count': count
});
var req = new RequestEnvelop.Requests(137, dropItemMessage.encode().toBuffer());
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.RecycleInventoryItemResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.ReleasePokemon = function (pokemon, callback) {
console.log(pokemon.toString());
var releasePokemon = new RequestEnvelop.ReleasePokemonMessage({
'pokemon_id': pokemon.toString()
});
var req = new RequestEnvelop.Requests(112, releasePokemon.encode().toBuffer());
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.ReleasePokemonResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.LevelUpRewards = function (level, callback) {
var levelUpRewards = new RequestEnvelop.LevelUpRewardsMessage({
'level': level
});
var req = new RequestEnvelop.Requests(128, levelUpRewards.encode().toBuffer());
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.LevelUpRewardsResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.UseItemEggIncubator = function (item_id, pokemonId, callback) {
var levelUpRewards = new RequestEnvelop.UseItemEggIncubatorMessage({
'item_id': item_id,
'PokemonId': pokemonId
});
var req = new RequestEnvelop.Requests(140, levelUpRewards.encode().toBuffer());
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.UseItemEggIncubatorResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.GetHatchedEggs = function (callback) {
var req = new RequestEnvelop.Requests(140);
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.GetHatchedEggsResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.UseItemXpBoost = function (itemId, count, callback) {
var useItemXpBoostMessage = new RequestEnvelop.UseItemXpBoostMessage({
'item_id': itemId,
});
var req = new RequestEnvelop.Requests(139, useItemXpBoostMessage.encode().toBuffer());
var _self$playerInfo3 = self.playerInfo;
var apiEndpoint = _self$playerInfo3.apiEndpoint;
var accessToken = _self$playerInfo3.accessToken;
api_req(apiEndpoint, accessToken, req, function (err, f_ret) {
if (err) {
return callback(err);
} else if (!f_ret || !f_ret.payload || !f_ret.payload[0]) {
return callback('No result');
}
var dErr, response;
try {
response = ResponseEnvelop.UseItemXpBoostResponse.decode(f_ret.payload[0]);
} catch (err) {
dErr = err;
}
callback(dErr, response);
});
};
self.GetLocationCoords = function () {
var _self$playerInfo5 = self.playerInfo;
var latitude = _self$playerInfo5.latitude;
var longitude = _self$playerInfo5.longitude;
var altitude = _self$playerInfo5.altitude;
return {
latitude: latitude,
longitude: longitude,
altitude: altitude
};
};
self.SetLocation = function (location, callback) {
if (location.type !== 'name' && location.type !== 'coords') {
return callback(new Error('Invalid location type'));
}
if (location.type === 'name') {
if (!location.name) {
return callback(new Error('You should add a location name'));
}
var locationName = location.name;
geocoder.geocode(locationName, function (err, data) {
if (err || data.status === 'ZERO_RESULTS') {
return callback(new Error('location not found'));
}
var _data$results$0$geome = data.results[0].geometry.location;
var lat = _data$results$0$geome.lat;
var lng = _data$results$0$geome.lng;
self.playerInfo.latitude = lat;
self.playerInfo.longitude = lng;
self.playerInfo.locationName = locationName;
callback(null, self.GetLocationCoords());
});
} else if (location.type === 'coords') {
if (!location.coords) {
return callback(new Error('Coords object missing'));
}
self.playerInfo.latitude = location.coords.latitude || self.playerInfo.latitude;
self.playerInfo.longitude = location.coords.longitude || self.playerInfo.longitude;
self.playerInfo.altitude = location.coords.altitude || self.playerInfo.altitude;
geocoder.reverseGeocode.apply(geocoder, _toConsumableArray(GetCoords(self)).concat([function (err, data) {
if (err) return callback(err);
if (data && data.status !== 'ZERO_RESULTS' && data.results && data.results[0]) {
self.playerInfo.locationName = data.results[0].formatted_address;
}
callback(null, self.GetLocationCoords());
}]));
}
};
// Set device info for uk6
self.SetDeviceInfo = function(devInfo) {
self.playerInfo.device_info = devInfo;
};
}
module.exports = new Pokeio();
module.exports.Pokeio = Pokeio;