Skip to content

Commit

Permalink
💥 调整 jsondata 模型结构 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
KomoriDev authored Jan 23, 2025
1 parent 30d375c commit 5805193
Show file tree
Hide file tree
Showing 28 changed files with 557 additions and 531 deletions.
1 change: 0 additions & 1 deletion marisa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require("nonebot_plugin_htmlrender")

from . import migrations
from .models import Buff as Buff
from .models import Sect as Sect
from .models import User as User
from .plugins import base as base
Expand Down
8 changes: 8 additions & 0 deletions marisa/configs/configs/level_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
class LevelUpConfig(BaseConfig):
"""突破"""

theme: str = Field(default="default", alias="境界名称沿用")
"""境界名称"""
base_exp: int = Field(default=100, alias="基础经验")
"""基础经验"""
max_level: int = Field(default=200, alias="最高等级")
"""最高等级"""
cardinality: float = Field(default=1.1, alias="经验系数")
"""经验系数"""
cd: int = Field(default=0, alias="冷却时间")
"""突破 CD (分钟)"""
punishment: list[int] = Field(default=[10, 35], alias="失败后扣除修为范围")
Expand Down
15 changes: 15 additions & 0 deletions marisa/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from nonebot.exception import NoneBotException


class MarisaException(NoneBotException):
"""异常基类"""


class ItemNotFoundError(MarisaException):
"""物品未找到异常"""

def __init__(self, item_id: int) -> None:
self.item_id = item_id

def __repr__(self) -> str:
return f"ItemNotFoundError(item_id={self.item_id})"
49 changes: 49 additions & 0 deletions marisa/migrations/6bf5eefd0cbd_first_revision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""first revision
迁移 ID: 6bf5eefd0cbd
父迁移: 0cdb9777e97d
创建时间: 2025-01-22 19:22:54.513452
"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "6bf5eefd0cbd"
down_revision: str | Sequence[str] | None = "0cdb9777e97d"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("backpack", schema=None) as batch_op:
batch_op.add_column(sa.Column("is_bundle", sa.Boolean(), nullable=False))
batch_op.add_column(sa.Column("is_equipped", sa.Boolean(), nullable=False))
batch_op.drop_column("item_amount")
batch_op.drop_column("item_type")
batch_op.drop_column("bundle_item_amount")

# ### end Alembic commands ###


def downgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("backpack", schema=None) as batch_op:
batch_op.add_column(
sa.Column("bundle_item_amount", sa.INTEGER(), nullable=False)
)
batch_op.add_column(sa.Column("item_type", sa.VARCHAR(), nullable=False))
batch_op.add_column(sa.Column("item_amount", sa.INTEGER(), nullable=False))
batch_op.drop_column("is_equipped")
batch_op.drop_column("is_bundle")

# ### end Alembic commands ###
168 changes: 0 additions & 168 deletions marisa/migrations/aab5a786b5de_first_revision.py

This file was deleted.

1 change: 0 additions & 1 deletion marisa/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .buff import Buff as Buff
from .sect import Sect as Sect
from .user import User as User
from .backpack import Backpack as Backpack
Expand Down
10 changes: 4 additions & 6 deletions marisa/models/backpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ class Backpack(Model):
"""物品 ID"""
item_name: Mapped[str]
"""物品名称"""
item_type: Mapped[str]
"""物品类型"""
item_amount: Mapped[int]
"""物品数量"""
bundle_item_amount: Mapped[int] = mapped_column(default=0)
"""绑物数量"""
is_bundle: Mapped[bool]
"""是否为绑物"""
is_equipped: Mapped[bool] = mapped_column(default=False)
"""是否装备"""
create_time: Mapped[DateTime] = mapped_column(DateTime, default=func.now())
"""创建时间"""
update_time: Mapped[DateTime] = mapped_column(
Expand Down
38 changes: 0 additions & 38 deletions marisa/models/buff.py

This file was deleted.

31 changes: 22 additions & 9 deletions marisa/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from typing_extensions import Self

from nonebot_plugin_orm import Model, get_session
from sqlalchemy.orm import Mapped, relationship, mapped_column
from sqlalchemy.orm import Mapped, relationship, selectinload, mapped_column
from sqlalchemy import String, Boolean, DateTime, ForeignKey, and_, func, select

if TYPE_CHECKING:
from . import Buff, Sect, Backpack
from . import Sect, Backpack


class UserStatusType(Enum):
Expand Down Expand Up @@ -40,8 +40,10 @@ class User(Model):
"""灵根"""
root_type: Mapped[str]
"""灵根类型"""
level: Mapped[str]
level: Mapped[int]
"""等级"""
exp: Mapped[int]
"""经验"""
stone: Mapped[int]
"""灵石"""
create_time: Mapped[DateTime] = mapped_column(DateTime, default=func.now())
Expand All @@ -51,7 +53,6 @@ class User(Model):
)
"""上次检查时间"""

buff: Mapped["Buff"] = relationship("Buff", back_populates=None, uselist=False)
sect: Mapped["UserSect"] = relationship(
"UserSect", back_populates=None, uselist=False
)
Expand All @@ -63,13 +64,10 @@ class User(Model):
@classmethod
async def create_user(cls, user: Self):
"""创建用户"""
from . import Buff

sect = UserSect(id=user.id, user_id=user.id)
status = UserStatus(id=user.id, user_id=user.id)
buff = Buff(id=user.id, user_id=user.id)

objects = [user, sect, status, buff]
objects = [user, sect, status]

session = get_session()
async with session.begin():
Expand All @@ -80,7 +78,22 @@ async def delete_user(cls, user: Self):
"""删除用户"""
session = get_session()
async with session.begin():
await session.delete(user)
stmt = (
select(cls).options(
selectinload(cls.sect),
selectinload(cls.backpack),
selectinload(cls.status),
)
).where(cls.id == user.id)
obj = (await session.execute(stmt)).scalar()
if obj:
if obj.sect:
await session.delete(obj.sect)
if obj.status:
await session.delete(obj.status)
for backpack_item in obj.backpack:
await session.delete(backpack_item)
await session.delete(obj)

@classmethod
async def is_user_exist(cls, id: int, user_name: str) -> bool:
Expand Down
Loading

0 comments on commit 5805193

Please sign in to comment.