wokring marketplace without enchancts and durability on item

This commit is contained in:
2025-07-19 04:13:04 +05:00
parent 44e12723ad
commit 6b8f116608
6 changed files with 351 additions and 1 deletions

View File

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