67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
import threading
|
|
import time
|
|
import datetime
|
|
from .models import DailyRewardsList, DailyReward, User
|
|
|
|
def calculate_reward(day):
|
|
if day == 1:
|
|
return 20
|
|
elif day == 2:
|
|
return 50
|
|
elif day == 3:
|
|
return 100
|
|
elif day == 4:
|
|
return 150
|
|
elif day == 5:
|
|
return 200
|
|
elif day == 6:
|
|
return 250
|
|
elif day == 7:
|
|
return 300
|
|
elif day == 8:
|
|
return 350
|
|
elif day == 9:
|
|
return 400
|
|
elif day == 10:
|
|
return 500
|
|
else:
|
|
return 500 + (day - 10) * 100
|
|
|
|
def my_scheduled_job():
|
|
# Удаляем старые записи списка наград
|
|
DailyRewardsList.objects.all().delete()
|
|
|
|
# Определяем максимальное количество дней в текущем месяце
|
|
now = datetime.datetime.now()
|
|
next_month = now.month + 1 if now.month < 12 else 1
|
|
next_year = now.year if now.month < 12 else now.year + 1
|
|
max_day = (datetime.date(next_year, next_month, 1) - datetime.date(now.year, now.month, 1)).days
|
|
|
|
# Создаем новый DailyRewardsList
|
|
month_max_day = f'До {max_day}'
|
|
new_daily_rewards_list = DailyRewardsList.objects.create(active_day=1, month_max_day=month_max_day)
|
|
|
|
# Очищаем предыдущие награды
|
|
DailyReward.objects.all().delete()
|
|
|
|
# Создаем новые ежедневные награды для каждого пользователя
|
|
users = User.objects.all()
|
|
for user in users:
|
|
for day in range(1, max_day + 1):
|
|
reward_amount = calculate_reward(day)
|
|
DailyReward.objects.create(user=user, day=day, reward_amount=reward_amount)
|
|
|
|
def run_scheduler():
|
|
while True:
|
|
now = datetime.datetime.now()
|
|
# Проверяем, что текущее время 00:05
|
|
|
|
if now.day == 1 and now.hour == 21 and now.minute == 5:
|
|
my_scheduled_job()
|
|
# Ждем 60 секунд, чтобы избежать повторного выполнения задачи в эту же минуту
|
|
time.sleep(60)
|
|
|
|
def start_scheduler():
|
|
thread = threading.Thread(target=run_scheduler, daemon=True)
|
|
thread.start()
|