Improved internal achievement checks #268_achievements

This commit is contained in:
Sven Heidemann 2023-07-15 13:16:13 +02:00
parent cd5b3b6523
commit 2293849d94
4 changed files with 23 additions and 18 deletions

View File

@ -11,6 +11,7 @@ class DBContext(DatabaseContext):
self._logger = logger self._logger = logger
DatabaseContext.__init__(self) DatabaseContext.__init__(self)
self._fails = 0
def connect(self, database_settings: DatabaseSettings): def connect(self, database_settings: DatabaseSettings):
try: try:
@ -32,7 +33,11 @@ class DBContext(DatabaseContext):
try: try:
return super(DBContext, self).select(statement) return super(DBContext, self).select(statement)
except Exception as e: 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._logger.error(__name__, f"Database error caused by {statement}", e)
self._fails += 1
try: try:
time.sleep(0.5) time.sleep(0.5)
return self.select(statement) return self.select(statement)

View File

@ -4,7 +4,6 @@ from typing import Optional
from cpl_core.database import TableABC from cpl_core.database import TableABC
from bot_data.model.achievement import Achievement from bot_data.model.achievement import Achievement
from bot_data.model.server import Server
from bot_data.model.user import User from bot_data.model.user import User
@ -13,7 +12,6 @@ class UserGotAchievement(TableABC):
self, self,
user: Optional[User], user: Optional[User],
achievement: Optional[Achievement], achievement: Optional[Achievement],
server: Optional[Server],
created_at: datetime = None, created_at: datetime = None,
modified_at: datetime = None, modified_at: datetime = None,
id=0, id=0,
@ -21,7 +19,6 @@ class UserGotAchievement(TableABC):
self._id = id self._id = id
self._user = user self._user = user
self._achievement = achievement self._achievement = achievement
self._server = server
TableABC.__init__(self) TableABC.__init__(self)
self._created_at = created_at if created_at is not None else self._created_at self._created_at = created_at if created_at is not None else self._created_at

View File

@ -41,8 +41,8 @@ class AchievementRepositoryService(AchievementRepositoryABC):
return UserGotAchievement( return UserGotAchievement(
self._users.get_user_by_id(result[1]), self._users.get_user_by_id(result[1]),
self.get_achievement_by_id(result[2]), self.get_achievement_by_id(result[2]),
self._servers.get_server_by_id(result[3]), result[3],
result[5], result[4],
id=result[0], id=result[0],
) )

View File

@ -33,16 +33,20 @@ class AchievementService:
self._message_service = message_service self._message_service = message_service
self._t = t 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]: def get_attributes(self) -> List[AchievementAttribute]:
attributes = List(AchievementAttribute) return self._attributes
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
def _match(self, value: str, operator: str, expected_value: str) -> bool: def _match(self, value: str, operator: str, expected_value: str) -> bool:
match operator: match operator:
@ -66,14 +70,13 @@ class AchievementService:
return user_achievements.where(lambda x: x.name == achievement.name).count() > 0 return user_achievements.where(lambda x: x.name == achievement.name).count() > 0
def has_user_achievement(self, user: User, achievement: Achievement) -> bool: 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): async def validate_achievements_for_user(self, user: User):
achievements = self._achievements.get_achievements_by_server_id(user.server.id) achievements = self._achievements.get_achievements_by_server_id(user.server.id)
for achievement in achievements: for achievement in achievements:
if self.has_user_achievement_already(user, achievement) and not self.has_user_achievement( if self.has_user_achievement_already(user, achievement) or not self.has_user_achievement(user, achievement):
user, achievement
):
continue continue
self._achievements.add_user_got_achievement(UserGotAchievement(user, achievement, user.server)) self._achievements.add_user_got_achievement(UserGotAchievement(user, achievement, user.server))