Skip to content

Commit

Permalink
hitler winner and spectator poll option
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Kopylov committed Feb 6, 2024
1 parent 5a1b87b commit 88608cc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
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
27 changes: 16 additions & 11 deletions src/services/db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 88608cc

Please sign in to comment.