diff --git a/bot/src/bot_data/model/user.py b/bot/src/bot_data/model/user.py index 5d8ac8c2..dee1e355 100644 --- a/bot/src/bot_data/model/user.py +++ b/bot/src/bot_data/model/user.py @@ -2,6 +2,7 @@ from datetime import datetime, date from typing import Optional from cpl_core.database import TableABC +from cpl_core.database.context import DatabaseContextABC from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List @@ -92,10 +93,32 @@ class User(TableABC): @property @ServiceProviderABC.inject def ontime(self, services: ServiceProviderABC) -> float: - from bot_core.abc.client_utils_abc import ClientUtilsABC + db: DatabaseContextABC = services.get_service(DatabaseContextABC) + result = db.select( + f""" + SELECT ROUND(SUM(TIME_TO_SEC(TIMEDIFF(UserJoinedVoiceChannel.LeavedOn, UserJoinedVoiceChannel.JoinedOn)) / 3600),2) + FROM UserJoinedVoiceChannel + WHERE UserId = {self._user_id}; + """ + )[0][0] + if result is None: + return 0 + return float(result) - client_utils: ClientUtilsABC = services.get_service(ClientUtilsABC) - return client_utils.get_ontime_for_user(self) + @property + @ServiceProviderABC.inject + def game_ontime(self, services: ServiceProviderABC) -> float: + db: DatabaseContextABC = services.get_service(DatabaseContextABC) + result = db.select( + f""" + SELECT ROUND(SUM(TIME_TO_SEC(TIMEDIFF(UserJoinedGameServer.LeavedOn, UserJoinedGameServer.JoinedOn)) / 3600),2) + FROM UserJoinedGameServer + WHERE UserId = {self._user_id}; + """ + )[0][0] + if result is None: + return 0 + return float(result) @property @ServiceProviderABC.inject diff --git a/bot/src/bot_graphql/graphql/user.gql b/bot/src/bot_graphql/graphql/user.gql index f05ee950..f7d2b990 100644 --- a/bot/src/bot_graphql/graphql/user.gql +++ b/bot/src/bot_graphql/graphql/user.gql @@ -7,6 +7,7 @@ type User implements TableWithHistoryQuery { reactionCount: Int birthday: String ontime: Float + gameOntime: Float level: Level joinedServers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [UserJoinedServer] diff --git a/bot/src/bot_graphql/queries/server_statistic_query.py b/bot/src/bot_graphql/queries/server_statistic_query.py index 661d8142..e511dfdb 100644 --- a/bot/src/bot_graphql/queries/server_statistic_query.py +++ b/bot/src/bot_graphql/queries/server_statistic_query.py @@ -132,7 +132,7 @@ class ServerStatisticQuery(QueryABC): def _resolve_game_server_ontime(self, server, *_): query = f""" - SELECT ROUND(SUM(TIME_TO_SEC(TIMEDIFF(UserJoinedGameServer.LeavedOn, UserJoinedGameServer.JoinedOn)) / 3600),{server.server.id}) FROM UserJoinedGameServer + SELECT ROUND(SUM(TIME_TO_SEC(TIMEDIFF(UserJoinedGameServer.LeavedOn, UserJoinedGameServer.JoinedOn)) / 3600),2) FROM UserJoinedGameServer INNER JOIN Users ON UserJoinedGameServer.UserId = Users.UserId WHERE Users.ServerId = {server.server.id} AND UserJoinedGameServer.CreatedAt >= "{self._get_date(**server.kwargs)}"; diff --git a/bot/src/bot_graphql/queries/user_query.py b/bot/src/bot_graphql/queries/user_query.py index d503448c..219a1526 100644 --- a/bot/src/bot_graphql/queries/user_query.py +++ b/bot/src/bot_graphql/queries/user_query.py @@ -2,6 +2,7 @@ from cpl_core.database.context import DatabaseContextABC from cpl_discord.service import DiscordBotServiceABC from bot_core.abc.client_utils_abc import ClientUtilsABC +from bot_core.abc.permission_service_abc import PermissionServiceABC from bot_data.abc.achievement_repository_abc import AchievementRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import ( UserJoinedGameServerRepositoryABC, @@ -11,7 +12,6 @@ from bot_data.abc.user_joined_voice_channel_repository_abc import ( UserJoinedVoiceChannelRepositoryABC, ) from bot_data.abc.user_warnings_repository_abc import UserWarningsRepositoryABC -from bot_data.model.user import User from bot_data.model.user_history import UserHistory from bot_graphql.abc.data_query_with_history_abc import DataQueryWithHistoryABC from bot_graphql.filter.achievement_filter import AchievementFilter @@ -22,7 +22,6 @@ from bot_graphql.filter.user_joined_voice_channel_filter import ( ) from bot_graphql.filter.user_warning_filter import UserWarningFilter from modules.level.service.level_service import LevelService -from bot_core.abc.permission_service_abc import PermissionServiceABC class UserQuery(DataQueryWithHistoryABC): @@ -50,15 +49,16 @@ class UserQuery(DataQueryWithHistoryABC): self._permissions = permissions self._achievements = achievements - self.set_field("id", self.resolve_id) - self.set_field("discordId", self.resolve_discord_id) - self.set_field("name", self.resolve_name) - self.set_field("xp", self.resolve_xp) + self.set_field("id", lambda user, *_: user.id) + self.set_field("discordId", lambda user, *_: user.discord_id) + self.set_field("name", lambda user, *_: user.name) + self.set_field("xp", lambda user, *_: user.xp) self.set_field("messageCount", lambda x, *_: x.message_count) self.set_field("reactionCount", lambda x, *_: x.reaction_count) self.set_field("birthday", lambda x, *_: None if x.birthday is None else x.birthday.strftime("%d.%m.%Y")) - self.set_field("ontime", self.resolve_ontime) - self.set_field("level", self.resolve_level) + self.set_field("ontime", lambda user, *_: user.ontime) + self.set_field("gameOntime", lambda user, *_: user.game_ontime) + self.set_field("level", lambda user, *_: user.level) self.add_collection( "joinedServer", lambda user, *_: self._ujs.get_user_joined_servers_by_user_id(user.id), @@ -85,37 +85,5 @@ class UserQuery(DataQueryWithHistoryABC): UserWarningFilter, ) - self.set_field("server", self.resolve_server) - self.set_field("leftServer", self.resolve_left_server) - - @staticmethod - def resolve_id(user: User, *_): - return user.id - - @staticmethod - def resolve_discord_id(user: User, *_): - return user.discord_id - - @staticmethod - def resolve_name(user: User, *_): - return user.name - - @staticmethod - def resolve_xp(user: User, *_): - return user.xp - - @staticmethod - def resolve_ontime(user: User, *_): - return user.ontime - - @staticmethod - def resolve_level(user: User, *_): - return user.level - - @staticmethod - def resolve_server(user: User, *_): - return user.server - - @staticmethod - def resolve_left_server(user: User, *_): - return user.left_server + self.set_field("server", lambda user, *_: user.server) + self.set_field("leftServer", lambda user, *_: user.left_server) diff --git a/web/src/app/models/data/user.model.ts b/web/src/app/models/data/user.model.ts index 5dcd05aa..37c00950 100644 --- a/web/src/app/models/data/user.model.ts +++ b/web/src/app/models/data/user.model.ts @@ -16,6 +16,7 @@ export interface User extends DataWithHistory { reactionCount?: number; birthday?: string; ontime?: number; + gameOntime?: number; level?: Level; server?: Server; leftServer?: boolean; diff --git a/web/src/app/models/graphql/queries.model.ts b/web/src/app/models/graphql/queries.model.ts index aacf980d..bf803943 100644 --- a/web/src/app/models/graphql/queries.model.ts +++ b/web/src/app/models/graphql/queries.model.ts @@ -361,6 +361,7 @@ export class Queries { name xp ontime + gameOntime level { id name @@ -386,6 +387,7 @@ export class Queries { reactionCount birthday ontime + gameOntime level { id name diff --git a/web/src/app/modules/view/server/members/members.component.html b/web/src/app/modules/view/server/members/members.component.html index a5b465b2..b633ae05 100644 --- a/web/src/app/modules/view/server/members/members.component.html +++ b/web/src/app/modules/view/server/members/members.component.html @@ -3,7 +3,8 @@