From 88608cc65a75f12ea575f74225e992d152d8edb1 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 7 Feb 2024 01:55:08 +0400 Subject: [PATCH] hitler winner and spectator poll option --- pyproject.toml | 2 +- src/config.py | 2 ++ src/data_models/Record.py | 10 ++++++++-- src/handlers/save.py | 20 +++++++++----------- src/services/db_service.py | 27 ++++++++++++++++----------- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 085ec07..79c51d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ authors = ["alex"] readme = "README.md" [tool.poetry.dependencies] -python = "^3.11" +python = "^3.12" aiosqlite = "^0.19.0" python-dotenv = "^1.0.1" python-telegram-bot = "^20.7" diff --git a/src/config.py b/src/config.py index 803cad6..37df29e 100644 --- a/src/config.py +++ b/src/config.py @@ -24,9 +24,11 @@ MIN_FASCIST_VOTERS = 1 GAME_POLL_OUTCOMES: Tuple = ( + "👀 SPECTATOR | NOT A PLAYER 👀", "I'm Canceler Hitler", "I'm Dead Hitler", "I'm Hitler Loser", + "I'm Hitler Winner", "I'm Liberal Winner", "I'm Liberal Loser", "I'm Fascistic Winner", diff --git a/src/data_models/Record.py b/src/data_models/Record.py index 4829258..c010501 100644 --- a/src/data_models/Record.py +++ b/src/data_models/Record.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Literal +from typing import Literal, Optional from src import config from pydantic import BaseModel, field_validator @@ -13,12 +13,16 @@ class Record(BaseModel): @field_validator("role", mode="after") @classmethod - def shorten_role(cls, v: str) -> Literal["CH", "DH", "HL", "LW", "LL", "FW", "FL"]: + def shorten_role( + cls, v: str + ) -> Optional[Literal["CH", "DH", "HW", "HL", "LW", "LL", "FW", "FL"] | None]: match v: case "I'm Canceler Hitler": return "CH" case "I'm Dead Hitler": return "DH" + case "I'm Hitler Winner": + return "HW" case "I'm Liberal Winner": return "LW" case "I'm Hitler Loser": @@ -29,6 +33,8 @@ def shorten_role(cls, v: str) -> Literal["CH", "DH", "HL", "LW", "LL", "FW", "FL return "FW" case "I'm Fascistic Loser": return "FL" + case "👀 SPECTATOR | NOT A PLAYER 👀": + return None case _: raise ValueError( f"Invalid role '{v}' for Record. Role must be one of {config.GAME_POLL_OUTCOMES}" diff --git a/src/handlers/save.py b/src/handlers/save.py index c67db4d..1c0bd77 100644 --- a/src/handlers/save.py +++ b/src/handlers/save.py @@ -71,10 +71,6 @@ async def save(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await context.bot.stop_poll(update.effective_chat.id, msg_with_poll.id) poll_data = context.bot_data[msg_with_poll.poll.id] - await update.effective_message.reply_text( - "Poll stopped. Results: {}".format(poll_data["results"]) - ) - await asyncio.gather( *[ save_record( @@ -89,13 +85,15 @@ async def save(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: for player_id, result in poll_data["results"].items() ] ) - await save_game( - Game( - poll_id=poll_data["message_id"], - chat_id=poll_data["chat_id"], - creator_id=poll_data["creator_id"], - results=poll_data["results"].copy(), - ) + game = Game( + poll_id=poll_data["message_id"], + chat_id=poll_data["chat_id"], + creator_id=poll_data["creator_id"], + results=poll_data["results"].copy(), + ) + await save_game(game) + await update.effective_message.reply_text( + "The Game has been saved!. Results: {}".format(game.results) ) else: await update.effective_message.reply_text( diff --git a/src/services/db_service.py b/src/services/db_service.py index c98b58a..9e00b17 100644 --- a/src/services/db_service.py +++ b/src/services/db_service.py @@ -15,17 +15,22 @@ async def save_record(record: Record) -> None: - await execute( - """INSERT INTO records (creator_id, player_id, playroom_id, game_id, role) - VALUES (?, ?, ?, ?, ?)""", - ( - record.creator_id, - record.player_id, - record.playroom_id, - record.game_id, - record.role, - ), - ) + try: + await execute( + """INSERT INTO records (creator_id, player_id, playroom_id, game_id, role) + VALUES (?, ?, ?, ?, ?)""", + ( + record.creator_id, + record.player_id, + record.playroom_id, + record.game_id, + record.role, + ), + ) + except sqlite3.IntegrityError: + logging.info( + f"Something went wrong with game: {record.game_id} in playroom {record.playroom_id}" + ) async def save_playroom(playroom: Playroom) -> None: