-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.php
2027 lines (1635 loc) · 126 KB
/
bot.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
//ini_set('display_errors', 1);
include 'config.php';
header('Content-Type: text/html; charset=utf-8');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ERROR | E_PARSE);
$api = 'https://api.telegram.org/bot'.$tg_bot_token;
$input = file_get_contents('php://input');
$output = json_decode($input, TRUE); //сюда приходят все запросы по вебхука
//телеграмные события
$chat_id = isset($output['message']['chat']['id']) ? $output['message']['chat']['id'] : 'chat_id_empty'; //отделяем id чата, откуда идет обращение к боту
$chat = isset($output['message']['chat']['title']) ? $output['message']['chat']['title'] : 'chat_title_empty';
$chat_type = isset($output['message']['chat']['type']) ? $output['message']['chat']['type'] : 'chat_type_empty';
$new_chat_title = isset($output['message']['new_chat_title']) ? $output['message']['new_chat_title'] : 'new_chat_title_empty';
$message = isset($output['message']['text']) ? $output['message']['text'] : 'message_text_empty'; //сам текст сообщения
$user = isset($output['message']['from']['username']) ? $output['message']['from']['username'] : 'origin_user_empty';
$user_language_code = isset($output['message']['from']['language_code']) ? $output['message']['from']['language_code'] : 'no_language_set';
$user_id = isset($output['message']['from']['id']) ? $output['message']['from']['id'] : 'origin_user_id_empty';
$message_id = isset($output['message']['message_id']) ? $output['message']['message_id'] : 'message_id_empty';
$dice = isset($output['message']['dice']) ? $output['message']['dice'] : 'dice_empty';
$dice_emoji = isset($output['message']['dice']['emoji']) ? $output['message']['dice']['emoji'] : 'dice_emoji_empty';
$dice_result = isset($output['message']['dice']['value']) ? $output['message']['dice']['value'] : 'dice_value_empty';
$dice_chat = NULL;
$callback_query = isset($output['callback_query']) ? $output['callback_query'] : 'callback_query_empty'; //сюда получаем все, что приходит от inline клавиатуры
$callback_id = isset($callback_query['id']) ? $callback_query['id'] : 'callback_id_empty';
$callback_data = isset($callback_query['data']) ? $callback_query['data'] : 'callback_data_empty'; //ответ от клавиатуры идет сюда
$callback_chat_id = isset($callback_query['message']['chat']['id']) ? $callback_query['message']['chat']['id'] : 'callback_chat_id_empty'; //id чата, где был вызов клавиатуры
$callback_user_id = isset($callback_query['from']['id']) ? $callback_query['from']['id'] : 'callback_user_id_empty'; //id чата, где был вызов клавиатуры
$callback_message_text = isset($callback_query['message']['text']) ? $callback_query['message']['text'] : 'callback_message_text_empty'; //оригинальное сообщение с клавой
$callback_message_id = isset($callback_query['message']['message_id']) ? $callback_query['message']['message_id'] : 'callback_message_id_empty'; //id того сообщения, в котором нажата кнопка клавиатуры
echo "Init successful.\n";
//----------------------------------------------------------------------------------------------------------------------------------//
$markdownify_array = [
//In all other places characters '_‘, ’*‘, ’[‘, ’]‘, ’(‘, ’)‘, ’~‘, ’`‘, ’>‘, ’#‘, ’+‘, ’-‘, ’=‘, ’|‘, ’{‘, ’}‘, ’.‘, ’!‘ must be escaped with the preceding character ’\'.
'>' => "\>",
'#' => "\#",
'+' => "\+",
'-' => "\-",
'=' => "\=",
'|' => "\|",
'{' => "\{",
'}' => "\}",
'.' => "\.",
'!' => "\!",
'_' => "\_",
'*' => "\*",
'[' => "\[",
']' => "\]",
'(' => "\(",
')' => "\)",
'~' => "\~",
'`' => "\`",
];
if ($message == '/start' && $chat_id > 0) {
switch ($user_language_code) {
case 'ru':
sendMessage($chat_id, "Чтобы начать собирать статистику по 🎲, 🎯 и 🏀 добавьте меня в любой чат\.", NULL);
break;
default:
sendMessage($chat_id, "To start gathering 🎲, 🎯 and 🏀 results, add me to any chat\.", NULL);
}
}
if ($dice !== 'dice_empty') {
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
// error_log('1');
$query = mysqli_query($db, "select status, count, type from tournament_status where chat_id=".$chat_id);
$tournament_status = mysqli_fetch_all($query, MYSQLI_ASSOC);
if ($tournament_status[0]['status'] == 'ended' || $tournament_status[0]['status'] == 'never_ran' || $tournament_status[0] == NULL) {
// error_log('2');
if ($dice_emoji == "🎲") {
$query = mysqli_query($db, "select distinct chat_id from dice_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id from dice_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
}
if (in_array($user_id, $dice_user)) {
mysqli_query($db, "update dice_stats set dice_".$dice_result."=dice_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
mysqli_query($db, "insert into dice_stats (chat_id, user_id, dice_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into dice_stats (chat_id, user_id, dice_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
if ($tournament_status[0] == NULL) {
mysqli_query($db, "insert into tournament_status (chat_id) values (".$chat_id.")");
}
}
} elseif ($dice_emoji == "🎯") {
$query = mysqli_query($db, "select distinct chat_id from darts_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id from darts_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
}
if (in_array($user_id, $dice_user)) {
mysqli_query($db, "update darts_stats set darts_".$dice_result."=darts_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
mysqli_query($db, "insert into darts_stats (chat_id, user_id, darts_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into darts_stats (chat_id, user_id, darts_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
if ($tournament_status[0] == NULL) {
mysqli_query($db, "insert into tournament_status (chat_id) values (".$chat_id.")");
}
}
} elseif ($dice_emoji == "🏀") {
$query = mysqli_query($db, "select distinct chat_id from basket_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id from basket_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
}
if (in_array($user_id, $dice_user)) {
mysqli_query($db, "update basket_stats set basket_".$dice_result."=basket_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
mysqli_query($db, "insert into basket_stats (chat_id, user_id, basket_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into basket_stats (chat_id, user_id, basket_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
if ($tournament_status[0] == NULL) {
mysqli_query($db, "insert into tournament_status (chat_id) values (".$chat_id.")");
}
}
} elseif ($dice_emoji == "⚽") {
// error_log('3');
$query = mysqli_query($db, "select distinct chat_id from football_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
// error_log(print_r($dice_chat, TRUE));
if ($dice_chat !== NULL) {
// error_log('4');
$query = mysqli_query($db, "select user_id from football_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
}
// error_log(print_r($dice_user, TRUE));
if (in_array($user_id, $dice_user)) {
// error_log('5');
mysqli_query($db, "update football_stats set football_".$dice_result."=football_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
// error_log('6');
mysqli_query($db, "insert into football_stats (chat_id, user_id, football_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
// error_log('7');
mysqli_query($db, "insert into football_stats (chat_id, user_id, football_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
if ($tournament_status[0] == NULL) {
// error_log('8');
mysqli_query($db, "insert into tournament_status (chat_id) values (".$chat_id.")");
}
}
} elseif ($dice_emoji == "🎳") {
// error_log('3');
$query = mysqli_query($db, "select distinct chat_id from bowling_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
// error_log(print_r($dice_chat, TRUE));
if ($dice_chat !== NULL) {
// error_log('4');
$query = mysqli_query($db, "select user_id from bowling_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
}
// error_log(print_r($dice_user, TRUE));
if (in_array($user_id, $dice_user)) {
// error_log('5');
mysqli_query($db, "update bowling_stats set bowling_".$dice_result."=bowling_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
// error_log('6');
mysqli_query($db, "insert into bowling_stats (chat_id, user_id, bowling_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
// error_log('7');
mysqli_query($db, "insert into bowling_stats (chat_id, user_id, bowling_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
if ($tournament_status[0] == NULL) {
// error_log('8');
mysqli_query($db, "insert into tournament_status (chat_id) values (".$chat_id.")");
}
}
} elseif ($dice_emoji!=="🎰") {
sendMessage($chat_id, '_Unknown emoji, probably from iOS, wait for TG to fix_');
}
} elseif ($tournament_status[0]['status'] == 'ongoing') {
if ($tournament_status[0]['type'] == 'dice') {
switch ($dice_emoji) {
case "🎲":
$query = mysqli_query($db, "select distinct chat_id from dice_tournament_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id, dice_1+dice_2+dice_3+dice_4+dice_5+dice_6 as dice_sum from dice_tournament_stats where chat_id=".$chat_id." and user_id=".$user_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
$dice_sum = $sql->dice_sum;
}
if (in_array($user_id, $dice_user)) {
if ($dice_sum < $tournament_status[0]['count']) {
mysqli_query($db, "update dice_tournament_stats set dice_".$dice_result."=dice_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
sendReply($chat_id, "_You have depleted your ".$tournament_status[0]['count']." tries in an ongoing tournament\. Please, wait for the admin to end the tournament\!_", $message_id);
}
} else {
mysqli_query($db, "insert into dice_tournament_stats (chat_id, user_id, dice_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into dice_tournament_stats (chat_id, user_id, dice_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
break;
default:
sendReply($chat_id, "_Sorry, there's an ongoing 🎲 tournament in this chat\. To participate \- send a 🎲 emoji, or wait for an admin to end the tournament\._", $message_id);
break;
}
} elseif ($tournament_status[0]['type'] == 'darts') {
switch ($dice_emoji) {
case "🎯":
$query = mysqli_query($db, "select distinct chat_id from darts_tournament_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id, darts_1+darts_2+darts_3+darts_4+darts_5+darts_6 as dice_sum from darts_tournament_stats where chat_id=".$chat_id." and user_id=".$user_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
$dice_sum = $sql->dice_sum;
}
if (in_array($user_id, $dice_user)) {
if ($dice_sum < $tournament_status[0]['count']) {
mysqli_query($db, "update darts_tournament_stats set darts_".$dice_result."=darts_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
sendReply($chat_id, "_You have depleted your ".$tournament_status[0]['count']." tries in an ongoing tournament\. Please, wait for the admin to end the tournament\!_", $message_id);
}
} else {
mysqli_query($db, "insert into darts_tournament_stats (chat_id, user_id, darts_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into darts_tournament_stats (chat_id, user_id, darts_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
break;
default:
sendReply($chat_id, "_Sorry, there's an ongoing 🎯 tournament in this chat\. To participate \- send a 🎯 emoji, or wait for an admin to end the tournament\._", $message_id);
break;
}
} elseif ($tournament_status[0]['type'] == 'basket') {
switch ($dice_emoji) {
case "🏀":
$query = mysqli_query($db, "select distinct chat_id from basket_tournament_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id, basket_1+basket_2+basket_3+basket_4+basket_5 as dice_sum from basket_tournament_stats where chat_id=".$chat_id." and user_id=".$user_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
$dice_sum = $sql->dice_sum;
}
if (in_array($user_id, $dice_user)) {
if ($dice_sum < $tournament_status[0]['count']) {
mysqli_query($db, "update basket_tournament_stats set basket_".$dice_result."=basket_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
sendReply($chat_id, "_You have depleted your ".$tournament_status[0]['count']." tries in an ongoing tournament\. Please, wait for the admin to end the tournament\!_", $message_id);
}
} else {
mysqli_query($db, "insert into basket_tournament_stats (chat_id, user_id, basket_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into basket_tournament_stats (chat_id, user_id, basket_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
break;
default:
sendReply($chat_id, "_Sorry, there's an ongoing 🏀 tournament in this chat\. To participate \- send a 🏀 emoji, or wait for an admin to end the tournament\._", $message_id);
break;
}
} elseif ($tournament_status[0]['type'] == 'football') {
switch ($dice_emoji) {
case "⚽":
$query = mysqli_query($db, "select distinct chat_id from football_tournament_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id, football_1+football_2+football_3+football_4+football_5 as dice_sum from football_tournament_stats where chat_id=".$chat_id." and user_id=".$user_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
$dice_sum = $sql->dice_sum;
}
if (in_array($user_id, $dice_user)) {
if ($dice_sum < $tournament_status[0]['count']) {
mysqli_query($db, "update football_tournament_stats set football_".$dice_result."=football_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
sendReply($chat_id, "_You have depleted your ".$tournament_status[0]['count']." tries in an ongoing tournament\. Please, wait for the admin to end the tournament\!_", $message_id);
}
} else {
mysqli_query($db, "insert into football_tournament_stats (chat_id, user_id, football_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into football_tournament_stats (chat_id, user_id, football_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
break;
default:
sendReply($chat_id, "_Sorry, there's an ongoing ⚽ tournament in this chat\. To participate \- send a ⚽ emoji, or wait for an admin to end the tournament\._", $message_id);
break;
}
} elseif ($tournament_status[0]['type'] == 'bowling') {
switch ($dice_emoji) {
case "🎳":
$query = mysqli_query($db, "select distinct chat_id from bowling_tournament_stats where chat_id=".$chat_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_chat = $sql->chat_id;
}
if ($dice_chat !== NULL) {
$query = mysqli_query($db, "select user_id, bowling_1+bowling_2+bowling_3+bowling_4+bowling_5+bowling_6 as dice_sum from bowling_tournament_stats where chat_id=".$chat_id." and user_id=".$user_id);
while ($sql = mysqli_fetch_object($query)) {
$dice_user[] = $sql->user_id;
$dice_sum = $sql->dice_sum;
}
if (in_array($user_id, $dice_user)) {
if ($dice_sum < $tournament_status[0]['count']) {
mysqli_query($db, "update bowling_tournament_stats set bowling_".$dice_result."=bowling_".$dice_result."+1 where user_id=".$user_id." and chat_id=".$chat_id);
} else {
sendReply($chat_id, "_You have depleted your ".$tournament_status[0]['count']." tries in an ongoing tournament\. Please, wait for the admin to end the tournament\!_", $message_id);
}
} else {
mysqli_query($db, "insert into bowling_tournament_stats (chat_id, user_id, bowling_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
} else {
mysqli_query($db, "insert into bowling_tournament_stats (chat_id, user_id, bowling_".$dice_result.") values (".$chat_id.", ".$user_id.", 1)");
}
break;
default:
sendReply($chat_id, "_Sorry, there's an ongoing 🎳 tournament in this chat\. To participate \- send a 🎳 emoji, or wait for an admin to end the tournament\._", $message_id);
break;
}
}
} else {
sendReply($chat_id, "_Please wait until admin starts the tournament for your result to be recorded\!_", $message_id);
}
mysqli_free_result($query);
mysqli_close($db);
}
if ($message == '/tournament' || $message == '/tournament@dicestatbot') {
if ($chat_type == 'private') {
switch ($user_language_code) {
case 'ru':
sendMessage($chat_id, "_Извините, турниры доступны только в групповых чатах\._");
break;
default:
sendMessage($chat_id, "_Sorry, tournaments are only available in group chats\._");
break;
}
} else {
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$chat_id.'&user_id='.$user_id), TRUE);
if ($member['result']['status'] == 'creator' || $member['result']['status'] == 'administrator') {
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
mysqli_query($db, "update tournament_status set status='started' where chat_id=".$chat_id);
$tournament_keyboard = ['inline_keyboard' => [
[['text' => '🎲', 'callback_data' => 'init_dice_tournament'], ['text' => '🎯', 'callback_data' => 'init_darts_tournament'], ['text' => '🏀', 'callback_data' => 'init_basket_tournament'],
['text' => '⚽', 'callback_data' => 'init_football_tournament'], ['text' => '🎳', 'callback_data' => 'init_bowling_tournament']]
]];
sendMessage($chat_id, "You initiated a tournament in this chat\!\n\nPlease choose which type of tournament you want to host:", $tournament_keyboard);
mysqli_close($db);
} else {
sendReply($chat_id, "_Sorry, only admins can initiate tournaments in chats\._", $message_id);
}
}
}
if ($message == '/stats' || $message == '/stats@dicestatbot') {
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM dice_stats WHERE chat_id=".$chat_id." ORDER BY (dice_1+dice_2+dice_3+dice_4+dice_5+dice_6) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* 🎲 players by _*throws*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No 🎲 plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['dice_1']+$stats[$key]['dice_2']+$stats[$key]['dice_3']+$stats[$key]['dice_4']+$stats[$key]['dice_5']+$stats[$key]['dice_6'];
$points_sum[$key] = $stats[$key]['dice_1']+($stats[$key]['dice_2']*2)+($stats[$key]['dice_3']*3)+($stats[$key]['dice_4']*4)+($stats[$key]['dice_5']*5)+($stats[$key]['dice_6']*6);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '● 🎲 Throws ●', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
sendMessage($chat_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_close($db);
}
if ($message == '/mystats' || $message == '/mystats@dicestatbot') {
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$user_stats_for_chat = mysqli_fetch_all(mysqli_query($db, "SELECT * FROM dice_stats WHERE chat_id=".$chat_id." and user_id=".$user_id), MYSQLI_ASSOC);
$user_global_stats = mysqli_fetch_all(mysqli_query($db, "SELECT chat_id, user_id, SUM(dice_1) as dice_1, SUM(dice_2) as dice_2, SUM(dice_3) as dice_3, SUM(dice_4) as dice_4, SUM(dice_5) as dice_5, SUM(dice_6) as dice_6 FROM `dice_stats` WHERE user_id=".$user_id), MYSQLI_ASSOC);
$user_chats = mysqli_fetch_all(mysqli_query($db, "SELECT chat_id FROM dice_stats WHERE user_id=".$user_id), MYSQLI_ASSOC);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$chat_id.'&user_id='.$user_id), TRUE);
$chat_throw_sum = $user_stats_for_chat[0]['dice_1']+$user_stats_for_chat[0]['dice_2']+$user_stats_for_chat[0]['dice_3']+$user_stats_for_chat[0]['dice_4']+$user_stats_for_chat[0]['dice_5']+$user_stats_for_chat[0]['dice_6'];
$chat_points_sum = $user_stats_for_chat[0]['dice_1']+($user_stats_for_chat[0]['dice_2']*2)+($user_stats_for_chat[0]['dice_3']*3)+($user_stats_for_chat[0]['dice_4']*4)+($user_stats_for_chat[0]['dice_5']*5)+($user_stats_for_chat[0]['dice_6']*6);
$global_throw_sum = $user_global_stats[0]['dice_1']+$user_global_stats[0]['dice_2']+$user_global_stats[0]['dice_3']+$user_global_stats[0]['dice_4']+$user_global_stats[0]['dice_5']+$user_global_stats[0]['dice_6'];
$global_points_sum = $user_global_stats[0]['dice_1']+($user_global_stats[0]['dice_2']*2)+($user_global_stats[0]['dice_3']*3)+($user_global_stats[0]['dice_4']*4)+($user_global_stats[0]['dice_5']*5)+($user_global_stats[0]['dice_6']*6);
$user_stat_switcher_keyboard = ['inline_keyboard' => [
[['text' => '● 🎲 Stats ●', 'callback_data' => 'get_user_dice']],
[['text' => '🎯 Stats', 'callback_data' => 'get_user_darts']],
[['text' => '🏀 Stats', 'callback_data' => 'get_user_basket']],
[['text' => '⚽ Stats', 'callback_data' => 'get_user_football']],
[['text' => '🎳 Stats', 'callback_data' => 'get_user_bowling']]
]];
sendMessage($chat_id, "🎲 stats for *".strtr($member['result']['user']['first_name'], $markdownify_array)."*:\n
_In this chat_:
*". $chat_points_sum ."* total points: _*".$user_stats_for_chat[0]['dice_6']."*_ high rolls in _*".$chat_throw_sum."*_ throws\n\t├─ _1 point_: ".$user_stats_for_chat[0]['dice_1']."\n\t├─ _2 points_: ".$user_stats_for_chat[0]['dice_2']. "\n\t├─ _3 points_: ".$user_stats_for_chat[0]['dice_3']. "\n\t├─ _4 points_: ".$user_stats_for_chat[0]['dice_4']."\n\t├─ _5 points_: ".$user_stats_for_chat[0]['dice_5']."\n\t└─ _6 points_: ".$user_stats_for_chat[0]['dice_6']."
_Globally_ \(in ".count($user_chats)." chats\):
*". $global_points_sum ."* total points: _*".$user_global_stats[0]['dice_6']."*_ high rolls in _*".$global_throw_sum."*_ throws\n\t├─ _1 point_: ".$user_global_stats[0]['dice_1']."\n\t├─ _2 points_: ".$user_global_stats[0]['dice_2']. "\n\t├─ _3 points_: ".$user_global_stats[0]['dice_3']. "\n\t├─ _4 points_: ".$user_global_stats[0]['dice_4']."\n\t├─ _5 points_: ".$user_global_stats[0]['dice_5']."\n\t└─ _6 points_: ".$user_global_stats[0]['dice_6']."
Use buttons below or /mystats to get _your_ stats\!", $user_stat_switcher_keyboard);
mysqli_free_result($user_global_stats);
mysqli_free_result($user_stats_for_chat);
mysqli_free_result($user_chats);
mysqli_close($db);
}
$callback_data = explode(':', $callback_data);
switch ($callback_data[0]) {
case 'callback_data_empty':
break;
case 'get_dice_throw':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM dice_stats WHERE chat_id=".$callback_chat_id." ORDER BY (dice_1+dice_2+dice_3+dice_4+dice_5+dice_6) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* 🎲 players by _*throws*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No 🎲 plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['dice_1']+$stats[$key]['dice_2']+$stats[$key]['dice_3']+$stats[$key]['dice_4']+$stats[$key]['dice_5']+$stats[$key]['dice_6'];
$points_sum[$key] = $stats[$key]['dice_1']+($stats[$key]['dice_2']*2)+($stats[$key]['dice_3']*3)+($stats[$key]['dice_4']*4)+($stats[$key]['dice_5']*5)+($stats[$key]['dice_6']*6);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['dice_6']." high rolls, ". $points_sum[$key] ." total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '● 🎲 Throws ●', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_free_result($stats);
mysqli_close($db);
break;
case 'get_dice_win':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM dice_stats WHERE chat_id=".$callback_chat_id." ORDER BY dice_1+(dice_2*2)+(dice_3*3)+(dice_4*4)+(dice_5*5)+(dice_6*6) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* 🎲 players by _*total points*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No 🎲 plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['dice_1']+$stats[$key]['dice_2']+$stats[$key]['dice_3']+$stats[$key]['dice_4']+$stats[$key]['dice_5']+$stats[$key]['dice_6'];
$points_sum[$key] = $stats[$key]['dice_1']+($stats[$key]['dice_2']*2)+($stats[$key]['dice_3']*3)+($stats[$key]['dice_4']*4)+($stats[$key]['dice_5']*5)+($stats[$key]['dice_6']*6);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['dice_6']." high rolls, *". $points_sum[$key] ."* total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['dice_6']." high rolls, *". $points_sum[$key] ."* total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['dice_6']." high rolls, *". $points_sum[$key] ."* total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['dice_6']." high rolls, *". $points_sum[$key] ."* total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '🎲 Throws', 'callback_data' => 'get_dice_throw'], ['text' => '● 🎲 Points ●', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_close($db);
break;
case 'get_darts_throw':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM darts_stats WHERE chat_id=".$callback_chat_id." ORDER BY (darts_1+darts_2+darts_3+darts_4+darts_5+darts_6) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* 🎯 players by _*throws*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No 🎯 plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['darts_1']+$stats[$key]['darts_2']+$stats[$key]['darts_3']+$stats[$key]['darts_4']+$stats[$key]['darts_5']+$stats[$key]['darts_6'];
$points_sum[$key] = $stats[$key]['darts_2']+($stats[$key]['darts_3']*2)+($stats[$key]['darts_4']*3)+($stats[$key]['darts_5']*5)+($stats[$key]['darts_6']*10);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '🎲 Throws', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '● 🎯 Throws ●', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_free_result($stats);
mysqli_close($db);
break;
case 'get_darts_win':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM darts_stats WHERE chat_id=".$callback_chat_id." ORDER BY darts_2+(darts_3*2)+(darts_4*3)+(darts_5*5)+(darts_6*10) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* 🎯 players by _*total points*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No 🎯 plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['darts_1']+$stats[$key]['darts_2']+$stats[$key]['darts_3']+$stats[$key]['darts_4']+$stats[$key]['darts_5']+$stats[$key]['darts_6'];
$points_sum[$key] = $stats[$key]['darts_2']+($stats[$key]['darts_3']*2)+($stats[$key]['darts_4']*3)+($stats[$key]['darts_5']*5)+($stats[$key]['darts_6']*10);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ".$stats[$key]['darts_6']." bullseyes, *". $points_sum[$key] ."* total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '🎲 Throws', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '● 🎯 Points ●', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_close($db);
break;
case 'get_basket_throw':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM basket_stats WHERE chat_id=".$callback_chat_id." ORDER BY (basket_1+basket_2+basket_3+basket_4+basket_5) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* 🏀 players by _*throws*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No 🏀 plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['basket_1']+$stats[$key]['basket_2']+$stats[$key]['basket_3']+$stats[$key]['basket_4']+$stats[$key]['basket_5'];
$hoops[$key] = $stats[$key]['basket_4']+$stats[$key]['basket_5'];
$points_sum[$key] = $stats[$key]['basket_4']+($stats[$key]['basket_5']*2);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '🎲 Throws', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '● 🏀 Throws ●', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_close($db);
break;
case 'get_basket_win':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM basket_stats WHERE chat_id=".$callback_chat_id." ORDER BY basket_4+(basket_5*2) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* 🏀 players by _*total points*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No 🏀 plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['basket_1']+$stats[$key]['basket_2']+$stats[$key]['basket_3']+$stats[$key]['basket_4']+$stats[$key]['basket_5'];
$hoops[$key] = $stats[$key]['basket_4']+$stats[$key]['basket_5'];
$points_sum[$key] = $stats[$key]['basket_4']+($stats[$key]['basket_5']*2);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." throws, ". $hoops[$key] ." hoops \(".$stats[$key]['basket_5']." clean\), *". $points_sum[$key] ."* total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '🎲 Throws', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '● 🏀 Points ●', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_close($db);
break;
case 'get_football_throw':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM football_stats WHERE chat_id=".$callback_chat_id." ORDER BY (football_1+football_2+football_3+football_4+football_5) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* ⚽ players by _*kicks*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No ⚽ plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['football_1']+$stats[$key]['football_2']+$stats[$key]['football_3']+$stats[$key]['football_4']+$stats[$key]['football_5'];
$goals[$key] = $stats[$key]['football_3']+$stats[$key]['football_4']+$stats[$key]['football_5'];
$points_sum[$key] = $stats[$key]['football_3']+($stats[$key]['football_4']*2)+($stats[$key]['football_5']*3);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." corners\), *". $points_sum[$key] ."* total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." corners\), *". $points_sum[$key] ."* total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." corners\), *". $points_sum[$key] ."* total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- *".$sum[$key]."* kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." corners\), *". $points_sum[$key] ."* total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '🎲 Throws', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '● ⚽ Kicks ●', 'callback_data' => 'get_football_throw'], ['text' => '⚽ Points', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);
mysqli_close($db);
break;
case 'get_football_win':
$info = json_decode(file_get_contents($GLOBALS['api'].'/getChat?chat_id='.$callback_chat_id), TRUE);
$title = $info['result']['title'];
$db = mysqli_connect($db_host, $db_username, $db_pass, $db_schema);
mysqli_set_charset($db, 'utf8mb4');
mysqli_query($db, "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
if (mysqli_connect_errno()) error_log("Failed to connect to MySQL: " . mysqli_connect_error());
else echo "MySQL connect successful.\n";
$query = mysqli_query($db, "SELECT * FROM football_stats WHERE chat_id=".$callback_chat_id." ORDER BY football_3+(football_4*2)+(football_5*3) DESC LIMIT 10");
$stats = mysqli_fetch_all($query, MYSQLI_ASSOC);
$display_string_start = "🏆 *Top ".count($stats)."* ⚽ players by _*total points*_ for *".strtr($title, $markdownify_array)."*:\n───────────────────────\n";
$display_string = "";
if (count($stats) == NULL) {
$display_string .= "_No ⚽ plays registered_";
} else {
foreach ($stats as $key => $value) {
$sum[$key] = $stats[$key]['football_1']+$stats[$key]['football_2']+$stats[$key]['football_3']+$stats[$key]['football_4']+$stats[$key]['football_5'];
$goals[$key] = $stats[$key]['football_3']+$stats[$key]['football_4']+$stats[$key]['football_5'];
$points_sum[$key] = $stats[$key]['football_3']+($stats[$key]['football_4']*2)+($stats[$key]['football_5']*3);
$member = json_decode(file_get_contents($GLOBALS['api'].'/getChatMember?chat_id='.$callback_chat_id.'&user_id='.$stats[$key]['user_id']), TRUE);
$members[$key] = $member['result']['user']['first_name'];
switch ($key) {
case 0:
$display_string .= "🥇*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." nines\), *". $points_sum[$key] ."* total points\n";
break;
case 1:
$display_string .= "🥈*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." nines\), *". $points_sum[$key] ."* total points\n";
break;
case 2:
$display_string .= "🥉*".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." nines\), *". $points_sum[$key] ."* total points\n───────────────────────\n";
break;
default:
$display_string .= $key+1 ."\. *".strtr($members[$key], $markdownify_array)."* \- ".$sum[$key]." kicks, ". $goals[$key] ." goals \(".$stats[$key]['football_5']." nines\), *". $points_sum[$key] ."* total points\n";
break;
}
}
}
$stat_switcher_keyboard = ['inline_keyboard' => [
[ ['text' => '🎲 Throws', 'callback_data' => 'get_dice_throw'], ['text' => '🎲 Points', 'callback_data' => 'get_dice_win'] ],
[ ['text' => '🎯 Throws', 'callback_data' => 'get_darts_throw'] , ['text' => '🎯 Points', 'callback_data' => 'get_darts_win'] ],
[ ['text' => '🏀 Throws', 'callback_data' => 'get_basket_throw'], ['text' => '🏀 Points', 'callback_data' => 'get_basket_win'] ],
[ ['text' => '⚽ Kicks', 'callback_data' => 'get_football_throw'], ['text' => '● ⚽ Points ●', 'callback_data' => 'get_football_win']],
[ ['text' => '🎳 Throws', 'callback_data' => 'get_bowling_throw'], ['text' => '🎳 Points', 'callback_data' => 'get_bowling_win']]
]];
updateMessage($callback_chat_id, $callback_message_id, $display_string_start.$display_string, $stat_switcher_keyboard);