Skip to content

✨ 适配 Trending #539

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
62 changes: 58 additions & 4 deletions nonebot_plugin_tetris_stats/games/tetrio/query/v1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from asyncio import gather
from datetime import timedelta
from hashlib import md5
from typing import NamedTuple

from nonebot_plugin_orm import get_session
from sqlalchemy import func, select
from yarl import URL

from ....utils.chart import get_split, get_value_bounds, handle_history_data
Expand All @@ -15,11 +18,62 @@
from ....utils.render.schemas.v1.tetrio.user.info import Info, Multiplayer, Singleplayer, User
from ....utils.screenshot import screenshot
from ..api import Player
from ..api.schemas.summaries.league import RatedData
from ..api.models import TETRIOHistoricalData
from ..api.schemas.summaries.league import LeagueSuccessModel, NeverRatedData, RatedData
from ..constant import TR_MAX, TR_MIN
from .tools import flow_to_history, get_league_data


def compare_trending(old: float, new: float) -> Trending:
if old > new:
return Trending.DOWN
if old < new:
return Trending.UP
return Trending.KEEP

Check warning on line 32 in nonebot_plugin_tetris_stats/games/tetrio/query/v1.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/query/v1.py#L28-L32

Added lines #L28 - L32 were not covered by tests


class Trends(NamedTuple):
pps: Trending = Trending.KEEP
apm: Trending = Trending.KEEP
vs: Trending = Trending.KEEP


async def get_trending(player: Player) -> Trends:
league = await player.league
if not isinstance(league.data, RatedData | NeverRatedData):
return Trends()

Check warning on line 44 in nonebot_plugin_tetris_stats/games/tetrio/query/v1.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/query/v1.py#L42-L44

Added lines #L42 - L44 were not covered by tests

async with get_session() as session:

Check warning on line 46 in nonebot_plugin_tetris_stats/games/tetrio/query/v1.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/query/v1.py#L46

Added line #L46 was not covered by tests
# 查询约一天前的历史数据
historical = (

Check warning on line 48 in nonebot_plugin_tetris_stats/games/tetrio/query/v1.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/query/v1.py#L48

Added line #L48 was not covered by tests
await session.scalars(
select(TETRIOHistoricalData)
.where(
TETRIOHistoricalData.user_unique_identifier == (await player.user).unique_identifier,
TETRIOHistoricalData.api_type == 'league',
TETRIOHistoricalData.update_time > league.cache.cached_at - timedelta(days=1),
)
.order_by(
func.julianday(TETRIOHistoricalData.update_time)
- func.julianday(league.cache.cached_at - timedelta(days=1))
)
.limit(1)
)
).one_or_none()
if (

Check warning on line 63 in nonebot_plugin_tetris_stats/games/tetrio/query/v1.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/query/v1.py#L63

Added line #L63 was not covered by tests
historical is None
or not isinstance(historical.data, LeagueSuccessModel)
or not isinstance(historical.data.data, RatedData | NeverRatedData)
):
return Trends()

Check warning on line 68 in nonebot_plugin_tetris_stats/games/tetrio/query/v1.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/query/v1.py#L68

Added line #L68 was not covered by tests

return Trends(

Check warning on line 70 in nonebot_plugin_tetris_stats/games/tetrio/query/v1.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_tetris_stats/games/tetrio/query/v1.py#L70

Added line #L70 was not covered by tests
pps=compare_trending(historical.data.data.pps, league.data.pps),
apm=compare_trending(historical.data.data.apm, league.data.apm),
vs=compare_trending(historical.data.data.vs, league.data.vs),
)


async def make_query_image_v1(player: Player) -> bytes:
(
(user, user_info, league, sprint, blitz, leagueflow),
Expand Down Expand Up @@ -75,14 +129,14 @@
),
lpm=(metrics := get_metrics(pps=league_data.pps, apm=league_data.apm, vs=league_data.vs)).lpm,
pps=metrics.pps,
lpm_trending=Trending.KEEP,
lpm_trending=(trends := (await get_trending(player))).pps,
apm=metrics.apm,
apl=metrics.apl,
apm_trending=Trending.KEEP,
apm_trending=trends.apm,
adpm=metrics.adpm,
vs=metrics.vs,
adpl=metrics.adpl,
adpm_trending=Trending.KEEP,
adpm_trending=trends.vs,
app=(app := (league_data.apm / (60 * league_data.pps))),
dsps=(dsps := ((league_data.vs / 100) - (league_data.apm / 60))),
dspp=(dspp := (dsps / league_data.pps)),
Expand Down