mirror of
https://github.com/A-Minos/nonebot-plugin-tetris-stats.git
synced 2026-03-05 05:36:54 +08:00
🐛 修复 leagueflow 没有有效数据爆炸 (#508)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from datetime import datetime
|
||||
from enum import IntEnum
|
||||
from typing import NamedTuple
|
||||
from typing import Literal, NamedTuple
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -28,11 +28,16 @@ class Point(NamedTuple):
|
||||
|
||||
class Data(BaseModel):
|
||||
start_time: datetime = Field(..., alias='startTime')
|
||||
points: list[Point]
|
||||
points: list[Point] = Field(..., min_length=1)
|
||||
|
||||
|
||||
class Empty(BaseModel):
|
||||
start_time: Literal[9007199254740991] = Field(..., alias='startTime')
|
||||
points: list = Field(..., max_length=0)
|
||||
|
||||
|
||||
class LeagueFlowSuccess(BaseSuccessModel):
|
||||
data: Data
|
||||
data: Data | Empty
|
||||
|
||||
|
||||
LeagueFlow = LeagueFlowSuccess | FailedModel
|
||||
|
||||
@@ -5,7 +5,7 @@ from zoneinfo import ZoneInfo
|
||||
|
||||
from ....utils.exception import FallbackError
|
||||
from ....utils.render.schemas.tetrio.user.base import TetraLeagueHistoryData
|
||||
from ..api.schemas.labs.leagueflow import LeagueFlowSuccess
|
||||
from ..api.schemas.labs.leagueflow import Empty, LeagueFlowSuccess
|
||||
from ..api.schemas.summaries.league import LeagueSuccessModel, NeverPlayedData, NeverRatedData, RatedData
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ def flow_to_history(
|
||||
leagueflow: LeagueFlowSuccess,
|
||||
handle: Callable[[list[TetraLeagueHistoryData]], list[TetraLeagueHistoryData]] | None = None,
|
||||
) -> list[TetraLeagueHistoryData]:
|
||||
if isinstance(leagueflow.data, Empty):
|
||||
raise FallbackError
|
||||
start_time = leagueflow.data.start_time.astimezone(ZoneInfo('Asia/Shanghai'))
|
||||
ret = [
|
||||
TetraLeagueHistoryData(
|
||||
|
||||
@@ -4,6 +4,7 @@ from hashlib import md5
|
||||
|
||||
from yarl import URL
|
||||
|
||||
from ....utils.exception import FallbackError
|
||||
from ....utils.host import HostPage, get_self_netloc
|
||||
from ....utils.metrics import get_metrics
|
||||
from ....utils.render import render
|
||||
@@ -49,9 +50,12 @@ async def make_query_image_v2(player: Player) -> bytes:
|
||||
play_time = f'{game_time:.0f}s'
|
||||
else:
|
||||
play_time = game_time
|
||||
try:
|
||||
history = flow_to_history(leagueflow, lambda x: x[-100:])
|
||||
except FallbackError:
|
||||
history = None
|
||||
netloc = get_self_netloc()
|
||||
async with (
|
||||
HostPage(
|
||||
async with HostPage(
|
||||
await render(
|
||||
'v2/tetrio/user/info',
|
||||
Info(
|
||||
@@ -60,14 +64,12 @@ async def make_query_image_v2(player: Player) -> bytes:
|
||||
name=user.name.upper(),
|
||||
bio=user_info.data.bio,
|
||||
banner=str(
|
||||
URL(f'http://{netloc}/host/resource/tetrio/banners/{user.ID}')
|
||||
% {'revision': banner_revision}
|
||||
URL(f'http://{netloc}/host/resource/tetrio/banners/{user.ID}') % {'revision': banner_revision}
|
||||
)
|
||||
if banner_revision is not None and banner_revision != 0
|
||||
else None,
|
||||
avatar=str(
|
||||
URL(f'http://{netloc}/host/resource/tetrio/avatars/{user.ID}')
|
||||
% {'revision': avatar_revision}
|
||||
URL(f'http://{netloc}/host/resource/tetrio/avatars/{user.ID}') % {'revision': avatar_revision}
|
||||
)
|
||||
if avatar_revision is not None and avatar_revision != 0
|
||||
else Avatar(
|
||||
@@ -107,7 +109,7 @@ async def make_query_image_v2(player: Player) -> bytes:
|
||||
adpl=metrics.adpl,
|
||||
statistic=TetraLeagueStatistic(total=league.data.gamesplayed, wins=league.data.gameswon),
|
||||
decaying=league.data.decaying,
|
||||
history=flow_to_history(leagueflow, lambda x: x[-100:]),
|
||||
history=history,
|
||||
)
|
||||
if not isinstance(league.data, NeverPlayedData)
|
||||
else None,
|
||||
@@ -132,6 +134,5 @@ async def make_query_image_v2(player: Player) -> bytes:
|
||||
zen=Zen(level=zen.data.level, score=zen.data.score),
|
||||
),
|
||||
),
|
||||
) as page_hash
|
||||
):
|
||||
) as page_hash:
|
||||
return await screenshot(f'http://{netloc}/host/{page_hash}.html')
|
||||
|
||||
Reference in New Issue
Block a user