Skip to content

Commit

Permalink
Merge pull request #17 from noneplugin/feat/orm
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq authored Oct 20, 2023
2 parents 7f08457 + a09b75f commit e5da903
Show file tree
Hide file tree
Showing 8 changed files with 762 additions and 666 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*__pycache__/
dist/
.vscode/
.vscode/
.env
data.db
25 changes: 7 additions & 18 deletions nonebot_plugin_boardgame/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,19 @@
from nonebot.exception import ParserExit
from nonebot.matcher import Matcher
from nonebot.params import CommandArg, CommandStart, EventToMe, ShellCommandArgv
from nonebot.plugin import PluginMetadata
from nonebot.plugin import PluginMetadata, inherit_supported_adapters
from nonebot.rule import ArgumentParser, Rule

require("nonebot_plugin_saa")
require("nonebot_plugin_session")
require("nonebot_plugin_userinfo")
require("nonebot_plugin_datastore")
require("nonebot_plugin_orm")
require("nonebot_plugin_htmlrender")

from nonebot_plugin_saa import Image, MessageFactory
from nonebot_plugin_saa import __plugin_meta__ as saa_plugin_meta
from nonebot_plugin_session import SessionIdType, SessionLevel
from nonebot_plugin_session import __plugin_meta__ as session_plugin_meta
from nonebot_plugin_session import extract_session
from nonebot_plugin_userinfo import __plugin_meta__ as userinfo_plugin_meta
from nonebot_plugin_session import SessionIdType, SessionLevel, extract_session
from nonebot_plugin_userinfo import get_user_info

assert saa_plugin_meta.supported_adapters
assert session_plugin_meta.supported_adapters
assert userinfo_plugin_meta.supported_adapters
supported_adapters = (
saa_plugin_meta.supported_adapters
& session_plugin_meta.supported_adapters
& userinfo_plugin_meta.supported_adapters
)

from .game import Game, MoveResult, Player, Pos
from .go import Go
from .gomoku import Gomoku
Expand All @@ -50,12 +37,14 @@
),
type="application",
homepage="https://github.com/noneplugin/nonebot-plugin-boardgame",
supported_adapters=supported_adapters,
supported_adapters=inherit_supported_adapters(
"nonebot_plugin_saa", "nonebot_plugin_session", "nonebot_plugin_userinfo"
),
extra={
"unique_name": "boardgame",
"example": "@小Q 五子棋\n落子 G8\n结束下棋",
"author": "meetwq <[email protected]>",
"version": "0.3.1",
"version": "0.3.2",
},
)

Expand Down
26 changes: 16 additions & 10 deletions nonebot_plugin_boardgame/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from enum import Enum
from typing import List, Optional

from nonebot_plugin_datastore import create_session
from nonebot_plugin_htmlrender import html_to_pic
from nonebot_plugin_orm import get_session
from sqlalchemy import select

from .model import GameRecord
Expand Down Expand Up @@ -164,12 +164,13 @@ def pop(self):

async def save_record(self, session_id: str):
statement = select(GameRecord).where(GameRecord.game_id == self.id)
async with create_session() as session:
async with get_session() as session:
record: Optional[GameRecord] = await session.scalar(statement)
if not record:
record = GameRecord(
game_id=self.id, session_id=session_id, name=self.name
)

if self.player_black:
record.player_black_id = str(self.player_black.id)
record.player_black_name = self.player_black.name
Expand All @@ -181,6 +182,7 @@ async def save_record(self, session_id: str):
record.update_time = self.update_time
record.positions = " ".join((str(pos) for pos in self.positions))
record.is_game_over = self.is_game_over

session.add(record)
await session.commit()

Expand All @@ -191,16 +193,20 @@ def load_player(id: str, name: str) -> Optional[Player]:
return None
return Player(id, name)

statement = select(GameRecord).where(
GameRecord.session_id == session_id,
GameRecord.name == cls.name,
GameRecord.is_game_over == False,
statement = (
select(GameRecord)
.where(
GameRecord.session_id == session_id,
GameRecord.name == cls.name,
GameRecord.is_game_over == False,
)
.order_by(GameRecord.update_time.desc())
)
async with create_session() as session:
records = (await session.scalars(statement)).all()
if not records:
async with get_session() as session:
record = await session.scalar(statement)
if not record:
return None
record = sorted(records, key=lambda x: x.update_time)[-1]

game = cls()
game.id = record.game_id
game.player_black = load_player(
Expand Down
42 changes: 0 additions & 42 deletions nonebot_plugin_boardgame/migrations/b737d3c58568_init_db.py

This file was deleted.

51 changes: 51 additions & 0 deletions nonebot_plugin_boardgame/migrations/dc81a3212383_init_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""init_db
修订 ID: dc81a3212383
父修订:
创建时间: 2023-10-20 14:29:37.795528
"""
from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "dc81a3212383"
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = "nonebot_plugin_boardgame"
depends_on: str | Sequence[str] | None = None


def upgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"nonebot_plugin_boardgame_gamerecord",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("game_id", sa.String(length=128), nullable=False),
sa.Column("session_id", sa.String(length=128), nullable=False),
sa.Column("name", sa.String(length=32), nullable=False),
sa.Column("start_time", sa.DateTime(), nullable=False),
sa.Column("update_time", sa.DateTime(), nullable=False),
sa.Column("player_black_id", sa.String(length=64), nullable=False),
sa.Column("player_black_name", sa.Text(), nullable=False),
sa.Column("player_white_id", sa.String(length=64), nullable=False),
sa.Column("player_white_name", sa.Text(), nullable=False),
sa.Column("positions", sa.Text(), nullable=False),
sa.Column("is_game_over", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint(
"id", name=op.f("pk_nonebot_plugin_boardgame_gamerecord")
),
)
# ### end Alembic commands ###


def downgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("nonebot_plugin_boardgame_gamerecord")
# ### end Alembic commands ###
6 changes: 1 addition & 5 deletions nonebot_plugin_boardgame/model.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from datetime import datetime

from nonebot_plugin_datastore import get_plugin_data
from nonebot_plugin_orm import Model
from sqlalchemy import String, Text
from sqlalchemy.orm import Mapped, mapped_column

Model = get_plugin_data().Model


class GameRecord(Model):
__table_args__ = {"extend_existing": True}

id: Mapped[int] = mapped_column(primary_key=True)
game_id: Mapped[str] = mapped_column(String(128))
session_id: Mapped[str] = mapped_column(String(128))
Expand Down
Loading

0 comments on commit e5da903

Please sign in to comment.