forked from pokepark/PokemonRaidBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.php
More file actions
3897 lines (3377 loc) · 136 KB
/
logic.php
File metadata and controls
3897 lines (3377 loc) · 136 KB
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
/**
* Raid access check.
* @param $update
* @param $data
* @return bool
*/
function raid_access_check($update, $data, $permission, $return_result = false)
{
// Default: Deny access to raids
$raid_access = false;
// Build query.
$rs = my_query(
"
SELECT user_id
FROM raids
WHERE id = {$data['id']}
"
);
$raid = $rs->fetch_assoc();
// Check permissions
if ($update['callback_query']['from']['id'] != $raid['user_id']) {
// Check "-all" permission
debug_log('Checking permission:' . $permission . '-all');
$permission = $permission . '-all';
$raid_access = bot_access_check($update, $permission, $return_result);
} else {
// Check "-own" permission
debug_log('Checking permission:' . $permission . '-own');
$permission_own = $permission . '-own';
$permission_all = $permission . '-all';
$raid_access = bot_access_check($update, $permission_own, true);
// Check "-all" permission if we get "access denied"
// Maybe necessary if user has only "-all" configured, but not "-own"
if(!$raid_access) {
debug_log('Permission check for ' . $permission_own . ' failed! Maybe the access is just granted via ' . $permission . '-all ?');
debug_log('Checking permission:' . $permission_all);
$raid_access = bot_access_check($update, $permission_all, $return_result);
} else {
$raid_access = bot_access_check($update, $permission_own, $return_result);
}
}
// Return result
return $raid_access;
}
/**
* Active raid duplication check.
* @param $gym_id
* @return string
*/
function active_raid_duplication_check($gym_id)
{
debug_log('Running duplication check');
// Build query.
$rs = my_query(
"
SELECT id, pokemon, count(gym_id) AS active_raid
FROM raids
WHERE end_time > (UTC_TIMESTAMP() - INTERVAL 10 MINUTE)
AND gym_id = {$gym_id}
GROUP BY id
"
);
// Init counter and raid id.
$active_counter = 0;
$active_raid_id = 0;
// Get row - allow normal and ex-raid at the gym.
if(RAID_EXCLUDE_EXRAID_DUPLICATION == true) {
while ($raid = $rs->fetch_assoc()) {
$active = $raid['active_raid'];
if ($active > 0) {
// Exclude ex-raid pokemon.
$raid_level = get_raid_level($raid['pokemon']);
if($raid_level == 'X') {
continue;
} else {
$active_raid_id = $raid['id'];
$active_counter = $active_counter + 1;
break;
}
// No active raids.
} else {
break;
}
}
} else {
$raid = $rs->fetch_assoc();
$active_counter = $raid['active_raid'];
$active_raid_id = $raid['id'];
}
// Return 0 or raid id
if ($active_counter > 0) {
return $active_raid_id;
} else {
return 0;
}
}
/**
* Insert gym.
* @param $gym_name
* @param $latitude
* @param $longitude
* @param $address
*/
function insert_gym($name, $lat, $lon, $address)
{
global $db;
// Build query to check if gym is already in database or not
$rs = my_query(
"
SELECT COUNT(*)
FROM gyms
WHERE gym_name = '{$name}'
"
);
$row = $rs->fetch_row();
// Gym already in database or new
if (empty($row['0'])) {
// Build query for gyms table to add gym to database
debug_log('Gym not found in database gym list! Adding gym "' . $name . '" to the database gym list.');
$rs = my_query(
"
INSERT INTO gyms
SET lat = '{$lat}',
lon = '{$lon}',
gym_name = '{$db->real_escape_string($name)}',
address = '{$db->real_escape_string($address)}'
"
);
} else {
// Update gyms table to reflect gym changes.
debug_log('Gym found in database gym list! Updating gym "' . $name . '" now.');
$rs = my_query(
"
UPDATE gyms
SET lat = '{$lat}',
lon = '{$lon}',
address = '{$db->real_escape_string($address)}'
WHERE gym_name = '{$name}'
"
);
}
}
/**
* Disable raids for level.
* @param $id
* @return array
*/
function disable_raid_level($id)
{
// Get gym from database
$rs = my_query(
"
UPDATE pokemon
SET raid_level = '0'
WHERE raid_level IN ({$id})
"
);
}
/**
* Get raid level of a pokemon.
* @param $pokedex_id
* @return string
*/
function get_raid_level($pokedex_id)
{
// Split pokedex_id and form
$dex_id_form = explode('-',$pokedex_id);
$dex_id = $dex_id_form[0];
$dex_form = $dex_id_form[1];
// Make sure $dex_id is numeric
if(is_numeric($dex_id)) {
// Get raid level from database
$rs = my_query(
"
SELECT raid_level
FROM pokemon
WHERE pokedex_id = {$dex_id}
AND pokemon_form = '{$dex_form}'
"
);
$raid_level = '0';
while ($level = $rs->fetch_assoc()) {
$raid_level = $level['raid_level'];
}
} else {
$raid_level = '0';
}
return $raid_level;
}
/**
* Get raid data.
* @param $raid_id
* @return array
*/
function get_raid($raid_id)
{
// Get the raid data by id.
$rs = my_query(
"
SELECT raids.*,
gyms.lat, gyms.lon, gyms.address, gyms.gym_name, gyms.ex_gym, gyms.gym_note,
users.name,
TIME_FORMAT(TIMEDIFF(end_time, UTC_TIMESTAMP()) + INTERVAL 1 MINUTE, '%k:%i') AS t_left,
TIMESTAMPDIFF(MINUTE,raids.start_time,raids.end_time) as t_duration
FROM raids
LEFT JOIN gyms
ON raids.gym_id = gyms.id
LEFT JOIN users
ON raids.user_id = users.user_id
WHERE raids.id = {$raid_id}
"
);
// Get the row.
$raid = $rs->fetch_assoc();
debug_log($raid);
return $raid;
}
/**
* Get last 20 active raids.
* @return array
*/
function get_active_raids()
{
// Get last 20 active raids data.
$rs = my_query(
"
SELECT raids.*,
gyms.lat, gyms.lon, gyms.address, gyms.gym_name, gyms.ex_gym, gyms.gym_note,
start_time, end_time,
TIME_FORMAT(TIMEDIFF(end_time, UTC_TIMESTAMP()) + INTERVAL 1 MINUTE, '%k:%i') AS t_left
FROM raids
LEFT JOIN gyms
ON raids.gym_id = gyms.id
WHERE end_time>UTC_TIMESTAMP()
ORDER BY end_time ASC LIMIT 20
"
);
// Get the raids.
$raids = $rs->fetch_assoc();
debug_log($raids);
return $raids;
}
/**
* Get pokedex id by name of pokemon.
* @param $pokemon_name
* @return string
*/
function get_pokemon_id_by_name($pokemon_name)
{
// Init id and write name to search to log.
$pokemon_id = 0;
$pokemon_form = 'normal';
debug_log($pokemon_name,'P:');
// Explode pokemon name in case we have a form too.
$delimiter = '';
if(strpos($pokemon_name, ' ') !== false) {
$delimiter = ' ';
} else if (strpos($pokemon_name, '-') !== false) {
$delimiter = '-';
} else if (strpos($pokemon_name, ',') !== false) {
$delimiter = ',';
}
// Explode if delimiter was found.
$poke_name = $pokemon_name;
if($delimiter != '') {
$pokemon_name_form = explode($delimiter,$pokemon_name,2);
$poke_name = trim($pokemon_name_form[0]);
$poke_name = strtolower($poke_name);
$poke_form = trim($pokemon_name_form[1]);
$poke_form = strtolower($poke_form);
debug_log($poke_name,'P NAME:');
debug_log($poke_form,'P FORM:');
}
// Set language
$language = USERLANGUAGE;
// Make sure file exists, otherwise use English language as fallback.
if(!is_file(CORE_LANG_PATH . '/pokemon_' . strtolower($language) . '.json')) {
$language = 'EN';
}
// Get translation file
$str = file_get_contents(CORE_LANG_PATH . '/pokemon_' . strtolower($language) . '.json');
$json = json_decode($str, true);
// Search pokemon name in json
$key = array_search(ucfirst($poke_name), $json);
if($key !== FALSE) {
// Index starts at 0, so key + 1 for the correct id!
$pokemon_id = $key + 1;
} else {
// Try English language as fallback to get the pokemon id.
$str = file_get_contents(CORE_LANG_PATH . '/pokemon_' . strtolower(DEFAULT_LANGUAGE) . '.json');
$json = json_decode($str, true);
// Search pokemon name in json
$key = array_search(ucfirst($poke_name), $json);
if($key !== FALSE) {
// Index starts at 0, so key + 1 for the correct id!
$pokemon_id = $key + 1;
} else {
// Debug log.
debug_log('Error! Pokedex ID could not be found for pokemon with name: ' . $poke_name);
}
}
// Get form.
// Works like this: Search form in language file via language, e.g. 'DE' and local form translation, e.g. 'Alola' for 'DE'.
// In additon we are searching the DEFAULT_LANGUAGE and the key name for the form name.
// Once we found the key name, e.g. 'pokemon_form_attack', get the form name 'attack' from it via str_replace'ing the prefix 'pokemon_form'.
if($pokemon_id != 0 && isset($poke_form) && !empty($poke_form) && $poke_form != 'normal') {
debug_log('Searching for pokemon form: ' . $poke_form);
// Get forms translation file
$str_form = file_get_contents(CORE_LANG_PATH . '/pokemon_forms.json');
$json_form = json_decode($str_form, true);
// Search pokemon form in json
foreach($json_form as $key_form => $jform) {
// Stop search if we found it.
if ($jform[$language] === ucfirst($poke_form)) {
$pokemon_form = str_replace('pokemon_form_','',$key_form);
debug_log('Found pokemon form by user language: ' . $language);
break;
// Try DEFAULT_LANGUAGE too.
} else if ($jform[DEFAULT_LANGUAGE] === ucfirst($poke_form)) {
$pokemon_form = str_replace('pokemon_form_','',$key_form);
debug_log('Found pokemon form by default language: ' . DEFAULT_LANGUAGE);
break;
// Try key name.
} else if ($key_form === ('pokemon_form_' . $poke_form)) {
$pokemon_form = str_replace('pokemon_form_','',$key_form);
debug_log('Found pokemon form by json key name: pokemon_form_' . $key_form);
break;
}
}
}
// Write to log.
debug_log($pokemon_id,'P:');
debug_log($pokemon_form,'P:');
// Set pokemon form.
$pokemon_id = $pokemon_id . '-' . $pokemon_form;
// Return pokemon_id
return $pokemon_id;
}
/**
* Get local name of pokemon.
* @param $pokemon_id_form
* @param $override_language
* @return string
*/
function get_local_pokemon_name($pokemon_id_form, $override_language = false)
{
// Split pokedex_id and form
$dex_id_form = explode('-',$pokemon_id_form);
$pokedex_id = $dex_id_form[0];
$pokemon_form = $dex_id_form[1];
debug_log('Pokemon_form: ' . $pokemon_form);
// Get translation type
if($override_language == true) {
$getTypeTranslation = 'getPublicTranslation';
} 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 if ($pokemon_form != 'normal') {
$pokemon_name = $getTypeTranslation('pokemon_id_' . $pokedex_id);
$pokemon_name = (!empty($pokemon_name)) ? ($pokemon_name . SP . $getTypeTranslation('pokemon_form_' . $pokemon_form)) : '';
} 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)) {
$rs = my_query(
"
SELECT pokemon_name, pokemon_form
FROM pokemon
WHERE pokedex_id = {$pokedex_id}
AND pokemon_form = '{$pokemon_form}'
"
);
while ($pokemon = $rs->fetch_assoc()) {
// Pokemon name
$pokemon_name = $pokemon['pokemon_name'];
// Pokemon form
if(!empty($pokemon['pokemon_form']) && $pokemon['pokemon_form'] != 'normal') {
$pokemon_form = $getTypeTranslation('pokemon_form_' . $pokemon['pokemon_form']);
$pokemon_name = (!empty($pokemon_form)) ? ($pokemon_name . SP . $pokemon_form) : ($pokemon_name . SP . ucfirst($pokemon['pokemon_form']));
}
}
}
return $pokemon_name;
}
/**
* 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 gym by telegram id.
* @param $id
* @return array
*/
function get_gym_by_telegram_id($id)
{
// Get gym from database
$rs = my_query(
"
SELECT *
FROM gyms
WHERE gym_name = '{$id}'
ORDER BY id DESC
LIMIT 1
"
);
$gym = $rs->fetch_assoc();
return $gym;
}
/**
* Delete gym.
* @param $id
* @return array
*/
function delete_gym($id)
{
// Get gym from database
$rs = my_query(
"
DELETE FROM gyms
WHERE id = {$id}
"
);
}
/**
* Get gym details.
* @param $gym
* @param $extended
* @return string
*/
function get_gym_details($gym, $extended = false)
{
// Add gym name to message.
$msg = '<b>' . getTranslation('gym_details') . ':</b>' . CR . CR;
$msg .= '<b>ID = ' . $gym['id'] . '</b>' . CR;
$msg .= getTranslation('gym') . ':' . SP;
$ex_raid_gym_marker = (strtolower(RAID_EX_GYM_MARKER) == 'icon') ? EMOJI_STAR : '<b>' . RAID_EX_GYM_MARKER . '</b>';
$msg .= ($gym['ex_gym'] ? $ex_raid_gym_marker . SP : '') . '<b>' . $gym['gym_name'] . '</b>';
$msg .= CR;
// Add maps link to message.
if (!empty($gym['address'])) {
$msg .= '<a href="https://maps.google.com/?daddr=' . $gym['lat'] . ',' . $gym['lon'] . '">' . $gym['address'] . '</a>' . CR;
} else {
// Get the address.
$addr = get_address($gym['lat'], $gym['lon']);
$address = format_address($addr);
//Only store address if not empty
if(!empty($address)) {
//Use new address
$msg .= '<a href="https://maps.google.com/?daddr=' . $gym['lat'] . ',' . $gym['lon'] . '">' . $address . '</a>' . CR;
} else {
//If no address is found show maps link
$msg .= '<a href="http://maps.google.com/maps?q=' . $gym['lat'] . ',' . $gym['lon'] . '">http://maps.google.com/maps?q=' . $gym['lat'] . ',' . $gym['lon'] . '</a>' . CR;
}
}
// Add or hide gym note.
if(!empty($gym['gym_note'])) {
$msg .= EMOJI_INFO . SP . $gym['gym_note'];
}
// Get extended gym details?
if($extended == true) {
$msg .= CR . '<b>' . getTranslation('extended_gym_details') . '</b>';
// Normal gym?
if($gym['ex_gym'] == 1) {
$msg .= CR . '-' . SP . getTranslation('ex_gym');
}
// Hidden gym?
if($gym['show_gym'] == 1 && $gym['ex_gym'] == 0) {
$msg .= CR . '-' . SP . getTranslation('normal_gym');
} else if($gym['show_gym'] == 0) {
$msg .= CR . '-' . SP . getTranslation('hidden_gym');
}
}
return $msg;
}
/**
* Get pokemon info as formatted string.
* @param $pokemon_id_form
* @return array
*/
function get_pokemon_info($pokemon_id_form)
{
// Split pokedex_id and form
$dex_id_form = explode('-',$pokemon_id_form);
$pokedex_id = $dex_id_form[0];
$pokemon_form = $dex_id_form[1];
/** Example:
* Raid boss: Mewtwo (#ID)
* Weather: Icons
* CP: CP values (Boosted CP values)
*/
$info = '';
$info .= getTranslation('raid_boss') . ': <b>' . get_local_pokemon_name($pokemon_id_form) . ' (#' . $pokedex_id . ')</b>' . CR . CR;
$poke_raid_level = get_raid_level($pokemon_id_form);
$poke_cp = get_formatted_pokemon_cp($pokemon_id_form);
$poke_weather = get_pokemon_weather($pokemon_id_form);
$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 $pokemon_id_form
* @return array
*/
function get_pokemon_cp($pokemon_id_form)
{
// Split pokedex_id and form
$dex_id_form = explode('-',$pokemon_id_form);
$pokedex_id = $dex_id_form[0];
$pokemon_form = $dex_id_form[1];
// 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}
AND pokemon_form = '{$pokemon_form}'
"
);
$cp = $rs->fetch_assoc();
return $cp;
}
/**
* Get formatted pokemon cp values.
* @param $pokemon_id_form
* @param $override_language
* @return string
*/
function get_formatted_pokemon_cp($pokemon_id_form, $override_language = false)
{
// Split pokedex_id and form
$dex_id_form = explode('-',$pokemon_id_form);
$pokedex_id = $dex_id_form[0];
$pokemon_form = $dex_id_form[1];
// 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}
AND pokemon_form = '{$pokemon_form}'
"
);
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) ? (getPublicTranslation('pokedex_cp')) : (getTranslation('pokedex_cp'));
$cp = (!empty($cp20)) ? ($text . ' <b>' . $cp20 . '</b>') : '';
$cp .= (!empty($cp25)) ? (' (' . $cp25 . ')') : '';
return $cp;
}
/**
* Get pokemon weather.
* @param $pokemon_id_form
* @return string
*/
function get_pokemon_weather($pokemon_id_form)
{
// Split pokedex_id and form
$dex_id_form = explode('-',$pokemon_id_form);
$pokedex_id = $dex_id_form[0];
$pokemon_form = $dex_id_form[1];
if($pokedex_id !== "NULL" && $pokedex_id != 0) {
// Get pokemon weather from database
$rs = my_query(
"
SELECT weather
FROM pokemon
WHERE pokedex_id = {$pokedex_id}
AND pokemon_form = '{$pokemon_form}'
"
);
// 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;
}
// Add level.
if ($row['level'] != 0) {
$msg .= 'Level: <b>' . $row['level'] . '</b>' . CR;
}
return $msg;
}
/**
* Raid edit start keys.
* @param $gym_id
* @param $gym_first_letter
* @param $admin
* @return array
*/
function raid_edit_raidlevel_keys($gym_id, $gym_first_letter, $admin = false)
{
// Get all raid levels from database
$rs = my_query(
"
SELECT raid_level, COUNT(*) AS raid_level_count
FROM pokemon
WHERE raid_level != '0'
GROUP BY raid_level
ORDER BY FIELD(raid_level, '5', '4', '3', '2', '1', 'X')
"
);
// Init empty keys array.
$keys = [];
// Add key for each raid level
while ($level = $rs->fetch_assoc()) {
// Continue if user is not part of the BOT_ADMINS and raid_level is X
if($level['raid_level'] == 'X' && $admin === false) continue;
// Add key for pokemon if we have just 1 pokemon for a level
if($level['raid_level_count'] == 1) {
// Raid level and aciton
$raid_level = $level['raid_level'];
// Get pokemon from database
$rs_rl = my_query(
"
SELECT pokedex_id, pokemon_form
FROM pokemon
WHERE raid_level = '{$raid_level}'
"
);
// Add key for pokemon
while ($pokemon = $rs_rl->fetch_assoc()) {
$keys[] = array(
'text' => get_local_pokemon_name($pokemon['pokedex_id'] . '-' . $pokemon['pokemon_form']),
'callback_data' => $gym_id . ',' . $gym_first_letter . ':edit_starttime:' . $pokemon['pokedex_id'] . '-' . $pokemon['pokemon_form']
);
}
} else {
// Add key for raid level
$keys[] = array(
'text' => getTranslation($level['raid_level'] . 'stars'),
'callback_data' => $gym_id . ',' . $gym_first_letter . ':edit_pokemon:' . $level['raid_level']
);
}
}
// Get the inline key array.
$keys = inline_key_array($keys, 3);
return $keys;
}
/**
* Raid gym first letter selection
* @param $action
* @param $hidden
* @return array
*/
function raid_edit_gyms_first_letter_keys($action = 'raid_by_gym', $hidden = false)
{
// Special/Custom gym letters?
$case = '';
if(defined('RAID_CUSTOM_GYM_LETTERS') && !empty(RAID_CUSTOM_GYM_LETTERS)) {
// Explode special letters.
$special_keys = explode(',', RAID_CUSTOM_GYM_LETTERS);
foreach($special_keys as $id => $letter)
{
$letter = trim($letter);
debug_log($letter, 'Special gym letter:');
// Fix chinese chars, prior: $length = strlen($letter);
$length = strlen(utf8_decode($letter));
$case .= SP . "WHEN UPPER(LEFT(gym_name, " . $length . ")) = '" . $letter . "' THEN UPPER(LEFT(gym_name, " . $length . "))" . SP;
}
}
// Show hidden gyms?
if($hidden == true) {
$show_gym = 0;
} else {
$show_gym = 1;
}
// Case or not?
if(!empty($case)) {
// Get gyms from database
$rs = my_query(
"
SELECT CASE $case
ELSE UPPER(LEFT(gym_name, 1))
END AS first_letter
FROM gyms
WHERE show_gym = {$show_gym}
GROUP BY 1
ORDER BY gym_name
"
);
} else {
// Get gyms from database
$rs = my_query(
"
SELECT DISTINCT UPPER(SUBSTR(gym_name, 1, 1)) AS first_letter
FROM gyms
WHERE show_gym = {$show_gym}
ORDER BY 1
"
);
}
// Init empty keys array.
$keys = [];
while ($gym = $rs->fetch_assoc()) {
// Add first letter to keys array
$keys[] = array(
'text' => $gym['first_letter'],
'callback_data' => $show_gym . ':' . $action . ':' . $gym['first_letter']
);
}
// Get the inline key array.
$keys = inline_key_array($keys, 4);
// Add back navigation key.
if($hidden == false) {
$nav_keys = [];
$nav_keys[] = universal_inner_key($keys, '0', 'exit', '0', getTranslation('abort'));
// Get the inline key array.
$keys[] = $nav_keys;
}
return $keys;
}
/**
* Raid edit gym keys with active raids marker.
* @param $first
* @param $warn
* @param $action
* @param $delete
* @param $hidden
* @return array
*/
function raid_edit_gym_keys($first, $warn = true, $action = 'edit_raidlevel', $delete = false, $hidden = false)
{
// Length of first letter.
// Fix chinese chars, prior: $first_length = strlen($first);
$first_length = strlen(utf8_decode($first));
// Special/Custom gym letters?
$not = '';
if(defined('RAID_CUSTOM_GYM_LETTERS') && !empty(RAID_CUSTOM_GYM_LETTERS) && $first_length == 1) {
// Explode special letters.
$special_keys = explode(',', RAID_CUSTOM_GYM_LETTERS);
foreach($special_keys as $id => $letter)
{
$letter = trim($letter);
debug_log($letter, 'Special gym letter:');
// Fix chinese chars, prior: $length = strlen($letter);
$length = strlen(utf8_decode($letter));
$not .= SP . "AND UPPER(LEFT(gym_name, " . $length . ")) != UPPER('" . $letter . "')" . SP;
}
}
// Show hidden gyms?
if($hidden == true) {
$show_gym = 0;
} else {
$show_gym = 1;
}
// Exclude ex-raids?
$exraid_exclude = '';
if(RAID_EXCLUDE_EXRAID_DUPLICATION == true) {
$exraid_exclude = "pokemon.raid_level <> 'X' AND ";
}
// Get gyms from database
$rs = my_query(
"
SELECT gyms.id, gyms.gym_name, gyms.ex_gym,
CASE WHEN SUM($exraid_exclude raids.end_time > UTC_TIMESTAMP() - INTERVAL 10 MINUTE) THEN 1 ELSE 0 END AS active_raid
FROM gyms
LEFT JOIN raids
ON raids.gym_id = gyms.id
LEFT JOIN pokemon
ON raids.pokemon = CONCAT(pokemon.pokedex_id, '-', pokemon.pokemon_form)
WHERE UPPER(LEFT(gym_name, $first_length)) = UPPER('{$first}')
$not
AND gyms.show_gym = {$show_gym}
GROUP BY gym_name, raids.gym_id, gyms.id
ORDER BY gym_name
"
);
// Init empty keys array.
$keys = [];
while ($gym = $rs->fetch_assoc()) {
// Add delete argument to keys
if ($delete == true) {
$arg = $gym['id'] . '-delete';
} else {
$arg = $gym['id'];
}
// Write to log.
// debug_log($gym);
// No active raid OR only active ex-raid
if($gym['active_raid'] == 0 || $warn = false) {
// Show Ex-Gym-Marker?