-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1266 lines (1065 loc) · 50.9 KB
/
main.py
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
import json
import logging
import math
import os
import sys
import time
from datetime import datetime, timedelta
from math import sqrt
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import telegram
from telegram.ext import Updater, CommandHandler
help_text = """欢迎使用本 bot,请使用如下命令:
/w 或者 /weight 添加体重记录(只记录当天最后一条)
/height 修正身高记录(身高不统计变化,按常数计算)
/rank 查看指定天数的排名
/week 查看本周排名
/overall 查看总排名
/plot 查看指定天数和其他用户(支持 all)的的体重变化图
/new_challenge 在本群开展减肥挑战 admin only
/end_challenge 结束本群的挑战 admin only
/delete_user 删除用户数据 admin only
/strategy 选择排名策略 admin only
/join_challenge 加入本群的减肥挑战
/ckpt_add 添加检查点
/ckpt_del 删除检查点
/ckpt_list 查看所有检查点
/ckpt_result 检查点结果
/ckpt_overall 检查点完成情况
"""
start_help = """欢迎使用减肥群 bot,请将本 bot 拉入超级群组中开启减肥挑战。
使用 /help 可以查看所有命令。"""
challenges_path = './data/challenges.json'
job_path = './data/job'
metrics = {
'1': {'name': '体重变化', 'expression': '原体重-现体重', 'key': lambda x: (x['weight'][0][1] - x['weight'][-1][1])},
'2': {'name': '体重变化比例', 'expression': '(原体重-现体重)/原体重', 'key': lambda x: (x['weight'][0][1] - x['weight'][-1][1]) / x['original_weight']},
'3': {'name': '根号难度加权', 'expression': '(原体重-现体重)/√(初始体重-标准体重),其中标准体重按照 BMI = 21 计算',
'key': lambda x: math.copysign(((x['weight'][0][1] - x['weight'][-1][1]) / (sqrt(abs(x['original_weight'] - 21 * x['height'] ** 2)))),
x['original_weight'] - 21 * x['height'] ** 2)},
}
queueing_job = {}
def _get_timestamp():
return str(datetime.now().timestamp())
def _get_timestr(timestamp, format='%Y-%m-%d %H:%M:%S'):
a = datetime.fromtimestamp(float(timestamp))
return a.strftime(format)
def _is_today(timestamp):
record = datetime.fromtimestamp(float(timestamp))
today = datetime.now()
return record.year == today.year and record.month == today.month and record.day == today.day
def _get_username(bot, group_id, user_id):
try:
return bot.get_chat_member(group_id, user_id).to_dict()['user']['username']
except:
return user_id
def _get_fullname(bot, group_id, user_id):
try:
user = bot.get_chat_member(group_id, user_id).to_dict()['user']
except:
return str(user_id)
if 'last_name' in user:
return f'{user["first_name"]} {user["last_name"]}'
else:
return f'{user["first_name"]}'
def _get_info(update):
group_id = str(update.to_dict()['message']['chat']['id'])
user_id = str(update.to_dict()['message']['from']['id'])
username = update.to_dict()['message']['from']['username']
message_id = update.to_dict()['message']['message_id']
return group_id, user_id, username, message_id
def _get_challenges():
if not os.path.exists(challenges_path):
json.dump({}, open(challenges_path, "w"))
return json.load(open(challenges_path, "r"))
def _get_challenge(group_path, update):
group_id, user_id, username, message_id = _get_info(update)
challenge_path = f'{group_path}/challenge.json'
if not os.path.exists(challenge_path):
tmp = {
'group_id': group_id,
'challenges': {}
}
json.dump(tmp, open(challenge_path, "w"))
return json.load(open(challenge_path, "r"))
def _get_latest_challenge(update):
group_id, user_id, username, message_id = _get_info(update)
challenges = _get_challenges()
challenge_cnt = str(challenges[group_id]['challenge_cnt'])
group_path = f'./data/{group_id}'
_ensure_path(group_path)
challenge = _get_challenge(group_path, update)
return challenge, challenge_cnt
def _get_scale(challenge_cnt_path):
_ensure_path(challenge_cnt_path)
scale_path = f'{challenge_cnt_path}/scale.json'
if not os.path.exists(scale_path):
json.dump({}, open(scale_path, "w"))
return json.load(open(scale_path, "r"))
def _get_ckpt(ckpt_cnt_path):
_ensure_path(ckpt_cnt_path)
ckpt_path = f'{ckpt_cnt_path}/ckpt.json'
if not os.path.exists(ckpt_path):
json.dump({}, open(ckpt_path, "w"))
return json.load(open(ckpt_path, "r"))
def _ensure_ckpt(update):
group_id, user_id, username, message_id = _get_info(update)
challenge, challenge_cnt = _get_latest_challenge(update)
ckpt_path = f'./data/{group_id}/{challenge_cnt}'
ckpt = _get_ckpt(ckpt_path)
if 'ckpt_cnt' not in ckpt:
ckpt['ckpt_cnt'] = 0
if 'ckpt' not in ckpt:
ckpt['ckpt'] = {}
return ckpt, ckpt_path
def _get_running_jobs():
_ensure_path(job_path)
running_job_path = f'{job_path}/running.json'
if not os.path.exists(running_job_path):
json.dump({}, open(running_job_path, "w"))
return json.load(open(running_job_path, "r")), running_job_path
def _get_done_jobs():
_ensure_path(job_path)
done_job_path = f'{job_path}/done.json'
if not os.path.exists(done_job_path):
json.dump({}, open(done_job_path, "w"))
return json.load(open(done_job_path, "r")), done_job_path
def _parse_input_datetime(inputs):
try:
inputs = inputs.split('-')
assert len(inputs) == 4
input_date = [int(i) for i in inputs]
return datetime(input_date[0], input_date[1], input_date[2], input_date[3])
except:
return None
def _parse_input_datetime_pair(inputs):
try:
inputs = inputs.split(' ')
assert len(inputs) == 3
st = _parse_input_datetime(inputs[1])
ed = _parse_input_datetime(inputs[2])
assert st is not None, ed is not None
return [st, ed]
except:
return None
def _get_userid(update, context, usernames, all_flag):
scale, scale_path = _ensure_scale(update)
ret = {}
for userid in scale:
if not userid.isdigit():
continue
username = _get_username(context.bot, update.effective_chat.id, userid)
if username in usernames or all_flag:
ret[username] = userid
return ret
def _get_admin(bot, group_id):
admins = bot.get_chat_administrators(group_id)
ret = []
for i in admins:
ret.append(str(i.user['id']))
return ret
def _is_admin(bot, group_id, user_id):
admins = _get_admin(bot, group_id)
return user_id in admins
def _admin_only(update, context):
group_id, user_id, username, message_id = _get_info(update)
if not _is_admin(context.bot, group_id, user_id):
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='admin only')
return False
return True
def _is_supergroup(update):
return update.to_dict()['message']['chat']['type'] == 'supergroup'
def _in_challenge(update, context):
group_id, user_id, username, message_id = _get_info(update)
challenge, challenge_cnt = _get_latest_challenge(update)
if user_id not in challenge['challenges'][challenge_cnt]['challengers']:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 你还未加入挑战哦')
return False
return True
def _ensure_scale(update):
group_id, user_id, username, message_id = _get_info(update)
challenge, challenge_cnt = _get_latest_challenge(update)
scale_path = f'./data/{group_id}/{challenge_cnt}'
scale = _get_scale(scale_path)
if user_id not in scale:
scale[user_id] = {'weight': []}
return scale, scale_path
def _supergroup_only(update, context):
group_id, user_id, username, message_id = _get_info(update)
if not _is_supergroup(update):
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='仅可在超级群组中使用本功能。')
return False
return True
def _ensure_path(path):
if not os.path.exists(path):
os.makedirs(path)
def _calc_bmi(weight_scale, height_scale):
return weight_scale / height_scale ** 2
def _running_challenge_only(update, context):
if not (_supergroup_only(update, context)):
return False
group_id, user_id, username, message_id = _get_info(update)
challenges = _get_challenges()
if group_id in challenges:
if challenges[group_id]['status'] == 'ended':
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='没有正在进行的挑战')
return False
if group_id not in challenges:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'没有正在进行的挑战')
return False
return True
def _get_scale_data(update, context, time_limit, users=None):
group_id, user_id, username, message_id = _get_info(update)
scale, scale_path = _ensure_scale(update)
if 'strategy' not in scale:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请先使用 /strategy 指定比赛策略。')
return
strategy_id = scale['strategy']
compare = metrics[strategy_id]['key']
user_data = []
for user_id, data in scale.items():
if users and user_id not in users.values():
continue
if not user_id.isdigit():
continue
username = _get_username(context.bot, group_id, user_id)
fullname = _get_fullname(context.bot, group_id, user_id)
if 'height' not in data:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 没有添加过身高数据')
continue
if len(data['weight']) == 0:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 没有添加过体重数据')
continue
ret = {'fullname': fullname, 'username': username, 'height': data['height'], 'original_weight': data['weight'][0][1], 'weight': []}
for data_timestamp, weight_data in data['weight'][::-1]:
data_time = datetime.fromtimestamp(float(data_timestamp))
if data_time >= time_limit:
ret['weight'].append([data_timestamp, weight_data])
else:
if len(ret['weight']) == 0:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 在限定时间内没有添加体重数据')
break
last_time = datetime.fromtimestamp(float(ret['weight'][-1][0]))
if abs(data_time - time_limit) > abs(last_time - time_limit):
ret['weight'].append([data_timestamp, weight_data])
break
if len(ret['weight']) != 0:
ret['weight'] = ret['weight'][::-1]
ret['score'] = compare(ret)
user_data.append(ret)
return user_data
def _rank(update, context, time_limit):
group_id, user_id, username, message_id = _get_info(update)
user_data = _get_scale_data(update, context, time_limit)
if user_data is None:
return
user_data.sort(key=lambda x: -x['score'])
rank_list = '排名 username 体重变化 分数\n'
for i, user in enumerate(user_data):
rank_list += f'*{i + 1}* `{user["fullname"]} {user["weight"][0][1] - user["weight"][-1][1]:.2f} {user["score"]:.2f}`\n'
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=rank_list, parse_mode=telegram.ParseMode.MARKDOWN_V2)
def start(update, context):
group_id, user_id, username, message_id = _get_info(update)
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=start_help)
def print_help(update, context):
group_id, user_id, username, message_id = _get_info(update)
try:
print_help_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def print_help_(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=help_text)
def new_challenge(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
new_challenge_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def new_challenge_(update, context):
if not (_supergroup_only(update, context) and _admin_only(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
challenges = _get_challenges()
if group_id in challenges:
if challenges[group_id]['status'] != 'ended':
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='请先结束当前挑战')
return
challenges[group_id]['status'] = 'running'
challenges[group_id]['challenge_cnt'] += 1
if group_id not in challenges:
challenges[group_id] = {}
challenges[group_id]['status'] = 'running'
challenges[group_id]['challenge_cnt'] = 1
json.dump(challenges, open(challenges_path, "w"))
challenge_cnt = challenges[group_id]['challenge_cnt']
group_path = f'./data/{group_id}'
_ensure_path(group_path)
challenge = _get_challenge(group_path, update)
challenge['challenges'][challenge_cnt] = {
'start_time': _get_timestamp(),
'start_user': user_id,
'status': 'running',
'end_time': None,
'end_user': None,
'challengers': [user_id]
}
json.dump(challenge, open(f'{group_path}/challenge.json', "w"))
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='挑战已开始,请各位参赛选手使用 /join_challenge 加入挑战')
join_challenge(update, context)
def end_challenge(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
end_challenge_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def end_challenge_(update, context):
if not (_running_challenge_only(update, context) and _admin_only(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
challenges = _get_challenges()
if group_id in challenges:
challenges[group_id]['status'] = 'ended'
json.dump(challenges, open(challenges_path, "w"))
challenge_cnt = str(challenges[group_id]['challenge_cnt'])
group_path = f'./data/{group_id}'
_ensure_path(group_path)
challenge = _get_challenge(group_path, update)
challenge['challenges'][challenge_cnt]['end_time'] = _get_timestamp()
challenge['challenges'][challenge_cnt]['end_user'] = user_id
challenge['challenges'][challenge_cnt]['status'] = 'ended'
json.dump(challenge, open(f'{group_path}/challenge.json', "w"))
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='挑战已结束!')
def join_challenge(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
join_challenge_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def join_challenge_(update, context):
if not _running_challenge_only(update, context):
return
group_id, user_id, username, message_id = _get_info(update)
challenge, challenge_cnt = _get_latest_challenge(update)
if user_id in challenge['challenges'][challenge_cnt]['challengers']:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 已经在挑战中了!')
return
challenge['challenges'][challenge_cnt]['challengers'].append(user_id)
json.dump(challenge, open(f'./data/{group_id}/challenge.json', "w"))
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 已加入挑战!')
def delete_user(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
delete_user_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def delete_user_(update, context):
if not (_running_challenge_only(update, context) and _admin_only(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
try:
inputs = inputs.split(' ')[1]
username = inputs.strip().lstrip('@')
user_ids = _get_userid(update, context, [username], all_flag=False)
user_id = user_ids[username]
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'未找到 @{username},请正确输入被删除的用户名')
return
challenge, challenge_cnt = _get_latest_challenge(update)
if user_id not in challenge['challenges'][challenge_cnt]['challengers']:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 没有在挑战中!')
return
pos = challenge['challenges'][challenge_cnt]['challengers'].index(user_id)
challenge['challenges'][challenge_cnt]['challengers'].pop(pos)
json.dump(challenge, open(f'./data/{group_id}/challenge.json', "w"))
scale, scale_path = _ensure_scale(update)
if 'deleted_user_data' not in scale:
scale['deleted_user_data'] = {}
scale['deleted_user_data'][f'{user_id}_{datetime.now().strftime("%Y-%m-%d-%H:%M:%S")}'] = scale[user_id]
del scale[user_id]
json.dump(scale, open(f'{scale_path}/scale.json', "w"))
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'@{username} 已退出挑战!')
def weight(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
weight_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def weight_(update, context):
if not (_running_challenge_only(update, context) and _in_challenge(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
try:
inputs = inputs.split()[1]
inputs = float(inputs)
if inputs < 40 or inputs > 400:
raise ValueError
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请输入正确的体重数据')
return
scale, scale_path = _ensure_scale(update)
new_data = (_get_timestamp(), inputs)
outputs = f'{_get_timestr(new_data[0])} @{username} 添加体重记录 {new_data[1]} 千克。'
if len(scale[user_id]['weight']) > 0:
if _is_today(scale[user_id]['weight'][-1][0]):
scale[user_id]['weight'].pop(-1)
if len(scale[user_id]['weight']) > 0:
outputs += f'\n上次体重 {scale[user_id]["weight"][-1][1]:.2f} 千克,记录时间是 {_get_timestr(scale[user_id]["weight"][-1][0])}。体重变化了 {new_data[1] - scale[user_id]["weight"][-1][1]:.2f} 千克。'
outputs += f'\n初始体重 {scale[user_id]["weight"][0][1]:.2f} 千克,记录时间是 {_get_timestr(scale[user_id]["weight"][0][0])}。体重变化了 {new_data[1] - scale[user_id]["weight"][0][1]:.2f} 千克。'
if 'height' in scale[user_id]:
this_bmi = _calc_bmi(new_data[1], scale[user_id]["height"])
outputs += f'\n你的 BMI 是 {this_bmi:.2f}。'
if len(scale[user_id]['weight']) > 0:
last_bmi = _calc_bmi(scale[user_id]["weight"][-1][1], scale[user_id]["height"])
start_bmi = _calc_bmi(scale[user_id]["weight"][0][1], scale[user_id]["height"])
outputs += f'\n上次的 BMI 是 {last_bmi:.2f},变化了 {this_bmi - last_bmi:.2f}。'
outputs += f'\n初始的 BMI 是 {start_bmi:.2f},变化了 {this_bmi - start_bmi:.2f}。'
scale[user_id]['weight'].append(new_data)
json.dump(scale, open(f'{scale_path}/scale.json', "w"))
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=outputs)
if len(scale[user_id]['weight']) > 1 and abs(scale[user_id]["weight"][-2][1] - new_data[1]) > 5:
context.bot.send_message(
chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'*⚠️和上次的体重变化比较大,请注意是否输入错误⚠️️*',
parse_mode=telegram.ParseMode.MARKDOWN_V2)
def height(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
height_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def height_(update, context):
if not (_running_challenge_only(update, context) and _in_challenge(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
try:
inputs = inputs.split()[1]
inputs = float(inputs)
if inputs < 1.50 or inputs > 2.20:
raise ValueError
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请输入正确的身高数据')
return
scale, scale_path = _ensure_scale(update)
scale[user_id]['height'] = inputs
json.dump(scale, open(f'{scale_path}/scale.json', "w"))
context.bot.send_message(
chat_id=update.effective_chat.id, reply_to_message_id=message_id,
text=f'@{username} 更新身高记录 {inputs} 米')
def strategy(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
strategy_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def strategy_(update, context):
if not (_running_challenge_only(update, context) and _admin_only(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
try:
inputs = inputs.split()[1]
if inputs not in metrics:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请输入正确的比赛策略编号')
return
except:
outputs = f'比赛策略如下,请输入需要的比赛策略编号:\n'
for i, metirc in metrics.items():
outputs += f'{i} : {metirc["name"]} {metirc["expression"]}\n'
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=outputs)
return
scale, scale_path = _ensure_scale(update)
scale['strategy'] = inputs
json.dump(scale, open(f'{scale_path}/scale.json', "w"))
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'已成功切换为策略 {inputs}')
def week_rank(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
week_rank_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def week_rank_(update, context):
if not _running_challenge_only(update, context):
return
today = datetime.now()
today = datetime(today.year, today.month, today.day, 0, 0, 0, 0)
time_limit = today - timedelta(days=7)
_rank(update, context, time_limit)
def overall_rank(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
logging.info(f'user_id={user_id}')
try:
overall_rank_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def overall_rank_(update, context):
if not _running_challenge_only(update, context):
return
_rank(update, context, datetime.min)
def rank(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
rank_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def rank_(update, context):
if not _running_challenge_only(update, context):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
try:
inputs = inputs.split()[1]
inputs = int(inputs)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='请输入整数。')
return
today = datetime.now()
today = datetime(today.year, today.month, today.day, 0, 0, 0, 0)
time_limit = today - timedelta(days=inputs)
_rank(update, context, time_limit)
def plot(update, context):
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
group_id, user_id, username, message_id = _get_info(update)
try:
plot_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def plot_(update, context):
if not _running_challenge_only(update, context):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
compare_username = [username]
compare_day = 14
all_flag = False
try:
inputs = inputs.split()[1:]
for arg in inputs:
if arg == 'all':
all_flag = True
elif arg[0] == '@':
compare_username.append(arg[1:])
elif arg.isdigit():
compare_day = int(arg)
else:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'忽略无法识别的参数 {arg}')
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
except:
raise ValueError
today = datetime.now()
today = datetime(today.year, today.month, today.day, 0, 0, 0, 0)
time_limit = today - timedelta(days=compare_day)
if len(compare_username) > 1 or all_flag:
compare_userid = _get_userid(update, context, compare_username, all_flag)
for cmp_username in compare_username:
if cmp_username not in compare_userid:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'忽略无法找到的 @{cmp_username}')
context.bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
else:
compare_userid = {compare_username[0]: user_id}
users_data = _get_scale_data(update, context, time_limit, users=compare_userid)
plt.clf()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
for user_data in users_data:
weights = []
timestamps = []
for i, j in user_data['weight']:
timestamps.append(datetime.fromtimestamp((float(i))))
weights.append(j)
maxi = int(np.argmax(weights))
mini = int(np.argmin(weights))
plt.plot(timestamps, weights, label=f'@{user_data["username"]}', marker='o')
plt.annotate(weights[maxi], xy=(timestamps[maxi], weights[maxi]))
plt.annotate(weights[mini], xy=(timestamps[mini], weights[mini]))
plt.legend()
plt.title(f'{" ".join(list(compare_userid.keys()))} in last {compare_day} days')
plt.xlabel('time')
plt.ylabel('weight')
_ensure_path(f'./pic')
plt.savefig(f'./pic/{username}.png', dpi=120)
context.bot.send_photo(chat_id=update.effective_chat.id, reply_to_message_id=message_id, photo=open(f'./pic/{username}.png', 'rb'))
def ckpt_add(update, context):
group_id, user_id, username, message_id = _get_info(update)
try:
ckpt_add_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def ckpt_add_(update, context):
if not (_running_challenge_only(update, context) and _admin_only(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
ret = _parse_input_datetime_pair(inputs)
if ret is None:
context.bot.send_message(
chat_id=update.effective_chat.id, reply_to_message_id=message_id,
text=f'输入格式错误,请按照 开始年-月-日-小时 结束年-月-日-小时 输入,例如:2020-10-1-15 2020-10-1-21')
return
ckpt, ckpt_path = _ensure_ckpt(update)
ckpt['ckpt_cnt'] += 1
ckpt_cnt = ckpt['ckpt_cnt']
start_time, end_time = ret
if not start_time < end_time:
context.bot.send_message(
chat_id=update.effective_chat.id, reply_to_message_id=message_id,
text=f'结束时间必须在开始时间之后')
return
now = datetime.now()
if end_time <= now:
status = 'ended'
run_time = now + timedelta(seconds=1)
elif start_time <= now <= end_time:
status = 'running'
run_time = end_time
else:
status = 'pending'
run_time = end_time
time_window = f"{_get_timestr(start_time.timestamp(), format='%Y-%m-%d-%H')} {_get_timestr(end_time.timestamp(), format='%Y-%m-%d-%H')}"
if start_time - now > timedelta(hours=12):
text = f'请大家准备好参加 checkpoint 数据统计,时间窗口为 {time_window}'
job_dict = {
'id': datetime.now().timestamp(),
'func': 'print_alarm',
'timestamp': (start_time - timedelta(hours=12)).timestamp(),
'args': {'chat_id': update.effective_chat.id, 'text': text, 'ckpt_num': ckpt_cnt, 'ckpt_path': ckpt_path}
}
start_job(job_dict, context.job_queue)
else:
text = f'请大家准备好参加 checkpoint 数据统计,时间窗口为 {time_window}'
job_dict = {
'id': datetime.now().timestamp(),
'func': 'print_alarm',
'timestamp': (now + timedelta(seconds=1)).timestamp(),
'args': {'chat_id': update.effective_chat.id, 'text': text, 'ckpt_num': ckpt_cnt, 'ckpt_path': ckpt_path}
}
start_job(job_dict, context.job_queue)
text = f'请大家准备好参加 checkpoint 数据统计,时间窗口为 {time_window}'
job_dict = {
'id': datetime.now().timestamp(),
'func': 'print_alarm',
'timestamp': start_time.timestamp(),
'args': {'chat_id': update.effective_chat.id, 'text': text, 'ckpt_num': ckpt_cnt, 'ckpt_path': ckpt_path}
}
start_job(job_dict, context.job_queue)
ckpt['ckpt'][ckpt_cnt] = {'start': start_time.timestamp(), 'end': end_time.timestamp(), 'result': {}, 'status': status}
json.dump(ckpt, open(f'{ckpt_path}/ckpt.json', "w"))
_, scale_path = _ensure_scale(update)
job_dict = {
'id': datetime.now().timestamp(),
'func': 'calc_ckpt_result',
'timestamp': run_time.timestamp(),
'args': {
'ckpt_path': ckpt_path,
'scale_path': scale_path,
'ckpt_num': ckpt_cnt,
'info': (group_id, user_id, username, message_id),
'chat_id': update.effective_chat.id,
}
}
start_job(job_dict, context.job_queue)
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'已成功添加新的 checkpoint')
def ckpt_list(update, context):
group_id, user_id, username, message_id = _get_info(update)
try:
ckpt_list_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def ckpt_list_(update, context):
if not _running_challenge_only(update, context):
return
group_id, user_id, username, message_id = _get_info(update)
ckpts, ckpt_path = _ensure_ckpt(update)
ret_str = 'id start end\n'
ckpt_str = []
cnt = 0
for ckpt_id, ckpt in ckpts['ckpt'].items():
if ckpt['status'] in ['pending', 'running', 'ended']:
cnt += 1
start_time = _get_timestr(ckpt['start'], '%Y-%m-%d-%H')
end_time = _get_timestr(ckpt['end'], '%Y-%m-%d-%H')
ckpt_str.append([f'{ckpt_id} {start_time} {end_time}\n', float(ckpt['end'])])
ckpt_str = sorted(ckpt_str, key=lambda x: x[1])
for i in ckpt_str:
ret_str += i[0]
if cnt:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=ret_str)
else:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='还没有添加检查点')
def ckpt_del(update, context):
group_id, user_id, username, message_id = _get_info(update)
try:
ckpt_del_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def ckpt_del_(update, context):
if not (_running_challenge_only(update, context) and _admin_only(update, context)):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
try:
inputs = inputs.split(' ')[1]
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请正确输入被删除的编号')
return
ckpts, ckpt_path = _ensure_ckpt(update)
if inputs not in ckpts['ckpt'] or ckpts['ckpt'][inputs]['status'] not in ['pending', 'running', 'ended']:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请正确输入被删除的编号')
return
ckpts['ckpt'][inputs]['status'] = 'deleted'
json.dump(ckpts, open(f'{ckpt_path}/ckpt.json', 'w'))
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'检查点已被删除')
def ckpt_result(update, context):
group_id, user_id, username, message_id = _get_info(update)
try:
ckpt_result_(update, context)
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text='好像遇到了 bug,请联系 @sqyon')
logging.exception(f"ERROR gid={group_id} uid={user_id}")
return
def ckpt_result_(update, context):
if not _running_challenge_only(update, context):
return
group_id, user_id, username, message_id = _get_info(update)
inputs = update.to_dict()['message']['text']
try:
inputs = inputs.split(' ')[1]
except:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请正确输入检查点的编号')
return
ckpts, ckpt_path = _ensure_ckpt(update)
if inputs not in ckpts['ckpt'] or ckpts['ckpt'][inputs]['status'] not in ['pending', 'running', 'ended']:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请正确输入检查点的编号')
return
end_time = ckpts['ckpt'][inputs]['end']
end_time = datetime.fromtimestamp(float(end_time))
if datetime.now() <= end_time:
context.bot.send_message(chat_id=update.effective_chat.id, reply_to_message_id=message_id, text=f'请等待检查点结束')
return
if ckpts['ckpt'][inputs]['status'] != 'ended':
_, scale_path = _ensure_scale(update)
job_dict = {
'id': datetime.now().timestamp(),
'func': 'calc_ckpt_result',
'timestamp': (datetime.now() + timedelta(seconds=1)).timestamp(),
'args': {
'ckpt_path': ckpt_path,
'scale_path': scale_path,
'ckpt_num': inputs,
'info': (group_id, user_id, username, message_id),
'chat_id': update.effective_chat.id,
}
}
start_job(job_dict, context.job_queue)
time.sleep(2)
history_min = {}
next_goal = {}
scale, scale_path = _ensure_scale(update)
for user_id in scale:
if not user_id.isdigit():
continue
if len(scale[user_id]["weight"]) > 0:
history_min[user_id] = float(scale[user_id]["weight"][0][1])
for ckpt_id, ckpt in ckpts['ckpt'].items():
if ckpt_id > inputs:
continue
if ckpt_id == inputs:
for user_id, scale in ckpt['result'].items():
if not scale:
next_goal[user_id] = 0x3fffffff
continue
if user_id not in next_goal:
next_goal[user_id] = float(scale[1])
else:
next_goal[user_id] = min(next_goal[user_id], float(scale[1]))
continue
if ckpt['status'] != 'ended':
continue
for user_id, scale in ckpt['result'].items():
if not scale:
continue
if user_id not in history_min:
history_min[user_id] = float(scale[1])
else:
history_min[user_id] = min(history_min[user_id], float(scale[1]))
for user_id, weight in history_min.items():
if user_id not in next_goal:
next_goal[user_id] = weight
else:
next_goal[user_id] = min(next_goal[user_id], weight)
passed = []
failed = []
ckpt_preview = {}
for user_id, scale in ckpts['ckpt'][inputs]['result'].items():
username = _get_username(context.bot, group_id, user_id)
if user_id not in next_goal:
ckpt_preview[username] = '暂无数据'
else:
ckpt_preview[username] = next_goal[user_id]
if not scale: