mirror of
https://github.com/PaiGramTeam/GramCore.git
synced 2024-11-28 08:30:27 +00:00
fix
This commit is contained in:
parent
57111236c9
commit
a7db183ff8
@ -6,32 +6,38 @@ from gram_core.dependence.redisdb import RedisDB
|
|||||||
__all__ = ("GachaLogRankCache",)
|
__all__ = ("GachaLogRankCache",)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
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):
|
class GachaLogRankCache(BaseService.Component):
|
||||||
def __init__(self, redis: RedisDB):
|
def __init__(self, redis: RedisDB):
|
||||||
self.client = redis.client
|
self.client = redis.client
|
||||||
self.qname = "gacha_log_ranks:"
|
self.qname = "gacha_log_ranks"
|
||||||
|
|
||||||
def get_key(self, rank_type: "GachaLogQueryTypeEnum") -> str:
|
def get_key(self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum") -> str:
|
||||||
return f"{self.qname}:{rank_type.value}"
|
return f"{self.qname}:{rank_type.value}:{query_type.value}"
|
||||||
|
|
||||||
async def remove_all(self, rank_type: "GachaLogQueryTypeEnum") -> bool:
|
async def remove_all(self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum") -> bool:
|
||||||
key = self.get_key(rank_type)
|
key = self.get_key(rank_type, query_type)
|
||||||
return await self.client.delete(key)
|
return await self.client.delete(key)
|
||||||
|
|
||||||
async def add(self, rank_type: "GachaLogQueryTypeEnum", player_id: int, score: int) -> bool:
|
async def add(
|
||||||
key = self.get_key(rank_type)
|
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})
|
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)
|
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
|
func = self.client.zrevrank if desc else self.client.zrank
|
||||||
return await func(key, player_id)
|
return await func(key, player_id)
|
||||||
|
@ -25,8 +25,9 @@ class GachaLogQueryTypeEnum(str, enum.Enum):
|
|||||||
|
|
||||||
|
|
||||||
class GachaLogRank(SQLModel, table=True):
|
class GachaLogRank(SQLModel, table=True):
|
||||||
|
__tablename__ = "gacha_log_rank"
|
||||||
__table_args__ = dict(mysql_charset="utf8mb4", mysql_collate="utf8mb4_general_ci")
|
__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))
|
player_id: int = Field(sa_column=Column(BigInteger(), primary_key=True))
|
||||||
type: GachaLogTypeEnum = Field(sa_column=Column(Enum(GachaLogTypeEnum), primary_key=True))
|
type: GachaLogTypeEnum = Field(sa_column=Column(Enum(GachaLogTypeEnum), primary_key=True))
|
||||||
score_1: int = Field(sa_column=Column(BigInteger(), default=0))
|
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
|
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
|
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()
|
||||||
|
@ -5,7 +5,7 @@ from sqlmodel.ext.asyncio.session import AsyncSession
|
|||||||
|
|
||||||
from gram_core.base_service import BaseService
|
from gram_core.base_service import BaseService
|
||||||
from gram_core.dependence.database import Database
|
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",)
|
__all__ = ("GachaLogRankRepository",)
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ class GachaLogRankRepository(BaseService.Component):
|
|||||||
results = await session.exec(statement)
|
results = await session.exec(statement)
|
||||||
return results.all()
|
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:
|
async with AsyncSession(self.engine) as session:
|
||||||
statement = (
|
statement = (
|
||||||
select(GachaLogRank).where(col(GachaLogRank.player_id).in_(ids)).where(GachaLogRank.type == rank_type)
|
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)
|
results = await session.exec(statement)
|
||||||
return results.all()
|
return results.all()
|
||||||
|
|
||||||
async def get_by_player_id(
|
async def get_by_player_id(self, player_id: int, rank_type: Optional[GachaLogTypeEnum]) -> List[GachaLogRank]:
|
||||||
self, player_id: int, rank_type: Optional[GachaLogQueryTypeEnum]
|
|
||||||
) -> Optional[GachaLogRank]:
|
|
||||||
async with AsyncSession(self.engine) as session:
|
async with AsyncSession(self.engine) as session:
|
||||||
statement = select(GachaLogRank).where(GachaLogRank.player_id == player_id)
|
statement = select(GachaLogRank).where(GachaLogRank.player_id == player_id)
|
||||||
if rank_type:
|
if rank_type:
|
||||||
statement = statement.where(GachaLogRank.type == rank_type)
|
statement = statement.where(GachaLogRank.type == rank_type)
|
||||||
results = await session.exec(statement)
|
results = await session.exec(statement)
|
||||||
return results.first()
|
return results.all()
|
||||||
|
@ -2,7 +2,7 @@ from typing import List, Optional
|
|||||||
|
|
||||||
from gram_core.base_service import BaseService
|
from gram_core.base_service import BaseService
|
||||||
from gram_core.services.gacha_log_rank.cache import GachaLogRankCache
|
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
|
from gram_core.services.gacha_log_rank.repositories import GachaLogRankRepository
|
||||||
|
|
||||||
__all__ = ("GachaLogRankService",)
|
__all__ = ("GachaLogRankService",)
|
||||||
@ -17,40 +17,47 @@ class GachaLogRankService(BaseService):
|
|||||||
self._repository = gacha_log_rank_repository
|
self._repository = gacha_log_rank_repository
|
||||||
self._cache = gacha_log_rank_cache
|
self._cache = gacha_log_rank_cache
|
||||||
|
|
||||||
async def del_all_cache_by_type(self, rank_type: "GachaLogQueryTypeEnum") -> bool:
|
async def del_all_cache_by_type(self, rank_type: "GachaLogTypeEnum", query_type: "GachaLogQueryTypeEnum") -> bool:
|
||||||
return await self._cache.remove_all(rank_type)
|
return await self._cache.remove_all(rank_type, query_type)
|
||||||
|
|
||||||
async def add_cache(self, rank_type: "GachaLogQueryTypeEnum", player_id: int, score: int) -> bool:
|
async def add_cache(
|
||||||
return await self._cache.add(rank_type, player_id, score)
|
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):
|
async def update_cache(self, rank: GachaLogRank):
|
||||||
for type_str in GachaLogQueryTypeEnum:
|
for type_str in GachaLogQueryTypeEnum:
|
||||||
type_str: "GachaLogQueryTypeEnum"
|
type_str: "GachaLogQueryTypeEnum"
|
||||||
score = getattr(rank, type_str.value)
|
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):
|
async def add(self, rank: GachaLogRank):
|
||||||
req = await self._repository.add(rank)
|
|
||||||
await self.update_cache(rank)
|
await self.update_cache(rank)
|
||||||
|
req = await self._repository.add(rank)
|
||||||
return req
|
return req
|
||||||
|
|
||||||
async def update(self, rank: GachaLogRank) -> GachaLogRank:
|
async def update(self, rank: GachaLogRank) -> GachaLogRank:
|
||||||
req = await self._repository.update(rank)
|
|
||||||
await self.update_cache(rank)
|
await self.update_cache(rank)
|
||||||
|
req = await self._repository.update(rank)
|
||||||
return req
|
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)
|
return await self._repository.get_all_by_player_ids(rank_type, ranks_ids)
|
||||||
|
|
||||||
async def get_rank_by_user_id(
|
async def get_rank_by_user_id(
|
||||||
self, user_id: int, rank_type: Optional["GachaLogQueryTypeEnum"]
|
self, user_id: int, rank_type: Optional["GachaLogTypeEnum"] = None
|
||||||
) -> Optional[GachaLogRank]:
|
) -> List[GachaLogRank]:
|
||||||
return await self._repository.get_by_player_id(user_id, rank_type)
|
return await self._repository.get_by_player_id(user_id, rank_type)
|
||||||
|
Loading…
Reference in New Issue
Block a user