Files
new_lottery_bot/conduct_draw.py
2025-11-12 20:57:36 +09:00

102 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""
Скрипт для проведения розыгрыша
"""
import asyncio
import json
from database import async_session_maker
from services import LotteryService, ParticipationService
from models import Lottery, User
async def conduct_lottery_draw(lottery_id: int):
"""Проводим розыгрыш для указанного ID"""
print(f"🎲 Проведение розыгрыша #{lottery_id}")
async with async_session_maker() as session:
# Получаем розыгрыш
lottery = await LotteryService.get_lottery(session, lottery_id)
if not lottery:
print(f"❌ Розыгрыш {lottery_id} не найден")
return
print(f"📋 Розыгрыш: {lottery.title}")
# Призы уже загружены как список
prizes = lottery.prizes if isinstance(lottery.prizes, list) else json.loads(lottery.prizes)
print(f"🏆 Призы: {len(prizes)} шт.")
# Получаем участников
participants = await ParticipationService.get_participants(session, lottery_id)
print(f"👥 Участников: {len(participants)}")
if len(participants) == 0:
print("⚠️ Нет участников для розыгрыша")
return
# Выводим список участников
print("\n👥 Список участников:")
for i, user in enumerate(participants, 1):
accounts = user.account_number.split(',') if user.account_number else ['Нет счетов']
print(f" {i}. {user.first_name} (@{user.username}) - {len(accounts)} счет(ов)")
# Проводим розыгрыш
print(f"\n🎲 Проводим розыгрыш...")
# Используем метод из LotteryService
try:
winners = await LotteryService.conduct_draw(session, lottery_id)
if winners:
print(f"\n🎉 Победители определены:")
for i, winner_data in enumerate(winners, 1):
user = winner_data['user']
prize = winner_data['prize']
print(f" 🏆 {i} место: {user.first_name} (@{user.username})")
print(f" 💎 Приз: {prize}")
# Обновляем статус розыгрыша
await LotteryService.set_lottery_completed(session, lottery_id, True)
print(f"\n✅ Розыгрыш завершен и помечен как завершенный")
else:
print("❌ Ошибка при проведении розыгрыша")
except Exception as e:
print(f"💥 Ошибка: {e}")
async def main():
"""Главная функция"""
print("🎯 Скрипт проведения розыгрышей")
print("=" * 50)
# Показываем доступные розыгрыши
async with async_session_maker() as session:
lotteries = await LotteryService.get_active_lotteries(session)
if not lotteries:
print("❌ Нет активных розыгрышей")
return
print("🎲 Активные розыгрыши:")
for lottery in lotteries:
participants = await ParticipationService.get_participants(session, lottery.id)
print(f" {lottery.id}. {lottery.title} ({len(participants)} участников)")
# Просим выбрать розыгрыш
print("\nВведите ID розыгрыша для проведения (или 'all' для всех):")
choice = input("> ").strip().lower()
if choice == 'all':
# Проводим все розыгрыши
for lottery in lotteries:
await conduct_lottery_draw(lottery.id)
print("-" * 30)
else:
try:
lottery_id = int(choice)
await conduct_lottery_draw(lottery_id)
except ValueError:
print("❌ Неверный ID розыгрыша")
if __name__ == "__main__":
asyncio.run(main())