add today online check in dailyreward

This commit is contained in:
2025-12-13 01:21:47 +05:00
parent 04dcf7bf9d
commit 67d85a71c1

View File

@ -21,6 +21,24 @@ class DailyRewardService:
user = await users_collection.find_one({"username": username})
if not user:
return {"claimed": False, "reason": "user_not_found"}
was_online_today = await coins_sessions_collection.find_one(
{
"player_name": username,
"update_type": "coins_update",
"timestamp": {
"$gte": start_today_utc.replace(tzinfo=None),
"$lt": start_tomorrow_utc.replace(tzinfo=None),
},
}
)
if not was_online_today:
return {
"claimed": False,
"reason": "not_online_today",
"message": "Вы должны зайти на сервер сегодня, чтобы получить ежедневную награду",
}
last_claim_at = user.get("daily_last_claim_at") # ожидаем datetime (лучше хранить UTC)
last_local_day = last_claim_at.replace(tzinfo=timezone.utc).astimezone(TZ).date() if last_claim_at else None
@ -71,10 +89,20 @@ class DailyRewardService:
if not user:
return {"ok": False, "reason": "user_not_found"}
was_online_today = await coins_sessions_collection.find_one({
"player_name": username,
"update_type": "coins_update",
"timestamp": {
"$gte": start_today_utc.replace(tzinfo=None),
"$lt": start_tomorrow_utc.replace(tzinfo=None),
},
})
last_claim_at = user.get("daily_last_claim_at")
last_local_day = last_claim_at.replace(tzinfo=timezone.utc).astimezone(TZ).date() if last_claim_at else None
can_claim = (last_local_day != today_local)
can_claim = (last_local_day != today_local) and bool(was_online_today)
seconds_to_next = 0 if can_claim else int((start_tomorrow_utc - now_utc).total_seconds())
if seconds_to_next < 0:
seconds_to_next = 0
@ -82,8 +110,8 @@ class DailyRewardService:
return {
"ok": True,
"can_claim": can_claim,
"was_online_today": bool(was_online_today),
"seconds_to_next": seconds_to_next,
# можно отдавать и UTC, и локальное время для UI:
"next_claim_at_utc": start_tomorrow_utc.isoformat().replace("+00:00", "Z"),
"next_claim_at_local": (start_today_local + timedelta(days=1)).isoformat(),
"streak": int(user.get("daily_streak", 0) or 0),