From aee56dbbe0298b29b498ffd707103dbeb6f82a30 Mon Sep 17 00:00:00 2001 From: olred Date: Tue, 28 Feb 2023 18:28:05 +0500 Subject: [PATCH 1/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=83=20"=D0=9C=D0=BE?= =?UTF-8?q?=D1=8F=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0!",=20=D0=B2=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B8=D1=82=D1=81=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB-=D0=B2=D0=BE=20=D0=BF=D0=BE=D0=B1=D0=B5?= =?UTF-8?q?=D0=B4=20=D1=87=D0=B5=D0=BB=D0=BE=D0=B2=D0=B5=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/store/bot/manager.py | 53 ++++++++++++++++++++++++++++++++++++++-- app/web/app.py | 2 -- app/web/mw.py | 8 ------ app/web/urls.py | 10 -------- 4 files changed, 51 insertions(+), 22 deletions(-) delete mode 100644 app/web/mw.py delete mode 100644 app/web/urls.py diff --git a/app/store/bot/manager.py b/app/store/bot/manager.py index c85b2b2..ef8b0c1 100644 --- a/app/store/bot/manager.py +++ b/app/store/bot/manager.py @@ -49,7 +49,7 @@ def __init__(self, app: "Application"): async def handle_updates(self, updates: list[Update]): for i in self.storage.keys(): - if time() - self.storage[i][0] > 30 and self.storage[i][1]: + if time() - self.storage[i][0] > 10 and self.storage[i][1]: updates.append( Update( type="time_out", @@ -120,18 +120,67 @@ async def handle_updates(self, updates: list[Update]): text="Данная команда недоступна во время игры!", ) ) + if update.object.body == "Моя статистика!": + if not this_chat.state_in_game: + await self.app.database.connect() + async with self.app.database.session.begin() as session: + user_check_wins = select( + ParticipantsModel.__table__.c.wins, + ParticipantsModel.__table__.c.name + ).where( + ParticipantsModel.__table__.columns.chat_id + == update.object.chat_id, + ParticipantsModel.__table__.c.owner_id == update.object.id, + ) + result = await session.execute(user_check_wins) + result = result.fetchall() + print(result) + await self.app.store.vk_api.send_message( + Message( + chat_id=update.object.chat_id, + text=f"Статистика игрока {result[-1][1]}:", + ) + ) + await self.app.store.vk_api.send_message( + Message( + chat_id=update.object.chat_id, + text=f"Кол-во побед: {result[-1][0]}", + ) + ) + else: + await self.app.store.vk_api.send_message( + Message( + chat_id=update.object.chat_id, + text=f"Данная команда недоступна во время игры!", + ) + ) if this_chat.state_send_photo: await self.command_send_photo(update, this_chat) if this_chat.state_wait_votes: await self.command_write_answers(update, this_chat) if this_chat.state_in_game and ( (not this_chat.state_wait_votes) - or time() - self.storage[update.object.chat_id][0] > 30 + or time() - self.storage[update.object.chat_id][0] > 10 ): await self.command_send_preresult(update, this_chat) if self.check_users(this_chat): if len(this_chat.users) == 1: this_chat.last_winner = this_chat.users[-1][0] + await self.app.database.connect() + async with self.app.database.session.begin() as session: + user_new_win = ( + refresh(ParticipantsModel.__table__) + .where( + ParticipantsModel.__table__.c.name + == this_chat.last_winner, + ParticipantsModel.__table__.c.chat_id == update.object.chat_id, + ) + .values( + wins = ParticipantsModel.__table__.c.wins + 1, + ) + ) + await session.execute(user_new_win) + await session.commit() await self.app.store.vk_api.send_message( Message( chat_id=update.object.chat_id, diff --git a/app/web/app.py b/app/web/app.py index e6f5fb0..2520f47 100644 --- a/app/web/app.py +++ b/app/web/app.py @@ -2,8 +2,6 @@ from aiohttp.web import ( Application as AiohttpApplication, - Request as AiohttpRequest, - View as AiohttpView, ) from app.store import Store, setup_store diff --git a/app/web/mw.py b/app/web/mw.py deleted file mode 100644 index e36ca6b..0000000 --- a/app/web/mw.py +++ /dev/null @@ -1,8 +0,0 @@ -from aiohttp import web -from aiohttp.abc import Request - - -@web.middleware -async def example_mw(request: Request, handler): - - return await handler(request) diff --git a/app/web/urls.py b/app/web/urls.py deleted file mode 100644 index b21877e..0000000 --- a/app/web/urls.py +++ /dev/null @@ -1,10 +0,0 @@ -from aiohttp.web_app import Application -from aiohttp_cors import CorsConfig - -__all__ = ("register_urls",) - - -def register_urls(application: Application, cors: CorsConfig): - import app.users.urls - - app.users.urls.register_urls(application, cors) From f0485fbb0ec4698fa709d364546b76a1f7a3d044 Mon Sep 17 00:00:00 2001 From: olred Date: Thu, 2 Mar 2023 00:10:20 +0500 Subject: [PATCH 2/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=D1=8B,=20=D0=B5=D1=89=D0=B5=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=B2=D1=81=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/kts_project_template.iml | 1 + .idea/modules.xml | 1 + .idea/vcs.xml | 1 + app/store/bot/manager.py | 29 +++-- pytest.ini | 4 +- requirements.txt | 1 + tests/__init__.py | 0 tests/bot/__init__.py | 0 tests/bot/test_bot.py | 197 +++++++++++++++++++++++++++++++++ tests/config.yaml | 4 - tests/conftest.py | 2 +- tests/fixtures.py | 2 - tests/fixtures/__init__.py | 1 + tests/fixtures/common.py | 75 +++++++++++++ tests/test_common.py | 13 --- 15 files changed, 299 insertions(+), 32 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/bot/__init__.py create mode 100644 tests/bot/test_bot.py delete mode 100644 tests/config.yaml delete mode 100644 tests/fixtures.py create mode 100644 tests/fixtures/__init__.py create mode 100644 tests/fixtures/common.py delete mode 100644 tests/test_common.py diff --git a/.idea/kts_project_template.iml b/.idea/kts_project_template.iml index 5195124..a23c19a 100644 --- a/.idea/kts_project_template.iml +++ b/.idea/kts_project_template.iml @@ -6,6 +6,7 @@ +