Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hitler winner and spectator poll options #7

Merged
merged 9 commits into from
Feb 6, 2024
Merged
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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions src/data_models/Record.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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":
Expand All @@ -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}"
Expand Down
20 changes: 9 additions & 11 deletions src/handlers/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
44 changes: 33 additions & 11 deletions src/services/db_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import logging
import sqlite3

from src.data_models.Playroom import Playroom
import logging
import sqlite3

from src.data_models.Game import Game
from src.data_models.Player import Player
Comment on lines +4 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate imports for logging, sqlite3, and data models Game, Player, and Playroom. Remove the redundant lines to clean up the code.

- import logging
- import sqlite3
- from src.data_models.Game import Game
- from src.data_models.Player import Player
- from src.data_models.Playroom import Playroom

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
from src.data_models.Playroom import Playroom
import logging
import sqlite3
from src.data_models.Game import Game
from src.data_models.Player import Player

from src.data_models.Game import Game
from src.data_models.Player import Player
from src.data_models.Playroom import Playroom
Expand All @@ -9,17 +15,33 @@


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}"
)
Comment on lines +18 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The save_record function correctly handles sqlite3.IntegrityError with a try-except block and logs an informative message. However, consider enhancing the log message to include more details about the error for better debugging.

-        logging.info(
+        logging.error(

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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}"
)
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.error(
f"Something went wrong with game: {record.game_id} in playroom {record.playroom_id}"
)



async def save_playroom(playroom: Playroom) -> None:
"""Add a game room to the bot_data"""
try:
await execute(
"INSERT INTO playrooms (id, name) VALUES (?, ?)",
(playroom.telegram_chat_id, playroom.name),
)
except sqlite3.IntegrityError:
logging.info(f"Playroom {playroom.name} already exists in the database")
Comment on lines +36 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The save_playroom function is duplicated. Remove the duplicate definition to maintain code cleanliness and avoid confusion.

- async def save_playroom(playroom: Playroom) -> None:
-     """Add a game room to the bot_data"""
-     try:
-         await execute(
-             "INSERT INTO playrooms (id, name) VALUES (?, ?)",
-             (playroom.telegram_chat_id, playroom.name),
-         )
-     except sqlite3.IntegrityError:
-         logging.info(f"Playroom {playroom.name} already exists in the database")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
async def save_playroom(playroom: Playroom) -> None:
"""Add a game room to the bot_data"""
try:
await execute(
"INSERT INTO playrooms (id, name) VALUES (?, ?)",
(playroom.telegram_chat_id, playroom.name),
)
except sqlite3.IntegrityError:
logging.info(f"Playroom {playroom.name} already exists in the database")



async def save_playroom(playroom: Playroom) -> None:
Expand Down