forked from pokepark/PokemonQuestBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.php
executable file
·2418 lines (2094 loc) · 72 KB
/
logic.php
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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Bot access check.
* @param $update
* @param $access_type
*/
function bot_access_check($update, $access_type = BOT_ACCESS, $return_result = false)
{
// Restricted or public access
if(!empty($access_type)) {
$all_chats = '';
// Always add maintainer and admins.
$all_chats .= !empty(MAINTAINER_ID) ? MAINTAINER_ID . ',' : '';
$all_chats .= !empty(BOT_ADMINS) ? BOT_ADMINS . ',' : '';
$all_chats .= ($access_type == BOT_ADMINS) ? '' : $access_type;
// Make sure all_chats does not end with ,
$all_chats = rtrim($all_chats,',');
// Get telegram ID to check access from $update - either message, callback_query or inline_query
$update_type = '';
$update_type = !empty($update['message']['from']['id']) ? 'message' : $update_type;
$update_type = (empty($update_type) && !empty($update['callback_query']['from']['id'])) ? 'callback_query' : $update_type;
$update_type = (empty($update_type) && !empty($update['inline_query']['from']['id'])) ? 'inline_query' : $update_type;
$update_id = $update[$update_type]['from']['id'];
// Check each admin chat defined in $access_type
$chats = explode(',', $all_chats);
$chats = array_unique($chats);
// Write to log.
debug_log('Telegram message type: ' . $update_type);
debug_log('Checking access for ID: ' . $update_id);
debug_log('Checking these chats now: ' . implode(',', $chats));
foreach($chats as $chat) {
// Get chat object
debug_log("Getting chat object for '" . $chat . "'");
$chat_obj = get_chat($chat);
// Check chat object for proper response.
if ($chat_obj['ok'] == true) {
debug_log('Proper chat object received, continuing with access check.');
$allow_access = false;
// ID matching $chat and private chat type?
if ($chat_obj['result']['id'] == $update_id && $chat_obj['result']['type'] == "private") {
debug_log('Positive result on access check!');
$allow_access = true;
break;
} else {
// Result was ok, but access not granted. Continue with next chat if type is private.
if ($chat_obj['result']['type'] == "private") {
debug_log('Negative result on access check! Continuing with next chat...');
continue;
}
}
} else {
debug_log('Chat ' . $chat . ' does not exist! Continuing with next chat...');
continue;
}
// Clear chat_obj since it did not match
$chat_obj = '';
// Get chat member object and check status
debug_log("Getting user from chat '" . $chat . "'");
$chat_obj = get_chatmember($chat, $update_id);
// Make sure we get a proper response
if ($chat_obj['ok'] == true) {
// Check user status
if ($chat_obj['result']['user']['id'] == $update_id && ($chat_obj['result']['status'] == 'creator' || $chat_obj['result']['status'] == 'administrator')) {
debug_log('Positive result on access check!');
$allow_access = true;
break;
} else if (BOT_ALLOW_MEMBERS == true) {
// Build chat arrays to check membership
$member_chats = '';
$member_chats = explode(',', BOT_ALLOW_MEMBERS_CHATS);
$member_chats = array_unique($member_chats);
// Allow access if being a member is enough
if (in_array($chat, $member_chats) && $chat_obj['result']['user']['id'] == $update_id && $chat_obj['result']['status'] == 'member') {
debug_log('Positive result on member access check!');
$allow_access = true;
break;
}
}
}
}
// Fallback: Get admins from chats via get_admins method.
if(!$allow_access) {
debug_log('Fallback method: Get admin list from the chats: ' . implode(',', $chats));
foreach($chats as $chat) {
// Clear chat_obj since it did not match
$chat_obj = '';
// Get administrators from chat
debug_log("Getting administrators from chat '" . $chat . "'");
$chat_obj = get_admins($chat);
// Make sure we get a proper response
if ($chat_obj['ok'] == true) {
foreach($chat_obj['result'] as $admin) {
// If user is found as administrator allow access to the bot
if ($admin['user']['id'] == $update_id) {
debug_log('Positive result on access check!');
$allow_access = true;
break 2;
}
}
}
}
}
// Prepare logging of id, username and/or first_name
$msg = '';
$msg .= !empty($update[$update_type]['from']['id']) ? "Id: " . $update[$update_type]['from']['id'] . CR : '';
$msg .= !empty($update[$update_type]['from']['username']) ? "Username: " . $update[$update_type]['from']['username'] . CR : '';
$msg .= !empty($update[$update_type]['from']['first_name']) ? "First Name: " . $update[$update_type]['from']['first_name'] . CR : '';
// Allow or deny access to the bot and log result
if ($allow_access && !$return_result) {
debug_log("Allowing access to the bot for user:" . CR . $msg);
} else if ($allow_access && $return_result) {
debug_log("Allowing access to the bot for user:" . CR . $msg);
return $allow_access;
} else if (!$allow_access && $return_result) {
debug_log("Denying access to the bot for user:" . CR . $msg);
return $allow_access;
} else {
debug_log("Denying access to the bot for user:" . CR . $msg);
$response_msg = '<b>' . getTranslation('bot_access_denied') . '</b>';
// Edit message or send new message based on value of $update_type
if ($update_type == 'callback_query') {
$keys = [];
// Edit message.
edit_message($update, $response_msg, $keys);
// Answer the callback.
answerCallbackQuery($update[$update_type]['id'], getTranslation('bot_access_denied'));
} else {
sendMessage($update[$update_type]['from']['id'], $response_msg);
}
exit;
}
} else {
$msg = '';
$msg .= !empty($update['message']['from']['id']) ? "Id: " . $update['message']['from']['id'] . CR : '';
$msg .= !empty($update['message']['from']['username']) ? "Username: " . $update['message']['from']['username'] . CR : '';
$msg .= !empty($update['message']['from']['first_name']) ? "First Name: " . $update['message']['from']['first_name'] . CR : '';
debug_log("Bot access is not restricted! Allowing access for user: " . CR . $msg);
return true;
}
}
/**
* Quest access check.
* @param $update
* @param $data
* @return bool
*/
function quest_access_check($update, $data, $return_result = false)
{
// Default: Deny access to quests
$quest_access = false;
// Build query.
$rs = my_query(
"
SELECT user_id
FROM quests
WHERE id = {$data['id']}
"
);
$quest = $rs->fetch_assoc();
if ($update['callback_query']['from']['id'] != $quest['user_id']) {
// Build query.
$rs = my_query(
"
SELECT COUNT(*)
FROM users
WHERE user_id = {$update['callback_query']['from']['id']}
AND moderator = 1
"
);
$row = $rs->fetch_row();
if (empty($row['0'])) {
$admin_access = bot_access_check($update, BOT_ADMINS, true);
if ($admin_access) {
// Allow quest access
$quest_access = true;
}
} else {
// Allow quest access
$quest_access = true;
}
} else {
// Allow quest access
$quest_access = true;
}
// Allow or deny access to the quest and log result
if ($quest_access && !$return_result) {
debug_log("Allowing access to the quest");
} else if ($quest_access && $return_result) {
debug_log("Allowing access to the quest");
return $quest_access;
} else if (!$quest_access && $return_result) {
debug_log("Denying access to the quest");
return $quest_access;
} else {
$keys = [];
if (isset($update['callback_query']['inline_message_id'])) {
editMessageText($update['callback_query']['inline_message_id'], '<b>' . getTranslation('quest_access_denied') . '</b>', $keys);
} else {
editMessageText($update['callback_query']['message']['message_id'], '<b>' . getTranslation('quest_access_denied') . '</b>', $keys, $update['callback_query']['message']['chat']['id'], $keys);
}
answerCallbackQuery($update['callback_query']['id'], getTranslation('quest_access_denied'));
exit;
}
}
/**
* Quest duplication check.
* @param $pokestop_id
* @return array
*/
function quest_duplication_check($pokestop_id)
{
// Check if quest already exists for this pokestop.
// Exclude unnamed pokestops with pokestop_id 0.
$rs = my_query(
"
SELECT id, pokestop_id
FROM quests
WHERE quest_date = CURDATE()
AND pokestop_id > 0
AND pokestop_id = {$pokestop_id}
"
);
// Get the row.
$quest = $rs->fetch_assoc();
debug_log($quest);
return $quest;
}
/**
* Get raid level of a pokemon.
* @param $pokedex_id
* @return string
*/
function get_raid_level($pokedex_id)
{
// Make sure $pokedex_id is numeric
if(is_numeric($pokedex_id)) {
// Get raid level from database
$rs = my_query(
"
SELECT raid_level
FROM pokemon
WHERE pokedex_id = $pokedex_id
"
);
$raid_level = '0';
while ($level = $rs->fetch_assoc()) {
$raid_level = $level['raid_level'];
}
} else {
$raid_level = '0';
}
return $raid_level;
}
/**
* Get local name of pokemon.
* @param $pokedex_id
* @param $override_language
* @param $type: raid|quest
* @return string
*/
function get_local_pokemon_name($pokedex_id, $override_language = false, $type = '')
{
// Get translation type
if($override_language == true && $type != '' && ($type == 'raid' || $type == 'quest')) {
$getTypeTranslation = 'get' . ucfirst($type) . 'Translation';
} else {
$getTypeTranslation = 'getTranslation';
}
// Init pokemon name and define fake pokedex ids used for raid eggs
$pokemon_name = '';
$eggs = $GLOBALS['eggs'];
// Get eggs from normal translation.
if(in_array($pokedex_id, $eggs)) {
$pokemon_name = $getTypeTranslation('egg_' . substr($pokedex_id, -1));
} else {
$pokemon_name = $getTypeTranslation('pokemon_id_' . $pokedex_id);
}
// Fallback 1: Valid pokedex id or just a raid egg?
if($pokedex_id === "NULL" || $pokedex_id == 0) {
$pokemon_name = $getTypeTranslation('egg_0');
// Fallback 2: Get original pokemon name from database
} else if(empty($pokemon_name) && $type == 'raid') {
$rs = my_query(
"
SELECT pokemon_name
FROM pokemon
WHERE pokedex_id = $pokedex_id
"
);
while ($pokemon = $rs->fetch_assoc()) {
$pokemon_name = $pokemon['pokemon_name'];
}
}
return $pokemon_name;
}
/**
* Get questlist entry.
* @param $questlist_id
* @return array
*/
function get_questlist_entry($questlist_id)
{
// Get the questlist entry by id.
$rs = my_query(
"
SELECT *
FROM questlist
WHERE id = {$questlist_id}
"
);
// Get the row.
$ql_entry = $rs->fetch_assoc();
debug_log($ql_entry);
return $ql_entry;
}
/**
* Get quest.
* @param $quest_id
* @return array
*/
function get_quest($quest_id)
{
// Get the quest data by id.
$rs = my_query(
"
SELECT quests.*,
users.name,
pokestops.pokestop_name, pokestops.lat, pokestops.lon, pokestops.address,
questlist.quest_type, questlist.quest_quantity, questlist.quest_action,
rewardlist.reward_type, rewardlist.reward_quantity,
encounterlist.pokedex_ids
FROM quests
LEFT JOIN users
ON quests.user_id = users.user_id
LEFT JOIN pokestops
ON quests.pokestop_id = pokestops.id
LEFT JOIN questlist
ON quests.quest_id = questlist.id
LEFT JOIN rewardlist
ON quests.reward_id = rewardlist.id
LEFT JOIN encounterlist
ON quests.quest_id = encounterlist.quest_id
WHERE quests.id = {$quest_id}
"
);
// Get the row.
$quest = $rs->fetch_assoc();
debug_log($quest);
return $quest;
}
/**
* Get quest and reward as formatted string.
* @param $quest array
* @param $add_creator bool
* @param $add_timestamp bool
* @param $compact_format bool
* @param $override_language bool
* @return array
*/
function get_formatted_quest($quest, $add_creator = false, $add_timestamp = false, $compact_format = false, $override_language = false)
{
/** Example:
* Pokestop: Reward-Stop Number 1
* Quest-Street 5, 13579 Poke-City
* Quest: Hatch 1 Egg
* Reward: Magikarp or Onix
*/
// Get translation type
if($override_language == true) {
$getTypeTranslation = 'getQuestTranslation';
} else {
$getTypeTranslation = 'getTranslation';
}
// Pokestop name and address.
$pokestop_name = SP . '<b>' . (!empty($quest['pokestop_name']) ? ($quest['pokestop_name']) : ($getTypeTranslation('unnamed_pokestop'))) . '</b>' . CR;
// Get pokestop info.
$stop = get_pokestop($quest['pokestop_id'], false);
// Add google maps link.
if(!empty($quest['address'])) {
$pokestop_address = '<a href="https://maps.google.com/?daddr=' . $quest['lat'] . ',' . $quest['lon'] . '">' . $quest['address'] . '</a>';
} else if(!empty($stop['address'])) {
$pokestop_address = '<a href="https://maps.google.com/?daddr=' . $stop['lat'] . ',' . $stop['lon'] . '">' . $stop['address'] . '</a>';
} else {
$pokestop_address = '<a href="https://maps.google.com/maps?q=' . $quest['lat'] . ',' . $quest['lon'] . '">https://maps.google.com/maps?q=' . $quest['lat'] . ',' . $quest['lon'] . '</a>';
}
// Quest action: Singular or plural?
$quest_action = explode(":", $getTypeTranslation('quest_action_' . $quest['quest_action']));
$quest_action_singular = $quest_action[0];
$quest_action_plural = $quest_action[1];
$qty_action = $quest['quest_quantity'] . SP . (($quest['quest_quantity'] > 1) ? ($quest_action_plural) : ($quest_action_singular));
// Reward type: Singular or plural?
$reward_type = explode(":", $getTypeTranslation('reward_type_' . $quest['reward_type']));
$reward_type_singular = $reward_type[0];
$reward_type_plural = $reward_type[1];
$qty_reward = $quest['reward_quantity'] . SP . (($quest['reward_quantity'] > 1) ? ($reward_type_plural) : ($reward_type_singular));
// Reward pokemon forecast?
$msg_poke = '';
if($quest['pokedex_ids'] != '0' && $quest['reward_type'] == 1) {
$quest_pokemons = explode(',', $quest['pokedex_ids']);
// Get local pokemon name
foreach($quest_pokemons as $pokedex_id) {
$msg_poke .= ($override_language == true) ? (get_local_pokemon_name($pokedex_id, true, 'quest')) : (get_local_pokemon_name($pokedex_id));
$msg_poke .= ' / ';
}
// Trim last slash
$msg_poke = rtrim($msg_poke,' / ');
$msg_poke = (!empty($msg_poke) ? $msg_poke : '');
}
// Build quest message
$msg = '';
if($compact_format == false) {
$msg .= $getTypeTranslation('pokestop') . ':' . $pokestop_name . $pokestop_address . CR;
$msg .= $getTypeTranslation('quest') . ': <b>' . $getTypeTranslation('quest_type_' . $quest['quest_type']) . SP . $qty_action . '</b>' . CR;
$msg .= $getTypeTranslation('reward') . ': <b>' . (!empty($msg_poke) ? $msg_poke : $qty_reward) . '</b>' . CR;
} else {
$msg .= $getTypeTranslation('quest_type_' . $quest['quest_type']) . SP . $qty_action . ' — ' . (!empty($msg_poke) ? $msg_poke : $qty_reward);
}
//Add custom message from the config.
if (defined('MAP_URL') && !empty(MAP_URL)) {
$msg .= CR . MAP_URL ;
}
// Display creator.
$msg .= ($quest['user_id'] && $add_creator == true) ? (CR . $getTypeTranslation('created_by') . ': <a href="tg://user?id=' . $quest['user_id'] . '">' . htmlspecialchars($quest['name']) . '</a>') : '';
// Add update time and quest id to message.
if($add_timestamp == true) {
$quest_date = explode(' ', $quest['quest_date']);
$msg .= CR . '<i>' . $getTypeTranslation('updated') . ': ' . $quest_date[0] . '</i>';
$msg .= ' ' . substr(strtoupper(BOT_ID), 0, 1) . '-ID = ' . $quest['id']; // DO NOT REMOVE! --> NEEDED FOR CLEANUP PREPARATION!
}
return $msg;
}
/**
* Get today's quests as formatted string.
* @return string
*/
function get_todays_formatted_quests()
{
// Get the quest data by id.
$rs = my_query(
"
SELECT id
FROM quests
WHERE quest_date = CURDATE()
"
);
// Init empty message and counter.
$msg = '';
$count = 0;
// Get the quests.
while ($todays_quests = $rs->fetch_assoc()) {
$quest = get_quest($todays_quests['id']);
$msg .= CR . '<b>' . (!empty($quest['pokestop_name']) ? ($quest['pokestop_name']) : (getTranslation('unnamed_pokestop'))) . '</b>' . CR;
$msg .= get_formatted_quest($quest, false, false, true, false);
$msg .= CR;
$count = $count + 1;
}
// No quests today?
if($count == 0) {
$msg = getTranslation('no_quests_today');
} else {
// Add update time to message.
$msg .= CR . '<i>' . getTranslation('updated') . ': ' . date('H:i:s') . '</i>';
}
return $msg;
}
/**
* Get rewardlist entry.
* @param $reward_id
* @return array
*/
function get_rewardlist_entry($reward_id)
{
// Get the reward data by id.
$rs = my_query(
"
SELECT *
FROM rewardlist
WHERE id = {$reward_id}
"
);
// Get the row.
$reward = $rs->fetch_assoc();
debug_log($reward);
return $reward;
}
/**
* Get encounterlist entry.
* @param $reward_id
* @return array
*/
function get_encounterlist_entry($quest_id)
{
// Get the reward data by id.
$rs = my_query(
"
SELECT pokedex_ids
FROM encounterlist
WHERE quest_id = {$quest_id}
"
);
// Get the row.
$encounters = $rs->fetch_assoc();
debug_log($encounters);
return $encounters;
}
/**
* Delete quest.
* @param $quest_id
*/
function delete_quest($quest_id)
{
global $db;
// Delete telegram messages for quest.
$rs = my_query(
"
SELECT *
FROM qleanup
WHERE quest_id = '{$quest_id}'
AND chat_id <> 0
"
);
// Counter
$counter = 0;
// Delete every telegram message
while ($row = $rs->fetch_assoc()) {
// Delete telegram message.
debug_log('Deleting telegram message ' . $row['message_id'] . ' from chat ' . $row['chat_id'] . ' for quest ' . $row['quest_id']);
delete_message($row['chat_id'], $row['message_id']);
$counter = $counter + 1;
}
// Nothing to delete on telegram.
if ($counter == 0) {
debug_log('Quest with ID ' . $quest_id . ' was not found in the cleanup table! Skipping deletion of telegram messages!');
}
// Delete quest from cleanup table.
debug_log('Deleting quest ' . $quest_id . ' from the cleanup table:');
$rs_cleanup = my_query(
"
DELETE FROM qleanup
WHERE quest_id = '{$quest_id}'
OR cleaned = '{$quest_id}'
"
);
// Delete quest from quest table.
debug_log('Deleting quest ' . $quest_id . ' from the quest table:');
$rs_quests = my_query(
"
DELETE FROM quests
WHERE id = '{$quest_id}'
"
);
}
/**
* Get pokestop.
* @param $pokestop_id
* @return array
*/
function get_pokestop($pokestop_id, $update_pokestop = true)
{
global $db;
// Pokestop from database
if($pokestop_id != 0) {
// Get pokestop from database
$rs = my_query(
"
SELECT *
FROM pokestops
WHERE id = {$pokestop_id}
"
);
$stop = $rs->fetch_assoc();
// Get address and update address string.
if(!empty(GOOGLE_API_KEY) && ($update_pokestop == true || empty($pokestop['address']))) {
// Get address.
$lat = $stop['lat'];
$lon = $stop['lon'];
$addr = get_address($lat, $lon);
// Get full address - Street #, ZIP District
$address = "";
$address .= (!empty($addr['street']) ? $addr['street'] : "");
$address .= (!empty($addr['street_number']) ? " " . $addr['street_number'] : "");
$address .= (!empty($addr) ? ", " : "");
$address .= (!empty($addr['postal_code']) ? $addr['postal_code'] . " " : "");
$address .= (!empty($addr['district']) ? $addr['district'] : "");
// Update pokestop address.
$rs = my_query(
"
UPDATE pokestops
SET address = '{$db->real_escape_string($address)}'
WHERE id = '{$pokestop_id}'
"
);
// Set pokestop address.
$stop['address'] = $address;
}
// Unnamend pokestop
} else {
$stop = 0;
}
debug_log($stop);
return $stop;
}
/**
* Get pokestops starting with the searchterm.
* @param $searchterm
* @return bool|array
*/
function get_pokestop_list_keys($searchterm)
{
// Make sure the search term is not empty
if(!empty($searchterm)) {
// Get pokestop from database
$rs = my_query(
"
SELECT id, pokestop_name
FROM pokestops
WHERE pokestop_name LIKE '$searchterm%'
OR pokestop_name LIKE '%$searchterm%'
ORDER BY
CASE
WHEN pokestop_name LIKE '$searchterm%' THEN 1
WHEN pokestop_name LIKE '%$searchterm%' THEN 2
ELSE 3
END
LIMIT 15
"
);
// Init empty keys array.
$keys = array();
// Add key for each found pokestop
while ($stops = $rs->fetch_assoc()) {
// Pokestop name.
$pokestop_name = (!empty($stops['pokestop_name']) ? ($stops['pokestop_name']) : (getTranslation('unnamed_pokestop')));
// Add keys.
$keys[] = array(
'text' => $pokestop_name,
'callback_data' => $stops['id'] . ':quest_create:0'
);
}
if($keys) {
// Get the inline key array.
$keys = inline_key_array($keys, 1);
} else {
$keys = true;
}
} else {
// Return false.
$keys = false;
}
return $keys;
}
/**
* Get pokestops within radius around lat/lon.
* @param $lat
* @param $lon
* @param $radius
* @return array
*/
function get_pokestops_in_radius_keys($lat, $lon, $radius)
{
$radius = $radius / 1000;
// Get all pokestop within the radius
$rs = my_query(
" SELECT id, pokestop_name,
(
6371 *
acos(
cos(radians({$lat})) *
cos(radians(lat)) *
cos(
radians(lon) - radians({$lon})
) +
sin(radians({$lat})) *
sin(radians(lat))
)
) AS distance
FROM pokestops
HAVING distance < {$radius}
ORDER BY distance
LIMIT 10
"
);
// Init empty keys array.
$keys = array();
// Add key for each found pokestop
while ($stops = $rs->fetch_assoc()) {
// Pokestop name.
$pokestop_name = (!empty($stops['pokestop_name']) ? ($stops['pokestop_name']) : (getTranslation('unnamed_pokestop')));
// Add keys.
$keys[] = array(
'text' => $pokestop_name,
'callback_data' => $stops['id'] . ':quest_create:0'
);
}
// Add unknown pokestop.
//$unknown_keys = array();
//$unknown_keys[] = universal_inner_key($keys, '0', 'quest_create', $lat . ',' . $lon, getTranslation('unnamed_pokestop'));
// Inline keys.
$keys = inline_key_array($keys, 1);
//$keys[] = $unknown_keys;
return $keys;
}
/**
* Get gym.
* @param $id
* @return array
*/
function get_gym($id)
{
// Get gym from database
$rs = my_query(
"
SELECT *
FROM gyms
WHERE id = {$id}
"
);
$gym = $rs->fetch_assoc();
return $gym;
}
/**
* Get pokemon info as formatted string.
* @param $pokedex_id
* @return array
*/
function get_pokemon_info($pokedex_id)
{
/** Example:
* Raid boss: Mewtwo (#ID)
* Weather: Icons
* CP: CP values (Boosted CP values)
*/
$info = '';
$info .= getTranslation('raid_boss') . ': <b>' . get_local_pokemon_name($pokedex_id) . ' (#' . $pokedex_id . ')</b>' . CR . CR;
$poke_raid_level = get_raid_level($pokedex_id);
$poke_cp = get_formatted_pokemon_cp($pokedex_id);
$poke_weather = get_pokemon_weather($pokedex_id);
$info .= getTranslation('pokedex_raid_level') . ': ' . getTranslation($poke_raid_level . 'stars') . CR;
$info .= (empty($poke_cp)) ? (getTranslation('pokedex_cp') . CR) : $poke_cp . CR;
$info .= getTranslation('pokedex_weather') . ': ' . get_weather_icons($poke_weather) . CR . CR;
return $info;
}
/**
* Get pokemon cp values.
* @param $pokedex_id
* @return array
*/
function get_pokemon_cp($pokedex_id)
{
// Get gyms from database
$rs = my_query(
"
SELECT min_cp, max_cp, min_weather_cp, max_weather_cp
FROM pokemon
WHERE pokedex_id = {$pokedex_id}
"
);
$cp = $rs->fetch_assoc();
return $cp;
}
/**
* Get formatted pokemon cp values.
* @param $pokedex_id
* @param $override_language
* @return string
*/
function get_formatted_pokemon_cp($pokedex_id, $override_language = false)
{
// Init cp text.
$cp20 = '';
$cp25 = '';
// Valid pokedex id?
if($pokedex_id !== "NULL" && $pokedex_id != 0) {
// Get gyms from database
$rs = my_query(
"
SELECT min_cp, max_cp, min_weather_cp, max_weather_cp
FROM pokemon
WHERE pokedex_id = {$pokedex_id}
"
);
while($row = $rs->fetch_assoc()) {
// CP
$cp20 .= ($row['min_cp'] > 0) ? $row['min_cp'] : '';
$cp20 .= (!empty($cp20) && $cp20 > 0) ? ('/' . $row['max_cp']) : ($row['max_cp']);
// Weather boosted CP
$cp25 .= ($row['min_weather_cp'] > 0) ? $row['min_weather_cp'] : '';
$cp25 .= (!empty($cp25) && $cp25 > 0) ? ('/' . $row['max_weather_cp']) : ($row['max_weather_cp']);
}
}
// Combine CP and weather boosted CP
$text = ($override_language == true) ? (getRaidTranslation('pokedex_cp')) : (getTranslation('pokedex_cp'));
$cp = (!empty($cp20)) ? ($text . ' <b>' . $cp20 . '</b>') : '';
$cp .= (!empty($cp25)) ? (' (' . $cp25 . ')') : '';
return $cp;
}
/**
* Get pokemon weather.
* @param $pokedex_id
* @return string
*/
function get_pokemon_weather($pokedex_id)
{
if($pokedex_id !== "NULL" && $pokedex_id != 0) {
// Get pokemon weather from database
$rs = my_query(
"
SELECT weather
FROM pokemon
WHERE pokedex_id = {$pokedex_id}
"
);
// Fetch the row.
$ww = $rs->fetch_assoc();
return $ww['weather'];
} else {
return 0;
}
}
/**
* Get weather icons.
* @param $weather_value
* @return string
*/
function get_weather_icons($weather_value)
{
if($weather_value > 0) {
// Get length of arg and split arg
$weather_value_length = strlen((string)$weather_value);
$weather_value_string = str_split((string)$weather_value);
// Init weather icons string.
$weather_icons = '';
// Add icons to string.
for ($i = 0; $i < $weather_value_length; $i = $i + 1) {
// Get weather icon from constants
$weather_icons .= $GLOBALS['weather'][$weather_value_string[$i]];
$weather_icons .= ' ';
}
// Trim space after last icon
$weather_icons = rtrim($weather_icons);
} else {
$weather_icons = '';
}
return $weather_icons;
}
/**
* Get user.
* @param $user_id
* @return message
*/
function get_user($user_id)
{
// Get user details.
$rs = my_query(
"
SELECT *
FROM users
WHERE user_id = {$user_id}
"
);
// Fetch the row.
$row = $rs->fetch_assoc();
// Build message string.
$msg = '';
// Add name.
$msg .= 'Name: <a href="tg://user?id=' . $row['user_id'] . '">' . htmlspecialchars($row['name']) . '</a>' . CR;
// Unknown team.
if ($row['team'] === NULL) {
$msg .= 'Team: ' . $GLOBALS['teams']['unknown'] . CR;
// Known team.
} else {
$msg .= 'Team: ' . $GLOBALS['teams'][$row['team']] . CR;
}