wokring marketplace without enchancts and durability on item
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
from datetime import datetime
|
||||
from app.db.database import users_collection, sessions_collection
|
||||
from fastapi import HTTPException
|
||||
|
||||
class CoinsService:
|
||||
async def update_player_coins(self, player_id: str, player_name: str, online_time: int, server_ip: str):
|
||||
@ -100,3 +101,42 @@ class CoinsService:
|
||||
"formatted": f"{hours}ч {minutes}м {seconds}с"
|
||||
}
|
||||
}
|
||||
|
||||
async def get_balance(self, username: str) -> int:
|
||||
"""Получить текущий баланс пользователя"""
|
||||
user = await users_collection.find_one({"username": username})
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail=f"Пользователь {username} не найден")
|
||||
return user.get("coins", 0)
|
||||
|
||||
async def increase_balance(self, username: str, amount: int) -> int:
|
||||
"""Увеличить баланс пользователя"""
|
||||
if amount <= 0:
|
||||
raise ValueError("Сумма должна быть положительной")
|
||||
|
||||
result = await users_collection.update_one(
|
||||
{"username": username},
|
||||
{"$inc": {"coins": amount}}
|
||||
)
|
||||
|
||||
if result.modified_count == 0:
|
||||
raise HTTPException(status_code=404, detail=f"Пользователь {username} не найден")
|
||||
|
||||
user = await users_collection.find_one({"username": username})
|
||||
return user.get("coins", 0)
|
||||
|
||||
async def decrease_balance(self, username: str, amount: int) -> int:
|
||||
"""Уменьшить баланс пользователя"""
|
||||
if amount <= 0:
|
||||
raise ValueError("Сумма должна быть положительной")
|
||||
|
||||
result = await users_collection.update_one(
|
||||
{"username": username},
|
||||
{"$inc": {"coins": -amount}} # Уменьшаем на отрицательное значение
|
||||
)
|
||||
|
||||
if result.modified_count == 0:
|
||||
raise HTTPException(status_code=404, detail=f"Пользователь {username} не найден")
|
||||
|
||||
user = await users_collection.find_one({"username": username})
|
||||
return user.get("coins", 0)
|
||||
|
Reference in New Issue
Block a user