2023-07-31 14:10:37 +00:00
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
from gram_core.base_service import BaseService
|
|
|
|
from gram_core.basemodel import RegionEnum
|
|
|
|
from gram_core.services.players.models import PlayersDataBase as Player
|
|
|
|
from gram_core.services.players.repositories import PlayersRepository
|
|
|
|
|
|
|
|
__all__ = "PlayersService"
|
|
|
|
|
|
|
|
|
|
|
|
class PlayersService(BaseService):
|
|
|
|
def __init__(self, players_repository: PlayersRepository) -> None:
|
|
|
|
self._repository = players_repository
|
|
|
|
|
|
|
|
async def get(
|
|
|
|
self,
|
2024-07-12 13:54:01 +00:00
|
|
|
user_id: Optional[int] = None,
|
2023-07-31 14:10:37 +00:00
|
|
|
player_id: Optional[int] = None,
|
|
|
|
account_id: Optional[int] = None,
|
|
|
|
region: Optional[RegionEnum] = None,
|
|
|
|
is_chosen: Optional[bool] = None,
|
2024-06-11 15:19:24 +00:00
|
|
|
offset: int = 0,
|
2023-07-31 14:10:37 +00:00
|
|
|
) -> Optional[Player]:
|
2024-06-11 15:19:24 +00:00
|
|
|
return await self._repository.get(user_id, player_id, account_id, region, is_chosen, offset)
|
2023-07-31 14:10:37 +00:00
|
|
|
|
2024-06-11 15:19:24 +00:00
|
|
|
async def get_player(
|
|
|
|
self, user_id: int, region: Optional[RegionEnum] = None, player_id: int = None, offset: int = 0
|
|
|
|
) -> Optional[Player]:
|
|
|
|
is_chosen = True
|
2024-06-17 15:23:31 +00:00
|
|
|
if player_id is not None or (offset is not None and offset != 0):
|
2024-06-11 15:19:24 +00:00
|
|
|
is_chosen = None
|
|
|
|
return await self._repository.get(user_id, player_id, region=region, is_chosen=is_chosen, offset=offset)
|
2023-07-31 14:10:37 +00:00
|
|
|
|
|
|
|
async def add(self, player: Player) -> None:
|
|
|
|
await self._repository.add(player)
|
|
|
|
|
|
|
|
async def update(self, player: Player) -> None:
|
|
|
|
await self._repository.update(player)
|
|
|
|
|
|
|
|
async def get_all_by_user_id(self, user_id: int) -> List[Player]:
|
|
|
|
return await self._repository.get_all_by_user_id(user_id)
|
|
|
|
|
|
|
|
async def remove_all_by_user_id(self, user_id: int):
|
|
|
|
players = await self._repository.get_all_by_user_id(user_id)
|
|
|
|
for player in players:
|
|
|
|
await self._repository.delete(player)
|
|
|
|
|
2024-07-30 08:18:42 +00:00
|
|
|
async def get_all_by_account_id(self, account_id: int, region: Optional[RegionEnum] = None) -> List[Player]:
|
|
|
|
return await self._repository.get_all_by_account_id(account_id, region)
|
|
|
|
|
|
|
|
async def set_player_to_main(self, user_id: int, player_id: int) -> bool:
|
|
|
|
players = await self._repository.get_all_by_user_id(user_id)
|
|
|
|
for player in players:
|
|
|
|
player.is_chosen = player.player_id == player_id
|
|
|
|
await self._repository.update(player)
|
|
|
|
|
2023-07-31 14:10:37 +00:00
|
|
|
async def delete(self, player: Player):
|
|
|
|
await self._repository.delete(player)
|