add websocket for coins

This commit is contained in:
aurinex
2025-12-29 13:10:39 +05:00
parent bddd68bc25
commit 99d9741e97
6 changed files with 99 additions and 5 deletions

View File

@ -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