This commit is contained in:
31
pomodoro_bot/timer_manager.py
Normal file
31
pomodoro_bot/timer_manager.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import asyncio
|
||||
from .user_manager import user_manager
|
||||
from .models import UserState
|
||||
|
||||
class TimerManager:
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.tasks = {}
|
||||
|
||||
async def start_timer(self, user_id, duration, chat_id, label):
|
||||
await self.stop_timer(user_id)
|
||||
|
||||
async def timer():
|
||||
await asyncio.sleep(duration)
|
||||
if label == 'Pomodoro':
|
||||
await user_manager.increment_pomodoros(user_id)
|
||||
await self.bot.send_message(chat_id, f"⏰ {label} завершён!")
|
||||
|
||||
task = asyncio.create_task(timer())
|
||||
user = user_manager.get_user(user_id)
|
||||
user.current_timer = label
|
||||
user.task = task
|
||||
self.tasks[user_id] = task
|
||||
|
||||
async def stop_timer(self, user_id):
|
||||
user = user_manager.get_user(user_id)
|
||||
if user.task:
|
||||
user.task.cancel()
|
||||
user.task = None
|
||||
user.current_timer = None
|
||||
self.tasks.pop(user_id, None)
|
||||
Reference in New Issue
Block a user