Skip to content
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

还原订单存储的不兼容更改 #8

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
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
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