diff --git a/draw_card/__init__.py b/draw_card/__init__.py index c829158..72be7c1 100644 --- a/draw_card/__init__.py +++ b/draw_card/__init__.py @@ -14,7 +14,7 @@ from .update_game_info import update_info from .util import is_number, check_num from .rule import is_switch -from .config import PRTS_FLAG, PRETTY_FLAG, GUARDIAN_FLAG, GENSHIN_FLAG, PCR_FLAG, AZUR_FLAG, FGO_FLAG, ONMYOJI_FLAG +from .config import draw_config from .async_update_game_info import async_update_game from nonebot.log import logger import re @@ -283,21 +283,21 @@ async def _(bot: Bot, event: MessageEvent, state: T_State=State()): ) async def _(): tasks = [] - if PRTS_FLAG: + if draw_config.PRTS_FLAG: tasks.append(asyncio.ensure_future(update_prts_info())) - if GENSHIN_FLAG: + if draw_config.GENSHIN_FLAG: tasks.append(asyncio.ensure_future(update_genshin_info())) - if PRETTY_FLAG: + if draw_config.PRETTY_FLAG: tasks.append(asyncio.ensure_future(update_pretty_info())) - if GUARDIAN_FLAG: + if draw_config.GUARDIAN_FLAG: tasks.append(asyncio.ensure_future(update_guardian_info())) - if PCR_FLAG: + if draw_config.PCR_FLAG: tasks.append(asyncio.ensure_future(update_pcr_info())) - if AZUR_FLAG: + if draw_config.AZUR_FLAG: tasks.append(asyncio.ensure_future(update_azur_info())) - if FGO_FLAG: + if draw_config.FGO_FLAG: tasks.append(asyncio.ensure_future(update_fgo_info())) - if ONMYOJI_FLAG: + if draw_config.ONMYOJI_FLAG: tasks.append(asyncio.ensure_future(update_onmyoji_info())) await asyncio.gather(*tasks) @@ -309,7 +309,7 @@ async def _(): minute=1, ) async def _(): - if PRTS_FLAG: + if draw_config.PRTS_FLAG: await reload_prts_pool() @@ -320,7 +320,7 @@ async def _(): minute=1, ) async def _(): - if PRETTY_FLAG: + if draw_config.PRETTY_FLAG: await reload_pretty_pool() @@ -331,7 +331,7 @@ async def _(): minute=1, ) async def _(): - if PRTS_FLAG: + if draw_config.PRTS_FLAG: await reload_genshin_pool() @@ -342,8 +342,5 @@ async def _(): minute=1, ) async def _(): - if GUARDIAN_FLAG: + if draw_config.GUARDIAN_FLAG: await reload_guardian_pool() - - - diff --git a/draw_card/announcement.py b/draw_card/announcement.py index b2bf8dd..193362d 100644 --- a/draw_card/announcement.py +++ b/draw_card/announcement.py @@ -3,7 +3,6 @@ import re from datetime import datetime, timedelta from .config import DRAW_PATH -from pathlib import Path from asyncio.exceptions import TimeoutError from nonebot.log import logger @@ -14,10 +13,10 @@ headers = {'User-Agent': '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"'} -prts_up_char = Path(DRAW_PATH + "/draw_card_up/prts_up_char.json") -genshin_up_char = Path(DRAW_PATH + "/draw_card_up/genshin_up_char.json") -pretty_up_char = Path(DRAW_PATH + "/draw_card_up/pretty_up_char.json") -guardian_up_char = Path(DRAW_PATH + "/draw_card_up/guardian_up_char.json") +prts_up_char = DRAW_PATH / "draw_card_up/prts_up_char.json" +genshin_up_char = DRAW_PATH / "draw_card_up/genshin_up_char.json" +pretty_up_char = DRAW_PATH / "draw_card_up/pretty_up_char.json" +guardian_up_char = DRAW_PATH / "draw_card_up/guardian_up_char.json" prts_url = "https://ak.hypergryph.com/news.html" genshin_url = "https://wiki.biligame.com/ys/%E7%A5%88%E6%84%BF" diff --git a/draw_card/async_update_game_info.py b/draw_card/async_update_game_info.py index 682559e..12f9581 100644 --- a/draw_card/async_update_game_info.py +++ b/draw_card/async_update_game_info.py @@ -10,11 +10,10 @@ from .genshin_handle import update_genshin_info, init_genshin_data from .fgo_handle import update_fgo_info, init_fgo_data from .onmyoji_handle import update_onmyoji_info, init_onmyoji_data -from .config import DRAW_PATH, PRTS_FLAG, PRETTY_FLAG, GUARDIAN_FLAG, PCR_FLAG, AZUR_FLAG, GENSHIN_FLAG, FGO_FLAG, \ - ONMYOJI_FLAG +from .config import DRAW_PATH, draw_config -driver: nonebot.Driver = nonebot.get_driver() +driver = nonebot.get_driver() @driver.on_startup @@ -22,37 +21,37 @@ async def async_update_game(): tasks = [] init_lst = [init_pcr_data, init_pretty_data, init_azur_data, init_prts_data, init_genshin_data, init_guardian_data, init_fgo_data, init_onmyoji_data] - if PRTS_FLAG and not os.path.exists(DRAW_PATH + 'prts.json'): + if draw_config.PRTS_FLAG and not (DRAW_PATH / 'prts.json').exists(): tasks.append(asyncio.ensure_future(update_prts_info())) init_lst.remove(init_prts_data) - if PRETTY_FLAG and (not os.path.exists(DRAW_PATH + 'pretty.json') or - not os.path.exists(DRAW_PATH + 'pretty_card.json')): + if draw_config.PRETTY_FLAG and (not (DRAW_PATH / 'pretty.json').exists() or + not (DRAW_PATH / 'pretty_card.json').exists()): tasks.append(asyncio.ensure_future(update_pretty_info())) init_lst.remove(init_pretty_data) - if GUARDIAN_FLAG and not os.path.exists(DRAW_PATH + 'guardian.json'): + if draw_config.GUARDIAN_FLAG and not (DRAW_PATH / 'guardian.json').exists(): tasks.append(asyncio.ensure_future(update_guardian_info())) - if PCR_FLAG and not os.path.exists(DRAW_PATH + 'pcr.json'): + if draw_config.PCR_FLAG and not (DRAW_PATH / 'pcr.json').exists(): tasks.append(asyncio.ensure_future(update_pcr_info())) init_lst.remove(init_pcr_data) - if GENSHIN_FLAG and (not os.path.exists(DRAW_PATH + 'genshin.json') or - not os.path.exists(DRAW_PATH + 'genshin_arms.json')): + if draw_config.GENSHIN_FLAG and (not (DRAW_PATH / 'genshin.json').exists() or + not (DRAW_PATH / 'genshin_arms.json').exists()): tasks.append(asyncio.ensure_future(update_genshin_info())) init_lst.remove(init_genshin_data) - if AZUR_FLAG and not os.path.exists(DRAW_PATH + 'azur.json'): + if draw_config.AZUR_FLAG and not (DRAW_PATH / 'azur.json').exists(): tasks.append(asyncio.ensure_future(update_azur_info())) init_lst.remove(init_azur_data) - if FGO_FLAG and (not os.path.exists(DRAW_PATH + 'fgo.json') or - not os.path.exists(DRAW_PATH + 'fgo_card.json')): + if draw_config.FGO_FLAG and (not (DRAW_PATH / 'fgo.json').exists() or + not (DRAW_PATH / 'fgo_card.json').exists()): tasks.append(asyncio.ensure_future(update_fgo_info())) init_lst.remove(init_fgo_data) - if ONMYOJI_FLAG and not os.path.exists(DRAW_PATH + 'onmyoji.json'): + if draw_config.ONMYOJI_FLAG and not (DRAW_PATH / 'onmyoji.json').exists(): tasks.append(asyncio.ensure_future(update_onmyoji_info())) init_lst.remove(init_onmyoji_data) try: @@ -62,8 +61,3 @@ async def async_update_game(): except asyncio.exceptions.CancelledError: logger.warning('更新异常:CancelledError,再次更新...') await async_update_game() - - - - - diff --git a/draw_card/azur_handle.py b/draw_card/azur_handle.py index 55be93f..e33c80e 100644 --- a/draw_card/azur_handle.py +++ b/draw_card/azur_handle.py @@ -1,9 +1,8 @@ - from nonebot.adapters.onebot.v11 import MessageSegment import random from .update_game_simple_info import update_simple_info from .util import generate_img, init_star_rst, BaseData, set_list, get_star, max_card, format_card_information -from .config import AZUR_ONE_P, AZUR_TWO_P, AZUR_THREE_P, AZUR_FOUR_P, AZUR_FLAG, DRAW_PATH +from .config import DRAW_PATH, draw_config from dataclasses import dataclass from .init_card_pool import init_game_pool try: @@ -42,8 +41,8 @@ async def update_azur_info(): async def init_azur_data(): global ALL_CHAR - if AZUR_FLAG: - with open(DRAW_PATH + 'azur.json', 'r', encoding='utf8') as f: + if draw_config.AZUR_FLAG: + with (DRAW_PATH / 'azur.json').open('r', encoding='utf8') as f: azur_dict = json.load(f) ALL_CHAR = init_game_pool('azur', azur_dict, AzurChar) @@ -51,13 +50,13 @@ async def init_azur_data(): # 抽取卡池 def _get_azur_card(pool_name: str, mode: int = 1): global ALL_CHAR + azur_config = draw_config.azur if pool_name == '轻型': itype = ['驱逐', '轻巡', '维修'] elif pool_name == '重型': itype = ['重巡', '战列', '战巡', '重炮'] else: itype = ['维修', '潜艇', '重巡', '轻航', '航母'] - star = get_star([4, 3, 2, 1], [AZUR_FOUR_P, AZUR_THREE_P, AZUR_TWO_P, AZUR_ONE_P]) + star = get_star([4, 3, 2, 1], [azur_config.AZUR_FOUR_P, azur_config.AZUR_THREE_P, azur_config.AZUR_TWO_P, azur_config.AZUR_ONE_P]) chars = [x for x in ALL_CHAR if x.star == star and x.itype in itype and not x.limited] return random.choice(chars), 4 - star - diff --git a/draw_card/config.py b/draw_card/config.py index 93bc423..f052abe 100644 --- a/draw_card/config.py +++ b/draw_card/config.py @@ -1,341 +1,168 @@ import nonebot from pathlib import Path from nonebot.log import logger +from pydantic import BaseModel, Extra, ValidationError + try: import ujson as json except ModuleNotFoundError: import json -DRAW_PATH = nonebot.get_driver().config.draw_path - -if not DRAW_PATH: - DRAW_PATH = str(Path("data/draw_card").absolute()) + '/' - -_draw_config = Path(rf"{DRAW_PATH}/draw_card_config/draw_card_config.json") +# 原神 +class GenshinConfig(BaseModel, extra=Extra.ignore): + GENSHIN_FIVE_P: float = 0.006 + GENSHIN_FOUR_P: float = 0.051 + GENSHIN_THREE_P: float = 0.43 + GENSHIN_G_FIVE_P: float = 0.13 + GENSHIN_G_FOUR_P: float = 0.016 + I72_ADD: float = 0.0585 -# 开关 -PRTS_FLAG = False if str(nonebot.get_driver().config.prts_flag).lower() == 'false' else True -GENSHIN_FLAG = False if str(nonebot.get_driver().config.genshin_flag).lower() == 'false' else True -PRETTY_FLAG = False if str(nonebot.get_driver().config.pretty_flag).lower() == 'false' else True -GUARDIAN_FLAG = False if str(nonebot.get_driver().config.guardian_flag).lower() == 'false' else True -PCR_FLAG = False if str(nonebot.get_driver().config.pcr_flag).lower() == 'false' else True -AZUR_FLAG = False if str(nonebot.get_driver().config.azur_flag).lower() == 'false' else True -FGO_FLAG = False if str(nonebot.get_driver().config.fgo_flag).lower() == 'false' else True -ONMYOJI_FLAG = False if str(nonebot.get_driver().config.onmyoji_flag).lower() == 'false' else True +# 明日方舟 +class PrtsConfig(BaseModel, extra=Extra.ignore): + PRTS_SIX_P: float = 0.02 + PRTS_FIVE_P: float = 0.08 + PRTS_FOUR_P: float = 0.48 + PRTS_THREE_P: float = 0.42 -SEMAPHORE = 5 if not nonebot.get_driver().config.semaphore else int(nonebot.get_driver().config.semaphore) -PCR_TAI = True if str(nonebot.get_driver().config.pcr_tai).lower() == 'true' else False -# 方舟概率 -PRTS_SIX_P = 0.02 -PRTS_FIVE_P = 0.08 -PRTS_FOUR_P = 0.48 -PRTS_THREE_P = 0.42 +# 赛马娘 +class PrettyConfig(BaseModel, extra=Extra.ignore): + PRETTY_THREE_P: float = 0.03 + PRETTY_TWO_P: float = 0.18 + PRETTY_ONE_P: float = 0.79 -# 原神概率 -GENSHIN_FIVE_P = 0.006 -GENSHIN_FOUR_P = 0.051 -GENSHIN_THREE_P = 0.43 -# 保底概率 -GENSHIN_G_FIVE_P = 0.016 -GENSHIN_G_FOUR_P = 0.13 -# 72抽后增加的概率 -I72_ADD = 0.0585 - -# 赛马娘概率 -PRETTY_THREE_P = 0.03 -PRETTY_TWO_P = 0.18 -PRETTY_ONE_P = 0.79 # 坎公骑冠剑 -# 角色概率 -GUARDIAN_THREE_CHAR_P = 0.0275 -GUARDIAN_TWO_CHAR_P = 0.19 -GUARDIAN_ONE_CHAR_P = 0.7825 -# UP角色 -GUARDIAN_THREE_CHAR_UP_P = 0.01375 -GUARDIAN_THREE_CHAR_OTHER_P = 0.01375 -# 武器概率 -GUARDIAN_EXCLUSIVE_ARMS_P = 0.03 -GUARDIAN_FIVE_ARMS_P = 0.03 -GUARDIAN_FOUR_ARMS_P = 0.09 -GUARDIAN_THREE_ARMS_P = 0.27 -GUARDIAN_TWO_ARMS_P = 0.58 -# UP武器 -GUARDIAN_EXCLUSIVE_ARMS_UP_P = 0.01 -GUARDIAN_EXCLUSIVE_ARMS_OTHER_P = 0.02 +class GuardianConfig(BaseModel, extra=Extra.ignore): + GUARDIAN_THREE_CHAR_P: float = 0.0275 + GUARDIAN_TWO_CHAR_P: float = 0.19 + GUARDIAN_ONE_CHAR_P: float = 0.7825 + GUARDIAN_THREE_CHAR_UP_P: float = 0.01375 + GUARDIAN_THREE_CHAR_OTHER_P: float = 0.01375 + GUARDIAN_EXCLUSIVE_ARMS_P: float = 0.03 + GUARDIAN_FIVE_ARMS_P: float = 0.03 + GUARDIAN_FOUR_ARMS_P: float = 0.09 + GUARDIAN_THREE_ARMS_P: float = 0.27 + GUARDIAN_TWO_ARMS_P: float = 0.58 + GUARDIAN_EXCLUSIVE_ARMS_UP_P: float = 0.01 + GUARDIAN_EXCLUSIVE_ARMS_OTHER_P: float = 0.02 + + +# 公主连结 +class PcrConfig(BaseModel, extra=Extra.ignore): + PCR_THREE_P: float = 0.025 + PCR_TWO_P: float = 0.18 + PCR_ONE_P: float = 0.795 + PCR_G_THREE_P: float = 0.025 + PCR_G_TWO_P: float = 0.975 -# PCR -PCR_THREE_P = 0.025 -PCR_TWO_P = 0.18 -PCR_ONE_P = 0.795 -# 保底 -PCR_G_THREE_P = 0.025 -PCR_G_TWO_P = 0.975 # 碧蓝航线 -AZUR_FIVE_P = 0.012 -AZUR_FOUR_P = 0.07 -AZUR_THREE_P = 0.12 -AZUR_TWO_P = 0.51 -AZUR_ONE_P = 0.3 - -# FGO -FGO_SERVANT_FIVE_P = 0.01 -FGO_SERVANT_FOUR_P = 0.03 -FGO_SERVANT_THREE_P = 0.4 -FGO_CARD_FIVE_P = 0.04 -FGO_CARD_FOUR_P = 0.12 -FGO_CARD_THREE_P = 0.4 - -# 阴阳师 -ONMYOJI_SP = 0.0025 -ONMYOJI_SSR = 0.01 -ONMYOJI_SR = 0.2 -ONMYOJI_R = 0.7875 - - -path_dict = { - 'genshin': '原神', - 'prts': '明日方舟', - 'pretty': '赛马娘', - 'guardian': '坎公骑冠剑', - 'pcr': '公主连结', - 'azur': '碧蓝航线', - 'fgo': '命运-冠位指定', - 'onmyoji': '阴阳师', -} - -driver: nonebot.Driver = nonebot.get_driver() +class AzurConfig(BaseModel, extra=Extra.ignore): + AZUR_FIVE_P: float = 0.012 + AZUR_FOUR_P: float = 0.07 + AZUR_THREE_P: float = 0.12 + AZUR_TWO_P: float = 0.51 + AZUR_ONE_P: float = 0.3 -config_default_data = { - 'path_dict': { - 'genshin': '原神', - 'prts': '明日方舟', - 'pretty': '赛马娘', - 'guardian': '坎公骑冠剑', - 'pcr': '公主连结', - 'azur': '碧蓝航线', - 'fgo': '命运-冠位指定', - 'onmyoji': '阴阳师', - }, +# 命运-冠位指定 +class FgoConfig(BaseModel, extra=Extra.ignore): + FGO_SERVANT_FIVE_P: float = 0.01 + FGO_SERVANT_FOUR_P: float = 0.03 + FGO_SERVANT_THREE_P: float = 0.4 + FGO_CARD_FIVE_P: float = 0.04 + FGO_CARD_FOUR_P: float = 0.12 + FGO_CARD_THREE_P: float = 0.4 - 'prts': { - 'PRTS_SIX_P': 0.02, - 'PRTS_FIVE_P': 0.08, - 'PRTS_FOUR_P': 0.48, - 'PRTS_THREE_P': 0.42, - }, - 'genshin': { - 'GENSHIN_FIVE_P': 0.006, - 'GENSHIN_FOUR_P': 0.051, - 'GENSHIN_THREE_P': 0.43, - 'GENSHIN_G_FIVE_P': 0.13, - 'GENSHIN_G_FOUR_P': 0.016, - 'I72_ADD': 0.0585, - }, - - 'pretty': { - 'PRETTY_THREE_P': 0.03, - 'PRETTY_TWO_P': 0.18, - 'PRETTY_ONE_P': 0.79, - }, - - 'guardian': { - 'GUARDIAN_THREE_CHAR_P': 0.0275, - 'GUARDIAN_TWO_CHAR_P': 0.19, - 'GUARDIAN_ONE_CHAR_P': 0.7825, - - 'GUARDIAN_THREE_CHAR_UP_P': 0.01375, - 'GUARDIAN_THREE_CHAR_OTHER_P': 0.01375, - - 'GUARDIAN_EXCLUSIVE_ARMS_P': 0.03, - 'GUARDIAN_FIVE_ARMS_P': 0.03, - 'GUARDIAN_FOUR_ARMS_P': 0.09, - 'GUARDIAN_THREE_ARMS_P': 0.27, - 'GUARDIAN_TWO_ARMS_P': 0.58, - - 'GUARDIAN_EXCLUSIVE_ARMS_UP_P': 0.01, - 'GUARDIAN_EXCLUSIVE_ARMS_OTHER_P': 0.02, - }, +# 阴阳师 +class OnmyojiConfig(BaseModel, extra=Extra.ignore): + ONMYOJI_SP: float = 0.0025 + ONMYOJI_SSR: float = 0.01 + ONMYOJI_SR: float = 0.2 + ONMYOJI_R: float = 0.7875 + + +class PathDict(BaseModel, extra=Extra.ignore): + genshin: str = "原神" + prts: str = "明日方舟" + pretty: str = "赛马娘" + guardian: str = "坎公骑冠剑" + pcr: str = "公主连结" + azur: str = "碧蓝航线" + fgo: str = "命运-冠位指定" + onmyoji: str = "阴阳师" + + +class Config(BaseModel, extra=Extra.ignore): + # 开关 + PRTS_FLAG: bool = True + GENSHIN_FLAG: bool = True + PRETTY_FLAG: bool = True + GUARDIAN_FLAG: bool = True + PCR_FLAG: bool = True + AZUR_FLAG: bool = True + FGO_FLAG: bool = True + ONMYOJI_FLAG: bool = True + + # 其他配置 + PCR_TAI: bool = True + SEMAPHORE: int = 5 + + # 路径 + path_dict: dict = { + "genshin": "原神", + "prts": "明日方舟", + "pretty": "赛马娘", + "guardian": "坎公骑冠剑", + "pcr": "公主连结", + "azur": "碧蓝航线", + "fgo": "命运-冠位指定", + "onmyoji": "阴阳师", + } - 'pcr': { - 'PCR_THREE_P': 0.025, - 'PCR_TWO_P': 0.18, - 'PCR_ONE_P': 0.795, - }, + # 抽卡概率 + prts: PrtsConfig = PrtsConfig() + genshin: GenshinConfig = GenshinConfig() + pretty: PrettyConfig = PrettyConfig() + guardian: GuardianConfig = GuardianConfig() + pcr: PcrConfig = PcrConfig() + azur: AzurConfig = AzurConfig() + fgo: FgoConfig = FgoConfig() + onmyoji: OnmyojiConfig = OnmyojiConfig() - 'azur': { - 'AZUR_FIVE_P': 0.012, - 'AZUR_FOUR_P': 0.07, - 'AZUR_THREE_P': 0.12, - 'AZUR_TWO_P': 0.51, - 'AZUR_ONE_P': 0.3, - }, - 'fgo': { - 'FGO_SERVANT_FIVE_P': 0.01, - 'FGO_SERVANT_FOUR_P': 0.03, - 'FGO_SERVANT_THREE_P': 0.4, - 'FGO_CARD_FIVE_P': 0.04, - 'FGO_CARD_FOUR_P': 0.12, - 'FGO_CARD_THREE_P': 0.4, - }, +driver = nonebot.get_driver() +global_config = driver.config +draw_path = global_config.draw_path +DRAW_PATH = Path(draw_path) if draw_path else Path("data/draw_card").absolute() +config_path = DRAW_PATH / "draw_card_config" / "draw_card_config.json" - 'onmyoji': { - 'ONMYOJI_SP': 0.0025, - 'ONMYOJI_SSR': 0.01, - 'ONMYOJI_SR': 0.2, - 'ONMYOJI_R': 0.7875, - } -} +draw_config: Config = Config() @driver.on_startup def check_config(): - global PRTS_SIX_P, PRTS_FOUR_P, PRTS_FIVE_P, PRTS_THREE_P, GENSHIN_G_FIVE_P, config_default_data, \ - GENSHIN_G_FOUR_P, GENSHIN_FOUR_P, GENSHIN_FIVE_P, I72_ADD, path_dict, PRETTY_THREE_P, \ - PRETTY_ONE_P, PRETTY_TWO_P, GENSHIN_THREE_P, GUARDIAN_THREE_CHAR_P, GUARDIAN_TWO_CHAR_P, GUARDIAN_ONE_CHAR_P, \ - GUARDIAN_THREE_CHAR_UP_P, GUARDIAN_THREE_CHAR_OTHER_P, GUARDIAN_EXCLUSIVE_ARMS_P, GUARDIAN_FIVE_ARMS_P, \ - GUARDIAN_FOUR_ARMS_P, GUARDIAN_THREE_ARMS_P, GUARDIAN_TWO_ARMS_P, GENSHIN_FLAG, PRTS_FLAG, \ - PRETTY_FLAG, GUARDIAN_FLAG, GUARDIAN_EXCLUSIVE_ARMS_UP_P, GUARDIAN_EXCLUSIVE_ARMS_OTHER_P, DRAW_PATH, \ - PCR_THREE_P, PCR_TWO_P, PCR_ONE_P, AZUR_FOUR_P, AZUR_THREE_P, AZUR_TWO_P, AZUR_ONE_P, AZUR_FIVE_P, FGO_CARD_FIVE_P,\ - FGO_CARD_FOUR_P, FGO_CARD_THREE_P, FGO_SERVANT_THREE_P, FGO_SERVANT_FOUR_P, FGO_SERVANT_FIVE_P, ONMYOJI_R, ONMYOJI_SP, \ - ONMYOJI_SSR, ONMYOJI_SR - _draw_config.parent.mkdir(parents=True, exist_ok=True) - try: - data = json.load(open(_draw_config, 'r', encoding='utf8')) - except (FileNotFoundError, ValueError): - _draw_config.parent.mkdir(parents=True, exist_ok=True) - json.dump(config_default_data, open(_draw_config, 'w', encoding='utf8'), indent=4, ensure_ascii=False) - logger.warning('draw_card:配置文件不存在或格式错误,已重新生成配置文件.....') - else: - - try: - PRTS_SIX_P = float(data['prts']['PRTS_SIX_P']) - PRTS_FIVE_P = float(data['prts']['PRTS_FIVE_P']) - PRTS_FOUR_P = float(data['prts']['PRTS_FOUR_P']) - PRTS_THREE_P = float(data['prts']['PRTS_THREE_P']) - except KeyError: - data['prts'] = {} - data['prts']['PRTS_SIX_P'] = config_default_data['prts']['PRTS_SIX_P'] - data['prts']['PRTS_FIVE_P'] = config_default_data['prts']['PRTS_FIVE_P'] - data['prts']['PRTS_FOUR_P'] = config_default_data['prts']['PRTS_FOUR_P'] - data['prts']['PRTS_THREE_P'] = config_default_data['prts']['PRTS_THREE_P'] - - try: - GENSHIN_FIVE_P = float(data['genshin']['GENSHIN_FIVE_P']) - GENSHIN_FOUR_P = float(data['genshin']['GENSHIN_FOUR_P']) - GENSHIN_THREE_P = float(data['genshin']['GENSHIN_THREE_P']) - GENSHIN_G_FIVE_P = float(data['genshin']['GENSHIN_G_FIVE_P']) - GENSHIN_G_FOUR_P = float(data['genshin']['GENSHIN_G_FOUR_P']) - I72_ADD = float(data['genshin']['I72_ADD']) - except KeyError: - data['genshin'] = {} - data['genshin']['GENSHIN_FIVE_P'] = config_default_data['genshin']['GENSHIN_FIVE_P'] - data['genshin']['GENSHIN_FOUR_P'] = config_default_data['genshin']['GENSHIN_FOUR_P'] - data['genshin']['GENSHIN_THREE_P'] = config_default_data['genshin']['GENSHIN_THREE_P'] - data['genshin']['GENSHIN_G_FIVE_P'] = config_default_data['genshin']['GENSHIN_G_FIVE_P'] - data['genshin']['GENSHIN_G_FOUR_P'] = config_default_data['genshin']['GENSHIN_G_FOUR_P'] - data['genshin']['I72_ADD'] = config_default_data['genshin']['I72_ADD'] - - try: - PRETTY_THREE_P = float(data['pretty']['PRETTY_THREE_P']) - PRETTY_TWO_P = float(data['pretty']['PRETTY_TWO_P']) - PRETTY_ONE_P = float(data['pretty']['PRETTY_ONE_P']) - except KeyError: - data['pretty'] = {} - data['pretty']['PRETTY_THREE_P'] = config_default_data['pretty']['PRETTY_THREE_P'] - data['pretty']['PRETTY_TWO_P'] = config_default_data['pretty']['PRETTY_TWO_P'] - data['pretty']['PRETTY_ONE_P'] = config_default_data['pretty']['PRETTY_ONE_P'] - - try: - GUARDIAN_THREE_CHAR_P = float(data['guardian']['GUARDIAN_THREE_CHAR_P']) - GUARDIAN_TWO_CHAR_P = float(data['guardian']['GUARDIAN_TWO_CHAR_P']) - GUARDIAN_ONE_CHAR_P = float(data['guardian']['GUARDIAN_ONE_CHAR_P']) - GUARDIAN_THREE_CHAR_UP_P = float(data['guardian']['GUARDIAN_THREE_CHAR_UP_P']) - GUARDIAN_THREE_CHAR_OTHER_P = float(data['guardian']['GUARDIAN_THREE_CHAR_OTHER_P']) - GUARDIAN_EXCLUSIVE_ARMS_P = float(data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_P']) - GUARDIAN_FIVE_ARMS_P = float(data['guardian']['GUARDIAN_FIVE_ARMS_P']) - GUARDIAN_FOUR_ARMS_P = float(data['guardian']['GUARDIAN_FOUR_ARMS_P']) - GUARDIAN_THREE_ARMS_P = float(data['guardian']['GUARDIAN_THREE_ARMS_P']) - GUARDIAN_TWO_ARMS_P = float(data['guardian']['GUARDIAN_TWO_ARMS_P']) - GUARDIAN_EXCLUSIVE_ARMS_UP_P = float(data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_UP_P']) - GUARDIAN_EXCLUSIVE_ARMS_OTHER_P = float(data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_OTHER_P']) - except KeyError: - data['guardian'] = {} - data['guardian']['GUARDIAN_THREE_CHAR_P'] = config_default_data['guardian']['GUARDIAN_THREE_CHAR_P'] - data['guardian']['GUARDIAN_TWO_CHAR_P'] = config_default_data['guardian']['GUARDIAN_TWO_CHAR_P'] - data['guardian']['GUARDIAN_ONE_CHAR_P'] = config_default_data['guardian']['GUARDIAN_ONE_CHAR_P'] - data['guardian']['GUARDIAN_THREE_CHAR_UP_P'] = config_default_data['guardian']['GUARDIAN_THREE_CHAR_UP_P'] - data['guardian']['GUARDIAN_THREE_CHAR_OTHER_P'] = config_default_data['guardian']['GUARDIAN_THREE_CHAR_OTHER_P'] - data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_P'] = config_default_data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_P'] - data['guardian']['GUARDIAN_FIVE_ARMS_P'] = config_default_data['guardian']['GUARDIAN_FIVE_ARMS_P'] - data['guardian']['GUARDIAN_FOUR_ARMS_P'] = config_default_data['guardian']['GUARDIAN_FOUR_ARMS_P'] - data['guardian']['GUARDIAN_THREE_ARMS_P'] = config_default_data['guardian']['GUARDIAN_THREE_ARMS_P'] - data['guardian']['GUARDIAN_TWO_ARMS_P'] = config_default_data['guardian']['GUARDIAN_TWO_ARMS_P'] - data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_UP_P'] = config_default_data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_UP_P'] - data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_OTHER_P'] = config_default_data['guardian']['GUARDIAN_EXCLUSIVE_ARMS_OTHER_P'] - - try: - PCR_THREE_P = float(data['pcr']['PCR_THREE_P']) - PCR_TWO_P = float(data['pcr']['PCR_TWO_P']) - PCR_ONE_P = float(data['pcr']['PCR_ONE_P']) - except KeyError: - data['pcr'] = {} - data['pcr']['PCR_THREE_P'] = config_default_data['pcr']['PCR_THREE_P'] - data['pcr']['PCR_TWO_P'] = config_default_data['pcr']['PCR_TWO_P'] - data['pcr']['PCR_ONE_P'] = config_default_data['pcr']['PCR_ONE_P'] - - try: - AZUR_FIVE_P = float(data['azur']['AZUR_FIVE_P']) - AZUR_FOUR_P = float(data['azur']['AZUR_FOUR_P']) - AZUR_THREE_P = float(data['azur']['AZUR_THREE_P']) - AZUR_TWO_P = float(data['azur']['AZUR_TWO_P']) - AZUR_ONE_P = float(data['azur']['AZUR_ONE_P']) - except KeyError: - data['azur'] = {} - data['azur']['AZUR_FIVE_P'] = config_default_data['azur']['AZUR_FIVE_P'] - data['azur']['AZUR_FOUR_P'] = config_default_data['azur']['AZUR_FOUR_P'] - data['azur']['AZUR_THREE_P'] = config_default_data['azur']['AZUR_THREE_P'] - data['azur']['AZUR_TWO_P'] = config_default_data['azur']['AZUR_TWO_P'] - data['azur']['AZUR_ONE_P'] = config_default_data['azur']['AZUR_ONE_P'] - - try: - FGO_SERVANT_FIVE_P = float(data['fgo']['FGO_SERVANT_FIVE_P']) - FGO_SERVANT_FOUR_P = float(data['fgo']['FGO_SERVANT_FOUR_P']) - FGO_SERVANT_THREE_P = float(data['fgo']['FGO_SERVANT_THREE_P']) - FGO_CARD_FIVE_P = float(data['fgo']['FGO_CARD_FIVE_P']) - FGO_CARD_FOUR_P = float(data['fgo']['FGO_CARD_FOUR_P']) - FGO_CARD_THREE_P = float(data['fgo']['FGO_CARD_THREE_P']) - except KeyError: - data['fgo'] = {} - data['fgo']['FGO_SERVANT_FIVE_P'] = config_default_data['fgo']['FGO_SERVANT_FIVE_P'] - data['fgo']['FGO_SERVANT_FOUR_P'] = config_default_data['fgo']['FGO_SERVANT_FOUR_P'] - data['fgo']['FGO_SERVANT_THREE_P'] = config_default_data['fgo']['FGO_SERVANT_THREE_P'] - data['fgo']['FGO_CARD_FIVE_P'] = config_default_data['fgo']['FGO_CARD_FIVE_P'] - data['fgo']['FGO_CARD_FOUR_P'] = config_default_data['fgo']['FGO_CARD_FOUR_P'] - data['fgo']['FGO_CARD_THREE_P'] = config_default_data['fgo']['FGO_CARD_THREE_P'] + global draw_config + if not config_path.exists(): + config_path.parent.mkdir(parents=True, exist_ok=True) + draw_config = Config() + logger.warning("draw_card:配置文件不存在,已重新生成配置文件.....") + else: + data = json.load(config_path.open("r", encoding="utf8")) try: - ONMYOJI_SP = float(data['onmyoji']['ONMYOJI_SP']) - ONMYOJI_SSR = float(data['onmyoji']['ONMYOJI_SSR']) - ONMYOJI_SR = float(data['onmyoji']['ONMYOJI_SR']) - ONMYOJI_R = float(data['onmyoji']['ONMYOJI_R']) - except KeyError: - data['onmyoji'] = {} - data['onmyoji']['ONMYOJI_SP'] = config_default_data['onmyoji']['ONMYOJI_SP'] - data['onmyoji']['ONMYOJI_SSR'] = config_default_data['onmyoji']['ONMYOJI_SSR'] - data['onmyoji']['ONMYOJI_SR'] = config_default_data['onmyoji']['ONMYOJI_SR'] - data['onmyoji']['ONMYOJI_R'] = config_default_data['onmyoji']['ONMYOJI_R'] - - json.dump(data, open(_draw_config, 'w', encoding='utf8'), indent=4, ensure_ascii=False) - - - - - + draw_config = Config.parse_obj(global_config.dict().update(data)) + except ValidationError: + draw_config = Config() + logger.warning("draw_card:配置文件格式错误,已重新生成配置文件.....") + + json.dump( + draw_config.dict(), + config_path.open("w", encoding="utf8"), + indent=4, + ensure_ascii=False, + ) diff --git a/draw_card/count_manager.py b/draw_card/count_manager.py index 63eb675..093375e 100644 --- a/draw_card/count_manager.py +++ b/draw_card/count_manager.py @@ -111,4 +111,3 @@ def check(self, key: int, *args) -> Optional[Union[str, int]]: self._data[key][f"count_{i}"] = self._data[key]['count'] return self._star2name[i] return None - diff --git a/draw_card/create_img.py b/draw_card/create_img.py index 99aa30e..4caa316 100644 --- a/draw_card/create_img.py +++ b/draw_card/create_img.py @@ -63,5 +63,3 @@ def pic2bs4(self): self.markImg.save(buf, format='PNG') base64_str = base64.b64encode(buf.getvalue()).decode() return base64_str - - diff --git a/draw_card/fgo_handle.py b/draw_card/fgo_handle.py index dd69ced..1874e14 100644 --- a/draw_card/fgo_handle.py +++ b/draw_card/fgo_handle.py @@ -1,10 +1,8 @@ - from nonebot.adapters.onebot.v11 import MessageSegment import random from .update_game_requests_info import update_requests_info from .util import generate_img, init_star_rst, BaseData, set_list, get_star, max_card -from .config import FGO_CARD_FOUR_P, FGO_CARD_FIVE_P, FGO_CARD_THREE_P, FGO_SERVANT_THREE_P, \ - FGO_SERVANT_FIVE_P, FGO_SERVANT_FOUR_P, FGO_FLAG, DRAW_PATH +from .config import DRAW_PATH, draw_config from dataclasses import dataclass from .init_card_pool import init_game_pool @@ -45,11 +43,11 @@ async def update_fgo_info(): async def init_fgo_data(): global ALL_CHAR, ALL_CARD - if FGO_FLAG: - with open(DRAW_PATH + 'fgo.json', 'r', encoding='utf8') as f: + if draw_config.FGO_FLAG: + with (DRAW_PATH / 'fgo.json').open('r', encoding='utf8') as f: fgo_dict = json.load(f) ALL_CHAR = init_game_pool('fgo', fgo_dict, FgoChar) - with open(DRAW_PATH + 'fgo_card.json', 'r', encoding='utf8') as f: + with (DRAW_PATH / 'fgo_card.json').open('r', encoding='utf8') as f: fgo_dict = json.load(f) ALL_CARD = init_game_pool('fgo', fgo_dict, FgoChar) @@ -57,13 +55,14 @@ async def init_fgo_data(): # 抽取卡池 def _get_fgo_card(mode: int = 1): global ALL_CHAR, ALL_CARD + fgo_config = draw_config.fgo if mode == 1: - star = get_star([8, 7, 6, 5, 4, 3], [FGO_SERVANT_FIVE_P, FGO_SERVANT_FOUR_P, FGO_SERVANT_THREE_P, - FGO_CARD_FIVE_P, FGO_CARD_FOUR_P, FGO_CARD_THREE_P]) + star = get_star([8, 7, 6, 5, 4, 3], [fgo_config.FGO_SERVANT_FIVE_P, fgo_config.FGO_SERVANT_FOUR_P, fgo_config.FGO_SERVANT_THREE_P, + fgo_config.FGO_CARD_FIVE_P, fgo_config.FGO_CARD_FOUR_P, fgo_config.FGO_CARD_THREE_P]) elif mode == 2: - star = get_star([5, 4], [FGO_CARD_FIVE_P, FGO_CARD_FOUR_P]) + star = get_star([5, 4], [fgo_config.FGO_CARD_FIVE_P, fgo_config.FGO_CARD_FOUR_P]) else: - star = get_star([8, 7, 6], [FGO_SERVANT_FIVE_P, FGO_SERVANT_FOUR_P, FGO_SERVANT_THREE_P]) + star = get_star([8, 7, 6], [fgo_config.FGO_SERVANT_FIVE_P, fgo_config.FGO_SERVANT_FOUR_P, fgo_config.FGO_SERVANT_THREE_P]) if star > 5: itype = 'servant' star -= 3 diff --git a/draw_card/genshin_handle.py b/draw_card/genshin_handle.py index 7ee64db..c3a7392 100644 --- a/draw_card/genshin_handle.py +++ b/draw_card/genshin_handle.py @@ -1,6 +1,4 @@ -import os from nonebot.adapters.onebot.v11 import MessageSegment, Message -import nonebot import random from .update_game_info import update_info from .util import ( @@ -11,16 +9,7 @@ get_star, init_up_char, ) -from .config import ( - GENSHIN_FIVE_P, - GENSHIN_FOUR_P, - GENSHIN_G_FIVE_P, - GENSHIN_G_FOUR_P, - GENSHIN_THREE_P, - I72_ADD, - DRAW_PATH, - GENSHIN_FLAG, -) +from .config import DRAW_PATH, draw_config from .count_manager import GenshinCountManager from dataclasses import dataclass from .init_card_pool import init_game_pool @@ -31,7 +20,6 @@ except ModuleNotFoundError: import json -driver: nonebot.Driver = nonebot.get_driver() announcement = GenshinAnnouncement() @@ -118,15 +106,13 @@ async def update_genshin_info(): async def init_genshin_data(): global ALL_CHAR, ALL_ARMS - if GENSHIN_FLAG: - if not os.path.exists(DRAW_PATH + "genshin.json") or not os.path.exists( - DRAW_PATH + "genshin_arms.json" - ): + if draw_config.GENSHIN_FLAG: + if not (DRAW_PATH / "genshin.json").exists() or not (DRAW_PATH / "genshin_arms.json").exists(): await update_genshin_info() else: - with open(DRAW_PATH + "genshin.json", "r", encoding="utf8") as f: + with (DRAW_PATH / "genshin.json").open("r", encoding="utf8") as f: genshin_dict = json.load(f) - with open(DRAW_PATH + "genshin_arms.json", "r", encoding="utf8") as f: + with (DRAW_PATH / "genshin_arms.json").open("r", encoding="utf8") as f: genshin_ARMS_dict = json.load(f) ALL_CHAR = init_game_pool("genshin", genshin_dict, GenshinChar) ALL_ARMS = init_game_pool("genshin_arms", genshin_ARMS_dict, GenshinChar) @@ -139,12 +125,13 @@ def _get_genshin_card(mode: int = 1, pool_name: str = "", add: float = 0.0, is_u mode 1:普通抽 2:四星保底 3:五星保底 """ global ALL_ARMS, ALL_CHAR, UP_ARMS, UP_CHAR, _CURRENT_ARMS_POOL_TITLE, _CURRENT_CHAR_POOL_TITLE + genshin_config = draw_config.genshin if mode == 1: star = get_star( - [5, 4, 3], [GENSHIN_FIVE_P + add, GENSHIN_FOUR_P, GENSHIN_THREE_P] + [5, 4, 3], [genshin_config.GENSHIN_FIVE_P + add, genshin_config.GENSHIN_FOUR_P, genshin_config.GENSHIN_THREE_P] ) elif mode == 2: - star = get_star([5, 4], [GENSHIN_G_FIVE_P + add, GENSHIN_G_FOUR_P]) + star = get_star([5, 4], [genshin_config.GENSHIN_G_FIVE_P + add, genshin_config.GENSHIN_G_FOUR_P]) else: star = 5 if pool_name == "char": @@ -201,7 +188,7 @@ def _format_card_information(_count: int, user_id, pool_name): draw_count_manager.increase(user_id) star = draw_count_manager.check(user_id) if (draw_count_manager.get_user_count(user_id) - draw_count_manager.get_user_count(user_id, 1)) % 90 >= 72: - add += I72_ADD + add += draw_config.genshin.I72_ADD if star: star = int(star) if star == 4: diff --git a/draw_card/guardian_handle.py b/draw_card/guardian_handle.py index c1483d5..06b0aee 100644 --- a/draw_card/guardian_handle.py +++ b/draw_card/guardian_handle.py @@ -1,15 +1,9 @@ - -import os -import nonebot from nonebot.adapters.onebot.v11 import MessageSegment, Message from .update_game_info import update_info from .util import init_star_rst, generate_img, max_card, BaseData,\ set_list, get_star, format_card_information, init_up_char import random -from .config import DRAW_PATH, GUARDIAN_ONE_CHAR_P, GUARDIAN_TWO_CHAR_P, GUARDIAN_THREE_CHAR_P, \ - GUARDIAN_THREE_CHAR_UP_P, GUARDIAN_TWO_ARMS_P, GUARDIAN_FIVE_ARMS_P, GUARDIAN_THREE_CHAR_OTHER_P, \ - GUARDIAN_FOUR_ARMS_P, GUARDIAN_THREE_ARMS_P, GUARDIAN_EXCLUSIVE_ARMS_P, GUARDIAN_EXCLUSIVE_ARMS_UP_P, \ - GUARDIAN_EXCLUSIVE_ARMS_OTHER_P, GUARDIAN_FLAG +from .config import DRAW_PATH, draw_config from dataclasses import dataclass from .init_card_pool import init_game_pool from .announcement import GuardianAnnouncement @@ -18,7 +12,6 @@ except ModuleNotFoundError: import json -driver: nonebot.Driver = nonebot.get_driver() announcement = GuardianAnnouncement() @@ -98,13 +91,13 @@ async def update_guardian_info(): async def init_guardian_data(): global ALL_CHAR, ALL_ARMS - if GUARDIAN_FLAG: - if not os.path.exists(DRAW_PATH + 'guardian.json') or not os.path.exists(DRAW_PATH + 'guardian_arms.json'): + if draw_config.GUARDIAN_FLAG: + if (DRAW_PATH / 'guardian.json').exists() or not (DRAW_PATH / 'guardian_arms.json').exists(): await update_guardian_info() else: - with open(DRAW_PATH + 'guardian.json', 'r', encoding='utf8') as f: + with (DRAW_PATH / 'guardian.json').open('r', encoding='utf8') as f: guardian_char_dict = json.load(f) - with open(DRAW_PATH + 'guardian_arms.json', 'r', encoding='utf8') as f: + with (DRAW_PATH / 'guardian_arms.json').open('r', encoding='utf8') as f: guardian_arms_dict = json.load(f) ALL_CHAR = init_game_pool('guardian', guardian_char_dict, GuardianChar) ALL_ARMS = init_game_pool('guardian_arms', guardian_arms_dict, GuardianArms) @@ -114,21 +107,22 @@ async def init_guardian_data(): # 抽取卡池 def _get_guardian_card(pool_name: str = '', mode: int = 1): global ALL_ARMS, ALL_CHAR, UP_ARMS, UP_CHAR, _CURRENT_ARMS_POOL_TITLE, _CURRENT_CHAR_POOL_TITLE + guardian_config = draw_config.guardian if pool_name == 'char': if mode == 1: - star = get_star([3, 2, 1], [GUARDIAN_THREE_CHAR_P, GUARDIAN_TWO_CHAR_P, GUARDIAN_ONE_CHAR_P]) + star = get_star([3, 2, 1], [guardian_config.GUARDIAN_THREE_CHAR_P, guardian_config.GUARDIAN_TWO_CHAR_P, guardian_config.GUARDIAN_ONE_CHAR_P]) else: - star = get_star([3, 2], [GUARDIAN_THREE_CHAR_P, GUARDIAN_TWO_CHAR_P]) + star = get_star([3, 2], [guardian_config.GUARDIAN_THREE_CHAR_P, guardian_config.GUARDIAN_TWO_CHAR_P]) up_lst = UP_CHAR flag = _CURRENT_CHAR_POOL_TITLE _max_star = 3 all_data = ALL_CHAR else: if mode == 1: - star = get_star([5, 4, 3, 2], [GUARDIAN_FIVE_ARMS_P, GUARDIAN_FOUR_ARMS_P, - GUARDIAN_THREE_ARMS_P, GUARDIAN_TWO_ARMS_P]) + star = get_star([5, 4, 3, 2], [guardian_config.GUARDIAN_FIVE_ARMS_P, guardian_config.GUARDIAN_FOUR_ARMS_P, + guardian_config.GUARDIAN_THREE_ARMS_P, guardian_config.GUARDIAN_TWO_ARMS_P]) else: - star = get_star([5, 4], [GUARDIAN_FIVE_ARMS_P, GUARDIAN_FOUR_ARMS_P]) + star = get_star([5, 4], [guardian_config.GUARDIAN_FIVE_ARMS_P, guardian_config.GUARDIAN_FOUR_ARMS_P]) up_lst = UP_ARMS flag = _CURRENT_ARMS_POOL_TITLE _max_star = 5 diff --git a/draw_card/init_card_pool.py b/draw_card/init_card_pool.py index 5118d7d..26899b7 100644 --- a/draw_card/init_card_pool.py +++ b/draw_card/init_card_pool.py @@ -2,7 +2,7 @@ from .config import DRAW_PATH from .util import is_number from pathlib import Path -from nonebot import logger +from nonebot.log import logger try: import ujson as json except ModuleNotFoundError: diff --git a/draw_card/onmyoji_handle.py b/draw_card/onmyoji_handle.py index fcd7ba3..685e36e 100644 --- a/draw_card/onmyoji_handle.py +++ b/draw_card/onmyoji_handle.py @@ -1,9 +1,8 @@ - from nonebot.adapters.onebot.v11 import MessageSegment import random from .update_game_requests_info import update_requests_info from .util import generate_img, init_star_rst, BaseData, set_list, get_star, max_card -from .config import ONMYOJI_SR, ONMYOJI_SSR, ONMYOJI_SP, ONMYOJI_R, DRAW_PATH, ONMYOJI_FLAG +from .config import DRAW_PATH, draw_config from dataclasses import dataclass from .init_card_pool import init_game_pool try: @@ -39,8 +38,8 @@ async def update_onmyoji_info(): async def init_onmyoji_data(): global ALL_CHAR - if ONMYOJI_FLAG: - with open(DRAW_PATH + 'onmyoji.json', 'r', encoding='utf8') as f: + if draw_config.ONMYOJI_FLAG: + with (DRAW_PATH / 'onmyoji.json').open('r', encoding='utf8') as f: azur_dict = json.load(f) ALL_CHAR = init_game_pool('onmyoji', azur_dict, OnmyojiChar) @@ -56,7 +55,8 @@ async def init_onmyoji_data(): # 抽取卡池 def _get_onmyoji_card(): global ALL_CHAR - star = get_star([5, 4, 3, 2], [ONMYOJI_SP, ONMYOJI_SSR, ONMYOJI_SR, ONMYOJI_R]) + onmyoji_config = draw_config.onmyoji + star = get_star([5, 4, 3, 2], [onmyoji_config.ONMYOJI_SP, onmyoji_config.ONMYOJI_SSR, onmyoji_config.ONMYOJI_SR, onmyoji_config.ONMYOJI_R]) chars = [x for x in ALL_CHAR if x.star == onmyoji_star[star] and not x.limited] return random.choice(chars), 5 - star @@ -79,4 +79,3 @@ def format_card_information(count: int): obj_dict[obj.name] = 1 obj_list.append(obj) return obj_list, obj_dict, star_list, rst - diff --git a/draw_card/pcr_handle.py b/draw_card/pcr_handle.py index 21c5ec4..d17b56e 100644 --- a/draw_card/pcr_handle.py +++ b/draw_card/pcr_handle.py @@ -1,15 +1,16 @@ -import ujson as json from nonebot.adapters.onebot.v11 import MessageSegment -import nonebot import random from .update_game_info import update_info from .update_game_simple_info import update_simple_info from .util import generate_img, init_star_rst, BaseData, set_list, get_star, max_card -from .config import PCR_TWO_P, PCR_THREE_P, PCR_ONE_P, DRAW_PATH, PCR_FLAG, PCR_G_TWO_P, PCR_G_THREE_P, PCR_TAI +from .config import DRAW_PATH, draw_config from dataclasses import dataclass from .init_card_pool import init_game_pool -driver: nonebot.Driver = nonebot.get_driver() +try: + import ujson as json +except ModuleNotFoundError: + import json ALL_CHAR = [] @@ -32,7 +33,7 @@ async def pcr_draw(count: int): async def update_pcr_info(): global ALL_CHAR - if PCR_TAI: + if draw_config.PCR_TAI: url = 'https://wiki.biligame.com/pcr/角色图鉴' data, code = await update_simple_info(url, 'pcr') else: @@ -44,8 +45,8 @@ async def update_pcr_info(): async def init_pcr_data(): global ALL_CHAR - if PCR_FLAG: - with open(DRAW_PATH + 'pcr.json', 'r', encoding='utf8') as f: + if draw_config.PCR_FLAG: + with (DRAW_PATH / 'pcr.json').open('r', encoding='utf8') as f: pcr_dict = json.load(f) ALL_CHAR = init_game_pool('pcr', pcr_dict, PcrChar) @@ -53,10 +54,11 @@ async def init_pcr_data(): # 抽取卡池 def _get_pcr_card(mode: int = 1): global ALL_CHAR + pcr_config = draw_config.pcr if mode == 2: - star = get_star([3, 2], [PCR_G_THREE_P, PCR_G_TWO_P]) + star = get_star([3, 2], [pcr_config.PCR_G_THREE_P, pcr_config.PCR_G_TWO_P]) else: - star = get_star([3, 2, 1], [PCR_THREE_P, PCR_TWO_P, PCR_ONE_P]) + star = get_star([3, 2, 1], [pcr_config.PCR_THREE_P, pcr_config.PCR_TWO_P, pcr_config.PCR_ONE_P]) chars = [x for x in ALL_CHAR if x.star == star and not x.limited] return random.choice(chars), 3 - star diff --git a/draw_card/pretty_handle.py b/draw_card/pretty_handle.py index 5cc1621..d1ae5b1 100644 --- a/draw_card/pretty_handle.py +++ b/draw_card/pretty_handle.py @@ -1,12 +1,10 @@ - -import nonebot -from nonebot.adapters.onebot.v11 import MessageSegment, Message +from nonebot.adapters.onebot.v11 import MessageSegment from .announcement import PrettyAnnouncement from .update_game_info import update_info from .util import init_star_rst, generate_img, max_card, BaseData, \ set_list, get_star, format_card_information, init_up_char import random -from .config import PRETTY_THREE_P, PRETTY_TWO_P, DRAW_PATH, PRETTY_ONE_P, PRETTY_FLAG +from .config import DRAW_PATH, draw_config from dataclasses import dataclass from .init_card_pool import init_game_pool @@ -15,7 +13,6 @@ except ModuleNotFoundError: import json -driver: nonebot.Driver = nonebot.get_driver() announcement = PrettyAnnouncement() @@ -96,10 +93,10 @@ async def update_pretty_info(): async def init_pretty_data(): global ALL_CHAR, ALL_CARD - if PRETTY_FLAG: - with open(DRAW_PATH + 'pretty.json', 'r', encoding='utf8') as f: + if draw_config.PRETTY_FLAG: + with (DRAW_PATH / 'pretty.json').open('r', encoding='utf8') as f: pretty_char_dict = json.load(f) - with open(DRAW_PATH + 'pretty_card.json', 'r', encoding='utf8') as f: + with (DRAW_PATH / 'pretty_card.json').open('r', encoding='utf8') as f: pretty_card_dict = json.load(f) ALL_CHAR = init_game_pool('pretty', pretty_char_dict, PrettyChar) ALL_CARD = init_game_pool('pretty_card', pretty_card_dict, PrettyChar) @@ -109,10 +106,11 @@ async def init_pretty_data(): # 抽取卡池 def _get_pretty_card(pool_name: str, mode: int = 1): global ALL_CHAR, ALL_CARD, _CURRENT_CHAR_POOL_TITLE, _CURRENT_CARD_POOL_TITLE + pretty_config = draw_config.pretty if mode == 1: - star = get_star([3, 2, 1], [PRETTY_THREE_P, PRETTY_TWO_P, PRETTY_ONE_P]) + star = get_star([3, 2, 1], [pretty_config.PRETTY_THREE_P, pretty_config.PRETTY_TWO_P, pretty_config.PRETTY_ONE_P]) else: - star = get_star([3, 2], [PRETTY_THREE_P, PRETTY_TWO_P]) + star = get_star([3, 2], [pretty_config.PRETTY_THREE_P, pretty_config.PRETTY_TWO_P]) if pool_name == 'card': title = _CURRENT_CARD_POOL_TITLE up_data = UP_CARD diff --git a/draw_card/prts_handle.py b/draw_card/prts_handle.py index 3b2c520..186ade1 100644 --- a/draw_card/prts_handle.py +++ b/draw_card/prts_handle.py @@ -1,8 +1,6 @@ - from nonebot.adapters.onebot.v11 import MessageSegment, Message -import nonebot import random -from .config import PRTS_FIVE_P, PRTS_FOUR_P, PRTS_SIX_P, PRTS_THREE_P, DRAW_PATH, PRTS_FLAG +from .config import DRAW_PATH, draw_config from .update_game_info import update_info from .util import generate_img, init_star_rst, max_card, BaseData, UpEvent, set_list, get_star from .init_card_pool import init_game_pool @@ -14,7 +12,6 @@ except ModuleNotFoundError: import json -driver: nonebot.Driver = nonebot.get_driver() announcement = PrtsAnnouncement() @@ -70,8 +67,8 @@ async def update_prts_info(): async def init_prts_data(): global prts_dict, ALL_OPERATOR - if PRTS_FLAG: - with open(DRAW_PATH + 'prts.json', 'r', encoding='utf8') as f: + if draw_config.PRTS_FLAG: + with (DRAW_PATH / 'prts.json').open('r', encoding='utf8') as f: prts_dict = json.load(f) ALL_OPERATOR = init_game_pool('prts', prts_dict, Operator) await _init_up_char() @@ -79,7 +76,8 @@ async def init_prts_data(): # 抽取干员 def _get_operator_card(add: float): - star = get_star([6, 5, 4, 3], [PRTS_SIX_P + add, PRTS_FIVE_P, PRTS_FOUR_P, PRTS_THREE_P]) + prts_config = draw_config.prts + star = get_star([6, 5, 4, 3], [prts_config.PRTS_SIX_P + add, prts_config.PRTS_FIVE_P, prts_config.PRTS_FOUR_P, prts_config.PRTS_THREE_P]) if _CURRENT_POOL_TITLE: zooms = [x.zoom for x in UP_OPERATOR if x.star == star] zoom = 0 diff --git a/draw_card/rule.py b/draw_card/rule.py index fc88fde..90ec8bb 100644 --- a/draw_card/rule.py +++ b/draw_card/rule.py @@ -2,42 +2,29 @@ from nonebot.adapters.onebot.v11 import Bot, MessageEvent from nonebot.typing import T_State from nonebot.params import State -from .config import GENSHIN_FLAG, PRTS_FLAG, PRETTY_FLAG, GUARDIAN_FLAG, PCR_FLAG, AZUR_FLAG, FGO_FLAG, ONMYOJI_FLAG +from .config import draw_config def is_switch(game_name: str) -> Rule: async def _is_switch(bot: Bot, event: MessageEvent, state: T_State=State()) -> bool: if game_name == 'prts': - return PRTS_FLAG + return draw_config.PRTS_FLAG if game_name == 'genshin': - return GENSHIN_FLAG + return draw_config.GENSHIN_FLAG if game_name == 'pretty': - return PRETTY_FLAG + return draw_config.PRETTY_FLAG if game_name == 'guardian': - return GUARDIAN_FLAG + return draw_config.GUARDIAN_FLAG if game_name == 'pcr': - return PCR_FLAG + return draw_config.PCR_FLAG if game_name == 'azur': - return AZUR_FLAG + return draw_config.AZUR_FLAG if game_name == 'fgo': - return FGO_FLAG + return draw_config.FGO_FLAG if game_name == 'onmyoji': - return ONMYOJI_FLAG + return draw_config.ONMYOJI_FLAG else: return False return Rule(_is_switch) - - - - - - - - - - - - - diff --git a/draw_card/update_game_info.py b/draw_card/update_game_info.py index 8fc547f..fee9ab9 100644 --- a/draw_card/update_game_info.py +++ b/draw_card/update_game_info.py @@ -1,5 +1,6 @@ #coding:utf-8 import aiohttp +from typing import Tuple from .config import DRAW_PATH from asyncio.exceptions import TimeoutError from bs4 import BeautifulSoup @@ -18,9 +19,10 @@ headers = {'User-Agent': '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"'} -async def update_info(url: str, game_name: str, info_list: list = None) -> 'dict, int': +async def update_info(url: str, game_name: str, info_list: list = None) -> Tuple[dict, int]: + info_path = DRAW_PATH / f"{game_name}.json" try: - with open(DRAW_PATH + f'{game_name}.json', 'r', encoding='utf8') as f: + with info_path.open('r', encoding='utf8') as f: data = json.load(f) except (ValueError, FileNotFoundError): data = {} @@ -62,7 +64,7 @@ async def update_info(url: str, game_name: str, info_list: list = None) -> 'dict except TimeoutError: logger.warning(f'更新 {game_name} 超时...') return {}, 999 - with open(DRAW_PATH + f'{game_name}.json', 'w', encoding='utf8') as wf: + with info_path.open('w', encoding='utf8') as wf: wf.write(json.dumps(data, ensure_ascii=False, indent=4)) return data, 200 diff --git a/draw_card/update_game_requests_info.py b/draw_card/update_game_requests_info.py index 60f70b1..800fa9a 100644 --- a/draw_card/update_game_requests_info.py +++ b/draw_card/update_game_requests_info.py @@ -1,5 +1,5 @@ import aiohttp -from .config import DRAW_PATH, SEMAPHORE +from .config import DRAW_PATH, draw_config from asyncio.exceptions import TimeoutError from .util import download_img from bs4 import BeautifulSoup @@ -16,8 +16,9 @@ async def update_requests_info(game_name: str): + info_path = DRAW_PATH / f"{game_name}.json" try: - with open(DRAW_PATH + f'{game_name}.json', 'r', encoding='utf8') as f: + with info_path.open('r', encoding='utf8') as f: data = json.load(f) except (ValueError, FileNotFoundError): data = {} @@ -52,7 +53,7 @@ async def update_requests_info(game_name: str): except TimeoutError: logger.warning(f'更新 {game_name} 超时...') return {}, 999 - with open(DRAW_PATH + f'{game_name}.json', 'w', encoding='utf8') as wf: + with info_path.open('w', encoding='utf8') as wf: json.dump(data, wf, ensure_ascii=False, indent=4) return data, 200 @@ -101,7 +102,7 @@ async def _last_check(data: dict, game_name: str, session: aiohttp.ClientSession if game_name == 'fgo': url = 'http://fgo.vgtime.com/servant/' tasks = [] - semaphore = asyncio.Semaphore(SEMAPHORE) + semaphore = asyncio.Semaphore(draw_config.SEMAPHORE) for key in data.keys(): tasks.append(asyncio.ensure_future( _async_update_fgo_extra_info(url, key, data[key]['id'], session, semaphore))) diff --git a/draw_card/update_game_simple_info.py b/draw_card/update_game_simple_info.py index 5ee6a31..a480959 100644 --- a/draw_card/update_game_simple_info.py +++ b/draw_card/update_game_simple_info.py @@ -1,5 +1,6 @@ import aiohttp -from .config import DRAW_PATH, SEMAPHORE +from typing import Tuple +from .config import DRAW_PATH, draw_config from asyncio.exceptions import TimeoutError from bs4 import BeautifulSoup from .util import download_img @@ -19,9 +20,10 @@ } -async def update_simple_info(url: str, game_name: str) -> "dict, int": +async def update_simple_info(url: str, game_name: str) -> Tuple[dict, int]: + info_path = DRAW_PATH / f"{game_name}.json" try: - with open(DRAW_PATH + f"{game_name}.json", "r", encoding="utf8") as f: + with info_path.open("r", encoding="utf8") as f: data = json.load(f) except (ValueError, FileNotFoundError): data = {} @@ -50,7 +52,7 @@ async def update_simple_info(url: str, game_name: str) -> "dict, int": except TimeoutError: logger.warning(f"更新 {game_name} 超时...") return {}, 999 - with open(DRAW_PATH + f"{game_name}.json", "w", encoding="utf8") as wf: + with info_path.open("w", encoding="utf8") as wf: wf.write(json.dumps(data, ensure_ascii=False, indent=4)) return data, 200 @@ -101,7 +103,7 @@ async def _last_check( await download_img(url, "azur", f"{idx}_star") idx += 1 tasks = [] - semaphore = asyncio.Semaphore(SEMAPHORE) + semaphore = asyncio.Semaphore(draw_config.SEMAPHORE) for key in data.keys(): tasks.append( asyncio.ensure_future( diff --git a/draw_card/util.py b/draw_card/util.py index 81f4001..b8455d9 100644 --- a/draw_card/util.py +++ b/draw_card/util.py @@ -3,9 +3,8 @@ import aiofiles from asyncio.exceptions import TimeoutError from aiohttp.client_exceptions import InvalidURL -from typing import List, Union, Set -from pathlib import Path -from .config import path_dict, DRAW_PATH +from typing import List, Tuple, Union, Set +from .config import draw_config, DRAW_PATH import nonebot import pypinyin from PIL import UnidentifiedImageError @@ -16,13 +15,9 @@ from dataclasses import dataclass import os import asyncio -try: - import ujson as json -except ModuleNotFoundError: - import json -driver: nonebot.Driver = nonebot.get_driver() +driver = nonebot.get_driver() loop = asyncio.get_event_loop() @@ -48,19 +43,20 @@ class UpEvent: async def download_img(url: str, path: str, name: str) -> bool: path = path.split('_')[0] codename = cn2py(name) - if not os.path.exists(DRAW_PATH + f'/draw_card/{path}/{codename}.png'): + img_path = DRAW_PATH / f'/draw_card/{path}/{codename}.png' + if not img_path.exists(): try: async with aiohttp.ClientSession(headers=headers) as session: async with session.get(url, timeout=7) as response: - async with aiofiles.open(DRAW_PATH + f'/draw_card/{path}/{codename}.png', 'wb') as f: + async with aiofiles.open(str(img_path), 'wb') as f: await f.write(await response.read()) - logger.info(f'下载 {path_dict[path]} 图片成功,名称:{name},url:{url}') + logger.info(f'下载 {draw_config.path_dict[path]} 图片成功,名称:{name},url:{url}') return True except TimeoutError: - logger.warning(f'下载 {path_dict[path]} 图片超时,名称:{name},url:{url}') + logger.warning(f'下载 {draw_config.path_dict[path]} 图片超时,名称:{name},url:{url}') return False except InvalidURL: - logger.warning(f'下载 {path_dict[path]} 链接错误,名称:{name},url:{url}') + logger.warning(f'下载 {draw_config.path_dict[path]} 链接错误,名称:{name},url:{url}') return False else: # logger.info(f'{path_dict[path]} 图片 {name} 已存在') @@ -69,10 +65,10 @@ async def download_img(url: str, path: str, name: str) -> bool: @driver.on_startup def _check_dir(): - for dir_name in path_dict.keys(): - _p = Path(DRAW_PATH + f'/draw_card/' + dir_name) - if not _p.exists(): - _p.mkdir(parents=True, exist_ok=True) + for dir_name in draw_config.path_dict.keys(): + dir_path = DRAW_PATH / f'draw_card/{dir_name}' + if not dir_path.exists(): + dir_path.mkdir(parents=True, exist_ok=True) async def generate_img(card_set: Union[Set[BaseData], List[BaseData]], game_name: str, star_list: list) -> str: @@ -89,11 +85,12 @@ async def generate_img(card_set: Union[Set[BaseData], List[BaseData]], game_name background_list.append('#9370D8') else: background_list.append('white') + img_path = DRAW_PATH / f'draw_card/{game_name}/{x.star}_star.png' if game_name == 'azur': - if os.path.exists(DRAW_PATH + f'/draw_card/{game_name}/{x.star}_star.png'): - background_list.append(DRAW_PATH + f'/draw_card/{game_name}/{x.star}_star.png') + if img_path.exists(): + background_list.append(str(img_path)) py_name = cn2py(x.name) - img_list.append(DRAW_PATH + f'/draw_card/{game_name}/{py_name}.png') + img_list.append(str(DRAW_PATH / f'draw_card/{game_name}/{py_name}.png')) img_len = len(img_list) w = 100 * 10 if img_len <= 10: @@ -321,7 +318,7 @@ def format_card_information(count: int, star_list: List[int], func, pool_name: s # 检测次数是否合法 -def check_num(num: str, max_num: int) -> 'str, bool': +def check_num(num: str, max_num: int) -> Tuple[str, bool]: if is_number(num): try: num = int(num) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..84afc6d --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,31 @@ +[tool.poetry] +name = "nonebot_plugin_gamedraw" +version = "0.2.4" +description = "nonebot2 实现自动更新的 原神/明日方舟/赛马娘/坎公骑冠剑/公主连结/碧蓝航线/FGO/阴阳师 抽卡插件" +authors = ["HibiKier <775757368@qq.com>"] +license = "MIT" +readme = "README.md" +homepage = "https://github.com/HibiKier/nonebot_plugin_gamedraw" +repository = "https://github.com/HibiKier/nonebot_plugin_gamedraw" + +[tool.poetry.dependencies] +python = "^3.7.3" +nonebot2 = "^2.0.0-beta.1" +nonebot-adapter-onebot = "^2.0.0-beta.1" +nonebot-plugin-apscheduler = "^0.1.2" +aiofiles = "^0.8.0" +aiohttp = "^3.7.4" +beautifulsoup4 = "^4.9.3" +Pillow = "^8.2.0" +pypinyin = "^0.42.0" +typing-extensions = ">=3.10.0,<5.0.0" +ujson = {version = "^4.0.2", optional = true} +lxml = "^4.6.3" + +[tool.poetry.dev-dependencies] +nb-cli = "^0.6.0" +black = "^22.1.0" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api"