love-bot/bot/utils/game_logic.py

38 lines
1.1 KiB
Python

from typing import Optional, Literal
from bot.database.models import GameChoice, User
WIN_CONDITIONS: dict[GameChoice, GameChoice] = {
"rock": "scissors",
"scissors": "paper",
"paper": "rock",
}
CHOICE_NAMES_RU: dict[GameChoice, str] = {
"rock": "Камень 🗿",
"scissors": "Ножницы ✂️",
"paper": "Бумага 📄",
}
def determine_winner(choice1: Optional[GameChoice], choice2: Optional[GameChoice]) -> Optional[Literal[1, 2]]:
"""
Определяет победителя игры Камень-Ножницы-Бумага.
Args:
choice1: Выбор первого игрока.
choice2: Выбор второго игрока.
Returns:
1, если победил игрок 1.
2, если победил игрок 2.
None, если ничья или кто-то не сделал выбор.
"""
if not choice1 or not choice2:
return None
if choice1 == choice2:
return None # Ничья
if WIN_CONDITIONS.get(choice1) == choice2:
return 1
else:
return 2