适配 v2 模板

This commit is contained in:
2024-06-07 21:18:12 +08:00
parent c2dd9c5d86
commit 21bee29146
7 changed files with 100 additions and 10 deletions

View File

@@ -41,7 +41,7 @@ async def _(bot: Bot, event: Event, account: Player, event_session: EventSession
if bind_status in (BindStatus.SUCCESS, BindStatus.UPDATE):
async with HostPage(
await render(
'binding',
'v1/binding',
Bind(
platform='TETR.IO',
status='unknown',

View File

@@ -229,7 +229,7 @@ async def make_query_image(
blitz_value = f'{blitz.endcontext.score:,}' if blitz is not None else 'N/A'
async with HostPage(
await render(
'tetrio/info',
'v1/tetrio/info',
Info(
user=TemplateUser(
avatar=f'https://tetr.io/user-content/avatars/{user_info.data.user.id}.jpg?rv={user_info.data.user.avatar_revision}'

View File

@@ -45,7 +45,7 @@ async def _(
if bind_status in (BindStatus.SUCCESS, BindStatus.UPDATE):
async with HostPage(
await render(
'binding',
'v1/binding',
Bind(
platform=GAME_TYPE,
status='unknown',

View File

@@ -46,7 +46,7 @@ async def _(
if bind_status in (BindStatus.SUCCESS, BindStatus.UPDATE):
async with HostPage(
await render(
'binding',
'v1/binding',
Bind(
platform=GAME_TYPE,
status='unknown',

View File

@@ -204,7 +204,7 @@ async def make_query_image(user_info: UserInfoSuccess, game_data: GameData, even
sprint_value = f'{duration:.3f}s' if duration < 60 else f'{duration // 60:.0f}m {duration % 60:.3f}s' # noqa: PLR2004
async with HostPage(
await render(
'tos/info',
'v1/tos/info',
Info(
user=People(avatar=await get_avatar(event_user_info, 'Data URI', None), name=user_info.data.name),
ranking=Ranking(rating=float(user_info.data.ranking), rd=round(float(user_info.data.rd_now), 2)),

View File

@@ -6,6 +6,7 @@ from nonebot.compat import PYDANTIC_V2
from ..templates import templates_dir
from .schemas.bind import Bind
from .schemas.tetrio_info import Info as TETRIOInfo
from .schemas.tetrio_info_v2 import Info as TETRIOInfoV2
from .schemas.top_info import Info as TOPInfo
from .schemas.tos_info import Info as TOSInfo
@@ -15,23 +16,28 @@ env = Environment(
@overload
async def render(render_type: Literal['binding'], data: Bind) -> str: ...
async def render(render_type: Literal['v1/binding'], data: Bind) -> str: ...
@overload
async def render(render_type: Literal['tetrio/info'], data: TETRIOInfo) -> str: ...
async def render(render_type: Literal['v1/tetrio/info'], data: TETRIOInfo) -> str: ...
@overload
async def render(render_type: Literal['top/info'], data: TOPInfo) -> str: ...
async def render(render_type: Literal['v2/tetrio/info'], data: TETRIOInfoV2) -> str: ...
@overload
async def render(render_type: Literal['tos/info'], data: TOSInfo) -> str: ...
async def render(render_type: Literal['v1/top/info'], data: TOPInfo) -> str: ...
@overload
async def render(render_type: Literal['v1/tos/info'], data: TOSInfo) -> str: ...
async def render(
render_type: Literal['binding', 'tetrio/info', 'top/info', 'tos/info'], data: Bind | TETRIOInfo | TOPInfo | TOSInfo
render_type: Literal['v1/binding', 'v1/tetrio/info', 'v2/tetrio/info', 'v1/top/info', 'v1/tos/info'],
data: Bind | TETRIOInfo | TETRIOInfoV2 | TOPInfo | TOSInfo,
) -> str:
if PYDANTIC_V2:
return await env.get_template('index.html').render_async(

View File

@@ -0,0 +1,84 @@
from datetime import datetime
from pydantic import BaseModel
from ....games.tetrio.api.typing import Rank
from ...typing import Number
from .base import Avatar
class User(BaseModel):
id: str
name: str
country: str | None
avatar: str | Avatar
banner: str | None
bio: str | None
friend_count: int
supporter_tier: int
verified: bool
bad_standing: bool
badges: list[str]
xp: Number
playtime: str
join_at: datetime | None
class Statistic(BaseModel):
total: int
wins: int
class TetraLeague(BaseModel):
rank: Rank
highest_rank: Rank
tr: Number
glicko: Number
rd: Number
global_rank: int
country_rank: int
pps: Number
apm: Number
adpm: Number
vs: Number
adpl: Number
statistic: Statistic
class Sprint(BaseModel):
time: str
global_rank: int | None
play_at: datetime
class Blitz(BaseModel):
score: int
global_rank: int | None
play_at: datetime
class Zen(BaseModel):
score: int
level: int
class Info(BaseModel):
user: User
tetra_league: TetraLeague | None
statistic: Statistic | None
sprint: Sprint | None
blitz: Blitz | None
zen: Zen