Improved game server handling for ontime #238
This commit is contained in:
47
kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py
Normal file
47
kdb-bot/src/bot_data/abc/user_game_ident_repository_abc.py
Normal 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
|
@@ -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
|
||||
|
@@ -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)
|
||||
|
@@ -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`;")
|
||||
|
@@ -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(
|
||||
|
131
kdb-bot/src/bot_data/model/user_game_ident.py
Normal file
131
kdb-bot/src/bot_data/model/user_game_ident.py
Normal 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}
|
||||
"""
|
||||
)
|
@@ -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))
|
@@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user