Improved internal achievement checks #268_achievements
This commit is contained in:
parent
cd5b3b6523
commit
2293849d94
@ -11,6 +11,7 @@ class DBContext(DatabaseContext):
|
||||
self._logger = logger
|
||||
|
||||
DatabaseContext.__init__(self)
|
||||
self._fails = 0
|
||||
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
try:
|
||||
@ -32,7 +33,11 @@ class DBContext(DatabaseContext):
|
||||
try:
|
||||
return super(DBContext, self).select(statement)
|
||||
except Exception as e:
|
||||
if self._fails >= 3:
|
||||
self._logger.fatal(__name__, f"Database error caused by {statement}", e)
|
||||
|
||||
self._logger.error(__name__, f"Database error caused by {statement}", e)
|
||||
self._fails += 1
|
||||
try:
|
||||
time.sleep(0.5)
|
||||
return self.select(statement)
|
||||
|
@ -4,7 +4,6 @@ from typing import Optional
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
from bot_data.model.achievement import Achievement
|
||||
from bot_data.model.server import Server
|
||||
from bot_data.model.user import User
|
||||
|
||||
|
||||
@ -13,7 +12,6 @@ class UserGotAchievement(TableABC):
|
||||
self,
|
||||
user: Optional[User],
|
||||
achievement: Optional[Achievement],
|
||||
server: Optional[Server],
|
||||
created_at: datetime = None,
|
||||
modified_at: datetime = None,
|
||||
id=0,
|
||||
@ -21,7 +19,6 @@ class UserGotAchievement(TableABC):
|
||||
self._id = id
|
||||
self._user = user
|
||||
self._achievement = achievement
|
||||
self._server = server
|
||||
|
||||
TableABC.__init__(self)
|
||||
self._created_at = created_at if created_at is not None else self._created_at
|
||||
|
@ -41,8 +41,8 @@ class AchievementRepositoryService(AchievementRepositoryABC):
|
||||
return UserGotAchievement(
|
||||
self._users.get_user_by_id(result[1]),
|
||||
self.get_achievement_by_id(result[2]),
|
||||
self._servers.get_server_by_id(result[3]),
|
||||
result[5],
|
||||
result[3],
|
||||
result[4],
|
||||
id=result[0],
|
||||
)
|
||||
|
||||
|
@ -33,16 +33,20 @@ class AchievementService:
|
||||
self._message_service = message_service
|
||||
self._t = t
|
||||
|
||||
self._attributes = List(AchievementAttribute)
|
||||
|
||||
self._attributes.extend(
|
||||
[
|
||||
AchievementAttribute("xp", lambda user: user.xp, "number"),
|
||||
AchievementAttribute("message_count", lambda user: user.message_count, "number"),
|
||||
AchievementAttribute("reaction_count", lambda user: user.reaction_count, "number"),
|
||||
AchievementAttribute("ontime", lambda user: user.ontime, "number"),
|
||||
AchievementAttribute("level", lambda user: user.level, "Level"),
|
||||
]
|
||||
)
|
||||
|
||||
def get_attributes(self) -> List[AchievementAttribute]:
|
||||
attributes = List(AchievementAttribute)
|
||||
|
||||
attributes.add(AchievementAttribute("xp", lambda user: user.xp, "number"))
|
||||
attributes.add(AchievementAttribute("message_count", lambda user: user.message_count, "number"))
|
||||
attributes.add(AchievementAttribute("reaction_count", lambda user: user.reaction_count, "number"))
|
||||
attributes.add(AchievementAttribute("ontime", lambda user: user.ontime, "number"))
|
||||
attributes.add(AchievementAttribute("level", lambda user: user.level, "Level"))
|
||||
|
||||
return attributes
|
||||
return self._attributes
|
||||
|
||||
def _match(self, value: str, operator: str, expected_value: str) -> bool:
|
||||
match operator:
|
||||
@ -66,14 +70,13 @@ class AchievementService:
|
||||
return user_achievements.where(lambda x: x.name == achievement.name).count() > 0
|
||||
|
||||
def has_user_achievement(self, user: User, achievement: Achievement) -> bool:
|
||||
return self._match(str(getattr(user, achievement.attribute)), achievement.operator, achievement.value)
|
||||
attribute: AchievementAttribute = self._attributes.where(lambda x: x.name == achievement.attribute).single()
|
||||
return self._match(str(attribute.resolve(user)), achievement.operator, achievement.value)
|
||||
|
||||
async def validate_achievements_for_user(self, user: User):
|
||||
achievements = self._achievements.get_achievements_by_server_id(user.server.id)
|
||||
for achievement in achievements:
|
||||
if self.has_user_achievement_already(user, achievement) and not self.has_user_achievement(
|
||||
user, achievement
|
||||
):
|
||||
if self.has_user_achievement_already(user, achievement) or not self.has_user_achievement(user, achievement):
|
||||
continue
|
||||
|
||||
self._achievements.add_user_got_achievement(UserGotAchievement(user, achievement, user.server))
|
||||
|
Loading…
Reference in New Issue
Block a user