This commit is contained in:
xtaodada 2024-09-12 13:51:35 +08:00
parent 57111236c9
commit a7db183ff8
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 55 additions and 34 deletions

View File

@ -6,32 +6,38 @@ from gram_core.dependence.redisdb import RedisDB
__all__ = ("GachaLogRankCache",)
if TYPE_CHECKING:
from gram_core.services.gacha_log_rank.models import GachaLogQueryTypeEnum
from gram_core.services.gacha_log_rank.models import GachaLogTypeEnum, GachaLogQueryTypeEnum
class GachaLogRankCache(BaseService.Component):
def __init__(self, redis: RedisDB):
self.client = redis.client
self.qname = "gacha_log_ranks:"
self.qname = "gacha_log_ranks"
def get_key(self, rank_type: "GachaLogQueryTypeEnum") -> str:
return f"{self.qname}:{rank_type.value}"
def get_key(self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum") -> str:
return f"{self.qname}:{rank_type.value}:{query_type.value}"
async def remove_all(self, rank_type: "GachaLogQueryTypeEnum") -> bool:
key = self.get_key(rank_type)
async def remove_all(self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum") -> bool:
key = self.get_key(rank_type, query_type)
return await self.client.delete(key)
async def add(self, rank_type: "GachaLogQueryTypeEnum", player_id: int, score: int) -> bool:
key = self.get_key(rank_type)
async def add(
self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum", player_id: int, score: int
) -> bool:
key = self.get_key(rank_type, query_type)
return await self.client.zadd(key, {player_id: score})
async def get_ranks(self, rank_type: "GachaLogQueryTypeEnum", limit: int, desc: bool):
async def get_ranks(
self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum", limit: int, desc: bool
):
"""获取排行榜,默认由高到低排序"""
key = self.get_key(rank_type)
key = self.get_key(rank_type, query_type)
return await self.client.zrange(key, 0, limit - 1, withscores=True, desc=desc)
async def get_rank_by_player_id(self, rank_type: "GachaLogQueryTypeEnum", player_id: int, desc: bool):
async def get_rank_by_player_id(
self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum", player_id: int, desc: bool
):
"""获取玩家在排行榜中的排名,默认由高到低排序"""
key = self.get_key(rank_type)
key = self.get_key(rank_type, query_type)
func = self.client.zrevrank if desc else self.client.zrank
return await func(key, player_id)

View File

@ -25,8 +25,9 @@ class GachaLogQueryTypeEnum(str, enum.Enum):
class GachaLogRank(SQLModel, table=True):
__tablename__ = "gacha_log_rank"
__table_args__ = dict(mysql_charset="utf8mb4", mysql_collate="utf8mb4_general_ci")
id: Optional[int] = Field(default=None, sa_column=Column(Integer(), autoincrement=True))
id: Optional[int] = Field(default=None, sa_column=Column(Integer(), primary_key=True, autoincrement=True))
player_id: int = Field(sa_column=Column(BigInteger(), primary_key=True))
type: GachaLogTypeEnum = Field(sa_column=Column(Enum(GachaLogTypeEnum), primary_key=True))
score_1: int = Field(sa_column=Column(BigInteger(), default=0))
@ -44,3 +45,12 @@ class GachaLogRank(SQLModel, table=True):
sa_column=Column(DateTime, server_default=func.now()) # pylint: disable=E1102
)
time_updated: Optional[datetime] = Field(sa_column=Column(DateTime, onupdate=func.now())) # pylint: disable=E1102
def update_by_new(self, new_ins: "GachaLogRank"):
self.score_1 = new_ins.score_1
self.score_2 = new_ins.score_2
self.score_3 = new_ins.score_3
self.score_4 = new_ins.score_4
self.score_5 = new_ins.score_5
self.data = new_ins.data
self.time_updated = datetime.now()

View File

@ -5,7 +5,7 @@ from sqlmodel.ext.asyncio.session import AsyncSession
from gram_core.base_service import BaseService
from gram_core.dependence.database import Database
from gram_core.services.gacha_log_rank.models import GachaLogRank, GachaLogQueryTypeEnum
from gram_core.services.gacha_log_rank.models import GachaLogRank, GachaLogTypeEnum, GachaLogQueryTypeEnum
__all__ = ("GachaLogRankRepository",)
@ -37,7 +37,7 @@ class GachaLogRankRepository(BaseService.Component):
results = await session.exec(statement)
return results.all()
async def get_all_by_player_ids(self, rank_type: GachaLogQueryTypeEnum, ids: List[int]) -> List[GachaLogRank]:
async def get_all_by_player_ids(self, rank_type: "GachaLogTypeEnum", ids: List[int]) -> List[GachaLogRank]:
async with AsyncSession(self.engine) as session:
statement = (
select(GachaLogRank).where(col(GachaLogRank.player_id).in_(ids)).where(GachaLogRank.type == rank_type)
@ -45,12 +45,10 @@ class GachaLogRankRepository(BaseService.Component):
results = await session.exec(statement)
return results.all()
async def get_by_player_id(
self, player_id: int, rank_type: Optional[GachaLogQueryTypeEnum]
) -> Optional[GachaLogRank]:
async def get_by_player_id(self, player_id: int, rank_type: Optional[GachaLogTypeEnum]) -> List[GachaLogRank]:
async with AsyncSession(self.engine) as session:
statement = select(GachaLogRank).where(GachaLogRank.player_id == player_id)
if rank_type:
statement = statement.where(GachaLogRank.type == rank_type)
results = await session.exec(statement)
return results.first()
return results.all()

View File

@ -2,7 +2,7 @@ from typing import List, Optional
from gram_core.base_service import BaseService
from gram_core.services.gacha_log_rank.cache import GachaLogRankCache
from gram_core.services.gacha_log_rank.models import GachaLogRank, GachaLogQueryTypeEnum
from gram_core.services.gacha_log_rank.models import GachaLogRank, GachaLogTypeEnum, GachaLogQueryTypeEnum
from gram_core.services.gacha_log_rank.repositories import GachaLogRankRepository
__all__ = ("GachaLogRankService",)
@ -17,40 +17,47 @@ class GachaLogRankService(BaseService):
self._repository = gacha_log_rank_repository
self._cache = gacha_log_rank_cache
async def del_all_cache_by_type(self, rank_type: "GachaLogQueryTypeEnum") -> bool:
return await self._cache.remove_all(rank_type)
async def del_all_cache_by_type(self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum") -> bool:
return await self._cache.remove_all(rank_type, query_type)
async def add_cache(self, rank_type: "GachaLogQueryTypeEnum", player_id: int, score: int) -> bool:
return await self._cache.add(rank_type, player_id, score)
async def add_cache(
self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum", player_id: int, score: int
) -> bool:
return await self._cache.add(rank_type, query_type, player_id, score)
async def get_ranks_cache(self, rank_type: "GachaLogQueryTypeEnum", limit: int = 20, desc: bool = True):
async def get_ranks_cache(
self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum", limit: int = 20, desc: bool = True
):
"""获取排行榜,默认由高到低排序"""
return await self._cache.get_ranks(rank_type, limit, desc)
return await self._cache.get_ranks(rank_type, query_type, limit, desc)
async def get_rank_by_player_id_cache(self, rank_type: "GachaLogQueryTypeEnum", player_id: int, desc: bool = True):
async def get_rank_by_player_id_cache(
self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum", player_id: int, desc: bool = True
):
"""获取玩家在排行榜中的排名,默认由高到低排序"""
return await self._cache.get_rank_by_player_id(rank_type, player_id, desc)
return await self._cache.get_rank_by_player_id(rank_type, query_type, player_id, desc)
async def update_cache(self, rank: GachaLogRank):
for type_str in GachaLogQueryTypeEnum:
type_str: "GachaLogQueryTypeEnum"
score = getattr(rank, type_str.value)
await self.add_cache(type_str, rank.player_id, score)
if score:
await self.add_cache(rank.type, type_str, rank.player_id, score)
async def add(self, rank: GachaLogRank):
req = await self._repository.add(rank)
await self.update_cache(rank)
req = await self._repository.add(rank)
return req
async def update(self, rank: GachaLogRank) -> GachaLogRank:
req = await self._repository.update(rank)
await self.update_cache(rank)
req = await self._repository.update(rank)
return req
async def get_ranks_by_ids(self, rank_type: "GachaLogQueryTypeEnum", ranks_ids: List[int]) -> List[GachaLogRank]:
async def get_ranks_by_ids(self, rank_type: "GachaLogTypeEnum", ranks_ids: List[int]) -> List[GachaLogRank]:
return await self._repository.get_all_by_player_ids(rank_type, ranks_ids)
async def get_rank_by_user_id(
self, user_id: int, rank_type: Optional["GachaLogQueryTypeEnum"]
) -> Optional[GachaLogRank]:
self, user_id: int, rank_type: Optional["GachaLogTypeEnum"] = None
) -> List[GachaLogRank]:
return await self._repository.get_by_player_id(user_id, rank_type)