From 2ef4d079ed9ef7c2e5cd818896c0673bb9c3710c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 16:55:35 +0100 Subject: [PATCH] Added minecraft id #181 --- .../user_joined_game_server_migration.py | 15 ++++++++ kdb-bot/src/bot_data/model/user.py | 10 +++++- .../service/user_repository_service.py | 35 ++++++++++++++----- kdb-bot/src/bot_graphql/filter/user_filter.py | 7 ++++ kdb-bot/src/bot_graphql/model/user.gql | 2 ++ kdb-bot/src/bot_graphql/queries/user_query.py | 5 +++ 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py index 1b5cfb85..e29854aa 100644 --- a/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py +++ b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py @@ -33,5 +33,20 @@ class UserJoinedGameServerMigration(MigrationABC): ) ) + self._cursor.execute( + str( + f""" + ALTER TABLE Users ADD MinecraftId VARCHAR(255) NULL AFTER XP; + """ + ) + ) + def downgrade(self): self._cursor.execute("DROP TABLE `UserJoinedGameServer`;") + self._cursor.execute( + str( + f""" + ALTER TABLE Users DROP COLUMN MinecraftId; + """ + ) + ) diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index ab97ecd1..7674e78b 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -11,6 +11,7 @@ class User(TableABC): self, dc_id: int, xp: int, + minecraft_id: Optional[str], server: Optional[Server], created_at: datetime = None, modified_at: datetime = None, @@ -19,6 +20,7 @@ class User(TableABC): self._user_id = id self._discord_id = dc_id self._xp = xp + self._minecraft_id = minecraft_id self._server = server TableABC.__init__(self) @@ -42,6 +44,10 @@ class User(TableABC): self._modified_at = datetime.now().isoformat() self._xp = value + @property + def minecraft_id(self) -> Optional[str]: + return self._minecraft_id + @property def server(self) -> Optional[Server]: return self._server @@ -96,10 +102,11 @@ class User(TableABC): return str( f""" INSERT INTO `Users` ( - `DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt` + `DiscordId`, `XP`, `MinecraftId`, `ServerId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._discord_id}, {self._xp}, + '{"NULL" if self._minecraft_id is None else self._minecraft_id}', {self._server.server_id}, '{self._created_at}', '{self._modified_at}' @@ -113,6 +120,7 @@ class User(TableABC): f""" UPDATE `Users` SET `XP` = {self._xp}, + `MinecraftId` = '{"NULL" if self._minecraft_id is None else self._minecraft_id}', `LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user_id}; """ diff --git a/kdb-bot/src/bot_data/service/user_repository_service.py b/kdb-bot/src/bot_data/service/user_repository_service.py index 9d44c0ba..44207824 100644 --- a/kdb-bot/src/bot_data/service/user_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_repository_service.py @@ -33,7 +33,10 @@ class UserRepositoryService(UserRepositoryABC): User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) ) @@ -47,7 +50,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) @@ -62,7 +68,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) @@ -78,7 +87,10 @@ class UserRepositoryService(UserRepositoryABC): User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) ) @@ -97,7 +109,10 @@ class UserRepositoryService(UserRepositoryABC): User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) ) @@ -114,7 +129,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) @@ -132,9 +150,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), - result[4], + result[3], + self._servers.get_server_by_id(result[4]), result[5], + result[6], id=result[0], ) diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index b0028674..8b9bdb74 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -30,6 +30,7 @@ class UserFilter(FilterABC): self._discord_id = None self._name = None self._xp = None + self._minecraft_id = None self._ontime = None self._level: Optional[LevelFilter] = None @@ -46,6 +47,9 @@ class UserFilter(FilterABC): if "xp" in values: self._xp = int(values["xp"]) + if "minecraftId" in values: + self._minecraft_id = values["minecraftId"] + if "ontime" in values: self._ontime = int(values["ontime"]) @@ -75,6 +79,9 @@ class UserFilter(FilterABC): if self._xp is not None: query = query.where(lambda x: x.xp == self._xp) + if self._minecraft_id is not None: + query = query.where(lambda x: x.minecraft_id == self._minecraft_id) + if self._ontime is not None: query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index b7cf5fdf..853b1ad4 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -3,6 +3,7 @@ type User implements TableQuery { discordId: String name: String xp: Int + minecraftId: String ontime: Float level: Level @@ -26,6 +27,7 @@ input UserFilter { discordId: String name: String xp: Int + minecraftId: String ontime: Float level: LevelFilter server: ServerFilter diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index 1716d488..ddcda7b9 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -35,6 +35,7 @@ class UserQuery(DataQueryABC): 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("minecraftId", self.resolve_minecraft_id) self.set_field("ontime", self.resolve_ontime) self.set_field("level", self.resolve_level) self.add_collection( @@ -71,6 +72,10 @@ class UserQuery(DataQueryABC): def resolve_xp(user: User, *_): return user.xp + @staticmethod + def resolve_minecraft_id(user: User, *_): + return user.minecraft_id + def resolve_ontime(self, user: User, *_): return self._client_utils.get_ontime_for_user(user)