Enka_Genshin_bot/defs/player.py

110 lines
3.7 KiB
Python
Raw Permalink Normal View History

2022-09-02 16:45:18 +00:00
import contextlib
2022-06-04 04:32:48 +00:00
import json
import time
from datetime import datetime
2022-06-04 07:52:42 +00:00
from os import listdir, remove
2022-06-04 04:32:48 +00:00
from os.path import exists
from typing import List
2022-09-02 16:45:18 +00:00
from PIL.Image import Image
2022-06-04 04:32:48 +00:00
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
2022-09-02 16:45:18 +00:00
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
2022-06-04 04:32:48 +00:00
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
2022-06-04 05:31:43 +00:00
self.time = 0
2022-09-03 16:08:35 +00:00
self.all_char = []
2022-06-04 04:32:48 +00:00
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)
2022-09-02 16:45:18 +00:00
with contextlib.suppress(ValueError):
2022-06-04 04:32:48 +00:00
all_char.remove(f"{self.uid}.json")
2022-09-02 16:45:18 +00:00
with contextlib.suppress(ValueError):
2022-06-04 04:32:48 +00:00
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)
2022-09-02 16:45:18 +00:00
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)
2022-06-04 04:32:48 +00:00
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
2022-06-04 05:31:43 +00:00
if temp_:
data.append(temp_)
2022-06-04 04:32:48 +00:00
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