2023-03-14 01:27:22 +00:00
|
|
|
from datetime import datetime, timedelta
|
2023-08-02 12:10:08 +00:00
|
|
|
from typing import Optional
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
from core.base_service import BaseService
|
|
|
|
from core.dependence.redisdb import RedisDB
|
|
|
|
from core.services.players.models import PlayersDataBase as Player, PlayerInfoSQLModel, PlayerInfo
|
2023-08-02 12:10:08 +00:00
|
|
|
from core.services.players.repositories import PlayerInfoRepository
|
2023-06-07 11:50:27 +00:00
|
|
|
from modules.apihelper.client.components.player_cards import PlayerCards, PlayerBaseInfo
|
2023-03-14 01:27:22 +00:00
|
|
|
from utils.log import logger
|
|
|
|
|
2023-08-02 12:10:08 +00:00
|
|
|
from gram_core.services.players import PlayersService
|
2023-03-14 01:27:22 +00:00
|
|
|
|
2023-08-02 12:10:08 +00:00
|
|
|
__all__ = ("PlayersService", "PlayerInfoService")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PlayerInfoService(BaseService):
|
|
|
|
def __init__(self, redis: RedisDB, players_info_repository: PlayerInfoRepository):
|
|
|
|
self.cache = redis.client
|
|
|
|
self._players_info_repository = players_info_repository
|
2023-06-07 11:50:27 +00:00
|
|
|
self.client = PlayerCards(redis)
|
2023-03-14 01:27:22 +00:00
|
|
|
self.qname = "players_info"
|
|
|
|
|
|
|
|
async def get_form_cache(self, player: Player):
|
|
|
|
qname = f"{self.qname}:{player.user_id}:{player.player_id}"
|
|
|
|
data = await self.cache.get(qname)
|
|
|
|
if data is None:
|
|
|
|
return None
|
|
|
|
json_data = str(data, encoding="utf-8")
|
|
|
|
return PlayerInfo.parse_raw(json_data)
|
|
|
|
|
|
|
|
async def set_form_cache(self, player: PlayerInfo):
|
|
|
|
qname = f"{self.qname}:{player.user_id}:{player.player_id}"
|
|
|
|
await self.cache.set(qname, player.json(), ex=60)
|
|
|
|
|
2023-06-07 11:50:27 +00:00
|
|
|
async def get_player_info_from_mihomo(self, player_id: int) -> Optional[PlayerBaseInfo]:
|
2023-03-14 01:27:22 +00:00
|
|
|
try:
|
2023-06-07 11:50:27 +00:00
|
|
|
response = await self.client.get_player_base_info(player_id)
|
|
|
|
return response
|
2023-03-14 01:27:22 +00:00
|
|
|
except Exception as exc:
|
2023-06-07 11:50:27 +00:00
|
|
|
logger.error("Mihomo 请求失败: %s", exc_info=exc)
|
2023-03-14 01:27:22 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
async def get(self, player: Player) -> Optional[PlayerInfo]:
|
|
|
|
player_info = await self.get_form_cache(player)
|
|
|
|
if player_info is not None:
|
|
|
|
return player_info
|
|
|
|
player_info = await self._players_info_repository.get(player.user_id, player.player_id)
|
|
|
|
if player_info is None:
|
2023-06-07 11:50:27 +00:00
|
|
|
player_info_mihomo = await self.get_player_info_from_mihomo(player.player_id)
|
|
|
|
if player_info_mihomo is None:
|
2023-03-14 01:27:22 +00:00
|
|
|
return None
|
|
|
|
player_info = PlayerInfo(
|
|
|
|
user_id=player.user_id,
|
|
|
|
player_id=player.player_id,
|
2023-06-07 11:50:27 +00:00
|
|
|
nickname=player_info_mihomo.nickname,
|
|
|
|
signature=player_info_mihomo.signature,
|
|
|
|
name_card=None,
|
|
|
|
hand_image=player_info_mihomo.headIcon,
|
2023-03-14 01:27:22 +00:00
|
|
|
create_time=datetime.now(),
|
|
|
|
last_save_time=datetime.now(),
|
|
|
|
is_update=True,
|
|
|
|
)
|
|
|
|
await self._players_info_repository.add(PlayerInfoSQLModel.from_orm(player_info))
|
|
|
|
await self.set_form_cache(player_info)
|
|
|
|
return player_info
|
|
|
|
if player_info.is_update:
|
|
|
|
expiration_time = datetime.now() - timedelta(days=7)
|
|
|
|
if player_info.last_save_time is None or player_info.last_save_time <= expiration_time:
|
2023-06-07 11:50:27 +00:00
|
|
|
player_info_mihomo = await self.get_player_info_from_mihomo(player.player_id)
|
|
|
|
if player_info_mihomo is None:
|
2023-03-14 01:27:22 +00:00
|
|
|
player_info.last_save_time = datetime.now()
|
2023-03-23 02:49:36 +00:00
|
|
|
await self._players_info_repository.update(player_info)
|
2023-03-14 01:27:22 +00:00
|
|
|
await self.set_form_cache(player_info)
|
|
|
|
return player_info
|
2023-06-07 11:50:27 +00:00
|
|
|
player_info.nickname = player_info_mihomo.nickname
|
|
|
|
player_info.name_card = None
|
|
|
|
player_info.signature = player_info_mihomo.signature
|
|
|
|
player_info.hand_image = player_info_mihomo.headIcon
|
|
|
|
player_info.nickname = player_info_mihomo.nickname
|
2023-03-14 01:27:22 +00:00
|
|
|
player_info.last_save_time = datetime.now()
|
2023-03-23 02:49:36 +00:00
|
|
|
await self._players_info_repository.update(player_info)
|
2023-03-14 01:27:22 +00:00
|
|
|
await self.set_form_cache(player_info)
|
|
|
|
return player_info
|
|
|
|
|
2023-06-07 11:50:27 +00:00
|
|
|
async def update_from_mihomo(self, player: Player) -> bool:
|
2023-03-14 01:27:22 +00:00
|
|
|
player_info = await self._players_info_repository.get(player.user_id, player.player_id)
|
|
|
|
if player_info is not None:
|
2023-06-07 11:50:27 +00:00
|
|
|
player_info_mihomo = await self.get_player_info_from_mihomo(player.player_id)
|
|
|
|
if player_info_mihomo is None:
|
2023-03-14 01:27:22 +00:00
|
|
|
return False
|
2023-06-07 11:50:27 +00:00
|
|
|
player_info.nickname = player_info_mihomo.nickname
|
|
|
|
player_info.name_card = None
|
|
|
|
player_info.signature = player_info_mihomo.signature
|
|
|
|
player_info.hand_image = player_info_mihomo.headIcon
|
|
|
|
player_info.nickname = player_info_mihomo.nickname
|
2023-03-14 01:27:22 +00:00
|
|
|
player_info.last_save_time = datetime.now()
|
|
|
|
await self._players_info_repository.update(player_info)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-06-07 11:50:27 +00:00
|
|
|
async def add_from_mihomo(self, player: Player) -> bool:
|
2023-03-14 01:27:22 +00:00
|
|
|
player_info = await self._players_info_repository.get(player.user_id, player.player_id)
|
|
|
|
if player_info is None:
|
2023-06-07 11:50:27 +00:00
|
|
|
player_info_enka = await self.get_player_info_from_mihomo(player.player_id)
|
2023-03-14 01:27:22 +00:00
|
|
|
if player_info_enka is None:
|
|
|
|
return False
|
|
|
|
player_info = PlayerInfoSQLModel(
|
|
|
|
user_id=player.user_id,
|
|
|
|
player_id=player.player_id,
|
|
|
|
nickname=player_info_enka.nickname,
|
|
|
|
signature=player_info_enka.signature,
|
2023-06-07 11:50:27 +00:00
|
|
|
name_card=None,
|
|
|
|
hand_image=player_info_enka.headIcon,
|
2023-03-14 01:27:22 +00:00
|
|
|
create_time=datetime.now(),
|
|
|
|
last_save_time=datetime.now(),
|
|
|
|
is_update=True,
|
|
|
|
)
|
|
|
|
await self._players_info_repository.add(player_info)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def get_form_sql(self, player: Player):
|
|
|
|
return await self._players_info_repository.get(player.user_id, player.player_id)
|
|
|
|
|
|
|
|
async def delete_form_player(self, player: Player):
|
|
|
|
await self._players_info_repository.delete_by_id(user_id=player.user_id, player_id=player.player_id)
|
|
|
|
|
|
|
|
async def add(self, player_info: PlayerInfo):
|
|
|
|
await self._players_info_repository.add(PlayerInfoSQLModel.from_orm(player_info))
|
|
|
|
|
2023-03-17 09:45:11 +00:00
|
|
|
async def delete(self, player_info: PlayerInfoSQLModel):
|
|
|
|
await self._players_info_repository.delete(player_info)
|
2023-12-16 10:01:27 +00:00
|
|
|
|
|
|
|
async def update(self, player_info: PlayerInfoSQLModel):
|
|
|
|
await self._players_info_repository.update(player_info)
|
|
|
|
|
|
|
|
async def get_all_by_user_id(self, user_id: int):
|
|
|
|
return await self._players_info_repository.get_all_by_user_id(user_id)
|
2024-03-29 13:40:23 +00:00
|
|
|
|
|
|
|
async def get_by_player_id(self, player_id: int):
|
|
|
|
return await self._players_info_repository.get_by_player_id(player_id)
|