2022-06-04 04:32:48 +00:00
|
|
|
import json
|
|
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
from os import listdir
|
|
|
|
from os.path import exists
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
|
|
|
|
from defs.drawCharCard import draw_char_card
|
|
|
|
from defs.sources 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
|
2022-06-04 05:31:43 +00:00
|
|
|
self.time = 0
|
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)
|
|
|
|
try:
|
|
|
|
all_char.remove(f"{self.uid}.json")
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
all_char.remove("rawData.json")
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
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)
|
|
|
|
path = await draw_char_card(data)
|
|
|
|
msg = await app.send_photo(channel_id, path)
|
|
|
|
self.all_char.append(gen_char_dict(i, msg.photo.file_id))
|
|
|
|
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
|