Skip to content

Commit

Permalink
还原订单存储的不兼容更改
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttkkl committed Nov 26, 2023
1 parent a579c72 commit a4bcacf
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 37 deletions.
17 changes: 13 additions & 4 deletions nonebot_plugin_nagabus/data/naga.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -55,6 +55,10 @@ class MajsoulOrderOrm(SqlModel):
)


class NagaReportWrapper(BaseModel):
report: NagaReport


class NagaRepository:
def __init__(self, sess: AsyncSession):
self.sess = sess
Expand Down Expand Up @@ -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
21 changes: 0 additions & 21 deletions nonebot_plugin_nagabus/data/utils/pydantic.py

This file was deleted.

4 changes: 3 additions & 1 deletion nonebot_plugin_nagabus/naga/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down
15 changes: 7 additions & 8 deletions nonebot_plugin_nagabus/naga/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from enum import IntEnum

from pydantic import BaseModel
from typing import NamedTuple


class NagaGameRule(IntEnum):
Expand All @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions nonebot_plugin_nagabus/naga/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ async def analyze_majsoul(
f"(kyoku: {kyoku}, honba: {honba})</y> "
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
Expand Down Expand Up @@ -448,7 +448,7 @@ async def analyze_tenhou(
f"Found a existing tenhou paipu <y>{haihu_id})</y> "
"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(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-nagabus"
version = "0.3.0"
version = "0.3.1"
description = ""
authors = ["ssttkkl <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit a4bcacf

Please sign in to comment.