Enka_Genshin_bot/defs/player.py
2022-09-04 00:08:35 +08:00

110 lines
3.7 KiB
Python

import contextlib
import json
import time
from datetime import datetime
from os import listdir, remove
from os.path import exists
from typing import List
from PIL.Image import Image
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from genshinuid_enka.draw_char_card import draw_char_img
from utils.draw_image_tools.send_image_tool import convert_img
from utils.enka_api.enka_to_data import PLAYER_PATH
from ci import channel_id, app, sqlite
def gen_char_dict(name: str, file_id: str) -> dict:
return {"name": name, "file_id": file_id, "time": int(time.time())}
class Player:
name: str = ""
uid: str = ""
all_char: List[dict] = []
time: int = 0
def __init__(self, uid: str):
self.uid = uid
self.time = 0
self.all_char = []
if not exists(PLAYER_PATH / uid / f"{uid}.json"):
return
with open(PLAYER_PATH / uid / f"{uid}.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
self.name = data.get("nickname", "")
def update_name(self):
with open(PLAYER_PATH / self.uid / f"{self.uid}.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
self.name = data.get("nickname", "")
async def update_char(self):
all_char = listdir(PLAYER_PATH / self.uid)
with contextlib.suppress(ValueError):
all_char.remove(f"{self.uid}.json")
with contextlib.suppress(ValueError):
all_char.remove("rawData.json")
all_char = [i[:-5] for i in all_char]
for i in all_char:
for f in self.all_char:
if f["name"] == i:
self.all_char.remove(f)
break
try:
with open(PLAYER_PATH / self.uid / f"{i}.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
im = await draw_char_img(data)
if isinstance(im, Image):
path = await convert_img(im)
msg = await app.send_photo(channel_id, path)
self.all_char.append(gen_char_dict(i, msg.photo.file_id))
remove(PLAYER_PATH / self.uid / f"{i}.json")
else:
print(im)
except Exception as e:
print(e)
continue
def export(self):
return {"name": self.name, "uid": self.uid, "time": int(time.time()), "all_char": self.all_char}
def restore(self):
sources = sqlite.get(self.uid, None)
if sources:
self.name = sources.get("name", "")
self.time = sources.get("time", 0)
self.all_char = sources.get("all_char", [])
def gen_keyboard(self) -> InlineKeyboardMarkup:
data = []
temp_ = []
num = 0
for i in self.all_char:
name = i.get("name", "")
temp_.append(InlineKeyboardButton(name, callback_data=f"{self.uid}|{name}"))
num += 1
if num == 3:
data.append(temp_)
temp_ = []
num = 0
if temp_:
data.append(temp_)
return InlineKeyboardMarkup(data)
def gen_back(self) -> InlineKeyboardMarkup:
return InlineKeyboardMarkup([[InlineKeyboardButton("返回", callback_data=self.uid)]])
@staticmethod
def parse_time(time_stamp: int) -> str:
return datetime.strftime(datetime.fromtimestamp(time_stamp), '%Y-%m-%d %H:%M:%S')
def gen_all_char(self) -> str:
if not self.all_char:
return ""
text = "缓存角色有:\n"
for i in self.all_char:
text += "🔸 " + i.get("name", "") + f" `{self.parse_time(i.get('time', time.time()))}`\n"
return text