zhenxun_bot/utils/data_utils.py

69 lines
2.1 KiB
Python
Raw Normal View History

2021-06-30 19:50:55 +08:00
from models.group_member_info import GroupInfoUser
2021-11-04 16:11:50 +08:00
from utils.image_utils import CreateMat
from configs.path_config import IMAGE_PATH
from typing import List, Union
import asyncio
import os
2021-07-30 21:21:51 +08:00
2021-11-04 16:11:50 +08:00
async def init_rank(
title: str, all_user_id: List[int], all_user_data: List[int], group_id: int, total_count: int = 10
) -> CreateMat:
2021-07-30 21:21:51 +08:00
"""
说明
初始化通用的数据排行榜
参数
2021-11-04 16:11:50 +08:00
:param title: 排行榜标题
2021-07-30 21:21:51 +08:00
:param all_user_id: 所有用户的qq号
:param all_user_data: 所有用户需要排行的对应数据
:param group_id: 群号用于从数据库中获取该用户在此群的昵称
2021-11-04 16:11:50 +08:00
:param total_count: 获取人数总数
2021-07-30 21:21:51 +08:00
"""
2021-11-04 16:11:50 +08:00
_uname_lst = []
_num_lst = []
for i in range(len(all_user_id) if len(all_user_id) < total_count else total_count):
2021-06-30 19:50:55 +08:00
_max = max(all_user_data)
max_user_id = all_user_id[all_user_data.index(_max)]
all_user_id.remove(max_user_id)
all_user_data.remove(_max)
try:
2021-11-04 16:11:50 +08:00
user_name = (
await GroupInfoUser.get_member_info(max_user_id, group_id)
).user_name
2021-06-30 19:50:55 +08:00
except AttributeError:
2021-11-04 16:11:50 +08:00
user_name = f"{max_user_id}"
_uname_lst.append(user_name)
_num_lst.append(_max)
_uname_lst.reverse()
_num_lst.reverse()
return await asyncio.get_event_loop().run_in_executor(
None, _init_rank_graph, title, _uname_lst, _num_lst
)
2021-06-30 19:50:55 +08:00
2021-11-04 16:11:50 +08:00
def _init_rank_graph(
title: str, _uname_lst: List[str], _num_lst: List[Union[int, float]]
) -> CreateMat:
"""
生成排行榜统计图
:param title: 排行榜标题
:param _uname_lst: 用户名列表
:param _num_lst: 数值列表
"""
image = CreateMat(
y=_num_lst,
y_name="* 可以在命令后添加数字来指定排行人数 至多 50 *",
mat_type="barh",
title=title,
x_index=_uname_lst,
display_num=True,
x_rotate=30,
background=[
f"{IMAGE_PATH}/background/create_mat/{x}"
for x in os.listdir(f"{IMAGE_PATH}/background/create_mat")
],
bar_color=["*"],
)
image.gen_graph()
return image