Skip to content
Open
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
2 changes: 1 addition & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = postgresql+asyncpg://kts_user:kts_pass@db:5432/kts
sqlalchemy.url = postgresql+asyncpg://kts_user:kts_pass@postgr:5432/kts


[post_write_hooks]
Expand Down
2 changes: 1 addition & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import AsyncEngine
from app.store.models.model import ParticipantsModel, GameModel
from app.store.models.model import ParticipantsModel, GameModel, GameSession

from alembic import context

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Add GameModel
"""empty message

Revision ID: e0fb8c912758
Revision ID: 02d03f3e7912
Revises:
Create Date: 2023-03-04 19:24:14.714777
Create Date: 2023-03-13 18:46:54.457627

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'e0fb8c912758'
revision = '02d03f3e7912'
down_revision = None
branch_labels = None
depends_on = None
Expand All @@ -25,7 +25,7 @@ def upgrade() -> None:
sa.UniqueConstraint('chat_id')
)
op.create_table('game',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('chat_id', sa.BigInteger(), nullable=True),
sa.Column('users', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('state_photo', sa.Boolean(), nullable=True),
Expand All @@ -38,12 +38,14 @@ def upgrade() -> None:
sa.Column('voters', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('amount_users', sa.BigInteger(), nullable=True),
sa.Column('last_winner', sa.Text(), nullable=True),
sa.Column('kicked_users', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['chat_id'], ['game_session.chat_id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_table('participants',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('name', sa.Text(), nullable=False),
sa.Column('user_id', sa.BigInteger(), nullable=True),
sa.Column('wins', sa.BigInteger(), nullable=True),
sa.Column('chat_id', sa.BigInteger(), nullable=True),
sa.Column('owner_id', sa.BigInteger(), nullable=True),
Expand Down
64 changes: 64 additions & 0 deletions alembic/versions/4a29dffe74c1_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""empty message

Revision ID: 4a29dffe74c1
Revises: af9f3a08a2fe
Create Date: 2023-03-13 19:58:38.369716

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql


# revision identifiers, used by Alembic.
revision = '4a29dffe74c1'
down_revision = 'af9f3a08a2fe'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('game_session',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('chat_id', sa.BigInteger(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('chat_id')
)
op.create_table('game',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('chat_id', sa.BigInteger(), nullable=True),
sa.Column('users', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('state_photo', sa.Boolean(), nullable=True),
sa.Column('state_in_game', sa.Boolean(), nullable=True),
sa.Column('state_wait_votes', sa.Boolean(), nullable=True),
sa.Column('new_pair', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('first_votes', sa.BigInteger(), nullable=True),
sa.Column('second_votes', sa.BigInteger(), nullable=True),
sa.Column('state_send_photo', sa.Boolean(), nullable=True),
sa.Column('voters', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('amount_users', sa.BigInteger(), nullable=True),
sa.Column('last_winner', sa.Text(), nullable=True),
sa.Column('kicked_users', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['chat_id'], ['game_session.chat_id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_table('participants',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('name', sa.Text(), nullable=False),
sa.Column('user_id', sa.BigInteger(), nullable=True),
sa.Column('wins', sa.BigInteger(), nullable=True),
sa.Column('chat_id', sa.BigInteger(), nullable=True),
sa.Column('owner_id', sa.BigInteger(), nullable=True),
sa.Column('photo_id', sa.BigInteger(), nullable=True),
sa.Column('access_key', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['chat_id'], ['game_session.chat_id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('game', sa.Column('state_photo', sa.BOOLEAN(), autoincrement=False, nullable=True))
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
"""Add GameModel
"""empty message

Revision ID: eb47d650c4bf
Revises: e0fb8c912758
Create Date: 2023-03-07 21:26:04.620744
Revision ID: af9f3a08a2fe
Revises: 02d03f3e7912
Create Date: 2023-03-13 19:55:17.806925

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql



# revision identifiers, used by Alembic.
revision = 'eb47d650c4bf'
down_revision = 'e0fb8c912758'
revision = 'af9f3a08a2fe'
down_revision = '02d03f3e7912'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('game', sa.Column('kicked_users', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('game', 'kicked_users')
pass
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion app/store/bot/bot_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def run():
loop = asyncio.get_event_loop()
bot = Bot(3)
bot = Bot(100)
try:
print("Bot has been started")
loop.create_task(bot.start())
Expand Down
1 change: 0 additions & 1 deletion app/store/bot/keyboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def get_but(text, color):
"buttons": [
[
get_but("Регистрация!", "primary"),
get_but("Загрузить фотографии!", "primary"),
],
[
get_but("Начать игру!", "primary"),
Expand Down
127 changes: 31 additions & 96 deletions app/store/bot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,6 @@ async def handle_updates(self, update):
self.reader = list(map(int, self.reader))
if update.object.body in self.commands:
await self.commands[update.object.body](update, game)
if (
update.object.body
== f"{lexicon_for_messages['ID_GROUP']} Загрузить фотографии!"
or game["state_photo"]
):
if len(users) == 0:
self.out_queue.put_nowait(
(
"message",
update.object.chat_id,
lexicon_for_messages["NO_REG"],
)
)
else:
if not game["state_in_game"]:
if not game["state_photo"]:
await self.set_state_photo(update, True)
else:
await self.command_download_photo(update)
else:
self.out_queue.put_nowait(
(
"message",
update.object.chat_id,
lexicon_for_messages["DUR_GAME"],
)
)
if (
len(update.object.body.split()) == 2
and "Исключить" in update.object.body.split()
Expand Down Expand Up @@ -157,24 +130,25 @@ async def command_registery(self, update, game):
result = await app.store.vk_api.make_userlist(
update.object.chat_id, self.app
)
for k, v in result:
users_exists_select = select(
ParticipantsModel.__table__.c.chat_id,
ParticipantsModel.__table__.c.name,
).where(
ParticipantsModel.__table__.columns.chat_id
== update.object.chat_id,
ParticipantsModel.__table__.c.name == k,
)
result = await session.execute(users_exists_select)
if not ((update.object.chat_id, k) in result.fetchall()):
users_exists_select = select(
ParticipantsModel.__table__.c.chat_id,
ParticipantsModel.__table__.c.name,
).where(
ParticipantsModel.__table__.columns.chat_id
== update.object.chat_id,
)
response_db = await session.execute(users_exists_select)
response_db = response_db.fetchall()
for i in result:
if not ((update.object.chat_id, i[0][0]) in response_db):
new_user = ParticipantsModel(
name=k,
name=i[0][0],
wins=0,
user_id=i[0][1],
chat_id=update.object.chat_id,
owner_id=v,
photo_id=None,
access_key=None,
owner_id=i[1][2],
photo_id=i[1][0],
access_key=i[1][1],
)
session.add(new_user)
await session.commit()
Expand All @@ -197,6 +171,8 @@ async def command_registery(self, update, game):
async def command_next_round(self, update, game):
if game["state_in_game"]:
game["state_wait_votes"] = False
if game["voters"] is None:
game["voters"] = {}
game["voters"]["already_voted"] = []
await self.set_state_wait_votes(update, game["state_wait_votes"])
await self.set_voters(update, [], game)
Expand Down Expand Up @@ -332,17 +308,16 @@ async def get_game(self, update):
result = {
"chat_id": temp[0][1],
"users": temp[0][2],
"state_photo": temp[0][3],
"state_in_game": temp[0][4],
"state_wait_votes": temp[0][5],
"new_pair": temp[0][6],
"first_votes": temp[0][7],
"second_votes": temp[0][8],
"state_send_photo": temp[0][9],
"voters": temp[0][10],
"amount_users": temp[0][11],
"last_winner": temp[0][12],
"kicked_users": temp[0][13],
"state_in_game": temp[0][3],
"state_wait_votes": temp[0][4],
"new_pair": temp[0][5],
"first_votes": temp[0][6],
"second_votes": temp[0][7],
"state_send_photo": temp[0][8],
"voters": temp[0][9],
"amount_users": temp[0][10],
"last_winner": temp[0][11],
"kicked_users": temp[0][12],
}
return result

Expand All @@ -368,18 +343,6 @@ async def set_last_winner(self, update, value):
await session.execute(query_state_photo)
await session.commit()

async def set_state_photo(self, update, value):
await self.app.database.connect()
async with self.app.database.session.begin() as session:
query_state_photo = (
refresh(GameModel.__table__)
.where(
GameModel.__table__.c.chat_id == update.object.chat_id,
)
.values(state_photo=value)
)
await session.execute(query_state_photo)
await session.commit()

async def set_state_send_photo(self, update, value):
await self.app.database.connect()
Expand Down Expand Up @@ -549,7 +512,7 @@ async def new_win(self, update, game):
.values(
wins=ParticipantsModel.__table__.c.wins + 1,
)
)
) # New win
await session.execute(user_new_win)
await session.commit()

Expand All @@ -562,7 +525,7 @@ async def get_statistics(self, update):
).where(
ParticipantsModel.__table__.columns.chat_id
== update.object.chat_id,
ParticipantsModel.__table__.c.owner_id == update.object.id,
ParticipantsModel.__table__.c.user_id == update.object.id,
)
result = await session.execute(user_check_wins)
return result.fetchall()
Expand All @@ -586,7 +549,7 @@ async def command_kick(self, update):
user_check_wins = delete(ParticipantsModel.__table__).where(
ParticipantsModel.__table__.columns.chat_id
== update.object.chat_id,
ParticipantsModel.__table__.c.owner_id == update.object.id,
ParticipantsModel.__table__.c.user_id == update.object.id,
)
await session.execute(user_check_wins)
await session.commit()
Expand Down Expand Up @@ -616,34 +579,6 @@ def check_users(self, game):
return 1
return 0

async def command_download_photo(self, update):
if hasattr(update.object, "type") and update.object.type == "photo":
await self.app.database.connect()
async with self.app.database.session.begin() as session:
users_add_photos = (
refresh(ParticipantsModel.__table__)
.where(
ParticipantsModel.__table__.c.owner_id
== update.object.owner_id,
ParticipantsModel.__table__.c.chat_id
== update.object.chat_id,
)
.values(
photo_id=update.object.photo_id,
access_key=update.object.access_key,
)
)
await self.set_state_photo(update, False)
await session.execute(users_add_photos)
await session.commit()

self.out_queue.put_nowait(
(
"message",
update.object.chat_id,
lexicon_for_messages["SUCC_PHOTO"],
)
)

async def proccess_start_game(self, chat_id):
await self.app.database.connect()
Expand Down
2 changes: 1 addition & 1 deletion app/store/bot/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def _worker(self):
self.out_queue.task_done()

async def start(self):
asyncio.create_task(self._worker())
self._tasks.append(asyncio.create_task(self._worker()))

async def stop(self):
await self.out_queue.join()
Expand Down
2 changes: 1 addition & 1 deletion app/store/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, app: "Application"):
async def connect(self, *_: list, **__: dict) -> None:
self._db = db
self._engine = create_async_engine(
"postgresql+asyncpg://kts_user:kts_pass@db:5432/kts",
"postgresql+asyncpg://kts_user:kts_pass@postgr:5432/kts", #db_1
echo=True,
)
self.session = sessionmaker(
Expand Down
Loading