From a4bcacf49c7e305cccf0e3b885ff984775624d3d Mon Sep 17 00:00:00 2001 From: ssttkkl Date: Sun, 26 Nov 2023 23:52:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=98=E5=8E=9F=E8=AE=A2=E5=8D=95=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E7=9A=84=E4=B8=8D=E5=85=BC=E5=AE=B9=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_nagabus/data/naga.py | 17 +++++++++++---- nonebot_plugin_nagabus/data/utils/pydantic.py | 21 ------------------- nonebot_plugin_nagabus/naga/api.py | 4 +++- nonebot_plugin_nagabus/naga/model.py | 15 +++++++------ nonebot_plugin_nagabus/naga/service.py | 4 ++-- pyproject.toml | 2 +- 6 files changed, 26 insertions(+), 37 deletions(-) delete mode 100644 nonebot_plugin_nagabus/data/utils/pydantic.py diff --git a/nonebot_plugin_nagabus/data/naga.py b/nonebot_plugin_nagabus/data/naga.py index e649d7a..8fdf446 100644 --- a/nonebot_plugin_nagabus/data/naga.py +++ b/nonebot_plugin_nagabus/data/naga.py @@ -1,14 +1,14 @@ +import json from enum import IntEnum from typing import Optional from datetime import datetime, timezone from nonebot import logger +from pydantic import BaseModel from nonebot_plugin_orm import AsyncSession from sqlalchemy import ForeignKey, select, update from sqlalchemy.orm import Mapped, relationship, mapped_column -from nonebot_plugin_nagabus.data.utils.pydantic import PydanticModel - from .base import SqlModel from .utils import UTCDateTime from ..naga.model import NagaReport, NagaGameRule, NagaOrderStatus @@ -29,7 +29,7 @@ class NagaOrderOrm(SqlModel): source: Mapped[NagaOrderSource] model_type: Mapped[str] status: Mapped[NagaOrderStatus] - naga_report: Mapped[Optional[NagaReport]] = mapped_column(PydanticModel(NagaReport)) + naga_report: Mapped[Optional[str]] # json of NagaReport create_time: Mapped[datetime] = mapped_column(UTCDateTime, index=True) update_time: Mapped[datetime] = mapped_column(UTCDateTime) @@ -55,6 +55,10 @@ class MajsoulOrderOrm(SqlModel): ) +class NagaReportWrapper(BaseModel): + report: NagaReport + + class NagaRepository: def __init__(self, sess: AsyncSession): self.sess = sess @@ -186,9 +190,14 @@ async def update_local_order(self, haihu_id: str, report: NagaReport): .where(NagaOrderOrm.haihu_id == haihu_id) .values( status=NagaOrderStatus.ok, - naga_report=report, + naga_report=json.dumps(report), update_time=datetime.now(timezone.utc), ) ) await self.sess.execute(stmt) await self.sess.commit() + + @staticmethod + def parse_report(raw_report: str) -> NagaReport: + wrapper_dict = {"report": json.loads(raw_report)} + return NagaReportWrapper.parse_obj(wrapper_dict).report diff --git a/nonebot_plugin_nagabus/data/utils/pydantic.py b/nonebot_plugin_nagabus/data/utils/pydantic.py deleted file mode 100644 index aa5cea1..0000000 --- a/nonebot_plugin_nagabus/data/utils/pydantic.py +++ /dev/null @@ -1,21 +0,0 @@ -from pydantic import BaseModel -from sqlalchemy import JSON, TypeDecorator - - -class PydanticModel(TypeDecorator): - impl = JSON - cache_ok = True - - def __init__(self, t_model: type[BaseModel], *args, **kwargs): - super().__init__(*args, **kwargs) - self.t_model = t_model - - def process_bind_param(self, value, dialect): - if value is None: - return None - return value.dict() - - def process_result_value(self, value, dialect): - if value is None: - return None - return self.t_model.parse_obj(value) diff --git a/nonebot_plugin_nagabus/naga/api.py b/nonebot_plugin_nagabus/naga/api.py index fcf05d1..d4f5357 100644 --- a/nonebot_plugin_nagabus/naga/api.py +++ b/nonebot_plugin_nagabus/naga/api.py @@ -81,7 +81,9 @@ async def order_report_list(self, year: int, month: int) -> OrderReportList: headers={"Referer": "https://naga.dmv.nico/naga_report/order_report_list/"}, params={"year": year, "month": month}, ) - return OrderReportList.parse_obj(resp.json()) + resp_json = resp.json() + assert resp_json["status"] == 200 + return OrderReportList.parse_obj(resp_json) async def _get_csrfmiddlewaretoken(self) -> str: resp = await self.client.get("/order_form/") diff --git a/nonebot_plugin_nagabus/naga/model.py b/nonebot_plugin_nagabus/naga/model.py index 890eb0c..5bec9b9 100644 --- a/nonebot_plugin_nagabus/naga/model.py +++ b/nonebot_plugin_nagabus/naga/model.py @@ -1,6 +1,5 @@ from enum import IntEnum - -from pydantic import BaseModel +from typing import NamedTuple class NagaGameRule(IntEnum): @@ -21,19 +20,19 @@ class NagaHanchanModelType(IntEnum): kagashi = 4 -class NagaReportPlayer(BaseModel): +class NagaReportPlayer(NamedTuple): nickname: str pt: int -class NagaModel(BaseModel): +class NagaModel(NamedTuple): major: int minor: int old_type: int # 新版本恒为0,旧版本为NagaHanchanModelType/NagaTonpuuModelType的枚举值 type: str # NagaHanchanModelType/NagaTonpuuModelType的枚举值,逗号分隔 -class NagaReport(BaseModel): +class NagaReport(NamedTuple): haihu_id: str players: list[NagaReportPlayer] report_id: str @@ -50,18 +49,18 @@ class NagaOrderStatus(IntEnum): failed2 = 4 -class NagaOrder(BaseModel): +class NagaOrder(NamedTuple): haihu_id: str status: NagaOrderStatus model: NagaModel rule: NagaGameRule -class NagaServiceOrder(BaseModel): +class NagaServiceOrder(NamedTuple): report: NagaReport cost_np: int -class NagaServiceUserStatistic(BaseModel): +class NagaServiceUserStatistic(NamedTuple): customer_id: int cost_np: int diff --git a/nonebot_plugin_nagabus/naga/service.py b/nonebot_plugin_nagabus/naga/service.py index 3a4a136..68d7cd6 100644 --- a/nonebot_plugin_nagabus/naga/service.py +++ b/nonebot_plugin_nagabus/naga/service.py @@ -342,7 +342,7 @@ async def analyze_majsoul( f"(kyoku: {kyoku}, honba: {honba}) " f"analyze report: {local_order.haihu_id}" ) - report = local_order.naga_report + report = repo.parse_report(local_order.naga_report) return NagaServiceOrder(report=report, cost_np=0) haihu_id = local_order.haihu_id @@ -448,7 +448,7 @@ async def analyze_tenhou( f"Found a existing tenhou paipu {haihu_id}) " "analyze report" ) - report = local_order.naga_report + report = repo.parse_report(local_order.naga_report) return NagaServiceOrder(report=report, cost_np=0) logger.opt(colors=True).info( diff --git a/pyproject.toml b/pyproject.toml index 74be951..d3b8f23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "nonebot-plugin-nagabus" -version = "0.3.0" +version = "0.3.1" description = "" authors = ["ssttkkl "] readme = "README.md"