Merge pull request '#238' (#242) from #238 into 1.0.0

Reviewed-on: sh-edraft.de/kd_discord_bot#242
Reviewed-by: Ebola-Chan <nick.jungmann@gmail.com>
Closes #238
This commit is contained in:
Sven Heidemann 2023-03-06 08:42:21 +01:00
commit f02268c1a1
28 changed files with 968 additions and 187 deletions

View File

@ -147,6 +147,23 @@
"base": { "base": {
"afk_command_channel_missing_message": "Zu unfähig einem Sprachkanal beizutreten?", "afk_command_channel_missing_message": "Zu unfähig einem Sprachkanal beizutreten?",
"afk_command_move_message": "Ich verschiebe dich ja schon... (◔_◔)", "afk_command_move_message": "Ich verschiebe dich ja schon... (◔_◔)",
"game_server": {
"error": {
"nothing_found": "Keine Gameserver 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 :(", "goodbye_message": "Schade, dass du uns so schnell verlässt :(",
"info": { "info": {
"description": "Informationen über mich", "description": "Informationen über mich",

View File

@ -18,6 +18,10 @@ class ApiKeyRepositoryABC(ABC):
def get_api_key(self, identifier: str, key: str) -> ApiKey: def get_api_key(self, identifier: str, key: str) -> ApiKey:
pass pass
@abstractmethod
def get_api_key_by_id(self, id: int) -> ApiKey:
pass
@abstractmethod @abstractmethod
def get_api_key_by_key(self, key: str) -> ApiKey: def get_api_key_by_key(self, key: str) -> ApiKey:
pass pass

View File

@ -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

View File

@ -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

View File

@ -9,9 +9,11 @@ from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC
from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.client_repository_abc import ClientRepositoryABC 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.known_user_repository_abc import KnownUserRepositoryABC
from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC 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_game_server_repository_abc import UserJoinedGameServerRepositoryABC
from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
from bot_data.abc.user_joined_voice_channel_repository_abc import ( from bot_data.abc.user_joined_voice_channel_repository_abc import (
@ -26,10 +28,12 @@ from bot_data.service.api_key_repository_service import ApiKeyRepositoryService
from bot_data.service.auth_user_repository_service import AuthUserRepositoryService from bot_data.service.auth_user_repository_service import AuthUserRepositoryService
from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService
from bot_data.service.client_repository_service import ClientRepositoryService 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.known_user_repository_service import KnownUserRepositoryService
from bot_data.service.level_repository_service import LevelRepositoryService from bot_data.service.level_repository_service import LevelRepositoryService
from bot_data.service.seeder_service import SeederService from bot_data.service.seeder_service import SeederService
from bot_data.service.server_repository_service import ServerRepositoryService 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_game_server_repository_service import UserJoinedGameServerRepositoryService
from bot_data.service.user_joined_server_repository_service import ( from bot_data.service.user_joined_server_repository_service import (
UserJoinedServerRepositoryService, UserJoinedServerRepositoryService,
@ -68,5 +72,7 @@ class DataModule(ModuleABC):
UserMessageCountPerHourRepositoryABC, UserMessageCountPerHourRepositoryABC,
UserMessageCountPerHourRepositoryService, UserMessageCountPerHourRepositoryService,
) )
services.add_transient(GameServerRepositoryABC, GameServerRepositoryService)
services.add_transient(UserGameIdentRepositoryABC, UserGameIdentRepositoryService)
services.add_transient(SeederService) services.add_transient(SeederService)

View File

@ -18,15 +18,15 @@ class UserJoinedGameServerMigration(MigrationABC):
self._cursor.execute( self._cursor.execute(
str( str(
f""" f"""
CREATE TABLE IF NOT EXISTS `UserJoinedGameServer` ( CREATE TABLE IF NOT EXISTS `GameServers` (
`Id` BIGINT NOT NULL AUTO_INCREMENT, `Id` BIGINT NOT NULL AUTO_INCREMENT,
`UserId` BIGINT NOT NULL, `Name` VARCHAR(255) NOT NULL,
`GameServer` VARCHAR(255) NOT NULL, `ServerId` BIGINT NOT NULL,
`JoinedOn` DATETIME(6) NOT NULL, `ApiKeyId` BIGINT NOT NULL,
`LeavedOn` DATETIME(6),
`CreatedAt` DATETIME(6), `CreatedAt` DATETIME(6),
`LastModifiedAt` 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`) PRIMARY KEY(`Id`)
); );
""" """
@ -36,17 +36,42 @@ class UserJoinedGameServerMigration(MigrationABC):
self._cursor.execute( self._cursor.execute(
str( str(
f""" 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`)
);
"""
)
)
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): def downgrade(self):
self._cursor.execute("DROP TABLE `GameServers`;")
self._cursor.execute("DROP TABLE `UserJoinedGameServer`;") self._cursor.execute("DROP TABLE `UserJoinedGameServer`;")
self._cursor.execute( self._cursor.execute("DROP TABLE `UserGameIdents`;")
str(
f"""
ALTER TABLE Users DROP COLUMN MinecraftId;
"""
)
)

View File

@ -25,6 +25,10 @@ class ApiKey(TableABC):
self._created_at = created_at if created_at is not None else self._created_at self._created_at = created_at if created_at is not None else self._created_at
self._modified_at = modified_at if modified_at is not None else self._modified_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 @property
def identifier(self) -> str: def identifier(self) -> str:
return self._identifier 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 @staticmethod
def get_select_by_key(key: str) -> str: def get_select_by_key(key: str) -> str:
return str( return str(

View File

@ -0,0 +1,142 @@
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 `GameServers`;
"""
)
@staticmethod
def get_select_by_id_string(id: int) -> str:
return str(
f"""
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 `GameServers`
WHERE `UserId` = {id};
"""
)
@property
def insert_string(self) -> str:
return str(
f"""
INSERT INTO `GameServers` (
`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 `GameServers`
SET `LastModifiedAt` = '{self._modified_at}'
WHERE `Id` = {self._id};
"""
)
@property
def delete_string(self) -> str:
return str(
f"""
DELETE FROM `GameServers`
WHERE `Id` = {self._id};
"""
)
@staticmethod
def delete_by_user_id_string(id: int) -> str:
return str(
f"""
DELETE FROM `GameServers`
WHERE `UserId` = {id}
"""
)

View File

@ -4,6 +4,7 @@ from typing import Optional
from cpl_core.database import TableABC from cpl_core.database import TableABC
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from bot_data.model.level import Level from bot_data.model.level import Level
from bot_data.model.server import Server from bot_data.model.server import Server
@ -14,7 +15,6 @@ class User(TableABC):
self, self,
dc_id: int, dc_id: int,
xp: int, xp: int,
minecraft_id: Optional[str],
server: Optional[Server], server: Optional[Server],
created_at: datetime = None, created_at: datetime = None,
modified_at: datetime = None, modified_at: datetime = None,
@ -23,7 +23,6 @@ class User(TableABC):
self._user_id = id self._user_id = id
self._discord_id = dc_id self._discord_id = dc_id
self._xp = xp self._xp = xp
self._minecraft_id = minecraft_id
self._server = server self._server = server
TableABC.__init__(self) TableABC.__init__(self)
@ -77,14 +76,6 @@ class User(TableABC):
levels: LevelService = services.get_service(LevelService) levels: LevelService = services.get_service(LevelService)
return levels.get_level(self) 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 @property
def server(self) -> Optional[Server]: def server(self) -> Optional[Server]:
return self._server return self._server
@ -100,6 +91,17 @@ class User(TableABC):
ujs: UserJoinedServerRepositoryABC = services.get_service(UserJoinedServerRepositoryABC) ujs: UserJoinedServerRepositoryABC = services.get_service(UserJoinedServerRepositoryABC)
return ujs.find_active_user_joined_server_by_user_id(self.id) is None 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 @staticmethod
def get_select_all_string() -> str: def get_select_all_string() -> str:
return str( return str(
@ -150,11 +152,10 @@ class User(TableABC):
return str( return str(
f""" f"""
INSERT INTO `Users` ( INSERT INTO `Users` (
`DiscordId`, `XP`, `MinecraftId`, `ServerId`, `CreatedAt`, `LastModifiedAt` `DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt`
) VALUES ( ) VALUES (
{self._discord_id}, {self._discord_id},
{self._xp}, {self._xp},
{"NULL" if self._minecraft_id is None else f"'{self._minecraft_id}'"},
{self._server.id}, {self._server.id},
'{self._created_at}', '{self._created_at}',
'{self._modified_at}' '{self._modified_at}'
@ -168,7 +169,6 @@ class User(TableABC):
f""" f"""
UPDATE `Users` UPDATE `Users`
SET `XP` = {self._xp}, SET `XP` = {self._xp},
`MinecraftId` = {"NULL" if self._minecraft_id is None else f"'{self._minecraft_id}'"},
`LastModifiedAt` = '{self._modified_at}' `LastModifiedAt` = '{self._modified_at}'
WHERE `UserId` = {self._user_id}; WHERE `UserId` = {self._user_id};
""" """

View File

@ -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}
"""
)

View File

@ -2,6 +2,7 @@ from datetime import datetime
from cpl_core.database import TableABC from cpl_core.database import TableABC
from bot_data.model.game_server import GameServer
from bot_data.model.user import User from bot_data.model.user import User
@ -9,7 +10,7 @@ class UserJoinedGameServer(TableABC):
def __init__( def __init__(
self, self,
user: User, user: User,
game_server: str, game_server: GameServer,
joined_on: datetime, joined_on: datetime,
leaved_on: datetime = None, leaved_on: datetime = None,
created_at: datetime = None, created_at: datetime = None,
@ -35,7 +36,7 @@ class UserJoinedGameServer(TableABC):
return self._user return self._user
@property @property
def game_server(self) -> str: def game_server(self) -> GameServer:
return self._game_server return self._game_server
@property @property
@ -101,10 +102,10 @@ class UserJoinedGameServer(TableABC):
return str( return str(
f""" f"""
INSERT INTO `UserJoinedGameServer` ( INSERT INTO `UserJoinedGameServer` (
`UserId`, `GameServer`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` `UserId`, `GameServerId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt`
) VALUES ( ) VALUES (
{self._user.id}, {self._user.id},
'{self._game_server}', '{self._game_server.id}',
'{self._joined_on}', '{self._joined_on}',
{"NULL" if self._leaved_on is None else f"'{self._leaved_on}'"}, {"NULL" if self._leaved_on is None else f"'{self._leaved_on}'"},
'{self._created_at}', '{self._created_at}',

View File

@ -56,6 +56,10 @@ class ApiKeyRepositoryService(ApiKeyRepositoryABC):
self._logger.trace(__name__, f"Send SQL command: {ApiKey.get_select_string(identifier, key)}") 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]) 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: def get_api_key_by_key(self, key: str) -> ApiKey:
self._logger.trace(__name__, f"Send SQL command: {ApiKey.get_select_by_key(key)}") 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]) return self._api_key_from_result(self._context.select(ApiKey.get_select_by_key(key))[0])

View File

@ -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)

View File

@ -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))

View File

@ -4,6 +4,7 @@ from cpl_core.database.context import DatabaseContextABC
from cpl_query.extension import List from cpl_query.extension import List
from bot_core.logging.database_logger import DatabaseLogger from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC
from bot_data.abc.user_joined_game_server_repository_abc import ( from bot_data.abc.user_joined_game_server_repository_abc import (
UserJoinedGameServerRepositoryABC, UserJoinedGameServerRepositoryABC,
) )
@ -17,14 +18,26 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC):
logger: DatabaseLogger, logger: DatabaseLogger,
db_context: DatabaseContextABC, db_context: DatabaseContextABC,
users: UserRepositoryABC, users: UserRepositoryABC,
game_servers: GameServerRepositoryABC,
): ):
self._logger = logger self._logger = logger
self._context = db_context self._context = db_context
self._users = users self._users = users
self._game_servers = game_servers
UserJoinedGameServerRepositoryABC.__init__(self) UserJoinedGameServerRepositoryABC.__init__(self)
def _from_result(self, result: tuple) -> UserJoinedGameServer:
return UserJoinedGameServer(
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_joined_game_servers(self) -> List[UserJoinedGameServer]: def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]:
joins = List(UserJoinedGameServer) joins = List(UserJoinedGameServer)
self._logger.trace( self._logger.trace(
@ -34,16 +47,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC):
results = self._context.select(UserJoinedGameServer.get_select_all_string()) results = self._context.select(UserJoinedGameServer.get_select_all_string())
for result in results: for result in results:
self._logger.trace(__name__, f"Get user-joined-game-server with id {result[0]}") self._logger.trace(__name__, f"Get user-joined-game-server with id {result[0]}")
joins.append( joins.append(self._from_result(result))
UserJoinedGameServer(
self._users.get_user_by_id(result[1]),
result[2],
result[3],
result[4],
result[5],
id=result[0],
)
)
return joins return joins
@ -53,14 +57,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC):
f"Send SQL command: {UserJoinedGameServer.get_select_by_id_string(id)}", f"Send SQL command: {UserJoinedGameServer.get_select_by_id_string(id)}",
) )
result = self._context.select(UserJoinedGameServer.get_select_by_id_string(id))[0] result = self._context.select(UserJoinedGameServer.get_select_by_id_string(id))[0]
return UserJoinedGameServer( return self._from_result(result)
self._users.get_user_by_id(result[1]),
result[2],
result[3],
result[4],
result[5],
id=result[0],
)
def get_user_joined_game_servers_by_user_id(self, user_id: int) -> List[UserJoinedGameServer]: def get_user_joined_game_servers_by_user_id(self, user_id: int) -> List[UserJoinedGameServer]:
joins = List(UserJoinedGameServer) joins = List(UserJoinedGameServer)
@ -70,16 +67,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC):
) )
results = self._context.select(UserJoinedGameServer.get_select_by_user_id_string(user_id)) results = self._context.select(UserJoinedGameServer.get_select_by_user_id_string(user_id))
for result in results: for result in results:
joins.append( joins.append(self._from_result(result))
UserJoinedGameServer(
self._users.get_user_by_id(result[1]),
result[2],
result[3],
result[4],
result[5],
id=result[0],
)
)
return joins return joins
@ -89,14 +77,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC):
f"Send SQL command: {UserJoinedGameServer.get_select_by_user_id_string(user_id)}", 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] result = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id))[0]
return UserJoinedGameServer( return self._from_result(result)
self._users.get_user_by_id(result[1]),
result[2],
result[3],
result[4],
result[5],
id=result[0],
)
def find_active_user_joined_game_server_by_user_id(self, user_id: int) -> Optional[UserJoinedGameServer]: def find_active_user_joined_game_server_by_user_id(self, user_id: int) -> Optional[UserJoinedGameServer]:
self._logger.trace( self._logger.trace(
@ -109,14 +90,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC):
result = result[0] result = result[0]
return UserJoinedGameServer( return self._from_result(result)
self._users.get_user_by_id(result[1]),
result[2],
result[3],
result[4],
result[5],
id=result[0],
)
def find_active_user_joined_game_servers_by_user_id(self, user_id: int) -> List[Optional[UserJoinedGameServer]]: def find_active_user_joined_game_servers_by_user_id(self, user_id: int) -> List[Optional[UserJoinedGameServer]]:
self._logger.trace( self._logger.trace(
@ -127,16 +101,7 @@ class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC):
db_results = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id)) db_results = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id))
for db_result in db_results: for db_result in db_results:
result.append( result.append(self._from_result(db_result))
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],
)
)
return result return result

View File

@ -23,23 +23,23 @@ class UserRepositoryService(UserRepositoryABC):
UserRepositoryABC.__init__(self) 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]: def get_users(self) -> List[User]:
users = List(User) users = List(User)
self._logger.trace(__name__, f"Send SQL command: {User.get_select_all_string()}") self._logger.trace(__name__, f"Send SQL command: {User.get_select_all_string()}")
results = self._context.select(User.get_select_all_string()) results = self._context.select(User.get_select_all_string())
for result in results: for result in results:
self._logger.trace(__name__, f"Get user with id {result[0]}") self._logger.trace(__name__, f"Get user with id {result[0]}")
users.append( users.append(self._from_result(result))
User(
result[1],
result[2],
result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0],
)
)
return users return users
@ -47,15 +47,7 @@ class UserRepositoryService(UserRepositoryABC):
self._logger.trace(__name__, f"Send SQL command: {User.get_select_by_id_string(id)}") 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] result = self._context.select(User.get_select_by_id_string(id))[0]
return User( return self._from_result(result)
result[1],
result[2],
result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0],
)
def find_user_by_id(self, id: int) -> Optional[User]: 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)}") 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] result = result[0]
return User( return self._from_result(result)
result[1],
result[2],
result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0],
)
def get_users_by_discord_id(self, discord_id: int) -> List[User]: def get_users_by_discord_id(self, discord_id: int) -> List[User]:
users = 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)) results = self._context.select(User.get_select_by_discord_id_string(discord_id))
for result in results: for result in results:
users.append( users.append(self._from_result(result))
User(
result[1],
result[2],
result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0],
)
)
return users return users
@ -105,17 +79,7 @@ class UserRepositoryService(UserRepositoryABC):
) )
results = self._context.select(User.get_select_by_server_id_string(server_id)) results = self._context.select(User.get_select_by_server_id_string(server_id))
for result in results: for result in results:
users.append( users.append(self._from_result(result))
User(
result[1],
result[2],
result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0],
)
)
return users 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] result = self._context.select(User.get_select_by_discord_id_and_server_id_string(discord_id, server_id))[0]
return User( return self._from_result(result)
result[1],
result[2],
result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0],
)
def find_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[User]: def find_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[User]:
self._logger.trace( self._logger.trace(
@ -147,15 +103,7 @@ class UserRepositoryService(UserRepositoryABC):
result = result[0] result = result[0]
return User( return self._from_result(result)
result[1],
result[2],
result[3],
self._servers.get_server_by_id(result[4]),
result[5],
result[6],
id=result[0],
)
def add_user(self, user: User): def add_user(self, user: User):
self._logger.trace(__name__, f"Send SQL command: {user.insert_string}") self._logger.trace(__name__, f"Send SQL command: {user.insert_string}")

View File

@ -82,7 +82,7 @@ class QueryABC(ObjectType):
break break
elif type(element) == AutoRoleRule: elif type(element) == AutoRoleRule:
element: AutoRole = element.auto_role element: AutoRole = element.game_server
for u in user.users: for u in user.users:
u: User = u u: User = u
guild = bot.get_guild(u.server.discord_id) guild = bot.get_guild(u.server.discord_id)

View File

@ -57,7 +57,7 @@ class AutoRoleRuleFilter(FilterABC):
query = query.where(get_role_name) query = query.where(get_role_name)
if self._auto_role is not None: 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) auto_roles = self._auto_role.filter(query.select(lambda x: x.game_server)).select(lambda x: x.id)
query = query.where(lambda x: x.auto_role.id in auto_roles) query = query.where(lambda x: x.game_server.id in auto_roles)
return query return query

View File

@ -3,7 +3,6 @@ type User implements TableQuery {
discordId: String discordId: String
name: String name: String
xp: Int xp: Int
minecraftId: String
ontime: Float ontime: Float
level: Level level: Level
@ -28,7 +27,6 @@ input UserFilter {
discordId: String discordId: String
name: String name: String
xp: Int xp: Int
minecraftId: String
ontime: Float ontime: Float
level: LevelFilter level: LevelFilter
server: ServerFilter server: ServerFilter

View File

@ -20,10 +20,9 @@ input UserJoinedGameServerFilter {
type UserJoinedGameServerMutation { type UserJoinedGameServerMutation {
userJoined(input: UserJoinedGameServerInput!): UserJoinedGameServer userJoined(input: UserJoinedGameServerInput!): UserJoinedGameServer
userLeaved(input: UserJoinedGameServerInput!): UserJoinedGameServer userLeft(input: UserJoinedGameServerInput!): UserJoinedGameServer
} }
input UserJoinedGameServerInput { input UserJoinedGameServerInput {
gameServer: String! ident: String!
userId: ID!
} }

View File

@ -1,12 +1,20 @@
import hashlib
from datetime import datetime from datetime import datetime
from typing import Optional
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
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.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_game_server_repository_abc import UserJoinedGameServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC 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_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
@ -23,9 +31,13 @@ class UserJoinedGameServerMutation(QueryABC):
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
users: UserRepositoryABC, users: UserRepositoryABC,
user_joined_game_servers: UserJoinedGameServerRepositoryABC, user_joined_game_servers: UserJoinedGameServerRepositoryABC,
api_keys: ApiKeyRepositoryABC,
game_servers: GameServerRepositoryABC,
user_game_idents: UserGameIdentRepositoryABC,
bot: DiscordBotServiceABC, bot: DiscordBotServiceABC,
db: DatabaseContextABC, db: DatabaseContextABC,
permissions: PermissionService, permissions: PermissionService,
auth_settings: AuthenticationSettings,
): ):
QueryABC.__init__(self, "UserJoinedGameServerMutation") QueryABC.__init__(self, "UserJoinedGameServerMutation")
@ -34,15 +46,43 @@ class UserJoinedGameServerMutation(QueryABC):
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
self._api_keys = api_keys
self._game_servers = game_servers
self._user_game_idents = user_game_idents
self._bot = bot self._bot = bot
self._db = db self._db = db
self._permissions = permissions self._permissions = permissions
self._auth_settings = auth_settings
self.set_field("userJoined", self.resolve_user_joined) 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(
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): 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) 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) active = self._user_joined_game_servers.find_active_user_joined_game_server_by_user_id(user.id)
@ -53,19 +93,27 @@ class UserJoinedGameServerMutation(QueryABC):
) )
return return
new = UserJoinedGameServer(user, input["gameServer"], datetime.now()) new = UserJoinedGameServer(user, game_server, 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()
return self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.id) return self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.id)
def resolve_user_leaved(self, *_, input: dict): def resolve_user_left(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_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) 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) active = self._user_joined_game_servers.find_active_user_joined_game_server_by_user_id(user.id)
if active is None: if active is None:
return None return
active.leaved_on = datetime.now() active.leaved_on = datetime.now()
settings: BaseServerSettings = self._base_helper.get_config(user.server.discord_id) settings: BaseServerSettings = self._base_helper.get_config(user.server.discord_id)

View File

@ -22,7 +22,7 @@ class UserJoinedGameServerQuery(DataQueryABC):
@staticmethod @staticmethod
def resolve_game_server(x: UserJoinedGameServer, *_): def resolve_game_server(x: UserJoinedGameServer, *_):
return x.game_server return x.game_server.name
@staticmethod @staticmethod
def resolve_user(x: UserJoinedGameServer, *_): def resolve_user(x: UserJoinedGameServer, *_):

View File

@ -38,7 +38,6 @@ class UserQuery(DataQueryABC):
self.set_field("discordId", self.resolve_discord_id) self.set_field("discordId", self.resolve_discord_id)
self.set_field("name", self.resolve_name) self.set_field("name", self.resolve_name)
self.set_field("xp", self.resolve_xp) 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("ontime", self.resolve_ontime)
self.set_field("level", self.resolve_level) self.set_field("level", self.resolve_level)
self.add_collection( self.add_collection(
@ -75,10 +74,6 @@ class UserQuery(DataQueryABC):
def resolve_xp(user: User, *_): def resolve_xp(user: User, *_):
return user.xp return user.xp
@staticmethod
def resolve_minecraft_id(user: User, *_):
return user.minecraft_id
@staticmethod @staticmethod
def resolve_ontime(user: User, *_): def resolve_ontime(user: User, *_):
return user.ontime return user.ontime

View File

@ -8,6 +8,7 @@ from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from modules.base.abc.base_helper_abc import BaseHelperABC from modules.base.abc.base_helper_abc import BaseHelperABC
from modules.base.command.afk_command import AFKCommand 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.help_command import HelpCommand
from modules.base.command.info_command import InfoCommand from modules.base.command.info_command import InfoCommand
from modules.base.command.mass_move_command import MassMoveCommand 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(UserGroup)
self._dc.add_command(RegisterGroup) self._dc.add_command(RegisterGroup)
self._dc.add_command(UnregisterGroup) self._dc.add_command(UnregisterGroup)
self._dc.add_command(GameServerGroup)
# events # events
self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent)
self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent)

View File

@ -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)
]

View File

@ -1,9 +1,12 @@
from typing import List as TList
import discord import discord
import requests import requests
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe from cpl_translation import TranslatePipe
from discord import app_commands
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import Context 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.abc.message_service_abc import MessageServiceABC
from bot_core.helper.command_checks import CommandChecks from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger 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.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.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.user_game_ident import UserGameIdent
class RegisterGroup(DiscordCommandABC): class RegisterGroup(DiscordCommandABC):
@ -24,6 +30,8 @@ class RegisterGroup(DiscordCommandABC):
client_utils: ClientUtilsABC, client_utils: ClientUtilsABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
users: UserRepositoryABC, users: UserRepositoryABC,
game_server: GameServerRepositoryABC,
user_game_ident: UserGameIdentRepositoryABC,
db: DatabaseContextABC, db: DatabaseContextABC,
t: TranslatePipe, t: TranslatePipe,
): ):
@ -35,6 +43,8 @@ class RegisterGroup(DiscordCommandABC):
self._client_utils = client_utils self._client_utils = client_utils
self._servers = servers self._servers = servers
self._users = users self._users = users
self._game_server = game_server
self._user_game_ident = user_game_ident
self._db = db self._db = db
self._t = t self._t = t
@ -49,7 +59,7 @@ class RegisterGroup(DiscordCommandABC):
@commands.guild_only() @commands.guild_only()
@CommandChecks.check_is_ready() @CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator() @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}") self._logger.debug(__name__, f"Received command register minecraft {ctx}")
minecraft_id = None minecraft_id = None
@ -74,8 +84,10 @@ class RegisterGroup(DiscordCommandABC):
server = self._servers.get_server_by_discord_id(ctx.guild.id) 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 = self._users.get_user_by_discord_id_and_server_id(member.id, server.id)
user.minecraft_id = minecraft_id gs = self._game_server.get_game_server_by_id(game_server)
self._users.update_user(user)
game_ident = UserGameIdent(user, gs, minecraft_id)
self._user_game_ident.add_user_game_ident(game_ident)
self._db.save_changes() self._db.save_changes()
await self._message_service.send_interaction_msg( await self._message_service.send_interaction_msg(
@ -83,3 +95,15 @@ class RegisterGroup(DiscordCommandABC):
) )
self._logger.trace(__name__, f"Finished register minecraft command") 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)
]

View File

@ -1,8 +1,11 @@
from typing import List as TList
import discord import discord
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe from cpl_translation import TranslatePipe
from discord import app_commands
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import Context 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.abc.message_service_abc import MessageServiceABC
from bot_core.helper.command_checks import CommandChecks from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger 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.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.abc.user_repository_abc import UserRepositoryABC
@ -23,6 +28,8 @@ class UnregisterGroup(DiscordCommandABC):
client_utils: ClientUtilsABC, client_utils: ClientUtilsABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
users: UserRepositoryABC, users: UserRepositoryABC,
game_server: GameServerRepositoryABC,
user_game_idents: UserGameIdentRepositoryABC,
db: DatabaseContextABC, db: DatabaseContextABC,
t: TranslatePipe, t: TranslatePipe,
): ):
@ -34,26 +41,29 @@ class UnregisterGroup(DiscordCommandABC):
self._client_utils = client_utils self._client_utils = client_utils
self._servers = servers self._servers = servers
self._users = users self._users = users
self._game_server = game_server
self._user_game_idents = user_game_idents
self._db = db self._db = db
self._t = t self._t = t
self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}") self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}")
@commands.hybrid_group() # @commands.hybrid_group()
@commands.guild_only() # @commands.guild_only()
async def unregister(self, ctx: Context): # async def unregister(self, ctx: Context):
pass # pass
@unregister.command() @commands.hybrid_command()
@commands.guild_only() @commands.guild_only()
@CommandChecks.check_is_ready() @CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator() @CommandChecks.check_is_member_moderator()
async def minecraft(self, ctx: Context, member: discord.Member): async def unregister(self, ctx: Context, member: discord.Member, game_server: int):
self._logger.debug(__name__, f"Received command register minecraft {ctx}") self._logger.debug(__name__, f"Received command unregister {ctx}")
server = self._servers.get_server_by_discord_id(ctx.guild.id) 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 = 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._users.update_user(user)
self._db.save_changes() self._db.save_changes()
@ -61,4 +71,16 @@ class UnregisterGroup(DiscordCommandABC):
ctx.interaction, self._t.transform("modules.base.unregister.success") 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)
]

View File

@ -193,7 +193,7 @@ class DatabaseOnReadyEvent(OnReadyABC):
self._logger.warn(__name__, f"User not found in database: {u.id}") self._logger.warn(__name__, f"User not found in database: {u.id}")
self._logger.debug(__name__, f"Add user: {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._db_context.save_changes()
self._logger.debug(__name__, f"Added User: {u.id}") self._logger.debug(__name__, f"Added User: {u.id}")