Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用pydantic解析配置 #31

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions draw_card/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -309,7 +309,7 @@ async def _():
minute=1,
)
async def _():
if PRTS_FLAG:
if draw_config.PRTS_FLAG:
await reload_prts_pool()


Expand All @@ -320,7 +320,7 @@ async def _():
minute=1,
)
async def _():
if PRETTY_FLAG:
if draw_config.PRETTY_FLAG:
await reload_pretty_pool()


Expand All @@ -331,7 +331,7 @@ async def _():
minute=1,
)
async def _():
if PRTS_FLAG:
if draw_config.PRTS_FLAG:
await reload_genshin_pool()


Expand All @@ -342,8 +342,5 @@ async def _():
minute=1,
)
async def _():
if GUARDIAN_FLAG:
if draw_config.GUARDIAN_FLAG:
await reload_guardian_pool()



9 changes: 4 additions & 5 deletions draw_card/announcement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down
32 changes: 13 additions & 19 deletions draw_card/async_update_game_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,48 @@
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
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:
Expand All @@ -62,8 +61,3 @@ async def async_update_game():
except asyncio.exceptions.CancelledError:
logger.warning('更新异常:CancelledError,再次更新...')
await async_update_game()





11 changes: 5 additions & 6 deletions draw_card/azur_handle.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -42,22 +41,22 @@ 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)


# 抽取卡池
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

Loading