add websocket for coins
This commit is contained in:
@ -3,6 +3,7 @@ import re
|
||||
from app.db.database import users_collection
|
||||
from fastapi import HTTPException
|
||||
from app.db.database import db
|
||||
from app.realtime.coins_hub import coins_hub
|
||||
|
||||
MAX_MINUTES_PER_UPDATE = 120
|
||||
|
||||
@ -70,6 +71,8 @@ class CoinsService:
|
||||
{"$set": {"coins": new_coins, "total_time_played": new_total_time}}
|
||||
)
|
||||
|
||||
await coins_hub.send_update(user["username"], new_coins)
|
||||
|
||||
await coins_sessions_collection.insert_one({
|
||||
"player_id": player_id,
|
||||
"player_name": player_name,
|
||||
@ -138,7 +141,9 @@ class CoinsService:
|
||||
raise HTTPException(status_code=404, detail=f"Пользователь {username} не найден")
|
||||
|
||||
user = await users_collection.find_one({"username": username})
|
||||
return user.get("coins", 0)
|
||||
new_balance = user.get("coins", 0)
|
||||
await coins_hub.send_update(username, new_balance)
|
||||
return new_balance
|
||||
|
||||
async def decrease_balance(self, username: str, amount: int) -> int:
|
||||
"""Уменьшить баланс пользователя"""
|
||||
@ -154,4 +159,6 @@ class CoinsService:
|
||||
raise HTTPException(status_code=404, detail=f"Пользователь {username} не найден")
|
||||
|
||||
user = await users_collection.find_one({"username": username})
|
||||
return user.get("coins", 0)
|
||||
new_balance = user.get("coins", 0)
|
||||
await coins_hub.send_update(username, new_balance)
|
||||
return new_balance
|
||||
|
||||
@ -8,6 +8,8 @@ import random
|
||||
from fastapi import HTTPException
|
||||
from app.db.database import db, users_collection
|
||||
|
||||
from app.services.coins import CoinsService
|
||||
|
||||
TZ = ZoneInfo("Asia/Yekaterinburg") # как в dailyreward :contentReference[oaicite:1]{index=1}
|
||||
|
||||
coins_sessions_collection = db.coins_sessions
|
||||
@ -168,7 +170,8 @@ class DailyQuestsService:
|
||||
return {"claimed": False, "reason": "not_completed"}
|
||||
|
||||
# 2) начисляем coins
|
||||
await users_collection.update_one({"username": username}, {"$inc": {"coins": reward}})
|
||||
coins_service = CoinsService()
|
||||
await coins_service.increase_balance(username, reward)
|
||||
|
||||
# 3) лог в coins_sessions (как daily_login) :contentReference[oaicite:4]{index=4}
|
||||
await coins_sessions_collection.insert_one({
|
||||
@ -219,7 +222,8 @@ class DailyQuestsService:
|
||||
continue # уже claimed/не completed
|
||||
|
||||
# начисляем coins
|
||||
await users_collection.update_one({"username": username}, {"$inc": {"coins": reward}})
|
||||
coins_service = CoinsService()
|
||||
await coins_service.increase_balance(username, reward)
|
||||
total_added += reward
|
||||
|
||||
# лог (как в claim) :contentReference[oaicite:2]{index=2}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from zoneinfo import ZoneInfo
|
||||
from app.db.database import users_collection, db
|
||||
from app.realtime.coins_hub import coins_hub
|
||||
|
||||
coins_sessions_collection = db.coins_sessions
|
||||
|
||||
@ -70,6 +71,9 @@ class DailyRewardService:
|
||||
if result.modified_count == 0:
|
||||
user2 = await users_collection.find_one({"username": username})
|
||||
return {"claimed": False, "reason": "already_claimed_today", "streak": user2.get("daily_streak", 0)}
|
||||
|
||||
new_balance = (await users_collection.find_one({"username": username})).get("coins", 0)
|
||||
await coins_hub.send_update(username, new_balance)
|
||||
|
||||
await coins_sessions_collection.insert_one({
|
||||
"player_name": username,
|
||||
|
||||
Reference in New Issue
Block a user