From 55f3cc7effbbb7a1dae06ef4628d01ae63bcea9b Mon Sep 17 00:00:00 2001 From: Leonid-Murzinov Date: Sun, 20 Nov 2022 12:29:24 +0300 Subject: [PATCH] =?UTF-8?q?backend:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=20=D1=81=D0=BE=D1=81=D1=82=D0=BE=D1=8F=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=B5=D1=80=D1=81=D0=BE=D0=BD=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/migrations/versions/4ab34de53087_.py | 41 ++++++++++++++++++++ backend/scripts/init_data.json | 13 +++++++ backend/textsouls/admin.py | 8 ++++ backend/textsouls/main.py | 9 +++++ backend/textsouls/models.py | 24 ++++++++++-- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 backend/migrations/versions/4ab34de53087_.py diff --git a/backend/migrations/versions/4ab34de53087_.py b/backend/migrations/versions/4ab34de53087_.py new file mode 100644 index 0000000..8431322 --- /dev/null +++ b/backend/migrations/versions/4ab34de53087_.py @@ -0,0 +1,41 @@ +"""empty message + +Revision ID: 4ab34de53087 +Revises: 1da93403ba52 +Create Date: 2022-11-20 12:22:55.594622 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '4ab34de53087' +down_revision = '1da93403ba52' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('character_states', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + with op.batch_alter_table('characters', schema=None) as batch_op: + batch_op.add_column(sa.Column('state_id', sa.Integer(), nullable=False)) + batch_op.create_foreign_key(None, 'character_states', ['state_id'], ['id'], ondelete='CASCADE') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('characters', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.drop_column('state_id') + + op.drop_table('character_states') + # ### end Alembic commands ### diff --git a/backend/scripts/init_data.json b/backend/scripts/init_data.json index 3d05551..b7d3c4b 100644 --- a/backend/scripts/init_data.json +++ b/backend/scripts/init_data.json @@ -62,5 +62,18 @@ "wisdom_koef": 1.5 } ] + }, + { + "table_name": "character_states", + "records": [ + { + "id": 1, + "name": "Default" + }, + { + "id": 2, + "name": "Duel" + } + ] } ] \ No newline at end of file diff --git a/backend/textsouls/admin.py b/backend/textsouls/admin.py index 0384aa6..77c2f1a 100644 --- a/backend/textsouls/admin.py +++ b/backend/textsouls/admin.py @@ -8,8 +8,12 @@ from textsouls.models import User from textsouls.models import CharacterRace from textsouls.models import CharacterClass +from textsouls.models import CharacterState from textsouls.models import Character +from textsouls.models import DuelParticipants +from textsouls.models import Duel + ts_admin = Blueprint("ts_admin", __name__) @@ -30,4 +34,8 @@ admin.add_view(AdminView(User, db.session)) admin.add_view(CommonView(CharacterRace, db.session)) admin.add_view(CommonView(CharacterClass, db.session)) +admin.add_view(CommonView(CharacterState, db.session)) admin.add_view(CommonView(Character, db.session)) + +admin.add_view(CommonView(DuelParticipants, db.session)) +admin.add_view(CommonView(Duel, db.session)) diff --git a/backend/textsouls/main.py b/backend/textsouls/main.py index 104e8de..ac00e27 100644 --- a/backend/textsouls/main.py +++ b/backend/textsouls/main.py @@ -5,12 +5,21 @@ from textsouls.models import User from textsouls.models import CharacterRace from textsouls.models import CharacterClass +from textsouls.models import CharacterState from textsouls.models import Character +from textsouls.models import DuelParticipants +from textsouls.models import Duel + main = Blueprint("main", __name__) register_api(main, User, "users") register_api(main, CharacterRace, "character_races") register_api(main, CharacterClass, "character_classes") +register_api(main, CharacterState, "character_states") register_api(main, Character, "characters", "owner") + + +register_api(main, DuelParticipants, "duels_participants") +register_api(main, Duel, "duels") diff --git a/backend/textsouls/models.py b/backend/textsouls/models.py index 6ff2f51..ca5d817 100644 --- a/backend/textsouls/models.py +++ b/backend/textsouls/models.py @@ -66,6 +66,18 @@ class CharacterClass(db.Model, SerializerMixin): return self.name +class CharacterState(db.Model, SerializerMixin): + + __tablename__ = "character_states" + + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(255), nullable=False, unique=True) + characters = db.relationship("Character", backref="state", lazy="dynamic") + + def __str__(self): + return self.name + + class Character(db.Model, SerializerMixin): __tablename__ = "characters" @@ -96,6 +108,12 @@ class Character(db.Model, SerializerMixin): agility_base = db.Column(db.Integer(), nullable=False, unique=False) defence_base = db.Column(db.Integer(), nullable=False, unique=False) wisdom_base = db.Column(db.Integer(), nullable=False, unique=False) + state_id = db.Column( + db.Integer, + db.ForeignKey("character_states.id", ondelete="CASCADE"), + nullable=False, + default=1, + ) def __init__(self, **kwargs): super().__init__(**kwargs) @@ -165,6 +183,9 @@ class DuelParticipants(db.Model, SerializerMixin): db.ForeignKey("duels.id", ondelete="CASCADE"), ) + def __str__(self): + return self.duel_id, self.participant_id + class Duel(db.Model, SerializerMixin): @@ -177,6 +198,3 @@ class Duel(db.Model, SerializerMixin): participants = db.relationship( "DuelParticipants", backref="duel", lazy="dynamic" ) - - def __str__(self): - return self.name