Improved user joined game server mutation #181

This commit is contained in:
Sven Heidemann 2023-02-12 19:47:27 +01:00
parent 54cb6cf8a0
commit 667bb708d9
3 changed files with 18 additions and 18 deletions

View File

@ -20,8 +20,6 @@ class UserJoinedGameServerFilter(FilterABC):
self._id = None self._id = None
self._game_server = None self._game_server = None
self._user = None self._user = None
self._joined_on = None
self._leaved_on = None
def from_dict(self, values: dict): def from_dict(self, values: dict):
if "id" in values: if "id" in values:
@ -36,12 +34,6 @@ class UserJoinedGameServerFilter(FilterABC):
self._user: UserFilter = self._services.get_service(UserFilter) self._user: UserFilter = self._services.get_service(UserFilter)
self._user.from_dict(values["user"]) self._user.from_dict(values["user"])
if "joinedOn" in values:
self._joined_on = values["joinedOn"]
if "leavedOn" in values:
self._leaved_on = values["leavedOn"]
def filter(self, query: List[UserJoinedGameServer]) -> List[UserJoinedGameServer]: def filter(self, query: List[UserJoinedGameServer]) -> List[UserJoinedGameServer]:
if self._id is not None: if self._id is not None:
query = query.where(lambda x: x.id == self._id) query = query.where(lambda x: x.id == self._id)
@ -53,10 +45,4 @@ class UserJoinedGameServerFilter(FilterABC):
users = self._user.filter(query.select(lambda x: x.user)).select(lambda x: x.user_id) users = self._user.filter(query.select(lambda x: x.user)).select(lambda x: x.user_id)
query = query.where(lambda x: x.user.user_id in users) query = query.where(lambda x: x.user.user_id in users)
if self._joined_on is not None:
query = query.where(lambda x: x.joined_on == self._joined_on or self._joined_on in x.joined_on)
if self._leaved_on is not None:
query = query.where(lambda x: x.leaved_on == self._leaved_on or self._leaved_on in x.leaved_on)
return query return query

View File

@ -25,6 +25,4 @@ type UserJoinedGameServerMutation {
input UserJoinedGameServerInput { input UserJoinedGameServerInput {
gameServer: String! gameServer: String!
userId: ID! userId: ID!
joinedOn: String
leavedOn: String
} }

View File

@ -1,3 +1,5 @@
from datetime import datetime
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
@ -8,6 +10,8 @@ from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.user_joined_game_server import UserJoinedGameServer from bot_data.model.user_joined_game_server import UserJoinedGameServer
from bot_data.model.user_role_enum import UserRoleEnum from bot_data.model.user_role_enum import UserRoleEnum
from bot_graphql.abc.query_abc import QueryABC from bot_graphql.abc.query_abc import QueryABC
from modules.base.abc.base_helper_abc import BaseHelperABC
from modules.base.configuration.base_server_settings import BaseServerSettings
from modules.permission.service.permission_service import PermissionService from modules.permission.service.permission_service import PermissionService
@ -15,6 +19,7 @@ class UserJoinedGameServerMutation(QueryABC):
def __init__( def __init__(
self, self,
logger: LoggerABC, logger: LoggerABC,
base_helper: BaseHelperABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
users: UserRepositoryABC, users: UserRepositoryABC,
user_joined_game_servers: UserJoinedGameServerRepositoryABC, user_joined_game_servers: UserJoinedGameServerRepositoryABC,
@ -25,6 +30,7 @@ class UserJoinedGameServerMutation(QueryABC):
QueryABC.__init__(self, "UserJoinedGameServerMutation") QueryABC.__init__(self, "UserJoinedGameServerMutation")
self._logger = logger self._logger = logger
self._base_helper = base_helper
self._servers = servers self._servers = servers
self._users = users self._users = users
self._user_joined_game_servers = user_joined_game_servers self._user_joined_game_servers = user_joined_game_servers
@ -47,7 +53,7 @@ class UserJoinedGameServerMutation(QueryABC):
) )
return return
new = UserJoinedGameServer(user, input["gameServer"], input["joinedOn"]) new = UserJoinedGameServer(user, input["gameServer"], datetime.now())
self._user_joined_game_servers.add_user_joined_game_server(new) self._user_joined_game_servers.add_user_joined_game_server(new)
self._db.save_changes() self._db.save_changes()
@ -58,9 +64,19 @@ class UserJoinedGameServerMutation(QueryABC):
self._can_user_mutate_data(user.server, UserRoleEnum.admin) self._can_user_mutate_data(user.server, UserRoleEnum.admin)
active = self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.user_id) active = self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.user_id)
active.leaved_on = input["leavedOn"] active.leaved_on = datetime.now()
settings: BaseServerSettings = self._base_helper.get_config(user.server.discord_server_id)
ontime = round((active.leaved_on - active.joined_on).total_seconds() / 3600, 2)
old_xp = user.xp
user.xp += round(ontime * settings.xp_per_ontime_hour)
self._user_joined_game_servers.update_user_joined_game_server(active) self._user_joined_game_servers.update_user_joined_game_server(active)
self._db.save_changes() self._db.save_changes()
self._logger.debug(
__name__,
f"User {user} leaved_on {active.leaved_on}. Ontime: {ontime}h | xp: from {old_xp} to {user.xp}",
)
return active return active