Improved game server handling for ontime #238

This commit is contained in:
Sven Heidemann 2023-03-05 13:47:30 +01:00
parent 15dfc3f47c
commit 34c362a391
14 changed files with 412 additions and 76 deletions

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

@ -11,10 +11,6 @@ class UserJoinedGameServerRepositoryABC(ABC):
def __init__(self): def __init__(self):
pass pass
# @abstractmethod
# def get_game_server_by_api_key(self) -> List[UserJoinedGameServer]:
# pass
@abstractmethod @abstractmethod
def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]: def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]:
pass pass

View File

@ -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.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 (
@ -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.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,
@ -71,5 +73,6 @@ class DataModule(ModuleABC):
UserMessageCountPerHourRepositoryService, UserMessageCountPerHourRepositoryService,
) )
services.add_transient(GameServerRepositoryABC, GameServerRepositoryService) services.add_transient(GameServerRepositoryABC, GameServerRepositoryService)
services.add_transient(UserGameIdentRepositoryABC, UserGameIdentRepositoryService)
services.add_transient(SeederService) services.add_transient(SeederService)

View File

@ -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): def downgrade(self):
self._cursor.execute("DROP TABLE `GameServers`;") self._cursor.execute("DROP TABLE `GameServers`;")
self._cursor.execute("DROP TABLE `UserJoinedGameServer`;") self._cursor.execute("DROP TABLE `UserJoinedGameServer`;")
self._cursor.execute("DROP TABLE `UserGameIdents`;")

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
@ -90,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(

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

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

@ -57,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)
@ -74,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
@ -93,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(
@ -113,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(
@ -131,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

@ -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,9 +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 {
userId: ID! ident: String!
} }

View File

@ -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.api_key_repository_abc import ApiKeyRepositoryABC
from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC 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.api_key import ApiKey
@ -32,6 +33,7 @@ class UserJoinedGameServerMutation(QueryABC):
user_joined_game_servers: UserJoinedGameServerRepositoryABC, user_joined_game_servers: UserJoinedGameServerRepositoryABC,
api_keys: ApiKeyRepositoryABC, api_keys: ApiKeyRepositoryABC,
game_servers: GameServerRepositoryABC, game_servers: GameServerRepositoryABC,
user_game_idents: UserGameIdentRepositoryABC,
bot: DiscordBotServiceABC, bot: DiscordBotServiceABC,
db: DatabaseContextABC, db: DatabaseContextABC,
permissions: PermissionService, permissions: PermissionService,
@ -46,13 +48,14 @@ class UserJoinedGameServerMutation(QueryABC):
self._user_joined_game_servers = user_joined_game_servers self._user_joined_game_servers = user_joined_game_servers
self._api_keys = api_keys self._api_keys = api_keys
self._game_servers = game_servers 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._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: def _get_api_key_str(self, api_key: ApiKey) -> str:
return hashlib.sha256( return hashlib.sha256(
@ -72,7 +75,14 @@ class UserJoinedGameServerMutation(QueryABC):
) )
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)
@ -83,21 +93,27 @@ class UserJoinedGameServerMutation(QueryABC):
) )
return 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()) 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

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

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