From a8dad6b2231790917a9ee06f82d50fe2643ad0dd Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 4 Mar 2023 19:34:11 +0100 Subject: [PATCH 1/5] Added GameServer data model #238 --- .../bot_data/abc/api_key_repository_abc.py | 4 + .../user_joined_game_server_migration.py | 33 +++-- kdb-bot/src/bot_data/model/api_key.py | 13 ++ kdb-bot/src/bot_data/model/game_server.py | 124 ++++++++++++++++++ kdb-bot/src/bot_data/model/user.py | 14 +- .../bot_data/model/user_joined_game_server.py | 9 +- .../service/api_key_repository_service.py | 4 + ...r_joined_game_server_repository_service.py | 44 +++++-- .../service/user_repository_service.py | 86 +++--------- .../database/database_on_ready_event.py | 2 +- 10 files changed, 222 insertions(+), 111 deletions(-) create mode 100644 kdb-bot/src/bot_data/model/game_server.py diff --git a/kdb-bot/src/bot_data/abc/api_key_repository_abc.py b/kdb-bot/src/bot_data/abc/api_key_repository_abc.py index c21d22df..aaa22dbe 100644 --- a/kdb-bot/src/bot_data/abc/api_key_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/api_key_repository_abc.py @@ -18,6 +18,10 @@ class ApiKeyRepositoryABC(ABC): def get_api_key(self, identifier: str, key: str) -> ApiKey: pass + @abstractmethod + def get_api_key_by_id(self, id: int) -> ApiKey: + pass + @abstractmethod def get_api_key_by_key(self, key: str) -> ApiKey: pass 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 e29854aa..6e1588b2 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 @@ -18,15 +18,15 @@ class UserJoinedGameServerMigration(MigrationABC): self._cursor.execute( str( f""" - CREATE TABLE IF NOT EXISTS `UserJoinedGameServer` ( + CREATE TABLE IF NOT EXISTS `GameServers` ( `Id` BIGINT NOT NULL AUTO_INCREMENT, - `UserId` BIGINT NOT NULL, - `GameServer` VARCHAR(255) NOT NULL, - `JoinedOn` DATETIME(6) NOT NULL, - `LeavedOn` DATETIME(6), + `Name` VARCHAR(255) NOT NULL, + `ServerId` BIGINT NOT NULL, + `ApiKeyId` BIGINT NOT NULL, `CreatedAt` DATETIME(6), `LastModifiedAt` DATETIME(6), - FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`), + FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`), + FOREIGN KEY (`ApiKeyId`) REFERENCES ApiKeys(`Id`), PRIMARY KEY(`Id`) ); """ @@ -36,17 +36,22 @@ class UserJoinedGameServerMigration(MigrationABC): self._cursor.execute( str( f""" - ALTER TABLE Users ADD MinecraftId VARCHAR(255) NULL AFTER XP; + CREATE TABLE IF NOT EXISTS `UserJoinedGameServer` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `UserId` BIGINT NOT NULL, + `GameServerId` BIGINT NOT NULL, + `JoinedOn` DATETIME(6) NOT NULL, + `LeavedOn` DATETIME(6), + `CreatedAt` DATETIME(6), + `LastModifiedAt` DATETIME(6), + FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`), + FOREIGN KEY (`GameServerId`) REFERENCES GameServers(`Id`), + PRIMARY KEY(`Id`) + ); """ ) ) def downgrade(self): + self._cursor.execute("DROP TABLE `GameServers`;") 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/api_key.py b/kdb-bot/src/bot_data/model/api_key.py index 39f69677..e4bc6e9c 100644 --- a/kdb-bot/src/bot_data/model/api_key.py +++ b/kdb-bot/src/bot_data/model/api_key.py @@ -25,6 +25,10 @@ class ApiKey(TableABC): self._created_at = created_at if created_at is not None else self._created_at self._modified_at = modified_at if modified_at is not None else self._modified_at + @property + def id(self) -> int: + return self._id + @property def identifier(self) -> str: return self._identifier @@ -55,6 +59,15 @@ class ApiKey(TableABC): """ ) + @staticmethod + def get_select_by_id(id: int) -> str: + return str( + f""" + SELECT * FROM `ApiKeys` + WHERE `Id` = {id}; + """ + ) + @staticmethod def get_select_by_key(key: str) -> str: return str( diff --git a/kdb-bot/src/bot_data/model/game_server.py b/kdb-bot/src/bot_data/model/game_server.py new file mode 100644 index 00000000..0afc1ab3 --- /dev/null +++ b/kdb-bot/src/bot_data/model/game_server.py @@ -0,0 +1,124 @@ +from datetime import datetime + +from cpl_core.database import TableABC + +from bot_data.model.api_key import ApiKey +from bot_data.model.server import Server + + +class GameServer(TableABC): + def __init__( + self, + name: str, + server: Server, + api_key: ApiKey, + created_at: datetime = None, + modified_at: datetime = None, + id=0, + ): + self._id = id + self._name = name + self._server = server + self._api_key = api_key + + TableABC.__init__(self) + self._created_at = created_at if created_at is not None else self._created_at + self._modified_at = modified_at if modified_at is not None else self._modified_at + + @property + def id(self) -> int: + return self._id + + @property + def name(self) -> str: + return self._name + + @name.setter + def name(self, value: str): + self._name = value + + @property + def server(self) -> Server: + return self._server + + @server.setter + def server(self, value: Server): + self._server = value + + @property + def api_key(self) -> ApiKey: + return self._api_key + + @api_key.setter + def api_key(self, value: ApiKey): + self._api_key = value + + @staticmethod + def get_select_all_string() -> str: + return str( + f""" + SELECT * FROM `GameServer`; + """ + ) + + @staticmethod + def get_select_by_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `GameServer` + WHERE `Id` = {id}; + """ + ) + + @staticmethod + def get_select_by_user_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `GameServer` + WHERE `UserId` = {id}; + """ + ) + + @property + def insert_string(self) -> str: + return str( + f""" + INSERT INTO `GameServer` ( + `Name`, `ServerId`, `ApiKeyId`, CreatedAt`, `LastModifiedAt` + ) VALUES ( + '{self._name}', + {self._server.id}, + {self._api_key.id}, + '{self._created_at}', + '{self._modified_at}' + ); + """ + ) + + @property + def udpate_string(self) -> str: + return str( + f""" + UPDATE `GameServer` + SET `LastModifiedAt` = '{self._modified_at}' + WHERE `Id` = {self._id}; + """ + ) + + @property + def delete_string(self) -> str: + return str( + f""" + DELETE FROM `GameServer` + WHERE `Id` = {self._id}; + """ + ) + + @staticmethod + def delete_by_user_id_string(id: int) -> str: + return str( + f""" + DELETE FROM `GameServer` + WHERE `UserId` = {id} + """ + ) diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index c153a979..2c4c7a3d 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -14,7 +14,6 @@ class User(TableABC): self, dc_id: int, xp: int, - minecraft_id: Optional[str], server: Optional[Server], created_at: datetime = None, modified_at: datetime = None, @@ -23,7 +22,6 @@ 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) @@ -77,14 +75,6 @@ class User(TableABC): levels: LevelService = services.get_service(LevelService) return levels.get_level(self) - @property - def minecraft_id(self) -> Optional[str]: - return self._minecraft_id - - @minecraft_id.setter - def minecraft_id(self, value: str): - self._minecraft_id = value - @property def server(self) -> Optional[Server]: return self._server @@ -150,11 +140,10 @@ class User(TableABC): return str( f""" INSERT INTO `Users` ( - `DiscordId`, `XP`, `MinecraftId`, `ServerId`, `CreatedAt`, `LastModifiedAt` + `DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._discord_id}, {self._xp}, - {"NULL" if self._minecraft_id is None else f"'{self._minecraft_id}'"}, {self._server.id}, '{self._created_at}', '{self._modified_at}' @@ -168,7 +157,6 @@ class User(TableABC): f""" UPDATE `Users` SET `XP` = {self._xp}, - `MinecraftId` = {"NULL" if self._minecraft_id is None else f"'{self._minecraft_id}'"}, `LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user_id}; """ diff --git a/kdb-bot/src/bot_data/model/user_joined_game_server.py b/kdb-bot/src/bot_data/model/user_joined_game_server.py index e25d6b1d..51fcbed2 100644 --- a/kdb-bot/src/bot_data/model/user_joined_game_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_game_server.py @@ -2,6 +2,7 @@ from datetime import datetime from cpl_core.database import TableABC +from bot_data.model.game_server import GameServer from bot_data.model.user import User @@ -9,7 +10,7 @@ class UserJoinedGameServer(TableABC): def __init__( self, user: User, - game_server: str, + game_server: GameServer, joined_on: datetime, leaved_on: datetime = None, created_at: datetime = None, @@ -35,7 +36,7 @@ class UserJoinedGameServer(TableABC): return self._user @property - def game_server(self) -> str: + def game_server(self) -> GameServer: return self._game_server @property @@ -101,10 +102,10 @@ class UserJoinedGameServer(TableABC): return str( f""" INSERT INTO `UserJoinedGameServer` ( - `UserId`, `GameServer`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` + `UserId`, `GameServerId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._user.id}, - '{self._game_server}', + '{self._game_server.id}', '{self._joined_on}', {"NULL" if self._leaved_on is None else f"'{self._leaved_on}'"}, '{self._created_at}', diff --git a/kdb-bot/src/bot_data/service/api_key_repository_service.py b/kdb-bot/src/bot_data/service/api_key_repository_service.py index e7919373..d4f57dfc 100644 --- a/kdb-bot/src/bot_data/service/api_key_repository_service.py +++ b/kdb-bot/src/bot_data/service/api_key_repository_service.py @@ -56,6 +56,10 @@ class ApiKeyRepositoryService(ApiKeyRepositoryABC): self._logger.trace(__name__, f"Send SQL command: {ApiKey.get_select_string(identifier, key)}") return self._api_key_from_result(self._context.select(ApiKey.get_select_string(identifier, key))[0]) + def get_api_key_by_id(self, id: int) -> ApiKey: + self._logger.trace(__name__, f"Send SQL command: {ApiKey.get_select_by_id(id)}") + return self._api_key_from_result(self._context.select(ApiKey.get_select_by_id(id))[0]) + def get_api_key_by_key(self, key: str) -> ApiKey: self._logger.trace(__name__, f"Send SQL command: {ApiKey.get_select_by_key(key)}") return self._api_key_from_result(self._context.select(ApiKey.get_select_by_key(key))[0]) diff --git a/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py index 4d8099f6..131e5802 100644 --- a/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py @@ -4,10 +4,13 @@ from cpl_core.database.context import DatabaseContextABC from cpl_query.extension import List from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import ( UserJoinedGameServerRepositoryABC, ) from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.game_server import GameServer from bot_data.model.user_joined_game_server import UserJoinedGameServer @@ -17,14 +20,44 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): logger: DatabaseLogger, db_context: DatabaseContextABC, users: UserRepositoryABC, + servers: ServerRepositoryABC, + api_keys: ApiKeyRepositoryABC, ): self._logger = logger self._context = db_context self._users = users + self._servers = servers + self._api_keys = api_keys UserJoinedGameServerRepositoryABC.__init__(self) + def _game_server_from_id(self, id: int): + self._logger.trace( + __name__, + f"Send SQL command: {GameServer.get_select_by_id_string(id)}", + ) + result = self._context.select(GameServer.get_select_by_id_string(id))[0] + + return GameServer( + result[1], + self._servers.get_server_by_id(result[2]), + self._api_keys.get_api_key_by_id(result[3]), + result[4], + result[5], + id=result[0], + ) + + def _from_result(self, result: tuple) -> UserJoinedGameServer: + return UserJoinedGameServer( + self._users.get_user_by_id(result[1]), + self._game_server_from_id(result[2]), + result[3], + result[4], + result[5], + id=result[0], + ) + def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]: joins = List(UserJoinedGameServer) self._logger.trace( @@ -34,16 +67,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): results = self._context.select(UserJoinedGameServer.get_select_all_string()) for result in results: self._logger.trace(__name__, f"Get user-joined-game-server with id {result[0]}") - joins.append( - UserJoinedGameServer( - self._users.get_user_by_id(result[1]), - result[2], - result[3], - result[4], - result[5], - id=result[0], - ) - ) + joins.append(self._from_result(result)) return joins 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 44207824..adc9685c 100644 --- a/kdb-bot/src/bot_data/service/user_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_repository_service.py @@ -23,23 +23,23 @@ class UserRepositoryService(UserRepositoryABC): UserRepositoryABC.__init__(self) + def _from_result(self, result: tuple): + return User( + result[1], + result[2], + self._servers.get_server_by_id(result[3]), + result[4], + result[5], + id=result[0], + ) + def get_users(self) -> List[User]: users = List(User) self._logger.trace(__name__, f"Send SQL command: {User.get_select_all_string()}") results = self._context.select(User.get_select_all_string()) for result in results: self._logger.trace(__name__, f"Get user with id {result[0]}") - users.append( - User( - result[1], - result[2], - result[3], - self._servers.get_server_by_id(result[4]), - result[5], - result[6], - id=result[0], - ) - ) + users.append(self._from_result(result)) return users @@ -47,15 +47,7 @@ class UserRepositoryService(UserRepositoryABC): self._logger.trace(__name__, f"Send SQL command: {User.get_select_by_id_string(id)}") result = self._context.select(User.get_select_by_id_string(id))[0] - return User( - result[1], - result[2], - result[3], - self._servers.get_server_by_id(result[4]), - result[5], - result[6], - id=result[0], - ) + return self._from_result(result) def find_user_by_id(self, id: int) -> Optional[User]: self._logger.trace(__name__, f"Send SQL command: {User.get_select_by_id_string(id)}") @@ -65,15 +57,7 @@ class UserRepositoryService(UserRepositoryABC): result = result[0] - return User( - result[1], - result[2], - result[3], - self._servers.get_server_by_id(result[4]), - result[5], - result[6], - id=result[0], - ) + return self._from_result(result) def get_users_by_discord_id(self, discord_id: int) -> List[User]: users = List(User) @@ -83,17 +67,7 @@ class UserRepositoryService(UserRepositoryABC): ) results = self._context.select(User.get_select_by_discord_id_string(discord_id)) for result in results: - users.append( - User( - result[1], - result[2], - result[3], - self._servers.get_server_by_id(result[4]), - result[5], - result[6], - id=result[0], - ) - ) + users.append(self._from_result(result)) return users @@ -105,17 +79,7 @@ class UserRepositoryService(UserRepositoryABC): ) results = self._context.select(User.get_select_by_server_id_string(server_id)) for result in results: - users.append( - User( - result[1], - result[2], - result[3], - self._servers.get_server_by_id(result[4]), - result[5], - result[6], - id=result[0], - ) - ) + users.append(self._from_result(result)) return users @@ -126,15 +90,7 @@ class UserRepositoryService(UserRepositoryABC): ) result = self._context.select(User.get_select_by_discord_id_and_server_id_string(discord_id, server_id))[0] - return User( - result[1], - result[2], - result[3], - self._servers.get_server_by_id(result[4]), - result[5], - result[6], - id=result[0], - ) + return self._from_result(result) def find_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[User]: self._logger.trace( @@ -147,15 +103,7 @@ class UserRepositoryService(UserRepositoryABC): result = result[0] - return User( - result[1], - result[2], - result[3], - self._servers.get_server_by_id(result[4]), - result[5], - result[6], - id=result[0], - ) + return self._from_result(result) def add_user(self, user: User): self._logger.trace(__name__, f"Send SQL command: {user.insert_string}") diff --git a/kdb-bot/src/modules/database/database_on_ready_event.py b/kdb-bot/src/modules/database/database_on_ready_event.py index 3b66f5e0..d0d7c78a 100644 --- a/kdb-bot/src/modules/database/database_on_ready_event.py +++ b/kdb-bot/src/modules/database/database_on_ready_event.py @@ -193,7 +193,7 @@ class DatabaseOnReadyEvent(OnReadyABC): self._logger.warn(__name__, f"User not found in database: {u.id}") self._logger.debug(__name__, f"Add user: {u.id}") - self._users.add_user(User(u.id, 0, None, server)) + self._users.add_user(User(u.id, 0, server)) self._db_context.save_changes() self._logger.debug(__name__, f"Added User: {u.id}") From 91b2cf754651f5dd81667b824e2c4e370ae70c1a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 4 Mar 2023 20:24:49 +0100 Subject: [PATCH 2/5] Added GameServer handling & commands #238 --- kdb-bot/src/bot/translation/de.json | 17 ++ .../abc/game_server_repository_abc.py | 39 +++++ .../user_joined_game_server_repository_abc.py | 4 + kdb-bot/src/bot_data/data_module.py | 3 + kdb-bot/src/bot_data/model/game_server.py | 34 +++- .../service/game_server_repository_service.py | 89 +++++++++++ ...r_joined_game_server_repository_service.py | 28 +--- kdb-bot/src/bot_graphql/abc/query_abc.py | 2 +- .../filter/auto_role_rule_filter.py | 4 +- .../queries/user_joined_game_server_query.py | 2 +- kdb-bot/src/modules/base/base_module.py | 2 + .../modules/base/command/game_server_group.py | 151 ++++++++++++++++++ 12 files changed, 339 insertions(+), 36 deletions(-) create mode 100644 kdb-bot/src/bot_data/abc/game_server_repository_abc.py create mode 100644 kdb-bot/src/bot_data/service/game_server_repository_service.py create mode 100644 kdb-bot/src/modules/base/command/game_server_group.py diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index a377bcb4..889bf26a 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -147,6 +147,23 @@ "base": { "afk_command_channel_missing_message": "Zu unfähig einem Sprachkanal beizutreten?", "afk_command_move_message": "Ich verschiebe dich ja schon... (◔_◔)", + "game_server": { + "error": { + "nothing_found": "Keine Level Einträge gefunden." + }, + "list": { + "title": "Gameserver", + "description": "Konfigurierte Gameserver:", + "name": "Name", + "api_key": "API Key" + }, + "add": { + "success": "Gameserver {} wurde hinzugefügt :)" + }, + "remove": { + "success": "Gameserver wurde entfernt :D" + } + }, "goodbye_message": "Schade, dass du uns so schnell verlässt :(", "info": { "description": "Informationen über mich", diff --git a/kdb-bot/src/bot_data/abc/game_server_repository_abc.py b/kdb-bot/src/bot_data/abc/game_server_repository_abc.py new file mode 100644 index 00000000..50e6fe7a --- /dev/null +++ b/kdb-bot/src/bot_data/abc/game_server_repository_abc.py @@ -0,0 +1,39 @@ +from abc import ABC, abstractmethod + +from cpl_query.extension import List + +from bot_data.model.game_server import GameServer + + +class GameServerRepositoryABC(ABC): + @abstractmethod + def __init__(self): + pass + + @abstractmethod + def get_game_servers(self) -> List[GameServer]: + pass + + @abstractmethod + def get_game_server_by_id(self, id: int) -> GameServer: + pass + + @abstractmethod + def get_game_servers_by_server_id(self, id: int) -> List[GameServer]: + pass + + @abstractmethod + def get_game_server_by_api_key_id(self, id: int) -> GameServer: + pass + + @abstractmethod + def add_game_server(self, game_server: GameServer): + pass + + @abstractmethod + def update_game_server(self, game_server: GameServer): + pass + + @abstractmethod + def delete_game_server(self, game_server: GameServer): + pass diff --git a/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py b/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py index 97a0358b..266e90b5 100644 --- a/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py @@ -11,6 +11,10 @@ class UserJoinedGameServerRepositoryABC(ABC): def __init__(self): pass + # @abstractmethod + # def get_game_server_by_api_key(self) -> List[UserJoinedGameServer]: + # pass + @abstractmethod def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]: pass diff --git a/kdb-bot/src/bot_data/data_module.py b/kdb-bot/src/bot_data/data_module.py index f1bf1e41..d180e97e 100644 --- a/kdb-bot/src/bot_data/data_module.py +++ b/kdb-bot/src/bot_data/data_module.py @@ -9,6 +9,7 @@ from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC @@ -26,6 +27,7 @@ from bot_data.service.api_key_repository_service import ApiKeyRepositoryService from bot_data.service.auth_user_repository_service import AuthUserRepositoryService from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService from bot_data.service.client_repository_service import ClientRepositoryService +from bot_data.service.game_server_repository_service import GameServerRepositoryService from bot_data.service.known_user_repository_service import KnownUserRepositoryService from bot_data.service.level_repository_service import LevelRepositoryService from bot_data.service.seeder_service import SeederService @@ -68,5 +70,6 @@ class DataModule(ModuleABC): UserMessageCountPerHourRepositoryABC, UserMessageCountPerHourRepositoryService, ) + services.add_transient(GameServerRepositoryABC, GameServerRepositoryService) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_data/model/game_server.py b/kdb-bot/src/bot_data/model/game_server.py index 0afc1ab3..ff886044 100644 --- a/kdb-bot/src/bot_data/model/game_server.py +++ b/kdb-bot/src/bot_data/model/game_server.py @@ -57,7 +57,7 @@ class GameServer(TableABC): def get_select_all_string() -> str: return str( f""" - SELECT * FROM `GameServer`; + SELECT * FROM `GameServers`; """ ) @@ -65,16 +65,34 @@ class GameServer(TableABC): def get_select_by_id_string(id: int) -> str: return str( f""" - SELECT * FROM `GameServer` + SELECT * FROM `GameServers` WHERE `Id` = {id}; """ ) + @staticmethod + def get_select_by_api_key_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `GameServers` + WHERE `ApiKeyId` = {id}; + """ + ) + + @staticmethod + def get_select_by_server_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `GameServers` + WHERE `ServerId` = {id}; + """ + ) + @staticmethod def get_select_by_user_id_string(id: int) -> str: return str( f""" - SELECT * FROM `GameServer` + SELECT * FROM `GameServers` WHERE `UserId` = {id}; """ ) @@ -83,8 +101,8 @@ class GameServer(TableABC): def insert_string(self) -> str: return str( f""" - INSERT INTO `GameServer` ( - `Name`, `ServerId`, `ApiKeyId`, CreatedAt`, `LastModifiedAt` + INSERT INTO `GameServers` ( + `Name`, `ServerId`, `ApiKeyId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( '{self._name}', {self._server.id}, @@ -99,7 +117,7 @@ class GameServer(TableABC): def udpate_string(self) -> str: return str( f""" - UPDATE `GameServer` + UPDATE `GameServers` SET `LastModifiedAt` = '{self._modified_at}' WHERE `Id` = {self._id}; """ @@ -109,7 +127,7 @@ class GameServer(TableABC): def delete_string(self) -> str: return str( f""" - DELETE FROM `GameServer` + DELETE FROM `GameServers` WHERE `Id` = {self._id}; """ ) @@ -118,7 +136,7 @@ class GameServer(TableABC): def delete_by_user_id_string(id: int) -> str: return str( f""" - DELETE FROM `GameServer` + DELETE FROM `GameServers` WHERE `UserId` = {id} """ ) diff --git a/kdb-bot/src/bot_data/service/game_server_repository_service.py b/kdb-bot/src/bot_data/service/game_server_repository_service.py new file mode 100644 index 00000000..20f89433 --- /dev/null +++ b/kdb-bot/src/bot_data/service/game_server_repository_service.py @@ -0,0 +1,89 @@ +from cpl_core.database.context import DatabaseContextABC +from cpl_query.extension import List + +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.game_server import GameServer + + +class GameServerRepositoryService(GameServerRepositoryABC): + def __init__( + self, + logger: DatabaseLogger, + db_context: DatabaseContextABC, + servers: ServerRepositoryABC, + api_keys: ApiKeyRepositoryABC, + ): + self._logger = logger + + self._context = db_context + self._servers = servers + self._api_keys = api_keys + + GameServerRepositoryABC.__init__(self) + + def _from_result(self, result: tuple): + return GameServer( + result[1], + self._servers.get_server_by_id(result[2]), + self._api_keys.get_api_key_by_id(result[3]), + result[4], + result[5], + id=result[0], + ) + + def get_game_servers(self) -> List[GameServer]: + game_servers = List(GameServer) + self._logger.trace( + __name__, + f"Send SQL command: {GameServer.get_select_all_string()}", + ) + results = self._context.select(GameServer.get_select_all_string()) + for result in results: + self._logger.trace(__name__, f"Get user-joined-game-server with id {result[0]}") + game_servers.append(self._from_result(result)) + + return game_servers + + def get_game_servers_by_server_id(self, id: int) -> List[GameServer]: + game_servers = List(GameServer) + self._logger.trace( + __name__, + f"Send SQL command: {GameServer.get_select_by_server_id_string(id)}", + ) + results = self._context.select(GameServer.get_select_by_server_id_string(id)) + for result in results: + self._logger.trace(__name__, f"Get user-joined-game-server with id {result[0]}") + game_servers.append(self._from_result(result)) + + return game_servers + + def get_game_server_by_id(self, id: int) -> GameServer: + self._logger.trace( + __name__, + f"Send SQL command: {GameServer.get_select_by_id_string(id)}", + ) + result = self._context.select(GameServer.get_select_by_id_string(id))[0] + return self._from_result(result) + + def get_game_server_by_api_key_id(self, id: int) -> GameServer: + self._logger.trace( + __name__, + f"Send SQL command: {GameServer.get_select_by_api_key_id_string(id)}", + ) + result = self._context.select(GameServer.get_select_by_api_key_id_string(id))[0] + return self._from_result(result) + + def add_game_server(self, game_server: GameServer): + self._logger.trace(__name__, f"Send SQL command: {game_server.insert_string}") + self._context.cursor.execute(game_server.insert_string) + + def update_game_server(self, game_server: GameServer): + self._logger.trace(__name__, f"Send SQL command: {game_server.udpate_string}") + self._context.cursor.execute(game_server.udpate_string) + + def delete_game_server(self, game_server: GameServer): + self._logger.trace(__name__, f"Send SQL command: {game_server.delete_string}") + self._context.cursor.execute(game_server.delete_string) diff --git a/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py index 131e5802..a166ce55 100644 --- a/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py @@ -4,13 +4,11 @@ from cpl_core.database.context import DatabaseContextABC from cpl_query.extension import List from bot_core.logging.database_logger import DatabaseLogger -from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC -from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import ( UserJoinedGameServerRepositoryABC, ) from bot_data.abc.user_repository_abc import UserRepositoryABC -from bot_data.model.game_server import GameServer from bot_data.model.user_joined_game_server import UserJoinedGameServer @@ -20,38 +18,20 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): logger: DatabaseLogger, db_context: DatabaseContextABC, users: UserRepositoryABC, - servers: ServerRepositoryABC, - api_keys: ApiKeyRepositoryABC, + game_servers: GameServerRepositoryABC, ): self._logger = logger self._context = db_context self._users = users - self._servers = servers - self._api_keys = api_keys + self._game_servers = game_servers UserJoinedGameServerRepositoryABC.__init__(self) - def _game_server_from_id(self, id: int): - self._logger.trace( - __name__, - f"Send SQL command: {GameServer.get_select_by_id_string(id)}", - ) - result = self._context.select(GameServer.get_select_by_id_string(id))[0] - - return GameServer( - result[1], - self._servers.get_server_by_id(result[2]), - self._api_keys.get_api_key_by_id(result[3]), - result[4], - result[5], - id=result[0], - ) - def _from_result(self, result: tuple) -> UserJoinedGameServer: return UserJoinedGameServer( self._users.get_user_by_id(result[1]), - self._game_server_from_id(result[2]), + self._game_servers.get_game_server_by_id(result[2]), result[3], result[4], result[5], diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index 22a4213b..a90ccfd3 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -82,7 +82,7 @@ class QueryABC(ObjectType): break elif type(element) == AutoRoleRule: - element: AutoRole = element.auto_role + element: AutoRole = element.game_server for u in user.users: u: User = u guild = bot.get_guild(u.server.discord_id) diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index 01a90c66..b9571d8f 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -57,7 +57,7 @@ class AutoRoleRuleFilter(FilterABC): query = query.where(get_role_name) if self._auto_role is not None: - auto_roles = self._auto_role.filter(query.select(lambda x: x.auto_role)).select(lambda x: x.id) - query = query.where(lambda x: x.auto_role.id in auto_roles) + auto_roles = self._auto_role.filter(query.select(lambda x: x.game_server)).select(lambda x: x.id) + query = query.where(lambda x: x.game_server.id in auto_roles) return query diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py index 6e29ec7d..854419e5 100644 --- a/kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py @@ -22,7 +22,7 @@ class UserJoinedGameServerQuery(DataQueryABC): @staticmethod def resolve_game_server(x: UserJoinedGameServer, *_): - return x.game_server + return x.game_server.name @staticmethod def resolve_user(x: UserJoinedGameServer, *_): diff --git a/kdb-bot/src/modules/base/base_module.py b/kdb-bot/src/modules/base/base_module.py index e83e2cf4..4d8c4f2d 100644 --- a/kdb-bot/src/modules/base/base_module.py +++ b/kdb-bot/src/modules/base/base_module.py @@ -8,6 +8,7 @@ from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.base.abc.base_helper_abc import BaseHelperABC from modules.base.command.afk_command import AFKCommand +from modules.base.command.game_server_group import GameServerGroup from modules.base.command.help_command import HelpCommand from modules.base.command.info_command import InfoCommand from modules.base.command.mass_move_command import MassMoveCommand @@ -66,6 +67,7 @@ class BaseModule(ModuleABC): self._dc.add_command(UserGroup) self._dc.add_command(RegisterGroup) self._dc.add_command(UnregisterGroup) + self._dc.add_command(GameServerGroup) # events self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) diff --git a/kdb-bot/src/modules/base/command/game_server_group.py b/kdb-bot/src/modules/base/command/game_server_group.py new file mode 100644 index 00000000..7d3073be --- /dev/null +++ b/kdb-bot/src/modules/base/command/game_server_group.py @@ -0,0 +1,151 @@ +from typing import List as TList + +import discord +from cpl_core.database.context import DatabaseContextABC +from cpl_discord.command import DiscordCommandABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_translation import TranslatePipe +from discord import app_commands +from discord.ext import commands +from discord.ext.commands import Context + +from bot_core.abc.client_utils_abc import ClientUtilsABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks +from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.game_server import GameServer +from modules.permission.abc.permission_service_abc import PermissionServiceABC + + +class GameServerGroup(DiscordCommandABC): + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsABC, + translate: TranslatePipe, + servers: ServerRepositoryABC, + game_servers: GameServerRepositoryABC, + api_keys: ApiKeyRepositoryABC, + db: DatabaseContextABC, + permission_service: PermissionServiceABC, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._t = translate + self._servers = servers + self._game_servers = game_servers + self._api_keys = api_keys + self._db = db + self._permissions = permission_service + + self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}") + + @commands.hybrid_group(name="game-server") + @commands.guild_only() + async def game_server(self, ctx: Context): + pass + + @game_server.command(alias="game-servers") + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def list(self, ctx: Context, wait: int = None): + self._logger.debug(__name__, f"Received command game_server list {ctx}") + + if ctx.guild is None: + return + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + game_servers = self._game_servers.get_game_servers_by_server_id(server.id) + if game_servers.count() < 1: + await self._message_service.send_ctx_msg( + ctx, self._t.transform("modules.base.game_server.error.nothing_found") + ) + self._logger.trace(__name__, f"Finished command game_server list") + return + + game_server_name = "" + api_key = "" + for game_server in game_servers: + game_server: GameServer = game_server + game_server_name += f"\n{game_server.name}" + api_key += f"\n{game_server.api_key.identifier}" + + embed = discord.Embed( + title=self._t.transform("modules.base.game_server.list.title"), + description=self._t.transform("modules.base.game_server.list.description"), + color=int("ef9d0d", 16), + ) + embed.add_field( + name=self._t.transform("modules.base.game_server.list.name"), + value=game_server_name, + inline=True, + ) + embed.add_field(name=self._t.transform("modules.base.game_server.list.api_key"), value=api_key, inline=True) + + await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) + self._logger.trace(__name__, f"Finished command game_server list") + + @game_server.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_admin() + async def add(self, ctx: Context, name: str, api_key_id: int): + self._logger.debug(__name__, f"Received command game-server add {ctx}: {name} {api_key_id}") + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + api_key = self._api_keys.get_api_key_by_id(api_key_id) + game_server = GameServer(name, server, api_key) + + self._game_servers.add_game_server(game_server) + self._db.save_changes() + await self._message_service.send_ctx_msg( + ctx, + self._t.transform("modules.base.game_server.add.success").format(name), + ) + + self._logger.trace(__name__, f"Finished command game-server add") + + @add.autocomplete("api_key_id") + async def api_key_id_autocomplete( + self, interaction: discord.Interaction, current: str + ) -> TList[app_commands.Choice[str]]: + keys = self._api_keys.get_api_keys() + + return [ + app_commands.Choice(name=f"{key.identifier}: {key.key}", value=key.id) + for key in self._client_utils.get_auto_complete_list(keys, current, lambda x: x.key) + ] + + @game_server.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_admin() + async def remove(self, ctx: Context, id: int): + self._logger.debug(__name__, f"Received command game-server remove {ctx}: {id}") + + game_server = self._game_servers.get_game_server_by_id(id) + self._game_servers.delete_game_server(game_server) + self._db.save_changes() + await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.game_server.remove.success")) + + self._logger.trace(__name__, f"Finished command game-server remove") + + @remove.autocomplete("id") + async def id_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + server = self._servers.get_server_by_discord_id(interaction.guild.id) + game_servers = self._game_servers.get_game_servers_by_server_id(server.id) + + return [ + app_commands.Choice(name=gs.name, value=gs.id) + for gs in self._client_utils.get_auto_complete_list(game_servers, current, lambda x: x.name) + ] From 15dfc3f47c1a785cef88cea603fa69097e92e81e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 4 Mar 2023 20:51:34 +0100 Subject: [PATCH 3/5] Added GameServer & ApiKey logic #238 --- .../model/userJoinedGameServer.gql | 1 - .../user_joined_game_server_mutation.py | 34 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql index 44aa523b..6c4659a2 100644 --- a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql +++ b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql @@ -24,6 +24,5 @@ type UserJoinedGameServerMutation { } input UserJoinedGameServerInput { - gameServer: String! userId: ID! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py index fa7cb3bf..f3ef07f1 100644 --- a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py @@ -1,12 +1,19 @@ +import hashlib from datetime import datetime +from typing import Optional from cpl_core.database.context import DatabaseContextABC from cpl_core.logging import LoggerABC from cpl_discord.service import DiscordBotServiceABC +from flask import request +from bot_api.configuration.authentication_settings import AuthenticationSettings +from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.api_key import ApiKey from bot_data.model.user_joined_game_server import UserJoinedGameServer from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC @@ -23,9 +30,12 @@ class UserJoinedGameServerMutation(QueryABC): servers: ServerRepositoryABC, users: UserRepositoryABC, user_joined_game_servers: UserJoinedGameServerRepositoryABC, + api_keys: ApiKeyRepositoryABC, + game_servers: GameServerRepositoryABC, bot: DiscordBotServiceABC, db: DatabaseContextABC, permissions: PermissionService, + auth_settings: AuthenticationSettings, ): QueryABC.__init__(self, "UserJoinedGameServerMutation") @@ -34,13 +44,33 @@ class UserJoinedGameServerMutation(QueryABC): self._servers = servers self._users = users self._user_joined_game_servers = user_joined_game_servers + self._api_keys = api_keys + self._game_servers = game_servers self._bot = bot self._db = db self._permissions = permissions + self._auth_settings = auth_settings self.set_field("userJoined", self.resolve_user_joined) self.set_field("userLeaved", self.resolve_user_leaved) + def _get_api_key_str(self, api_key: ApiKey) -> str: + return hashlib.sha256( + f"{api_key.identifier}:{api_key.key}+{self._auth_settings.secret_key}".encode("utf-8") + ).hexdigest() + + def _get_api_key(self) -> Optional[ApiKey]: + authorization = request.headers.get("Authorization").split() + if len(authorization) != 2: + return None + + api_key_str = authorization[1] + return ( + self._api_keys.get_api_keys() + .where(lambda key: self._get_api_key_str(key) == api_key_str) + .first_or_default() + ) + def resolve_user_joined(self, *_, input: dict): user = self._users.get_user_by_id(input["userId"]) self._can_user_mutate_data(user.server, UserRoleEnum.admin) @@ -53,7 +83,9 @@ class UserJoinedGameServerMutation(QueryABC): ) return - new = UserJoinedGameServer(user, input["gameServer"], datetime.now()) + api_key = self._get_api_key() + game_server = self._game_servers.get_game_server_by_api_key_id(api_key.id) + new = UserJoinedGameServer(user, game_server, datetime.now()) self._user_joined_game_servers.add_user_joined_game_server(new) self._db.save_changes() From 34c362a391e8985212c4b3f0d880bc2538bf2af7 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 5 Mar 2023 13:47:30 +0100 Subject: [PATCH 4/5] Improved game server handling for ontime #238 --- .../abc/user_game_ident_repository_abc.py | 47 +++++++ .../user_joined_game_server_repository_abc.py | 4 - kdb-bot/src/bot_data/data_module.py | 3 + .../user_joined_game_server_migration.py | 20 +++ kdb-bot/src/bot_data/model/user.py | 12 ++ kdb-bot/src/bot_data/model/user_game_ident.py | 131 ++++++++++++++++++ .../user_game_ident_repository_service.py | 111 +++++++++++++++ ...r_joined_game_server_repository_service.py | 49 +------ kdb-bot/src/bot_graphql/model/user.gql | 2 - .../model/userJoinedGameServer.gql | 4 +- .../user_joined_game_server_mutation.py | 30 +++- kdb-bot/src/bot_graphql/queries/user_query.py | 5 - .../modules/base/command/register_group.py | 30 +++- .../modules/base/command/unregister_group.py | 40 ++++-- 14 files changed, 412 insertions(+), 76 deletions(-) create mode 100644 kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py create mode 100644 kdb-bot/src/bot_data/model/user_game_ident.py create mode 100644 kdb-bot/src/bot_data/service/user_game_ident_repository_service.py diff --git a/kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py b/kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py new file mode 100644 index 00000000..fbaa0831 --- /dev/null +++ b/kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py @@ -0,0 +1,47 @@ +from abc import ABC, abstractmethod + +from cpl_query.extension import List + +from bot_data.model.user_game_ident import UserGameIdent + + +class UserGameIdentRepositoryABC(ABC): + @abstractmethod + def __init__(self): + pass + + @abstractmethod + def get_user_game_idents(self) -> List[UserGameIdent]: + pass + + @abstractmethod + def get_user_game_ident_by_id(self, id: int) -> UserGameIdent: + pass + + @abstractmethod + def get_user_game_ident_by_ident(self, ident: str) -> UserGameIdent: + pass + + @abstractmethod + def find_user_game_ident_by_ident(self, ident: str) -> UserGameIdent: + pass + + @abstractmethod + def get_user_game_idents_by_user_id(self, user_id: int) -> List[UserGameIdent]: + pass + + @abstractmethod + def add_user_game_ident(self, user_game_ident: UserGameIdent): + pass + + @abstractmethod + def update_user_game_ident(self, user_game_ident: UserGameIdent): + pass + + @abstractmethod + def delete_user_game_ident(self, user_game_ident: UserGameIdent): + pass + + @abstractmethod + def delete_user_game_ident_by_user_id(self, user_id: int): + pass diff --git a/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py b/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py index 266e90b5..97a0358b 100644 --- a/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py @@ -11,10 +11,6 @@ class UserJoinedGameServerRepositoryABC(ABC): def __init__(self): pass - # @abstractmethod - # def get_game_server_by_api_key(self) -> List[UserJoinedGameServer]: - # pass - @abstractmethod def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]: pass diff --git a/kdb-bot/src/bot_data/data_module.py b/kdb-bot/src/bot_data/data_module.py index d180e97e..fb37c20d 100644 --- a/kdb-bot/src/bot_data/data_module.py +++ b/kdb-bot/src/bot_data/data_module.py @@ -13,6 +13,7 @@ from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import ( @@ -32,6 +33,7 @@ from bot_data.service.known_user_repository_service import KnownUserRepositorySe from bot_data.service.level_repository_service import LevelRepositoryService from bot_data.service.seeder_service import SeederService from bot_data.service.server_repository_service import ServerRepositoryService +from bot_data.service.user_game_ident_repository_service import UserGameIdentRepositoryService from bot_data.service.user_joined_game_server_repository_service import UserJoinedGameServerRepositoryService from bot_data.service.user_joined_server_repository_service import ( UserJoinedServerRepositoryService, @@ -71,5 +73,6 @@ class DataModule(ModuleABC): UserMessageCountPerHourRepositoryService, ) services.add_transient(GameServerRepositoryABC, GameServerRepositoryService) + services.add_transient(UserGameIdentRepositoryABC, UserGameIdentRepositoryService) services.add_transient(SeederService) 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 6e1588b2..22302a9f 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 @@ -52,6 +52,26 @@ class UserJoinedGameServerMigration(MigrationABC): ) ) + self._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `UserGameIdents` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `UserId` BIGINT NOT NULL, + `GameServerId` BIGINT NOT NULL, + `Ident` VARCHAR(255) NOT NULL, + `CreatedAt` DATETIME(6), + `LastModifiedAt` DATETIME(6), + FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`), + FOREIGN KEY (`GameServerId`) REFERENCES GameServers(`Id`), + CONSTRAINT UC_UserGameIdent UNIQUE (`GameServerId`,`Ident`), + PRIMARY KEY(`Id`) + ); + """ + ) + ) + def downgrade(self): self._cursor.execute("DROP TABLE `GameServers`;") self._cursor.execute("DROP TABLE `UserJoinedGameServer`;") + self._cursor.execute("DROP TABLE `UserGameIdents`;") diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index 2c4c7a3d..9f975550 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -4,6 +4,7 @@ from typing import Optional from cpl_core.database import TableABC from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List from bot_data.model.level import Level from bot_data.model.server import Server @@ -90,6 +91,17 @@ class User(TableABC): ujs: UserJoinedServerRepositoryABC = services.get_service(UserJoinedServerRepositoryABC) return ujs.find_active_user_joined_server_by_user_id(self.id) is None + @property + @ServiceProviderABC.inject + def game_idents( + self, + services: ServiceProviderABC, + ) -> List["UserGameIdent"]: + from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC + + game_idents_repo: UserGameIdentRepositoryABC = services.get_service(UserGameIdentRepositoryABC) + return game_idents_repo.get_user_game_idents_by_user_id(self.id) + @staticmethod def get_select_all_string() -> str: return str( diff --git a/kdb-bot/src/bot_data/model/user_game_ident.py b/kdb-bot/src/bot_data/model/user_game_ident.py new file mode 100644 index 00000000..6d6bf781 --- /dev/null +++ b/kdb-bot/src/bot_data/model/user_game_ident.py @@ -0,0 +1,131 @@ +from datetime import datetime + +from cpl_core.database import TableABC + +from bot_data.model.game_server import GameServer +from bot_data.model.user import User + + +class UserGameIdent(TableABC): + def __init__( + self, + user: User, + game_server: GameServer, + ident: str, + created_at: datetime = None, + modified_at: datetime = None, + id=0, + ): + self._id = id + self._user = user + self._game_server = game_server + self._ident = ident + + TableABC.__init__(self) + self._created_at = created_at if created_at is not None else self._created_at + self._modified_at = modified_at if modified_at is not None else self._modified_at + + @property + def id(self) -> int: + return self._id + + @property + def user(self) -> User: + return self._user + + @property + def game_server(self) -> GameServer: + return self._game_server + + @property + def ident(self) -> str: + return self._ident + + @staticmethod + def get_select_all_string() -> str: + return str( + f""" + SELECT * FROM `UserGameIdents`; + """ + ) + + @staticmethod + def get_select_by_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserGameIdents` + WHERE `Id` = {id}; + """ + ) + + @staticmethod + def get_select_by_ident_string(ident: str) -> str: + return str( + f""" + SELECT * FROM `UserGameIdents` + WHERE `Ident` = '{ident}'; + """ + ) + + @staticmethod + def get_select_by_user_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserGameIdents` + WHERE `UserId` = {id}; + """ + ) + + @staticmethod + def get_select_active_by_user_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserGameIdents` + WHERE `UserId` = {id} + AND `LeavedOn` IS NULL; + """ + ) + + @property + def insert_string(self) -> str: + return str( + f""" + INSERT INTO `UserGameIdents` ( + `UserId`, `GameServerId`, `Ident`, `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.id}, + '{self._game_server.id}', + '{self._ident}', + '{self._created_at}', + '{self._modified_at}' + ); + """ + ) + + @property + def udpate_string(self) -> str: + return str( + f""" + UPDATE `UserGameIdents` + SET `LastModifiedAt` = '{self._modified_at}' + WHERE `Id` = {self._id}; + """ + ) + + @property + def delete_string(self) -> str: + return str( + f""" + DELETE FROM `UserGameIdents` + WHERE `Id` = {self._id}; + """ + ) + + @staticmethod + def delete_by_user_id_string(id: int) -> str: + return str( + f""" + DELETE FROM `UserGameIdents` + WHERE `UserId` = {id} + """ + ) diff --git a/kdb-bot/src/bot_data/service/user_game_ident_repository_service.py b/kdb-bot/src/bot_data/service/user_game_ident_repository_service.py new file mode 100644 index 00000000..5967d78b --- /dev/null +++ b/kdb-bot/src/bot_data/service/user_game_ident_repository_service.py @@ -0,0 +1,111 @@ +from typing import Optional + +from cpl_core.database.context import DatabaseContextABC +from cpl_query.extension import List + +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC +from bot_data.abc.user_game_ident_repository_abc import ( + UserGameIdentRepositoryABC, +) +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.user_game_ident import UserGameIdent + + +class UserGameIdentRepositoryService(UserGameIdentRepositoryABC): + def __init__( + self, + logger: DatabaseLogger, + db_context: DatabaseContextABC, + users: UserRepositoryABC, + game_servers: GameServerRepositoryABC, + ): + self._logger = logger + self._context = db_context + + self._users = users + self._game_servers = game_servers + + UserGameIdentRepositoryABC.__init__(self) + + def _from_result(self, result: tuple) -> UserGameIdent: + return UserGameIdent( + self._users.get_user_by_id(result[1]), + self._game_servers.get_game_server_by_id(result[2]), + result[3], + result[4], + result[5], + id=result[0], + ) + + def get_user_game_idents(self) -> List[UserGameIdent]: + joins = List(UserGameIdent) + self._logger.trace( + __name__, + f"Send SQL command: {UserGameIdent.get_select_all_string()}", + ) + results = self._context.select(UserGameIdent.get_select_all_string()) + for result in results: + self._logger.trace(__name__, f"Get UserGameIdent with id {result[0]}") + joins.append(self._from_result(result)) + + return joins + + def get_user_game_ident_by_id(self, id: int) -> UserGameIdent: + self._logger.trace( + __name__, + f"Send SQL command: {UserGameIdent.get_select_by_id_string(id)}", + ) + result = self._context.select(UserGameIdent.get_select_by_id_string(id))[0] + return self._from_result(result) + + def get_user_game_ident_by_ident(self, ident: str) -> UserGameIdent: + self._logger.trace( + __name__, + f"Send SQL command: {UserGameIdent.get_select_by_ident_string(ident)}", + ) + result = self._context.select(UserGameIdent.get_select_by_ident_string(ident))[0] + return self._from_result(result) + + def find_user_game_ident_by_ident(self, ident: str) -> Optional[UserGameIdent]: + self._logger.trace( + __name__, + f"Send SQL command: {UserGameIdent.get_select_by_ident_string(ident)}", + ) + result = self._context.select(UserGameIdent.get_select_by_ident_string(ident)) + if len(result) == 0: + return None + + result = result[0] + return self._from_result(result) + + def get_user_game_idents_by_user_id(self, user_id: int) -> List[UserGameIdent]: + joins = List(UserGameIdent) + self._logger.trace( + __name__, + f"Send SQL command: {UserGameIdent.get_select_by_user_id_string(user_id)}", + ) + results = self._context.select(UserGameIdent.get_select_by_user_id_string(user_id)) + for result in results: + joins.append(self._from_result(result)) + + return joins + + def add_user_game_ident(self, user_game_ident: UserGameIdent): + self._logger.trace(__name__, f"Send SQL command: {user_game_ident.insert_string}") + self._context.cursor.execute(user_game_ident.insert_string) + + def update_user_game_ident(self, user_game_ident: UserGameIdent): + self._logger.trace(__name__, f"Send SQL command: {user_game_ident.udpate_string}") + self._context.cursor.execute(user_game_ident.udpate_string) + + def delete_user_game_ident(self, user_game_ident: UserGameIdent): + self._logger.trace(__name__, f"Send SQL command: {user_game_ident.delete_string}") + self._context.cursor.execute(user_game_ident.delete_string) + + def delete_user_game_ident_by_user_id(self, user_id: int): + self._logger.trace( + __name__, + f"Send SQL command: {UserGameIdent.delete_by_user_id_string}", + ) + self._context.cursor.execute(UserGameIdent.delete_by_user_id_string(user_id)) diff --git a/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py index a166ce55..e9ded8ba 100644 --- a/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py @@ -57,14 +57,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): f"Send SQL command: {UserJoinedGameServer.get_select_by_id_string(id)}", ) result = self._context.select(UserJoinedGameServer.get_select_by_id_string(id))[0] - return UserJoinedGameServer( - self._users.get_user_by_id(result[1]), - result[2], - result[3], - result[4], - result[5], - id=result[0], - ) + return self._from_result(result) def get_user_joined_game_servers_by_user_id(self, user_id: int) -> List[UserJoinedGameServer]: joins = List(UserJoinedGameServer) @@ -74,16 +67,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): ) results = self._context.select(UserJoinedGameServer.get_select_by_user_id_string(user_id)) for result in results: - joins.append( - UserJoinedGameServer( - self._users.get_user_by_id(result[1]), - result[2], - result[3], - result[4], - result[5], - id=result[0], - ) - ) + joins.append(self._from_result(result)) return joins @@ -93,14 +77,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): f"Send SQL command: {UserJoinedGameServer.get_select_by_user_id_string(user_id)}", ) result = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id))[0] - return UserJoinedGameServer( - self._users.get_user_by_id(result[1]), - result[2], - result[3], - result[4], - result[5], - id=result[0], - ) + return self._from_result(result) def find_active_user_joined_game_server_by_user_id(self, user_id: int) -> Optional[UserJoinedGameServer]: self._logger.trace( @@ -113,14 +90,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): result = result[0] - return UserJoinedGameServer( - self._users.get_user_by_id(result[1]), - result[2], - result[3], - result[4], - result[5], - id=result[0], - ) + return self._from_result(result) def find_active_user_joined_game_servers_by_user_id(self, user_id: int) -> List[Optional[UserJoinedGameServer]]: self._logger.trace( @@ -131,16 +101,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): db_results = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id)) for db_result in db_results: - result.append( - UserJoinedGameServer( - self._users.get_user_by_id(db_result[1]), - db_result[2], - db_result[3], - db_result[4], - db_result[5], - id=db_result[0], - ) - ) + result.append(self._from_result(db_result)) return result diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index ee8b27cb..37d84b00 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -3,7 +3,6 @@ type User implements TableQuery { discordId: String name: String xp: Int - minecraftId: String ontime: Float level: Level @@ -28,7 +27,6 @@ input UserFilter { discordId: String name: String xp: Int - minecraftId: String ontime: Float level: LevelFilter server: ServerFilter diff --git a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql index 6c4659a2..90475043 100644 --- a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql +++ b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql @@ -20,9 +20,9 @@ input UserJoinedGameServerFilter { type UserJoinedGameServerMutation { userJoined(input: UserJoinedGameServerInput!): UserJoinedGameServer - userLeaved(input: UserJoinedGameServerInput!): UserJoinedGameServer + userLeft(input: UserJoinedGameServerInput!): UserJoinedGameServer } input UserJoinedGameServerInput { - userId: ID! + ident: String! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py index f3ef07f1..0f68ed53 100644 --- a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py @@ -11,6 +11,7 @@ from bot_api.configuration.authentication_settings import AuthenticationSettings from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.api_key import ApiKey @@ -32,6 +33,7 @@ class UserJoinedGameServerMutation(QueryABC): user_joined_game_servers: UserJoinedGameServerRepositoryABC, api_keys: ApiKeyRepositoryABC, game_servers: GameServerRepositoryABC, + user_game_idents: UserGameIdentRepositoryABC, bot: DiscordBotServiceABC, db: DatabaseContextABC, permissions: PermissionService, @@ -46,13 +48,14 @@ class UserJoinedGameServerMutation(QueryABC): self._user_joined_game_servers = user_joined_game_servers self._api_keys = api_keys self._game_servers = game_servers + self._user_game_idents = user_game_idents self._bot = bot self._db = db self._permissions = permissions self._auth_settings = auth_settings self.set_field("userJoined", self.resolve_user_joined) - self.set_field("userLeaved", self.resolve_user_leaved) + self.set_field("userLeft", self.resolve_user_left) def _get_api_key_str(self, api_key: ApiKey) -> str: return hashlib.sha256( @@ -72,7 +75,14 @@ class UserJoinedGameServerMutation(QueryABC): ) def resolve_user_joined(self, *_, input: dict): - user = self._users.get_user_by_id(input["userId"]) + api_key = self._get_api_key() + if api_key is None: + self._logger.warn(__name__, f"UserJoinedGameServer not saved. Api-Key not available!") + return + + game_server = self._game_servers.get_game_server_by_api_key_id(api_key.id) + game_ident = self._user_game_idents.get_user_game_ident_by_ident(input["ident"]) + user = game_ident.user self._can_user_mutate_data(user.server, UserRoleEnum.admin) active = self._user_joined_game_servers.find_active_user_joined_game_server_by_user_id(user.id) @@ -83,21 +93,27 @@ class UserJoinedGameServerMutation(QueryABC): ) return - api_key = self._get_api_key() - game_server = self._game_servers.get_game_server_by_api_key_id(api_key.id) new = UserJoinedGameServer(user, game_server, datetime.now()) self._user_joined_game_servers.add_user_joined_game_server(new) self._db.save_changes() return self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.id) - def resolve_user_leaved(self, *_, input: dict): - user = self._users.get_user_by_id(input["userId"]) + def resolve_user_left(self, *_, input: dict): + api_key = self._get_api_key() + if api_key is None: + self._logger.warn(__name__, f"UserJoinedGameServer not saved. Api-Key not available!") + return + + game_ident = self._user_game_idents.find_user_game_ident_by_ident(input["ident"]) + if game_ident is None: + return + user = game_ident.user self._can_user_mutate_data(user.server, UserRoleEnum.admin) active = self._user_joined_game_servers.find_active_user_joined_game_server_by_user_id(user.id) if active is None: - return None + return active.leaved_on = datetime.now() settings: BaseServerSettings = self._base_helper.get_config(user.server.discord_id) diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index 857d668d..d9439981 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -38,7 +38,6 @@ 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( @@ -75,10 +74,6 @@ class UserQuery(DataQueryABC): def resolve_xp(user: User, *_): return user.xp - @staticmethod - def resolve_minecraft_id(user: User, *_): - return user.minecraft_id - @staticmethod def resolve_ontime(user: User, *_): return user.ontime diff --git a/kdb-bot/src/modules/base/command/register_group.py b/kdb-bot/src/modules/base/command/register_group.py index e7d9c7cf..dc3fed15 100644 --- a/kdb-bot/src/modules/base/command/register_group.py +++ b/kdb-bot/src/modules/base/command/register_group.py @@ -1,9 +1,12 @@ +from typing import List as TList + import discord import requests from cpl_core.database.context import DatabaseContextABC from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe +from discord import app_commands from discord.ext import commands from discord.ext.commands import Context @@ -11,8 +14,11 @@ from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.user_game_ident import UserGameIdent class RegisterGroup(DiscordCommandABC): @@ -24,6 +30,8 @@ class RegisterGroup(DiscordCommandABC): client_utils: ClientUtilsABC, servers: ServerRepositoryABC, users: UserRepositoryABC, + game_server: GameServerRepositoryABC, + user_game_ident: UserGameIdentRepositoryABC, db: DatabaseContextABC, t: TranslatePipe, ): @@ -35,6 +43,8 @@ class RegisterGroup(DiscordCommandABC): self._client_utils = client_utils self._servers = servers self._users = users + self._game_server = game_server + self._user_game_ident = user_game_ident self._db = db self._t = t @@ -49,7 +59,7 @@ class RegisterGroup(DiscordCommandABC): @commands.guild_only() @CommandChecks.check_is_ready() @CommandChecks.check_is_member_moderator() - async def minecraft(self, ctx: Context, member: discord.Member, name: str): + async def minecraft(self, ctx: Context, member: discord.Member, game_server: int, name: str): self._logger.debug(__name__, f"Received command register minecraft {ctx}") minecraft_id = None @@ -74,8 +84,10 @@ class RegisterGroup(DiscordCommandABC): server = self._servers.get_server_by_discord_id(ctx.guild.id) user = self._users.get_user_by_discord_id_and_server_id(member.id, server.id) - user.minecraft_id = minecraft_id - self._users.update_user(user) + gs = self._game_server.get_game_server_by_id(game_server) + + game_ident = UserGameIdent(user, gs, minecraft_id) + self._user_game_ident.add_user_game_ident(game_ident) self._db.save_changes() await self._message_service.send_interaction_msg( @@ -83,3 +95,15 @@ class RegisterGroup(DiscordCommandABC): ) self._logger.trace(__name__, f"Finished register minecraft command") + + @minecraft.autocomplete("game_server") + async def game_server_autocomplete( + self, interaction: discord.Interaction, current: str + ) -> TList[app_commands.Choice[str]]: + server = self._servers.get_server_by_discord_id(interaction.guild.id) + game_servers = self._game_server.get_game_servers_by_server_id(server.id) + + return [ + app_commands.Choice(name=gs.name, value=gs.id) + for gs in self._client_utils.get_auto_complete_list(game_servers, current, lambda x: x.name) + ] diff --git a/kdb-bot/src/modules/base/command/unregister_group.py b/kdb-bot/src/modules/base/command/unregister_group.py index c9c1282d..4d5f796a 100644 --- a/kdb-bot/src/modules/base/command/unregister_group.py +++ b/kdb-bot/src/modules/base/command/unregister_group.py @@ -1,8 +1,11 @@ +from typing import List as TList + import discord from cpl_core.database.context import DatabaseContextABC from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe +from discord import app_commands from discord.ext import commands from discord.ext.commands import Context @@ -10,7 +13,9 @@ from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC @@ -23,6 +28,8 @@ class UnregisterGroup(DiscordCommandABC): client_utils: ClientUtilsABC, servers: ServerRepositoryABC, users: UserRepositoryABC, + game_server: GameServerRepositoryABC, + user_game_idents: UserGameIdentRepositoryABC, db: DatabaseContextABC, t: TranslatePipe, ): @@ -34,26 +41,29 @@ class UnregisterGroup(DiscordCommandABC): self._client_utils = client_utils self._servers = servers self._users = users + self._game_server = game_server + self._user_game_idents = user_game_idents self._db = db self._t = t self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}") - @commands.hybrid_group() - @commands.guild_only() - async def unregister(self, ctx: Context): - pass + # @commands.hybrid_group() + # @commands.guild_only() + # async def unregister(self, ctx: Context): + # pass - @unregister.command() + @commands.hybrid_command() @commands.guild_only() @CommandChecks.check_is_ready() @CommandChecks.check_is_member_moderator() - async def minecraft(self, ctx: Context, member: discord.Member): - self._logger.debug(__name__, f"Received command register minecraft {ctx}") + async def unregister(self, ctx: Context, member: discord.Member, game_server: int): + self._logger.debug(__name__, f"Received command unregister {ctx}") server = self._servers.get_server_by_discord_id(ctx.guild.id) user = self._users.get_user_by_discord_id_and_server_id(member.id, server.id) - user.minecraft_id = None + ident = user.game_idents.where(lambda x: x.game_server.id == game_server).single() + self._user_game_idents.delete_user_game_ident(ident) self._users.update_user(user) self._db.save_changes() @@ -61,4 +71,16 @@ class UnregisterGroup(DiscordCommandABC): ctx.interaction, self._t.transform("modules.base.unregister.success") ) - self._logger.trace(__name__, f"Finished register minecraft command") + self._logger.trace(__name__, f"Finished unregister command") + + @unregister.autocomplete("game_server") + async def game_server_autocomplete( + self, interaction: discord.Interaction, current: str + ) -> TList[app_commands.Choice[str]]: + server = self._servers.get_server_by_discord_id(interaction.guild.id) + game_servers = self._game_server.get_game_servers_by_server_id(server.id) + + return [ + app_commands.Choice(name=gs.name, value=gs.id) + for gs in self._client_utils.get_auto_complete_list(game_servers, current, lambda x: x.name) + ] From 1b328dc20e81b6530104af1f3eb819c6fcd31611 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 6 Mar 2023 08:05:30 +0100 Subject: [PATCH 5/5] Fixed translation #238 --- kdb-bot/src/bot/translation/de.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 889bf26a..4089a9b8 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -149,7 +149,7 @@ "afk_command_move_message": "Ich verschiebe dich ja schon... (◔_◔)", "game_server": { "error": { - "nothing_found": "Keine Level Einträge gefunden." + "nothing_found": "Keine Gameserver gefunden." }, "list": { "title": "Gameserver",