From b37c8ef7d31686c9e64df6df0d8490ee53a9baec Mon Sep 17 00:00:00 2001 From: kapselccc Date: Wed, 1 Mar 2023 21:02:01 +0100 Subject: [PATCH 01/25] added database_game --- .../dnd_bot/database/database_connection.py | 84 +------------------ dnd-bot/dnd_bot/database/database_game.py | 84 +++++++++++++++++++ dnd-bot/dnd_bot/logic/lobby/handler_create.py | 5 +- dnd-bot/dnd_bot/logic/lobby/handler_start.py | 5 +- 4 files changed, 93 insertions(+), 85 deletions(-) create mode 100644 dnd-bot/dnd_bot/database/database_game.py diff --git a/dnd-bot/dnd_bot/database/database_connection.py b/dnd-bot/dnd_bot/database/database_connection.py index 7e9c04a4..c6c244f2 100644 --- a/dnd-bot/dnd_bot/database/database_connection.py +++ b/dnd-bot/dnd_bot/database/database_connection.py @@ -1,5 +1,7 @@ import os -from psycopg2 import connect, ProgrammingError +from sqlite3 import ProgrammingError + +from psycopg2 import connect class DatabaseConnection: @@ -57,41 +59,6 @@ def execute_query(query: str): DatabaseConnection.cursor.execute(query) DatabaseConnection.connection.commit() - @staticmethod - def add_game(token: str, id_host: int, game_state: str, campaign_name: str) -> int | None: - """start game and add game to database - :param token: lobby/game token (5 digit password) - :param id_host: discord id of host - :param game_state: string enum, initial value of added game is 'LOBBY' - :param campaign_name: campaign name - :return: - on success: game id, on failure: None - """ - - DatabaseConnection.cursor.execute('INSERT INTO public."Game" (token, id_host, game_state, campaign_name) VALUES ' - '(%s, %s, %s, %s)', - (token, id_host, game_state, campaign_name)) - DatabaseConnection.cursor.execute('SELECT LASTVAL()') - - try: - game_id = DatabaseConnection.cursor.fetchone()[0] - except ProgrammingError as err: - print(f"db: error adding game {err}") - return None - - DatabaseConnection.connection.commit() - return game_id - - @staticmethod - def start_game(id_game: int) -> None: - """starts game - :param id_game: database game id - """ - 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: """add user to a game @@ -136,48 +103,3 @@ def find_game_by_token(token: str) -> dict | None: return {'id_game': game_tuple[0], 'token': game_tuple[1], 'id_host': game_tuple[2], 'id_campaign': game_tuple[3], 'game_state': game_tuple[4], 'players': users} - - @staticmethod - def update_game_state(id_game: int, game_state: str) -> None: - """updates game state on the one provided - """ - DatabaseConnection.cursor.execute('UPDATE public."Game" SET game_state = (%s) WHERE id_game = (%s)', - (game_state, id_game)) - - DatabaseConnection.connection.commit() - - @staticmethod - def get_all_game_tokens(): - """returns all tokens currently existing in the database - """ - DatabaseConnection.cursor.execute(f'SELECT token FROM public."Game"') - tokens = DatabaseConnection.cursor.fetchall() - DatabaseConnection.connection.commit() - - return [x[0] for x in tokens] - - @staticmethod - def get_id_game_from_game_token(token: str) -> int | None: - """returns database game id based on the token, None if the query fails - """ - DatabaseConnection.cursor.execute(f'SELECT id_game FROM public."Game" WHERE token = (%s)', (token,)) - game_id = DatabaseConnection.cursor.fetchone() - DatabaseConnection.connection.commit() - - if not game_id: - return None - - return game_id - - @staticmethod - def get_game_token_from_id_game(id_game: int) -> str | None: - """returns game token based on database game id, None if the query fails - """ - DatabaseConnection.cursor.execute(f'SELECT token FROM public."Game" WHERE id_game = (%s)', (id_game,)) - game_token = DatabaseConnection.cursor.fetchone() - DatabaseConnection.connection.commit() - - if not game_token: - return None - - return game_token diff --git a/dnd-bot/dnd_bot/database/database_game.py b/dnd-bot/dnd_bot/database/database_game.py new file mode 100644 index 00000000..fa7318c1 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_game.py @@ -0,0 +1,84 @@ +from sqlite3 import ProgrammingError + +from dnd_bot.database.database_connection import DatabaseConnection + + +class DatabaseGame: + + @staticmethod + def add_game(token: str, id_host: int, game_state: str, campaign_name: str) -> int | None: + """start game and add game to database + :param token: lobby/game token (5 digit password) + :param id_host: discord id of host + :param game_state: string enum, initial value of added game is 'LOBBY' + :param campaign_name: campaign name + :return: + on success: game id, on failure: None + """ + + DatabaseConnection.cursor.execute( + 'INSERT INTO public."Game" (token, id_host, game_state, campaign_name) VALUES ' + '(%s, %s, %s, %s)', + (token, id_host, game_state, campaign_name)) + DatabaseConnection.cursor.execute('SELECT LASTVAL()') + + try: + game_id = DatabaseConnection.cursor.fetchone()[0] + except ProgrammingError as err: + print(f"db: error adding game {err}") + return None + + DatabaseConnection.connection.commit() + return game_id + + @staticmethod + def start_game(id_game: int) -> None: + """starts game + :param id_game: database game id + """ + DatabaseGame.update_game_state(id_game, 'ACTIVE') + + @staticmethod + def update_game_state(id_game: int, game_state: str) -> None: + """updates game state on the one provided + """ + DatabaseConnection.cursor.execute('UPDATE public."Game" SET game_state = (%s) WHERE id_game = (%s)', + (game_state, id_game)) + + DatabaseConnection.connection.commit() + + @staticmethod + def get_all_game_tokens(): + """returns all tokens currently existing in the database + """ + DatabaseConnection.cursor.execute(f'SELECT token FROM public."Game"') + tokens = DatabaseConnection.cursor.fetchall() + DatabaseConnection.connection.commit() + + return [x[0] for x in tokens] + + @staticmethod + def get_id_game_from_game_token(token: str) -> int | None: + """returns database game id based on the token, None if the query fails + """ + DatabaseConnection.cursor.execute(f'SELECT id_game FROM public."Game" WHERE token = (%s)', (token,)) + game_id = DatabaseConnection.cursor.fetchone() + DatabaseConnection.connection.commit() + + if not game_id: + return None + + return game_id + + @staticmethod + def get_game_token_from_id_game(id_game: int) -> str | None: + """returns game token based on database game id, None if the query fails + """ + DatabaseConnection.cursor.execute(f'SELECT token FROM public."Game" WHERE id_game = (%s)', (id_game,)) + game_token = DatabaseConnection.cursor.fetchone() + DatabaseConnection.connection.commit() + + if not game_token: + return None + + return game_token diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_create.py b/dnd-bot/dnd_bot/logic/lobby/handler_create.py index 406afb8d..a9b9b12e 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_create.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_create.py @@ -1,6 +1,7 @@ import random from dnd_bot.database.database_connection import DatabaseConnection +from dnd_bot.database.database_game import DatabaseGame from dnd_bot.logic.lobby.handler_join import HandlerJoin from dnd_bot.logic.prototype.game import Game from dnd_bot.logic.prototype.multiverse import Multiverse @@ -47,12 +48,12 @@ async def create_lobby(host_id, host_dm_channel, host_username) -> (bool, int, s :param host_username: discord username :return: status, (if creation was successful, new game token, optional error message) """ - tokens = DatabaseConnection.get_all_game_tokens() + tokens = DatabaseGame.get_all_game_tokens() token = await HandlerCreate.generate_token() while token in tokens: token = await HandlerCreate.generate_token() - game_id = DatabaseConnection.add_game(token, host_id, "LOBBY", "Storm King's Thunder") + game_id = DatabaseGame.add_game(token, host_id, "LOBBY", "Storm King's Thunder") if game_id is None: return False, -1, ":no_entry: Error creating game!" diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_start.py b/dnd-bot/dnd_bot/logic/lobby/handler_start.py index a205d0c2..7af79888 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_start.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_start.py @@ -1,3 +1,4 @@ +from dnd_bot.database.database_game import DatabaseGame from dnd_bot.logic.game.game_loop import GameLoop from dnd_bot.logic.game.game_start import GameStart from dnd_bot.logic.prototype.multiverse import Multiverse @@ -17,7 +18,7 @@ async def start_game(token, user_id) -> (bool, list, str): :return: status, (if start was successful, users list, optional error message) """ game = Multiverse.get_game(token) - game_id = DatabaseConnection.get_id_game_from_game_token(token) + game_id = DatabaseGame.get_id_game_from_game_token(token) if game_id is None: return False, [], ":no_entry: Error creating game!" @@ -35,7 +36,7 @@ async def start_game(token, user_id) -> (bool, list, str): if game.game_state == 'LOBBY': game.game_state = "STARTING" - DatabaseConnection.update_game_state(game_id, 'STARTING') + DatabaseGame.update_game_state(game_id, 'STARTING') if game_id is None: game.game_state = 'LOBBY' From 8b490e22050bb397f0213a9ea084949887b0cb67 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 19:09:12 +0100 Subject: [PATCH 02/25] added database_user --- .../dnd_bot/database/database_connection.py | 43 ------------------- dnd-bot/dnd_bot/database/database_game.py | 25 +++++++++++ dnd-bot/dnd_bot/database/database_user.py | 26 +++++++++++ 3 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 dnd-bot/dnd_bot/database/database_user.py diff --git a/dnd-bot/dnd_bot/database/database_connection.py b/dnd-bot/dnd_bot/database/database_connection.py index c6c244f2..8104caa6 100644 --- a/dnd-bot/dnd_bot/database/database_connection.py +++ b/dnd-bot/dnd_bot/database/database_connection.py @@ -59,47 +59,4 @@ def execute_query(query: str): DatabaseConnection.cursor.execute(query) DatabaseConnection.connection.commit() - @staticmethod - def add_user(id_game: int, discord_id: int) -> int | None: - """add user to a game - :param id_game: database game id - :param discord_id: user discord id - """ - - DatabaseConnection.cursor.execute('INSERT INTO public."User" (id_game, discord_id) VALUES (%s, %s)', - (id_game, discord_id)) - DatabaseConnection.cursor.execute('SELECT LASTVAL()') - - try: - user_id = DatabaseConnection.cursor.fetchone()[0] - except ProgrammingError as err: - print(f"db: error adding user {err}") - return None - - DatabaseConnection.connection.commit() - return user_id - - @staticmethod - def find_game_by_token(token: str) -> dict | None: - """find game by token/password - :param token: game token/password to find the game by - :return: on success: dictionary containing game data - it's keys: 'id game' - database game id, 'token' - game token, 'id_host' - - discord host id, 'id_campaign' - campaign id, 'game_state' - string enum, 'players' - list of players - """ - - 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: - return None - - DatabaseConnection.cursor.execute(f'SELECT * FROM public."User" WHERE id_game = {game_tuple[0]}') - users_tuples = DatabaseConnection.cursor.fetchall() - - users = [{'id_user': user_tuple[0], 'id_game': user_tuple[1], 'discord_id': user_tuple[2]} - for user_tuple in users_tuples] - return {'id_game': game_tuple[0], 'token': game_tuple[1], 'id_host': game_tuple[2], - 'id_campaign': game_tuple[3], - 'game_state': game_tuple[4], 'players': users} diff --git a/dnd-bot/dnd_bot/database/database_game.py b/dnd-bot/dnd_bot/database/database_game.py index fa7318c1..da6248e2 100644 --- a/dnd-bot/dnd_bot/database/database_game.py +++ b/dnd-bot/dnd_bot/database/database_game.py @@ -82,3 +82,28 @@ def get_game_token_from_id_game(id_game: int) -> str | None: return None return game_token + + @staticmethod + def find_game_by_token(token: str) -> dict | None: + """find game by token/password + :param token: game token/password to find the game by + :return: on success: dictionary containing game data - it's keys: 'id game' - database game id, 'token' - game token, 'id_host' - + discord host id, 'id_campaign' - campaign id, 'game_state' - string enum, 'players' - list of players + """ + + 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: + return None + + DatabaseConnection.cursor.execute(f'SELECT * FROM public."User" WHERE id_game = {game_tuple[0]}') + users_tuples = DatabaseConnection.cursor.fetchall() + + users = [{'id_user': user_tuple[0], 'id_game': user_tuple[1], 'discord_id': user_tuple[2]} + for user_tuple in users_tuples] + + return {'id_game': game_tuple[0], 'token': game_tuple[1], 'id_host': game_tuple[2], + 'id_campaign': game_tuple[3], + 'game_state': game_tuple[4], 'players': users} diff --git a/dnd-bot/dnd_bot/database/database_user.py b/dnd-bot/dnd_bot/database/database_user.py new file mode 100644 index 00000000..1e847943 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_user.py @@ -0,0 +1,26 @@ +from sqlite3 import ProgrammingError + +from dnd_bot.database.database_connection import DatabaseConnection + + +class DatabaseUser: + + @staticmethod + def add_user(id_game: int, discord_id: int) -> int | None: + """add user to a game + :param id_game: database game id + :param discord_id: user discord id + """ + + DatabaseConnection.cursor.execute('INSERT INTO public."User" (id_game, discord_id) VALUES (%s, %s)', + (id_game, discord_id)) + DatabaseConnection.cursor.execute('SELECT LASTVAL()') + + try: + user_id = DatabaseConnection.cursor.fetchone()[0] + except ProgrammingError as err: + print(f"db: error adding user {err}") + return None + + DatabaseConnection.connection.commit() + return user_id From 663c77e62f923412ae4e201fa9273b4f0ee46b67 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 19:20:53 +0100 Subject: [PATCH 03/25] added database_entity --- dnd-bot/dnd_bot/database/database_entity.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 dnd-bot/dnd_bot/database/database_entity.py diff --git a/dnd-bot/dnd_bot/database/database_entity.py b/dnd-bot/dnd_bot/database/database_entity.py new file mode 100644 index 00000000..8241be23 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_entity.py @@ -0,0 +1,10 @@ +class DatabaseEntity: + + def add_entity(self) -> int | None: + pass + + def update_entity(self, x: int = 0, y: int = 0) -> None: + pass + + def get_entity(self, id_entity: int) -> dict | None: + pass From 125b0e434e70c539e29b0ce39a029be2ce4b9b30 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 19:42:34 +0100 Subject: [PATCH 04/25] added database_creature --- dnd-bot/dnd_bot/database/database_creature.py | 17 +++++++++++++++++ dnd-bot/dnd_bot/database/database_entity.py | 9 ++++++--- dnd-bot/dnd_bot/database/database_user.py | 4 ++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 dnd-bot/dnd_bot/database/database_creature.py diff --git a/dnd-bot/dnd_bot/database/database_creature.py b/dnd-bot/dnd_bot/database/database_creature.py new file mode 100644 index 00000000..49037e39 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_creature.py @@ -0,0 +1,17 @@ +class DatabaseCreature: + + @staticmethod + def add_creature(level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, intelligence: int = 0, + perception: int = 0, initiative: int = 0, action_points: int = 0, money: int = 0, + id_entity: int = 0) -> int | None: + pass + + @staticmethod + def update_creature(id_entity: int = 0, level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, + intelligence: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, + money: int = 0, ) -> None: + pass + + @staticmethod + def get_creature(id_creature: int = 0) -> dict | None: + pass diff --git a/dnd-bot/dnd_bot/database/database_entity.py b/dnd-bot/dnd_bot/database/database_entity.py index 8241be23..9c72020d 100644 --- a/dnd-bot/dnd_bot/database/database_entity.py +++ b/dnd-bot/dnd_bot/database/database_entity.py @@ -1,10 +1,13 @@ class DatabaseEntity: - def add_entity(self) -> int | None: + @staticmethod + def add_entity(name: str = "", x: int = 0, y: int = 0, sprite=None, id_game: int = 0) -> int | None: pass - def update_entity(self, x: int = 0, y: int = 0) -> None: + @staticmethod + def update_entity(id_entity: int = 0, x: int = 0, y: int = 0) -> None: pass - def get_entity(self, id_entity: int) -> dict | None: + @staticmethod + def get_entity(id_entity: int) -> dict | None: pass diff --git a/dnd-bot/dnd_bot/database/database_user.py b/dnd-bot/dnd_bot/database/database_user.py index 1e847943..b3ef4eb3 100644 --- a/dnd-bot/dnd_bot/database/database_user.py +++ b/dnd-bot/dnd_bot/database/database_user.py @@ -24,3 +24,7 @@ def add_user(id_game: int, discord_id: int) -> int | None: DatabaseConnection.connection.commit() return user_id + + @staticmethod + def get_user(discord_id: int = 0) -> dict | None: + pass From ca411bc28b58de3d09fcccc6daa92fac43b9e3c6 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 19:52:26 +0100 Subject: [PATCH 05/25] added database_player --- dnd-bot/dnd_bot/database/database_creature.py | 10 +++++----- dnd-bot/dnd_bot/database/database_player.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 dnd-bot/dnd_bot/database/database_player.py diff --git a/dnd-bot/dnd_bot/database/database_creature.py b/dnd-bot/dnd_bot/database/database_creature.py index 49037e39..62d6c7f2 100644 --- a/dnd-bot/dnd_bot/database/database_creature.py +++ b/dnd-bot/dnd_bot/database/database_creature.py @@ -1,15 +1,15 @@ class DatabaseCreature: @staticmethod - def add_creature(level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, intelligence: int = 0, - perception: int = 0, initiative: int = 0, action_points: int = 0, money: int = 0, - id_entity: int = 0) -> int | None: + def add_creature(id_entity: int = 0, level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, + intelligence: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, + money: int = 0) -> int | None: pass @staticmethod - def update_creature(id_entity: int = 0, level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, + def update_creature(id_creature: int = 0, level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, intelligence: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, - money: int = 0, ) -> None: + money: int = 0) -> None: pass @staticmethod diff --git a/dnd-bot/dnd_bot/database/database_player.py b/dnd-bot/dnd_bot/database/database_player.py new file mode 100644 index 00000000..b75ff60f --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_player.py @@ -0,0 +1,10 @@ +class DatabasePlayer: + + @staticmethod + def add_player(id_user: int = 0, alignment: str = "", backstory: str = "", id_equipment: int = 0, + id_creature: int = 0) -> int | None: + pass + + @staticmethod + def get_player(id_player) -> dict | None: + pass From 008eab4dd0f56ee7d12c3be63833a7426a2e5407 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 20:03:54 +0100 Subject: [PATCH 06/25] added database_item --- dnd-bot/dnd_bot/database/database_creature.py | 4 ++++ dnd-bot/dnd_bot/database/database_item.py | 16 ++++++++++++++++ dnd-bot/dnd_bot/logic/prototype/item.py | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 dnd-bot/dnd_bot/database/database_item.py diff --git a/dnd-bot/dnd_bot/database/database_creature.py b/dnd-bot/dnd_bot/database/database_creature.py index 62d6c7f2..a8d145dc 100644 --- a/dnd-bot/dnd_bot/database/database_creature.py +++ b/dnd-bot/dnd_bot/database/database_creature.py @@ -15,3 +15,7 @@ def update_creature(id_creature: int = 0, level: int = 0, hp: int = 0, strength: @staticmethod def get_creature(id_creature: int = 0) -> dict | None: pass + + @staticmethod + def get_creature_items(id_creature) -> list | None: + pass diff --git a/dnd-bot/dnd_bot/database/database_item.py b/dnd-bot/dnd_bot/database/database_item.py new file mode 100644 index 00000000..fadb7176 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_item.py @@ -0,0 +1,16 @@ +class DatabaseItem: + + @staticmethod + def add_item(name: str = "", hp: int = 0, strength: int = 0, dexterity: int = 0, intelligence: int = 0, + charisma: int = 0, perception: int = 0, action_points: int = 0, effect: str = "", + base_price: int = 0) -> int | None: + pass + + @staticmethod + def get_item(id_item) -> dict | None: + pass + + @staticmethod + def add_creature_item(id_creature: int = 0, id_item: int = 0, amount: int = 0) -> None: + pass + diff --git a/dnd-bot/dnd_bot/logic/prototype/item.py b/dnd-bot/dnd_bot/logic/prototype/item.py index 14827a58..ba13a2e1 100644 --- a/dnd-bot/dnd_bot/logic/prototype/item.py +++ b/dnd-bot/dnd_bot/logic/prototype/item.py @@ -1,7 +1,8 @@ class Item: """represents an item in the player's inventory""" def __init__(self, id_item: int = 0, name: str = "", hp: int = 0, strength: int = 0, dexterity: int = 0, - intelligence: int = 0, charisma: int = 0, perception: int = 0, action_points: int = 0, effect=None): + intelligence: int = 0, charisma: int = 0, perception: int = 0, action_points: int = 0, + effect: str = ""): self.id = id_item self.name = name self.hp = hp From 32337186510dc8d39fa41a167535cb65f7f5dcb8 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 20:10:01 +0100 Subject: [PATCH 07/25] added database_equipment --- dnd-bot/dnd_bot/database/database_equipment.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 dnd-bot/dnd_bot/database/database_equipment.py diff --git a/dnd-bot/dnd_bot/database/database_equipment.py b/dnd-bot/dnd_bot/database/database_equipment.py new file mode 100644 index 00000000..f0023745 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_equipment.py @@ -0,0 +1,15 @@ +class DatabaseEquipment: + + @staticmethod + def add_equipment(helmet: int = -1, chest: int = -1, leg_armor: int = -1, boots: int = -1, left_hand=-1, + right_hand: int = -1, accessory: int = -1) -> int | None: + pass + + @staticmethod + def update_equipment(id_equipment: int = 0, helmet: int = -1, chest: int = -1, leg_armor: int = -1, boots: int = -1, + left_hand: int = -1, right_hand: int = -1, accessory: int = -1) -> None: + pass + + @staticmethod + def get_equipment(id_equipment: int = 0) -> dict | None: + pass From a2f72c4f3bf9f331054921fe2c5e4d577da33682 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 20:14:54 +0100 Subject: [PATCH 08/25] added database_skill --- dnd-bot/dnd_bot/database/database_entity.py | 4 ++++ dnd-bot/dnd_bot/database/database_skill.py | 13 +++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 dnd-bot/dnd_bot/database/database_skill.py diff --git a/dnd-bot/dnd_bot/database/database_entity.py b/dnd-bot/dnd_bot/database/database_entity.py index 9c72020d..75a99774 100644 --- a/dnd-bot/dnd_bot/database/database_entity.py +++ b/dnd-bot/dnd_bot/database/database_entity.py @@ -11,3 +11,7 @@ def update_entity(id_entity: int = 0, x: int = 0, y: int = 0) -> None: @staticmethod def get_entity(id_entity: int) -> dict | None: pass + + @staticmethod + def get_entity_skills(id_entity: int = 0) -> list | None: + pass \ No newline at end of file diff --git a/dnd-bot/dnd_bot/database/database_skill.py b/dnd-bot/dnd_bot/database/database_skill.py new file mode 100644 index 00000000..0e62aa56 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_skill.py @@ -0,0 +1,13 @@ +class DatabaseSkill: + + @staticmethod + def add_skill(name: str = "") -> int | None: + pass + + @staticmethod + def get_skill(id_skill) -> dict | None: + pass + + @staticmethod + def add_entity_skill(id_entity: int = 0, id_skill: int = 0) -> None: + pass From 8057f583124c104a7b3fd9052a7b56fcea648be4 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 20:19:19 +0100 Subject: [PATCH 09/25] added database_event --- dnd-bot/dnd_bot/database/database_event.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 dnd-bot/dnd_bot/database/database_event.py diff --git a/dnd-bot/dnd_bot/database/database_event.py b/dnd-bot/dnd_bot/database/database_event.py new file mode 100644 index 00000000..219c3a91 --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_event.py @@ -0,0 +1,14 @@ +class DatabaseEvent: + + @staticmethod + def add_event(x: int = 0, y: int = 0, range: int = 0, status: str = "", content: str = "", + id_game: int = 0) -> int | None: + pass + + @staticmethod + def get_event(id_event) -> dict | None: + pass + + @staticmethod + def get_all_events() -> list | None: + pass From 2e37fb594a9148037e4bb39945d8264579224dfb Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 20:23:15 +0100 Subject: [PATCH 10/25] added database_dialog --- dnd-bot/dnd_bot/database/database_dialog.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 dnd-bot/dnd_bot/database/database_dialog.py diff --git a/dnd-bot/dnd_bot/database/database_dialog.py b/dnd-bot/dnd_bot/database/database_dialog.py new file mode 100644 index 00000000..2dab7a5f --- /dev/null +++ b/dnd-bot/dnd_bot/database/database_dialog.py @@ -0,0 +1,17 @@ +class DatabaseDialog: + + @staticmethod + def add_dialog(id_speaker: int = 0, id_listener: int = 0, content: str = "", status: str = "") -> int | None: + pass + + @staticmethod + def update_dialog(id_dialog: int, status: str = "") -> None: + pass + + @staticmethod + def get_dialog(id_dialog: int = 0) -> dict | None: + pass + + @staticmethod + def get_all_dialogs() -> list | None: + pass From 1b68d4c4f4f3c118f2fc20c4d1ecb959e7f06083 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 20:28:40 +0100 Subject: [PATCH 11/25] added dialog to prototype --- dnd-bot/dnd_bot/logic/prototype/dialog.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 dnd-bot/dnd_bot/logic/prototype/dialog.py diff --git a/dnd-bot/dnd_bot/logic/prototype/dialog.py b/dnd-bot/dnd_bot/logic/prototype/dialog.py new file mode 100644 index 00000000..da8a6fc1 --- /dev/null +++ b/dnd-bot/dnd_bot/logic/prototype/dialog.py @@ -0,0 +1,7 @@ +class Dialog: + """represents a dialog in the game""" + def __init__(self, id_speaker: int = 0, id_listener: int = 0, content: str = "", status: str = ""): + self.id_speaker = id_speaker + self.id_listener = id_listener + self.content = content + self.status = status From 1f5224fcf072919bbce555b50fb770378fd646f5 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 3 Mar 2023 21:01:15 +0100 Subject: [PATCH 12/25] added id to every prototype class --- dnd-bot/dnd_bot/logic/prototype/database_object.py | 3 +++ dnd-bot/dnd_bot/logic/prototype/dialog.py | 8 +++++++- dnd-bot/dnd_bot/logic/prototype/entity.py | 9 +++++++-- dnd-bot/dnd_bot/logic/prototype/equipment.py | 5 ++++- dnd-bot/dnd_bot/logic/prototype/event.py | 7 ++++++- dnd-bot/dnd_bot/logic/prototype/game.py | 5 ++++- dnd-bot/dnd_bot/logic/prototype/item.py | 12 ++++++++++-- dnd-bot/dnd_bot/logic/prototype/skill.py | 7 ++++++- dnd-bot/dnd_bot/logic/prototype/user.py | 7 ++++++- 9 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 dnd-bot/dnd_bot/logic/prototype/database_object.py diff --git a/dnd-bot/dnd_bot/logic/prototype/database_object.py b/dnd-bot/dnd_bot/logic/prototype/database_object.py new file mode 100644 index 00000000..48a087fb --- /dev/null +++ b/dnd-bot/dnd_bot/logic/prototype/database_object.py @@ -0,0 +1,3 @@ +class DatabaseObject: + def __init__(self, id): + self.id = id \ No newline at end of file diff --git a/dnd-bot/dnd_bot/logic/prototype/dialog.py b/dnd-bot/dnd_bot/logic/prototype/dialog.py index da8a6fc1..1adc06a0 100644 --- a/dnd-bot/dnd_bot/logic/prototype/dialog.py +++ b/dnd-bot/dnd_bot/logic/prototype/dialog.py @@ -1,6 +1,12 @@ -class Dialog: +from dnd_bot.database.database_dialog import DatabaseDialog +from dnd_bot.logic.prototype.database_object import DatabaseObject + + +class Dialog(DatabaseObject): """represents a dialog in the game""" + def __init__(self, id_speaker: int = 0, id_listener: int = 0, content: str = "", status: str = ""): + ## TODO super().__init__(DatabaseDialog.add_dialog(id_speaker, id_listener, content, status)) self.id_speaker = id_speaker self.id_listener = id_listener self.content = content diff --git a/dnd-bot/dnd_bot/logic/prototype/entity.py b/dnd-bot/dnd_bot/logic/prototype/entity.py index 980cad74..8f72fdb1 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entity.py +++ b/dnd-bot/dnd_bot/logic/prototype/entity.py @@ -1,11 +1,16 @@ import cv2 as cv +from dnd_bot.logic.prototype.database_object import DatabaseObject -class Entity: + +class Entity(DatabaseObject): """This class is the base class for all entities in the game like creatures and elements on the map""" - def __init__(self, x: int = 0, y: int = 0, sprite=None, name: str = 'Entity', id_game: int = 0, game_token: str = '', + + def __init__(self, x: int = 0, y: int = 0, sprite=None, name: str = 'Entity', id_game: int = 0, + game_token: str = '', skills=None, fragile: bool = False, look_direction: str = 'down'): """":param fragile: if entity can be moved or destroyed from its position""" + ## TODO super().__init__(DatabaseEntity.add_entity(name, x, y, sprite, id_game)) if skills is None: skills = [] self.x = x diff --git a/dnd-bot/dnd_bot/logic/prototype/equipment.py b/dnd-bot/dnd_bot/logic/prototype/equipment.py index 4e5c061e..a9ff25aa 100644 --- a/dnd-bot/dnd_bot/logic/prototype/equipment.py +++ b/dnd-bot/dnd_bot/logic/prototype/equipment.py @@ -1,11 +1,14 @@ +from dnd_bot.database.database_equipment import DatabaseEquipment +from dnd_bot.logic.prototype.database_object import DatabaseObject from dnd_bot.logic.prototype.item import Item -class Equipment: +class Equipment(DatabaseObject): """represents the player's equipped items""" def __init__(self, id_equipment: int, helmet: Item = None, chest: Item = None, leg_armor: Item = None, boots: Item = None, left_hand: Item = None, right_hand: Item = None, item_list=None, accessory: Item = None): + ## TODO super().__init__() if item_list is None: item_list = [] self.id = id_equipment diff --git a/dnd-bot/dnd_bot/logic/prototype/event.py b/dnd-bot/dnd_bot/logic/prototype/event.py index faccd43c..5d462412 100644 --- a/dnd-bot/dnd_bot/logic/prototype/event.py +++ b/dnd-bot/dnd_bot/logic/prototype/event.py @@ -1,6 +1,11 @@ -class Event: +from dnd_bot.database.database_event import DatabaseEvent +from dnd_bot.logic.prototype.database_object import DatabaseObject + + +class Event(DatabaseObject): """represents various events that happen randomly in the game""" def __init__(self, x: int = 0, y: int = 0, range: int = 0, status: str = "", content: str = "", id_game:int = 0): + ## TODO super().__init__(DatabaseEvent.add_event(x, y, range, status, content, id_game)) self.x = x self.y = y self.range = range diff --git a/dnd-bot/dnd_bot/logic/prototype/game.py b/dnd-bot/dnd_bot/logic/prototype/game.py index 52bac87e..9c16ea76 100644 --- a/dnd-bot/dnd_bot/logic/prototype/game.py +++ b/dnd-bot/dnd_bot/logic/prototype/game.py @@ -1,15 +1,18 @@ from collections import deque +from dnd_bot.database.database_game import DatabaseGame from dnd_bot.logic.prototype.creature import Creature +from dnd_bot.logic.prototype.database_object import DatabaseObject from dnd_bot.logic.prototype.player import Player from dnd_bot.logic.prototype.user import User -class Game: +class Game(DatabaseObject): """class represents particular games and lobbies""" def __init__(self, token: str = None, id_host: int = None, id_campaign: int = None, game_state: str = "LOBBY", user_list=None, events=None, queue=None, world_width: int = 0, world_height: int = 0): + ## TODO super().__init__(DatabaseGame.add_game(token, id_host, game_state, id_campaign)) if user_list is None: user_list = [] if events is None: diff --git a/dnd-bot/dnd_bot/logic/prototype/item.py b/dnd-bot/dnd_bot/logic/prototype/item.py index ba13a2e1..daf0c6c4 100644 --- a/dnd-bot/dnd_bot/logic/prototype/item.py +++ b/dnd-bot/dnd_bot/logic/prototype/item.py @@ -1,8 +1,15 @@ -class Item: +from dnd_bot.database.database_item import DatabaseItem +from dnd_bot.logic.prototype.database_object import DatabaseObject + + +class Item(DatabaseObject): """represents an item in the player's inventory""" + def __init__(self, id_item: int = 0, name: str = "", hp: int = 0, strength: int = 0, dexterity: int = 0, intelligence: int = 0, charisma: int = 0, perception: int = 0, action_points: int = 0, - effect: str = ""): + effect: str = "", base_price: int = 0): + ## TODO super().__init__(DatabaseItem.add_item(name,hp,strength, dexterity, intelligence, charisma, perception, + ## action_points, effect, base_price)) self.id = id_item self.name = name self.hp = hp @@ -13,3 +20,4 @@ def __init__(self, id_item: int = 0, name: str = "", hp: int = 0, strength: int self.perception = perception self.action_points = action_points self.effect = effect + self.base_price = base_price diff --git a/dnd-bot/dnd_bot/logic/prototype/skill.py b/dnd-bot/dnd_bot/logic/prototype/skill.py index b192de92..f12bfe6e 100644 --- a/dnd-bot/dnd_bot/logic/prototype/skill.py +++ b/dnd-bot/dnd_bot/logic/prototype/skill.py @@ -1,6 +1,11 @@ -class Skill: +from dnd_bot.database.database_skill import DatabaseSkill +from dnd_bot.logic.prototype.database_object import DatabaseObject + + +class Skill(DatabaseObject): """Represents a skill that can be used by entity""" def __init__(self, id_skill: int = 0, name: str = ""): + ## TODO super().__init__(DatabaseSkill.add_skill(name)) self.id_skill = id_skill self.name = name diff --git a/dnd-bot/dnd_bot/logic/prototype/user.py b/dnd-bot/dnd_bot/logic/prototype/user.py index 27db65b8..33062918 100644 --- a/dnd-bot/dnd_bot/logic/prototype/user.py +++ b/dnd-bot/dnd_bot/logic/prototype/user.py @@ -1,7 +1,12 @@ -class User: +from dnd_bot.database.database_user import DatabaseUser +from dnd_bot.logic.prototype.database_object import DatabaseObject + + +class User(DatabaseObject): """defines discord user""" def __init__(self, game_token: str = "", discord_id: int = 0, channel_id: int = 0, username: str = "", color: str = ""): + ## TODO super().__init__(DatabaseUser.add_user()) self.game_token = game_token self.discord_id = discord_id self.channel_id = channel_id From 92a08954a684ae354c9996c19afc51e05794d275 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Sat, 4 Mar 2023 13:43:46 +0100 Subject: [PATCH 13/25] database_connection: added add_to_db method --- dnd-bot/dnd_bot/database/database_connection.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dnd-bot/dnd_bot/database/database_connection.py b/dnd-bot/dnd_bot/database/database_connection.py index 8104caa6..3d631c28 100644 --- a/dnd-bot/dnd_bot/database/database_connection.py +++ b/dnd-bot/dnd_bot/database/database_connection.py @@ -59,4 +59,18 @@ def execute_query(query: str): DatabaseConnection.cursor.execute(query) DatabaseConnection.connection.commit() + @staticmethod + def add_to_db(query: str = "", parameters: tuple = None) -> int | None: + DatabaseConnection.cursor.execute(query, parameters) + DatabaseConnection.cursor.execute('SELECT LASTVAL()') + + try: + id = DatabaseConnection.cursor.fetchone()[0] + except ProgrammingError as err: + print(f"db: error adding element {err}") + return None + + DatabaseConnection.connection.commit() + return id + From 5bba81b2a50b7f3f1a79a69eae51b709fa3c1dfb Mon Sep 17 00:00:00 2001 From: kapselccc Date: Sat, 4 Mar 2023 14:28:29 +0100 Subject: [PATCH 14/25] changed adding game to db --- dnd-bot/dnd_bot/database/database_game.py | 17 ++--------------- dnd-bot/dnd_bot/logic/lobby/handler_create.py | 9 ++++----- dnd-bot/dnd_bot/logic/lobby/handler_start.py | 5 +++-- dnd-bot/dnd_bot/logic/prototype/game.py | 6 +++--- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/dnd-bot/dnd_bot/database/database_game.py b/dnd-bot/dnd_bot/database/database_game.py index da6248e2..50ec27b6 100644 --- a/dnd-bot/dnd_bot/database/database_game.py +++ b/dnd-bot/dnd_bot/database/database_game.py @@ -15,21 +15,8 @@ def add_game(token: str, id_host: int, game_state: str, campaign_name: str) -> i :return: on success: game id, on failure: None """ - - DatabaseConnection.cursor.execute( - 'INSERT INTO public."Game" (token, id_host, game_state, campaign_name) VALUES ' - '(%s, %s, %s, %s)', - (token, id_host, game_state, campaign_name)) - DatabaseConnection.cursor.execute('SELECT LASTVAL()') - - try: - game_id = DatabaseConnection.cursor.fetchone()[0] - except ProgrammingError as err: - print(f"db: error adding game {err}") - return None - - DatabaseConnection.connection.commit() - return game_id + return DatabaseConnection.add_to_db('INSERT INTO public."Game" (token, id_host, game_state, campaign_name)' + 'VALUES (%s, %s, %s, %s)', (token, id_host, game_state, campaign_name)) @staticmethod def start_game(id_game: int) -> None: diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_create.py b/dnd-bot/dnd_bot/logic/lobby/handler_create.py index a9b9b12e..4852be04 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_create.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_create.py @@ -2,6 +2,7 @@ from dnd_bot.database.database_connection import DatabaseConnection from dnd_bot.database.database_game import DatabaseGame +from dnd_bot.database.database_user import DatabaseUser from dnd_bot.logic.lobby.handler_join import HandlerJoin from dnd_bot.logic.prototype.game import Game from dnd_bot.logic.prototype.multiverse import Multiverse @@ -53,16 +54,14 @@ async def create_lobby(host_id, host_dm_channel, host_username) -> (bool, int, s while token in tokens: token = await HandlerCreate.generate_token() - game_id = DatabaseGame.add_game(token, host_id, "LOBBY", "Storm King's Thunder") - if game_id is None: + game = Game(token, host_id, "Storm King's Thunder", "LOBBY") + if game.id is None: return False, -1, ":no_entry: Error creating game!" - user_id = DatabaseConnection.add_user(game_id, host_id) + user_id = DatabaseUser.add_user(game.id, host_id) if user_id is None: return False, -1, ":no_entry: Error creating host user" - game = Game(token) - if not Multiverse.masks: Multiverse.generate_masks() Multiverse.add_game(game) diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_start.py b/dnd-bot/dnd_bot/logic/lobby/handler_start.py index 7af79888..d33ef352 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_start.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_start.py @@ -1,4 +1,5 @@ from dnd_bot.database.database_game import DatabaseGame +from dnd_bot.database.database_user import DatabaseUser from dnd_bot.logic.game.game_loop import GameLoop from dnd_bot.logic.game.game_start import GameStart from dnd_bot.logic.prototype.multiverse import Multiverse @@ -23,7 +24,7 @@ async def start_game(token, user_id) -> (bool, list, str): 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) + DatabaseUser.add_user(game_id, user.discord_id) if game is None: return False, [], f':warning: Game of provided token doesn\'t exist!' @@ -42,7 +43,7 @@ async def start_game(token, user_id) -> (bool, list, str): game.game_state = 'LOBBY' return False, [], ":warning: Error creating game!" for user in game.user_list: - DatabaseConnection.add_user(game_id, user.discord_id) + DatabaseUser.add_user(game_id, user.discord_id) GameStart.start(token) await GameLoop.start_loop(token) diff --git a/dnd-bot/dnd_bot/logic/prototype/game.py b/dnd-bot/dnd_bot/logic/prototype/game.py index 9c16ea76..6501b3f2 100644 --- a/dnd-bot/dnd_bot/logic/prototype/game.py +++ b/dnd-bot/dnd_bot/logic/prototype/game.py @@ -10,16 +10,16 @@ class Game(DatabaseObject): """class represents particular games and lobbies""" - def __init__(self, token: str = None, id_host: int = None, id_campaign: int = None, game_state: str = "LOBBY", + def __init__(self, token: str = None, id_host: int = None, campaign_name: str = "", game_state: str = "LOBBY", user_list=None, events=None, queue=None, world_width: int = 0, world_height: int = 0): - ## TODO super().__init__(DatabaseGame.add_game(token, id_host, game_state, id_campaign)) + super().__init__(DatabaseGame.add_game(token, id_host, game_state, campaign_name)) if user_list is None: user_list = [] if events is None: events = [] self.id_host = id_host self.token = token - self.id_campaign = id_campaign + self.campaign_name = campaign_name self.game_state = game_state self.user_list = user_list self.entities = [] From 1f8dcf1c7c3fd72ad209214903fca7225123bdc3 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Sat, 4 Mar 2023 15:20:38 +0100 Subject: [PATCH 15/25] database_user: added adding user;lobby: changed handlers --- .../dnd_bot/database/database_connection.py | 4 +-- dnd-bot/dnd_bot/database/database_game.py | 2 +- dnd-bot/dnd_bot/database/database_user.py | 26 ++++++++++--------- dnd-bot/dnd_bot/logic/lobby/handler_create.py | 11 +++++--- dnd-bot/dnd_bot/logic/lobby/handler_join.py | 2 ++ dnd-bot/dnd_bot/logic/lobby/handler_start.py | 4 --- dnd-bot/dnd_bot/logic/prototype/game.py | 13 +++------- dnd-bot/dnd_bot/logic/prototype/user.py | 9 ++++--- 8 files changed, 36 insertions(+), 35 deletions(-) diff --git a/dnd-bot/dnd_bot/database/database_connection.py b/dnd-bot/dnd_bot/database/database_connection.py index 3d631c28..42bd349c 100644 --- a/dnd-bot/dnd_bot/database/database_connection.py +++ b/dnd-bot/dnd_bot/database/database_connection.py @@ -60,14 +60,14 @@ def execute_query(query: str): DatabaseConnection.connection.commit() @staticmethod - def add_to_db(query: str = "", parameters: tuple = None) -> int | None: + def add_to_db(query: str = "", parameters: tuple = None, element_name: str = "element") -> int | None: DatabaseConnection.cursor.execute(query, parameters) DatabaseConnection.cursor.execute('SELECT LASTVAL()') try: id = DatabaseConnection.cursor.fetchone()[0] except ProgrammingError as err: - print(f"db: error adding element {err}") + print(f"db: error adding {element_name} {err}") return None DatabaseConnection.connection.commit() diff --git a/dnd-bot/dnd_bot/database/database_game.py b/dnd-bot/dnd_bot/database/database_game.py index 50ec27b6..aadec09d 100644 --- a/dnd-bot/dnd_bot/database/database_game.py +++ b/dnd-bot/dnd_bot/database/database_game.py @@ -16,7 +16,7 @@ def add_game(token: str, id_host: int, game_state: str, campaign_name: str) -> i on success: game id, on failure: None """ return DatabaseConnection.add_to_db('INSERT INTO public."Game" (token, id_host, game_state, campaign_name)' - 'VALUES (%s, %s, %s, %s)', (token, id_host, game_state, campaign_name)) + 'VALUES (%s, %s, %s, %s)', (token, id_host, game_state, campaign_name), "game") @staticmethod def start_game(id_game: int) -> None: diff --git a/dnd-bot/dnd_bot/database/database_user.py b/dnd-bot/dnd_bot/database/database_user.py index b3ef4eb3..2d0b1b0d 100644 --- a/dnd-bot/dnd_bot/database/database_user.py +++ b/dnd-bot/dnd_bot/database/database_user.py @@ -11,20 +11,22 @@ def add_user(id_game: int, discord_id: int) -> int | None: :param id_game: database game id :param discord_id: user discord id """ + return DatabaseConnection.add_to_db('INSERT INTO public."User" (id_game, discord_id) VALUES (%s, %s)', + (id_game, discord_id), "user") - DatabaseConnection.cursor.execute('INSERT INTO public."User" (id_game, discord_id) VALUES (%s, %s)', - (id_game, discord_id)) - DatabaseConnection.cursor.execute('SELECT LASTVAL()') + @staticmethod + def get_user(discord_id: int = 0) -> dict | None: + pass + + @staticmethod + def get_user_id_from_discord_id(discord_id: int = 0, id_game: int = 0) -> int | None: + DatabaseConnection.cursor.execute(f'SELECT id_user FROM public."User" WHERE discord_id = (%s) AND id_game = (%s)', + (discord_id, id_game)) + id_user = DatabaseConnection.cursor.fetchone() + DatabaseConnection.connection.commit() - try: - user_id = DatabaseConnection.cursor.fetchone()[0] - except ProgrammingError as err: - print(f"db: error adding user {err}") + if not id_user: return None - DatabaseConnection.connection.commit() - return user_id + return id_user - @staticmethod - def get_user(discord_id: int = 0) -> dict | None: - pass diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_create.py b/dnd-bot/dnd_bot/logic/lobby/handler_create.py index 4852be04..7b041a2f 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_create.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_create.py @@ -6,6 +6,7 @@ from dnd_bot.logic.lobby.handler_join import HandlerJoin from dnd_bot.logic.prototype.game import Game from dnd_bot.logic.prototype.multiverse import Multiverse +from dnd_bot.logic.prototype.user import User generated_ids = [] MAX_RANDOM_VALUE = 10000 @@ -54,19 +55,21 @@ async def create_lobby(host_id, host_dm_channel, host_username) -> (bool, int, s while token in tokens: token = await HandlerCreate.generate_token() + DatabaseGame.add_game(token, host_id, "LOBBY", "Storm King's Thunder") game = Game(token, host_id, "Storm King's Thunder", "LOBBY") + if game.id is None: return False, -1, ":no_entry: Error creating game!" - user_id = DatabaseUser.add_user(game.id, host_id) - if user_id is None: + DatabaseUser.add_user(game.id, host_id) + user = User(token, host_id, host_dm_channel, host_username, HandlerJoin.get_color_by_index(0), True) + if user.id is None: return False, -1, ":no_entry: Error creating host user" if not Multiverse.masks: Multiverse.generate_masks() Multiverse.add_game(game) - Multiverse.get_game(token).add_host(host_id, host_dm_channel, host_username, - HandlerJoin.get_color_by_index(0)) + Multiverse.get_game(token).add_host(user) return True, token, "" diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_join.py b/dnd-bot/dnd_bot/logic/lobby/handler_join.py index 1aa3fe42..7eff55ad 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_join.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_join.py @@ -1,3 +1,4 @@ +from dnd_bot.database.database_user import DatabaseUser from dnd_bot.logic.prototype.multiverse import Multiverse @@ -31,6 +32,7 @@ async def join_lobby(token, user_id, user_dm_channel, username) -> (bool, list, if game.game_state != 'LOBBY': return False, [], f':no_entry: This game has already started!' + DatabaseUser.add_user(game.id, user_id) game.add_player(user_id, user_dm_channel, username, HandlerJoin.get_color_by_index(len(game.user_list))) users = game.user_list diff --git a/dnd-bot/dnd_bot/logic/lobby/handler_start.py b/dnd-bot/dnd_bot/logic/lobby/handler_start.py index d33ef352..b8b5ced2 100644 --- a/dnd-bot/dnd_bot/logic/lobby/handler_start.py +++ b/dnd-bot/dnd_bot/logic/lobby/handler_start.py @@ -23,8 +23,6 @@ async def start_game(token, user_id) -> (bool, list, str): if game_id is None: return False, [], ":no_entry: Error creating game!" - for user in game.user_list: - DatabaseUser.add_user(game_id, user.discord_id) if game is None: return False, [], f':warning: Game of provided token doesn\'t exist!' @@ -42,8 +40,6 @@ async def start_game(token, user_id) -> (bool, list, str): if game_id is None: game.game_state = 'LOBBY' return False, [], ":warning: Error creating game!" - for user in game.user_list: - DatabaseUser.add_user(game_id, user.discord_id) GameStart.start(token) await GameLoop.start_loop(token) diff --git a/dnd-bot/dnd_bot/logic/prototype/game.py b/dnd-bot/dnd_bot/logic/prototype/game.py index 6501b3f2..b39615aa 100644 --- a/dnd-bot/dnd_bot/logic/prototype/game.py +++ b/dnd-bot/dnd_bot/logic/prototype/game.py @@ -12,7 +12,7 @@ class Game(DatabaseObject): def __init__(self, token: str = None, id_host: int = None, campaign_name: str = "", game_state: str = "LOBBY", user_list=None, events=None, queue=None, world_width: int = 0, world_height: int = 0): - super().__init__(DatabaseGame.add_game(token, id_host, game_state, campaign_name)) + super().__init__(DatabaseGame.get_id_game_from_game_token(token)) if user_list is None: user_list = [] if events is None: @@ -46,17 +46,12 @@ def add_player(self, user_id, user_channel_id, username, color): """ self.user_list.append(User(self.token, user_id, user_channel_id, username, color)) - def add_host(self, user_id, user_channel_id, username, color): + def add_host(self, user: User): """ adds player as the host to the game - :param user_id: users discord id - :param user_channel_id: private discord channel id - :param username: username - :param color: string representing color + :param user: user that is the host of the game :return: None """ - self.id_host = user_id - user = User(self.token, user_id, user_channel_id, username, color) - user.is_host = True + self.id_host = user.discord_id self.user_list.append(user) def find_user(self, discord_id): diff --git a/dnd-bot/dnd_bot/logic/prototype/user.py b/dnd-bot/dnd_bot/logic/prototype/user.py index 33062918..2c182e48 100644 --- a/dnd-bot/dnd_bot/logic/prototype/user.py +++ b/dnd-bot/dnd_bot/logic/prototype/user.py @@ -1,3 +1,4 @@ +from dnd_bot.database.database_game import DatabaseGame from dnd_bot.database.database_user import DatabaseUser from dnd_bot.logic.prototype.database_object import DatabaseObject @@ -5,13 +6,15 @@ class User(DatabaseObject): """defines discord user""" - def __init__(self, game_token: str = "", discord_id: int = 0, channel_id: int = 0, username: str = "", color: str = ""): - ## TODO super().__init__(DatabaseUser.add_user()) + def __init__(self, game_token: str = "", discord_id: int = 0, channel_id: int = 0, username: str = "", + color: str = "", is_host: bool = False): + game_id = DatabaseGame.get_id_game_from_game_token(game_token) + super().__init__(DatabaseUser.get_user_id_from_discord_id(discord_id, game_id)) self.game_token = game_token self.discord_id = discord_id self.channel_id = channel_id self.username = username - self.is_host = False + self.is_host = is_host self.is_ready = False self.color = color From 6cb79a99a5ccbb4632d505a0f21faa44ead2c986 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Mon, 6 Mar 2023 20:20:28 +0100 Subject: [PATCH 16/25] adding entity to db --- dnd-bot/dnd_bot/database/database_entity.py | 8 +++- dnd-bot/dnd_bot/database/database_player.py | 3 ++ .../dnd_bot/logic/game/initialize_world.py | 41 +++++++++++++------ .../dnd_bot/logic/prototype/entities/hole.py | 4 +- .../logic/prototype/entities/mushrooms.py | 4 +- .../dnd_bot/logic/prototype/entities/rock.py | 4 +- .../entities/walls/dungeon_connector.py | 4 +- .../entities/walls/dungeon_corner_in.py | 4 +- .../entities/walls/dungeon_corner_out.py | 4 +- .../entities/walls/dungeon_crossroads.py | 4 +- .../entities/walls/dungeon_pillar_a.py | 4 +- .../entities/walls/dungeon_pillar_b.py | 4 +- .../entities/walls/dungeon_pillar_c.py | 4 +- .../entities/walls/dungeon_straight_a.py | 4 +- .../entities/walls/dungeon_straight_b.py | 4 +- dnd-bot/dnd_bot/logic/prototype/entity.py | 4 +- 16 files changed, 64 insertions(+), 40 deletions(-) diff --git a/dnd-bot/dnd_bot/database/database_entity.py b/dnd-bot/dnd_bot/database/database_entity.py index 75a99774..1f2b3e52 100644 --- a/dnd-bot/dnd_bot/database/database_entity.py +++ b/dnd-bot/dnd_bot/database/database_entity.py @@ -1,8 +1,12 @@ +from dnd_bot.database.database_connection import DatabaseConnection + + class DatabaseEntity: @staticmethod def add_entity(name: str = "", x: int = 0, y: int = 0, sprite=None, id_game: int = 0) -> int | None: - pass + return DatabaseConnection.add_to_db('INSERT INTO public."Entity" (name, x, y, sprite, id_game) VALUES' + '(%s, %s, %s, %s, %s)', (name, x, y, sprite, id_game), "entity") @staticmethod def update_entity(id_entity: int = 0, x: int = 0, y: int = 0) -> None: @@ -14,4 +18,4 @@ def get_entity(id_entity: int) -> dict | None: @staticmethod def get_entity_skills(id_entity: int = 0) -> list | None: - pass \ No newline at end of file + pass diff --git a/dnd-bot/dnd_bot/database/database_player.py b/dnd-bot/dnd_bot/database/database_player.py index b75ff60f..0046cc19 100644 --- a/dnd-bot/dnd_bot/database/database_player.py +++ b/dnd-bot/dnd_bot/database/database_player.py @@ -1,3 +1,6 @@ +from dnd_bot.database.database_connection import DatabaseConnection + + class DatabasePlayer: @staticmethod diff --git a/dnd-bot/dnd_bot/logic/game/initialize_world.py b/dnd-bot/dnd_bot/logic/game/initialize_world.py index e446aac5..429e00bc 100644 --- a/dnd-bot/dnd_bot/logic/game/initialize_world.py +++ b/dnd-bot/dnd_bot/logic/game/initialize_world.py @@ -3,6 +3,7 @@ import random import cv2 as cv +from dnd_bot.database.database_entity import DatabaseEntity from dnd_bot.logic.prototype.entities.hole import Hole from dnd_bot.logic.prototype.entities.rock import Rock from dnd_bot.logic.prototype.entities.mushrooms import Mushrooms @@ -43,31 +44,41 @@ def load_entities(game, map_path): player_spawning_points.append((x, y)) entities_row.append(None) elif entity_types[str(entity)] == 'Rock': - entities_row.append(Rock(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, Rock, x, y, game.token, game.id, 'Rock') elif entity_types[str(entity)] == 'Hole': - entities_row.append(Hole(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, Hole, x, y, game.token, game.id, 'Hole') elif entity_types[str(entity)] == 'Mushrooms': - entities_row.append(Mushrooms(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, Mushrooms, x, y, game.token, game.id, + 'Mushrooms') # walls elif entity_types[str(entity)] == 'Dungeon connector': - entities_row.append(DungeonConnector(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonConnector, x, y, game.token, + game.id, 'Dungeon connector') elif entity_types[str(entity)] == 'Dungeon corner in': - entities_row.append(DungeonCornerIn(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonCornerIn, x, y, game.token, + game.id, 'Dungeon corner in') elif entity_types[str(entity)] == 'Dungeon corner out': - entities_row.append(DungeonCornerOut(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonCornerOut, x, y, game.token, + game.id, 'Dungeon corner out') elif entity_types[str(entity)] == 'Dungeon crossroads': - entities_row.append(DungeonCrossroads(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonCrossroads, x, y, game.token, + game.id, 'Dungeon crossroads') elif entity_types[str(entity)] == 'Dungeon pillar A': - entities_row.append(DungeonPillarA(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonPillarA, x, y, game.token, + game.id, 'Dungeon pillar A') elif entity_types[str(entity)] == 'Dungeon pillar B': - entities_row.append(DungeonPillarB(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonPillarB, x, y, game.token, + game.id, 'Dungeon pillar B') elif entity_types[str(entity)] == 'Dungeon pillar C': - entities_row.append(DungeonPillarC(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonPillarC, x, y, game.token, + game.id, 'Dungeon pillar C') elif entity_types[str(entity)] == 'Dungeon straight A': - entities_row.append(DungeonStraightA(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonStraightA, x, y, game.token, + game.id, 'Dungeon straight A') elif entity_types[str(entity)] == 'Dungeon straight B': - entities_row.append(DungeonStraightB(x=x, y=y, game_token=game.token)) + entities_row = InitializeWorld.add_entity(entities_row, DungeonStraightB, x, y, game.token, + game.id, 'Dungeon straight B') entities.append(entities_row) # handle entity rotations @@ -110,3 +121,9 @@ def spawn_players(spawning_points, num_players): players_positions.append((x, y)) return players_positions + + @staticmethod + def add_entity(entity_row, entity_class, x, y, game_token, game_id, entity_name): + id = DatabaseEntity.add_entity(entity_name, x, y, id_game=game_id) + entity_row.append(entity_class(id=id, x=x, y=y, game_token=game_token)) + return entity_row diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/hole.py b/dnd-bot/dnd_bot/logic/prototype/entities/hole.py index f2b9070a..73d9f8bf 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/hole.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/hole.py @@ -3,5 +3,5 @@ class Hole(Entity): - def __init__(self, x=0, y=0, game_token=''): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/entities/hole.png", name="Hole", game_token=game_token) + def __init__(self, id=0, x=0, y=0, game_token=''): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/entities/hole.png", name="Hole", game_token=game_token) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/mushrooms.py b/dnd-bot/dnd_bot/logic/prototype/entities/mushrooms.py index 74cd4fd7..9676ab9b 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/mushrooms.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/mushrooms.py @@ -3,5 +3,5 @@ class Mushrooms(Entity): - def __init__(self, x=0, y=0, game_token=''): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/entities/mushrooms.png", name="Mushrooms", game_token=game_token) + def __init__(self,id=0, x=0, y=0, game_token=''): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/entities/mushrooms.png", name="Mushrooms", game_token=game_token) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/rock.py b/dnd-bot/dnd_bot/logic/prototype/entities/rock.py index 4398a410..3e0e4217 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/rock.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/rock.py @@ -3,5 +3,5 @@ class Rock(Entity): - def __init__(self, x=0, y=0, game_token=''): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/entities/rock.png", name="Rock", game_token=game_token) + def __init__(self, id=0, x=0, y=0, game_token=''): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/entities/rock.png", name="Rock", game_token=game_token) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_connector.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_connector.py index 930bb544..d6300d70 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_connector.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_connector.py @@ -3,6 +3,6 @@ class DungeonConnector(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_connector.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_connector.png", name="Dungeon connector", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_in.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_in.py index 73e81a26..c5eb8bcd 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_in.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_in.py @@ -3,6 +3,6 @@ class DungeonCornerIn(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_corner_in_black.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_corner_in_black.png", name="Dungeon corner in", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_out.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_out.py index 9b253fe3..ab79a5ef 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_out.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_corner_out.py @@ -3,6 +3,6 @@ class DungeonCornerOut(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_corner_out_black.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_corner_out_black.png", name="Dungeon corner out", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_crossroads.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_crossroads.py index d7b8eb33..7d6e8c2c 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_crossroads.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_crossroads.py @@ -3,6 +3,6 @@ class DungeonCrossroads(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_crossroads.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_crossroads.png", name="Dungeon crossroads", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_a.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_a.py index 9567ebaa..0d55dc68 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_a.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_a.py @@ -3,6 +3,6 @@ class DungeonPillarA(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_pillar.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_pillar.png", name="Dungeon pillar A", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_b.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_b.py index 0995d7ca..1dd3208c 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_b.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_b.py @@ -3,6 +3,6 @@ class DungeonPillarB(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_pillar_2.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_pillar_2.png", name="Dungeon pillar B", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_c.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_c.py index 5debb2b5..13cffeac 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_c.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_pillar_c.py @@ -3,6 +3,6 @@ class DungeonPillarC(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_pillar_3.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_pillar_3.png", name="Dungeon pillar C", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_a.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_a.py index 29d7524a..545308d2 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_a.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_a.py @@ -3,6 +3,6 @@ class DungeonStraightA(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_straight_1_black.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_straight_1_black.png", name="Dungeon straight A", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_b.py b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_b.py index 4f9c4c39..b320a82c 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_b.py +++ b/dnd-bot/dnd_bot/logic/prototype/entities/walls/dungeon_straight_b.py @@ -3,6 +3,6 @@ class DungeonStraightB(Entity): - def __init__(self, x=0, y=0, game_token='', look_direction='down'): - super().__init__(x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_straight_2_black.png", + def __init__(self, id=0, x=0, y=0, game_token='', look_direction='down'): + super().__init__(id=id, x=x, y=y, sprite="dnd_bot/assets/gfx/walls/dungeon_straight_2_black.png", name="Dungeon straight B", game_token=game_token, look_direction=look_direction) diff --git a/dnd-bot/dnd_bot/logic/prototype/entity.py b/dnd-bot/dnd_bot/logic/prototype/entity.py index 8f72fdb1..71cb248f 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entity.py +++ b/dnd-bot/dnd_bot/logic/prototype/entity.py @@ -6,11 +6,11 @@ class Entity(DatabaseObject): """This class is the base class for all entities in the game like creatures and elements on the map""" - def __init__(self, x: int = 0, y: int = 0, sprite=None, name: str = 'Entity', id_game: int = 0, + def __init__(self, id: int = 0, x: int = 0, y: int = 0, sprite=None, name: str = 'Entity', id_game: int = 0, game_token: str = '', skills=None, fragile: bool = False, look_direction: str = 'down'): """":param fragile: if entity can be moved or destroyed from its position""" - ## TODO super().__init__(DatabaseEntity.add_entity(name, x, y, sprite, id_game)) + super().__init__(id) if skills is None: skills = [] self.x = x From 9c2f9ae388ff9fb3831392d12dd9a5196d5de511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Thu, 9 Mar 2023 14:08:27 +0100 Subject: [PATCH 17/25] database: implement adding player to database --- dnd-bot/dnd_bot/database/database_creature.py | 22 +++++++++++--- dnd-bot/dnd_bot/database/database_player.py | 14 +++++++-- .../dnd_bot/logic/game/initialize_world.py | 30 +++++++++++++------ .../logic/prototype/database_object.py | 2 +- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/dnd-bot/dnd_bot/database/database_creature.py b/dnd-bot/dnd_bot/database/database_creature.py index a8d145dc..f8899da7 100644 --- a/dnd-bot/dnd_bot/database/database_creature.py +++ b/dnd-bot/dnd_bot/database/database_creature.py @@ -1,10 +1,24 @@ +from dnd_bot.database.database_connection import DatabaseConnection +from dnd_bot.database.database_entity import DatabaseEntity +from dnd_bot.logic.prototype.creature import Creature + + class DatabaseCreature: @staticmethod - def add_creature(id_entity: int = 0, level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, - intelligence: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, - money: int = 0) -> int | None: - pass + def add_creature(c: Creature) -> int | None: + id_entity = DatabaseEntity.add_entity(c.name, c.x, c.y, 'sprite', c.id_game) + id_creature = DatabaseConnection.add_to_db('INSERT INTO public."Creature" (level, "HP", strength, dexterity, ' + 'intelligence, charisma, perception, initiative, action_points, ' + 'money, id_entity) VALUES' + '(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', + ( + c.level, c.hp, c.strength, c.dexterity, c.intelligence, + c.charisma, c.perception, + c.initiative, c.action_points, c.drop_money, id_entity), + "creature") + c.id = id_creature + return id_creature @staticmethod def update_creature(id_creature: int = 0, level: int = 0, hp: int = 0, strength: int = 0, dexterity: int = 0, diff --git a/dnd-bot/dnd_bot/database/database_player.py b/dnd-bot/dnd_bot/database/database_player.py index 0046cc19..b49b8ba3 100644 --- a/dnd-bot/dnd_bot/database/database_player.py +++ b/dnd-bot/dnd_bot/database/database_player.py @@ -1,12 +1,20 @@ from dnd_bot.database.database_connection import DatabaseConnection +from dnd_bot.database.database_creature import DatabaseCreature +from dnd_bot.database.database_user import DatabaseUser +from dnd_bot.logic.prototype.player import Player class DatabasePlayer: @staticmethod - def add_player(id_user: int = 0, alignment: str = "", backstory: str = "", id_equipment: int = 0, - id_creature: int = 0) -> int | None: - pass + def add_player(p: Player) -> int | None: + id_creature = DatabaseCreature.add_creature(p) + id_user = DatabaseUser.get_user_id_from_discord_id(p.discord_identity, p.id_game) + id_player = DatabaseConnection.add_to_db('INSERT INTO public."Player" (id_user, alignment, backstory, ' + 'id_creature) VALUES (%s, %s, %s, %s)', + (id_user, p.alignment, p.backstory, id_creature)) + p.id = id_player + return id_player @staticmethod def get_player(id_player) -> dict | None: diff --git a/dnd-bot/dnd_bot/logic/game/initialize_world.py b/dnd-bot/dnd_bot/logic/game/initialize_world.py index 429e00bc..22cf677c 100644 --- a/dnd-bot/dnd_bot/logic/game/initialize_world.py +++ b/dnd-bot/dnd_bot/logic/game/initialize_world.py @@ -3,7 +3,10 @@ import random import cv2 as cv +from dnd_bot.database.database_creature import DatabaseCreature from dnd_bot.database.database_entity import DatabaseEntity +from dnd_bot.database.database_player import DatabasePlayer +from dnd_bot.logic.prototype.creature import Creature from dnd_bot.logic.prototype.entities.hole import Hole from dnd_bot.logic.prototype.entities.rock import Rock from dnd_bot.logic.prototype.entities.mushrooms import Mushrooms @@ -97,13 +100,16 @@ def load_entities(game, map_path): entities[y][x].look_direction = 'down' # handle random spawning points - players_positions = InitializeWorld.spawn_players(player_spawning_points, len(game.user_list)) + players_positions = InitializeWorld.spawn_players_positions(player_spawning_points, len(game.user_list)) for i, player_pos in enumerate(players_positions): entities[player_pos[1]].pop(player_pos[0]) - entities[player_pos[1]].insert(player_pos[0], Player(x=player_pos[0], y=player_pos[1], - name=game.user_list[i].username, - discord_identity=game.user_list[i].discord_id, - game_token=game.token)) + player = Player(x=player_pos[0], y=player_pos[1], + name=game.user_list[i].username, + discord_identity=game.user_list[i].discord_id, + game_token=game.token) + player.id_game = game.id + DatabasePlayer.add_player(player) + entities[player_pos[1]].insert(player_pos[0], player) game.entities = copy.deepcopy(entities) game.sprite = str(map_json['map']['img_file']) # path to raw map image @@ -113,8 +119,8 @@ def load_entities(game, map_path): game.world_height = map_json['map']['size']['y'] @staticmethod - def spawn_players(spawning_points, num_players): - """function that places players in random available spawning points""" + def spawn_players_positions(spawning_points, num_players): + """function that returns random available spawning points that players can spawn in""" players_positions = [] for _ in range(num_players): x, y = spawning_points.pop(random.randint(0, len(spawning_points) - 1)) @@ -124,6 +130,12 @@ def spawn_players(spawning_points, num_players): @staticmethod def add_entity(entity_row, entity_class, x, y, game_token, game_id, entity_name): - id = DatabaseEntity.add_entity(entity_name, x, y, id_game=game_id) - entity_row.append(entity_class(id=id, x=x, y=y, game_token=game_token)) + new_entity = entity_class(x=x, y=y, game_token=game_token) + + if isinstance(entity_class, Creature): + DatabaseCreature.add_creature(new_entity) + else: + id_entity = DatabaseEntity.add_entity(entity_name, x, y, id_game=game_id) + new_entity.id = id_entity + entity_row.append(new_entity) return entity_row diff --git a/dnd-bot/dnd_bot/logic/prototype/database_object.py b/dnd-bot/dnd_bot/logic/prototype/database_object.py index 48a087fb..11c10013 100644 --- a/dnd-bot/dnd_bot/logic/prototype/database_object.py +++ b/dnd-bot/dnd_bot/logic/prototype/database_object.py @@ -1,3 +1,3 @@ class DatabaseObject: - def __init__(self, id): + def __init__(self, id=0): self.id = id \ No newline at end of file From 28ffa05ad48f4c59a69607ce6355d2942276835c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 Mar 2023 14:48:57 +0100 Subject: [PATCH 18/25] database: add get_db_object and get_player --- .../dnd_bot/database/database_connection.py | 24 +++++++++++++++++ dnd-bot/dnd_bot/database/database_game.py | 3 ++- dnd-bot/dnd_bot/database/database_player.py | 27 +++++++++++++++++-- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/dnd-bot/dnd_bot/database/database_connection.py b/dnd-bot/dnd_bot/database/database_connection.py index 42bd349c..2abbfc27 100644 --- a/dnd-bot/dnd_bot/database/database_connection.py +++ b/dnd-bot/dnd_bot/database/database_connection.py @@ -73,4 +73,28 @@ def add_to_db(query: str = "", parameters: tuple = None, element_name: str = "el DatabaseConnection.connection.commit() return id + @staticmethod + def get_object_from_db(query: str = '', parameters: tuple = None, element_name: str = '') -> tuple | None: + DatabaseConnection.cursor.execute(query, parameters) + obj = DatabaseConnection.cursor.fetchone() + + if not obj: + print(f"db: error getting {element_name}") + return None + + DatabaseConnection.connection.commit() + return obj + + @staticmethod + def get_multiple_objects_from_db(query: str = '', parameters: tuple = None, element_name: str = '') -> list | None: + """returns list of tuples representing a db object""" + DatabaseConnection.cursor.execute(query, parameters) + objs = DatabaseConnection.cursor.fetchall() + + if not objs: + print(f"db: error getting multiple {element_name}") + return None + + DatabaseConnection.connection.commit() + return objs diff --git a/dnd-bot/dnd_bot/database/database_game.py b/dnd-bot/dnd_bot/database/database_game.py index aadec09d..8abf43e0 100644 --- a/dnd-bot/dnd_bot/database/database_game.py +++ b/dnd-bot/dnd_bot/database/database_game.py @@ -16,7 +16,8 @@ def add_game(token: str, id_host: int, game_state: str, campaign_name: str) -> i on success: game id, on failure: None """ return DatabaseConnection.add_to_db('INSERT INTO public."Game" (token, id_host, game_state, campaign_name)' - 'VALUES (%s, %s, %s, %s)', (token, id_host, game_state, campaign_name), "game") + 'VALUES (%s, %s, %s, %s)', (token, id_host, game_state, campaign_name), + "game") @staticmethod def start_game(id_game: int) -> None: diff --git a/dnd-bot/dnd_bot/database/database_player.py b/dnd-bot/dnd_bot/database/database_player.py index b49b8ba3..16b12194 100644 --- a/dnd-bot/dnd_bot/database/database_player.py +++ b/dnd-bot/dnd_bot/database/database_player.py @@ -17,5 +17,28 @@ def add_player(p: Player) -> int | None: return id_player @staticmethod - def get_player(id_player) -> dict | None: - pass + def get_player(id_player) -> Player | None: + player_tuple = DatabaseConnection.get_object_from_db('SELECT * FROM public."Player" WHERE id_player = (%s)', + (id_player)) + + creature_tuple = DatabaseConnection.get_object_from_db('SELECT * FROM public."Creature" WHERE id_creature = (%s)', + (player_tuple[5])) + + entity_tuple = DatabaseConnection.get_object_from_db('SELECT * FROM public."Entity" WHERE id_entity = (%s)', + (creature_tuple[11])) + + if entity_tuple is None: + return None + + player = Player(entity_id=entity_tuple[0], x=entity_tuple[2], y=entity_tuple[3], name=entity_tuple[1], + hp=creature_tuple[2], level=creature_tuple[1], strength=creature_tuple[3], + dexterity=creature_tuple[4], intelligence=creature_tuple[5], charisma=creature_tuple[6], + perception=creature_tuple[7], initiative=creature_tuple[8], action_points=creature_tuple[9], + discord_identity=player_tuple[1], alignment=player_tuple[2], backstory=player_tuple[3]) + + player.id = player_tuple[0] + + return player + + + From e37e18af16f8029c1a81d42280e9701a85558b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 Mar 2023 14:49:19 +0100 Subject: [PATCH 19/25] tests: set up a basic test environment --- dnd-bot/dnd_bot/tests/__init__.py | 0 dnd-bot/dnd_bot/tests/autoconf.py | 20 ++ dnd-bot/dnd_bot/tests/create_tables.sql | 261 ++++++++++++++++++++++++ dnd-bot/dnd_bot/tests/test_game.py | 62 ++++++ dnd-bot/dnd_bot/tests/test_player.py | 34 +++ 5 files changed, 377 insertions(+) create mode 100644 dnd-bot/dnd_bot/tests/__init__.py create mode 100644 dnd-bot/dnd_bot/tests/autoconf.py create mode 100644 dnd-bot/dnd_bot/tests/create_tables.sql create mode 100644 dnd-bot/dnd_bot/tests/test_game.py create mode 100644 dnd-bot/dnd_bot/tests/test_player.py diff --git a/dnd-bot/dnd_bot/tests/__init__.py b/dnd-bot/dnd_bot/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/dnd-bot/dnd_bot/tests/autoconf.py b/dnd-bot/dnd_bot/tests/autoconf.py new file mode 100644 index 00000000..8427ecf5 --- /dev/null +++ b/dnd-bot/dnd_bot/tests/autoconf.py @@ -0,0 +1,20 @@ +import psycopg2 +from pytest_postgresql import factories + +from dnd_bot.database.database_connection import DatabaseConnection + + +def load_database(**kwargs): + db_connection = psycopg2.connect(**kwargs) + with db_connection.cursor() as cur: + query = open('create_tables.sql') + cur.execute(query.read()) + db_connection.commit() + + +def database_fixture(db_name): + postgresql_in_docker = factories.postgresql_noproc(host='172.20.0.2', password='admin', user='admin', + load=[load_database], dbname=db_name) + postgresql = factories.postgresql('postgresql_in_docker', dbname=db_name) + + return postgresql, postgresql_in_docker diff --git a/dnd-bot/dnd_bot/tests/create_tables.sql b/dnd-bot/dnd_bot/tests/create_tables.sql new file mode 100644 index 00000000..45379788 --- /dev/null +++ b/dnd-bot/dnd_bot/tests/create_tables.sql @@ -0,0 +1,261 @@ +CREATE TABLE public."Game" +( + id_game BIGSERIAL NOT NULL, + token VARCHAR, + id_host BIGINT, + game_state VARCHAR, + campaign_name VARCHAR, + PRIMARY KEY (id_game), + CONSTRAINT game_state_enum CHECK (game_state in ('LOBBY', 'STARTING', 'ACTIVE', 'INACTIVE', 'FINISHED')) +); + +ALTER TABLE IF EXISTS public."Game" + OWNER to admin; + + +CREATE TABLE public."User" +( + id_user BIGSERIAL NOT NULL, + id_game BIGINT, + discord_id BIGINT, + PRIMARY KEY (id_user), + CONSTRAINT id_game FOREIGN KEY (id_game) + REFERENCES public."Game" (id_game) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE +); + +ALTER TABLE IF EXISTS public."User" + OWNER to admin; + + +CREATE TABLE public."Event" +( + id_event BIGSERIAL NOT NULL, + x INTEGER, + y INTEGER, + range INTEGER, + status VARCHAR, + content VARCHAR, + id_game BIGINT, + PRIMARY KEY (id_event), + CONSTRAINT id_game FOREIGN KEY (id_game) + REFERENCES public."Game" (id_game) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE, + CONSTRAINT status_enum CHECK (status in ('AVAILABLE', 'NOT_AVAILABLE')) +); + +ALTER TABLE IF EXISTS public."Event" + OWNER to admin; + + +CREATE TABLE public."Entity" +( + id_entity BIGSERIAL NOT NULL, + name VARCHAR, + x INTEGER, + y INTEGER, + sprite VARCHAR, + id_game BIGINT, + PRIMARY KEY (id_entity), + CONSTRAINT id_game FOREIGN KEY (id_game) + REFERENCES public."Game" (id_game) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE +); + +ALTER TABLE IF EXISTS public."Entity" + OWNER to admin; + + +CREATE TABLE public."Creature" +( + id_creature BIGSERIAL NOT NULL, + level INTEGER, + "HP" INTEGER, + strength INTEGER, + dexterity INTEGER, + intelligence INTEGER, + charisma INTEGER, + perception INTEGER, + initiative INTEGER, + action_points INTEGER, + money INTEGER, + id_entity BIGINT, + PRIMARY KEY (id_creature), + CONSTRAINT base_entity FOREIGN KEY (id_entity) + REFERENCES public."Entity" (id_entity) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE +); + +ALTER TABLE IF EXISTS public."Creature" + OWNER to admin; + + +CREATE TABLE public."Dialog" +( + id_dialog BIGSERIAL NOT NULL, + id_speaker BIGINT, + id_listener BIGINT, + content VARCHAR, + status VARCHAR, + PRIMARY KEY (id_dialog), + CONSTRAINT id_speaker FOREIGN KEY (id_speaker) + REFERENCES public."Entity" (id_entity) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE, + CONSTRAINT id_listener FOREIGN KEY (id_listener) + REFERENCES public."Entity" (id_entity) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE, + CONSTRAINT status_enum CHECK (status in ('AVAILABLE', 'NOT_AVAILABLE', 'USED')) +); + +ALTER TABLE IF EXISTS public."Dialog" + OWNER to admin; + + +CREATE TABLE public."Item" +( + id_item BIGSERIAL NOT NULL, + name VARCHAR, + "HP" INTEGER, + strength INTEGER, + dexterity INTEGER, + intelligence INTEGER, + charisma INTEGER, + perception INTEGER, + action_points INTEGER, + effect VARCHAR, + base_price INTEGER, + PRIMARY KEY (id_item) +); + +ALTER TABLE IF EXISTS public."Item" + OWNER to admin; + + +CREATE TABLE public."Creature_Item" +( + id_creature_item BIGSERIAL NOT NULL, + id_creature BIGINT, + id_item BIGINT, + amount INTEGER, + PRIMARY KEY (id_creature_item), + CONSTRAINT id_creature FOREIGN KEY (id_creature) + REFERENCES public."Creature" (id_creature) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE, + CONSTRAINT id_item FOREIGN KEY (id_item) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE +); + +ALTER TABLE IF EXISTS public."Creature_Item" + OWNER to admin; + + +CREATE TABLE public."Skill" +( + id_skill BIGSERIAL NOT NULL, + name VARCHAR, + PRIMARY KEY (id_skill) +); + +ALTER TABLE IF EXISTS public."Skill" + OWNER to admin; + + +CREATE TABLE public."Entity_Skill" +( + id_entity_skill BIGSERIAL NOT NULL, + id_entity BIGINT, + id_skill BIGINT, + PRIMARY KEY (id_entity_skill), + CONSTRAINT id_entity FOREIGN KEY (id_entity) + REFERENCES public."Entity" (id_entity) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE, + CONSTRAINT id_skill FOREIGN KEY (id_skill) + REFERENCES public."Skill" (id_skill) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE +); + +ALTER TABLE IF EXISTS public."Entity_Skill" + OWNER to admin; + +CREATE TABLE public."Equipment" +( + id_equipment BIGSERIAL NOT NULL, + helmet BIGINT, + chest BIGINT, + leg_armor BIGINT, + boots BIGINT, + left_hand BIGINT, + right_hand BIGINT, + accessory BIGINT, + PRIMARY KEY (id_equipment), + CONSTRAINT helmet FOREIGN KEY (helmet) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL, + CONSTRAINT chest FOREIGN KEY (chest) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL, + CONSTRAINT leg_armor FOREIGN KEY (leg_armor) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL, + CONSTRAINT boots FOREIGN KEY (boots) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL, + CONSTRAINT left_hand FOREIGN KEY (left_hand) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL, + CONSTRAINT right_hand FOREIGN KEY (right_hand) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL, + CONSTRAINT accessory FOREIGN KEY (accessory) + REFERENCES public."Item" (id_item) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL +); + +ALTER TABLE IF EXISTS public."Equipment" + OWNER to admin; + + +CREATE TABLE public."Player" +( + id_player BIGSERIAL NOT NULL, + id_user BIGINT, + alignment VARCHAR, + backstory VARCHAR, + id_equipment BIGINT, + id_creature BIGINT, + PRIMARY KEY (id_player), + CONSTRAINT id_user FOREIGN KEY (id_user) + REFERENCES public."User" (id_user) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE NO ACTION, + CONSTRAINT id_equipment FOREIGN KEY (id_equipment) + REFERENCES public."Equipment" (id_equipment) MATCH SIMPLE + ON UPDATE SET NULL + ON DELETE SET NULL, + CONSTRAINT id_creature FOREIGN KEY (id_creature) + REFERENCES public."Creature" (id_creature) MATCH SIMPLE + ON UPDATE CASCADE + ON DELETE CASCADE +); + +ALTER TABLE IF EXISTS public."Player" + OWNER to admin; + \ No newline at end of file diff --git a/dnd-bot/dnd_bot/tests/test_game.py b/dnd-bot/dnd_bot/tests/test_game.py new file mode 100644 index 00000000..e4b147f5 --- /dev/null +++ b/dnd-bot/dnd_bot/tests/test_game.py @@ -0,0 +1,62 @@ +from dnd_bot.database.database_connection import DatabaseConnection + +from dnd_bot.tests.autoconf import database_fixture + +postgresql, postgresql_in_docker = database_fixture('game') + + +def test_add_game(postgresql): + cur = postgresql.cursor() + + DatabaseConnection.connection = postgresql + DatabaseConnection.cursor = cur + + DatabaseConnection.add_to_db('INSERT INTO public."Game" (token, id_host, game_state, campaign_name)' + ' VALUES (%s, %s, %s, %s)', ('12345', 1, 'LOBBY', 'test')) + + game_tuple = cur.execute(f'SELECT * FROM public."Game" WHERE id_game = (SELECT LASTVAL())').fetchone() + postgresql.commit() + + assert (game_tuple[1] == '12345') # token + assert (game_tuple[2] == 1) # id_host + assert (game_tuple[3] == 'LOBBY') + assert (game_tuple[4] == 'test') + + cur.close() + + +def test_add_game_no_token(postgresql): + cur = postgresql.cursor() + + DatabaseConnection.connection = postgresql + DatabaseConnection.cursor = cur + + DatabaseConnection.add_to_db('INSERT INTO public."Game" (id_host, game_state, campaign_name)' + ' VALUES (%s, %s, %s)', (1, 'LOBBY', 'test')) + + game_tuple = cur.execute(f'SELECT * FROM public."Game" WHERE id_game = (SELECT LASTVAL())').fetchone() + postgresql.commit() + + assert (game_tuple[1] is None) # token + assert (game_tuple[2] == 1) # id_host + assert (game_tuple[3] == 'LOBBY') + assert (game_tuple[4] == 'test') + + +def test_add_game_no_game_state(postgresql): + cur = postgresql.cursor() + + DatabaseConnection.connection = postgresql + DatabaseConnection.cursor = cur + + DatabaseConnection.add_to_db('INSERT INTO public."Game" (token, id_host, campaign_name)' + ' VALUES (%s, %s, %s)', ('12345', 1, 'test')) + + game_tuple = cur.execute(f'SELECT * FROM public."Game" WHERE id_game = (SELECT LASTVAL())').fetchone() + postgresql.commit() + + assert (game_tuple[1] == '12345') + assert (game_tuple[2] == 1) + assert (game_tuple[3] is None) + assert (game_tuple[4] == 'test') + diff --git a/dnd-bot/dnd_bot/tests/test_player.py b/dnd-bot/dnd_bot/tests/test_player.py new file mode 100644 index 00000000..6c6dad80 --- /dev/null +++ b/dnd-bot/dnd_bot/tests/test_player.py @@ -0,0 +1,34 @@ +from dnd_bot.database.database_connection import DatabaseConnection +from dnd_bot.database.database_player import DatabasePlayer + +from dnd_bot.logic.prototype.player import Player + +from dnd_bot.tests.autoconf import database_fixture + +postgresql, postgresql_in_docker = database_fixture('player') + + +def test_add_player(postgresql): + cur = postgresql.cursor() + + DatabaseConnection.connection = postgresql + DatabaseConnection.cursor = cur + + player = Player(x=1, y=2, name='silentsky', hp=3, strength=4, dexterity=5, intelligence=6, charisma=7, perception=8, + initiative=9, action_points=10, level=11, discord_identity=12, alignment='align', backstory='back', + game_token='12345') + DatabasePlayer.add_player(player) + + player_tuple = cur.execute(f'SELECT * FROM public."Player" WHERE id_player = (SELECT LASTVAL())').fetchone() + creature_tuple = cur.execute(f'SELECT * FROM public."Creature" WHERE id_creature = (SELECT LASTVAL())').fetchone() + entity_tuple = cur.execute(f'SELECT * FROM public."Entity" WHERE id_entity = (SELECT LASTVAL())').fetchone() + postgresql.commit() + + assert (entity_tuple[1] == 'silentsky') + assert (entity_tuple[2] == 1) + assert (entity_tuple[3] == 2) + assert (creature_tuple[1] == 11) + assert (creature_tuple[2] == 3) + assert (creature_tuple[3] == 4) + + From 42c619a4a80b90059e9de067ac0660170f5156fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 Mar 2023 14:54:46 +0100 Subject: [PATCH 20/25] prototype: player: handle exceptions when setting sprite path --- dnd-bot/dnd_bot/logic/prototype/player.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/dnd-bot/dnd_bot/logic/prototype/player.py b/dnd-bot/dnd_bot/logic/prototype/player.py index 40f34c83..f4b6107e 100644 --- a/dnd-bot/dnd_bot/logic/prototype/player.py +++ b/dnd-bot/dnd_bot/logic/prototype/player.py @@ -25,8 +25,17 @@ def __init__(self, entity_id=0, x=0, y=0, sprite="dnd_bot/assets/gfx/entities/pl action_points = random.randint(5, 10) # TODO remove above - self.sprite = Player.get_sprite_path_by_color(Multiverse.get_game(game_token). - get_user_by_id(discord_identity).color) + # request a sprite path for the player based on the user + game = Multiverse.get_game(game_token) + if game is None: + print('Warning: this player has no associated Game!') + else: + user = game.get_user_by_id(discord_identity) + if user is None: + print('Warning: this player has no associated User!') + self.sprite = Player.get_sprite_path_by_color('red') + else: + self.sprite = Player.get_sprite_path_by_color(user.color) super().__init__(x=x, y=y, sprite=self.sprite, name=name, hp=hp, strength=strength, dexterity=dexterity, intelligence=intelligence, charisma=charisma, perception=perception, initiative=initiative, From fe10df3cc755396b393679c30c6fafcc1a270c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 Mar 2023 14:57:42 +0100 Subject: [PATCH 21/25] prototype: multiverse: handle token not being found in dict --- dnd-bot/dnd_bot/logic/prototype/multiverse.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dnd-bot/dnd_bot/logic/prototype/multiverse.py b/dnd-bot/dnd_bot/logic/prototype/multiverse.py index 4f2bcbd9..b8a0de67 100644 --- a/dnd-bot/dnd_bot/logic/prototype/multiverse.py +++ b/dnd-bot/dnd_bot/logic/prototype/multiverse.py @@ -11,6 +11,9 @@ class Multiverse: @staticmethod def get_game(token): + if token not in Multiverse.games.keys(): + print(f'Error: game with token {token} has not been found!') + return None return Multiverse.games[token] @staticmethod From e54173dd6da9c98ca32eaf134cce8d013c068f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Fri, 10 Mar 2023 15:22:51 +0100 Subject: [PATCH 22/25] database: add more tests --- dnd-bot/dnd_bot/database/database_entity.py | 2 +- dnd-bot/dnd_bot/logic/prototype/player.py | 4 ++-- dnd-bot/dnd_bot/tests/autoconf.py | 6 ++++-- dnd-bot/dnd_bot/tests/populate_tables.sql | 8 ++++++++ 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 dnd-bot/dnd_bot/tests/populate_tables.sql diff --git a/dnd-bot/dnd_bot/database/database_entity.py b/dnd-bot/dnd_bot/database/database_entity.py index 1f2b3e52..d517380b 100644 --- a/dnd-bot/dnd_bot/database/database_entity.py +++ b/dnd-bot/dnd_bot/database/database_entity.py @@ -4,7 +4,7 @@ class DatabaseEntity: @staticmethod - def add_entity(name: str = "", x: int = 0, y: int = 0, sprite=None, id_game: int = 0) -> int | None: + def add_entity(name: str = "", x: int = 0, y: int = 0, sprite=None, id_game: int = 1) -> int | None: return DatabaseConnection.add_to_db('INSERT INTO public."Entity" (name, x, y, sprite, id_game) VALUES' '(%s, %s, %s, %s, %s)', (name, x, y, sprite, id_game), "entity") diff --git a/dnd-bot/dnd_bot/logic/prototype/player.py b/dnd-bot/dnd_bot/logic/prototype/player.py index f4b6107e..4ecf2ab7 100644 --- a/dnd-bot/dnd_bot/logic/prototype/player.py +++ b/dnd-bot/dnd_bot/logic/prototype/player.py @@ -8,7 +8,7 @@ class Player(Creature): """represents a player (which is controlled by a user)""" - def __init__(self, entity_id=0, x=0, y=0, sprite="dnd_bot/assets/gfx/entities/player.png", name: str = 'Player', + def __init__(self, entity_id=0, x=0, y=0, name: str = 'Player', hp: int = 0, strength: int = 0, dexterity: int = 0, intelligence: int = 0, charisma: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, level: int = 1, discord_identity: int = 0, alignment: str = '', backstory: str = '', equipment: Equipment = None, @@ -26,6 +26,7 @@ def __init__(self, entity_id=0, x=0, y=0, sprite="dnd_bot/assets/gfx/entities/pl # TODO remove above # request a sprite path for the player based on the user + self.sprite = None game = Multiverse.get_game(game_token) if game is None: print('Warning: this player has no associated Game!') @@ -33,7 +34,6 @@ def __init__(self, entity_id=0, x=0, y=0, sprite="dnd_bot/assets/gfx/entities/pl user = game.get_user_by_id(discord_identity) if user is None: print('Warning: this player has no associated User!') - self.sprite = Player.get_sprite_path_by_color('red') else: self.sprite = Player.get_sprite_path_by_color(user.color) diff --git a/dnd-bot/dnd_bot/tests/autoconf.py b/dnd-bot/dnd_bot/tests/autoconf.py index 8427ecf5..83861bc5 100644 --- a/dnd-bot/dnd_bot/tests/autoconf.py +++ b/dnd-bot/dnd_bot/tests/autoconf.py @@ -7,8 +7,10 @@ def load_database(**kwargs): db_connection = psycopg2.connect(**kwargs) with db_connection.cursor() as cur: - query = open('create_tables.sql') - cur.execute(query.read()) + query_create_tables = open('create_tables.sql') + cur.execute(query_create_tables.read()) + query_populate_tables = open('populate_tables.sql') + cur.execute(query_populate_tables.read()) db_connection.commit() diff --git a/dnd-bot/dnd_bot/tests/populate_tables.sql b/dnd-bot/dnd_bot/tests/populate_tables.sql new file mode 100644 index 00000000..5020ed86 --- /dev/null +++ b/dnd-bot/dnd_bot/tests/populate_tables.sql @@ -0,0 +1,8 @@ +INSERT INTO public."Game"( + token, id_host, game_state, campaign_name) + VALUES ('12345', 678, 'LOBBY', 'test_campaign'); + +INSERT INTO public."User"(id_game, discord_id) VALUES (1, 111); +INSERT INTO public."User"(id_game, discord_id) VALUES (1, 222); +INSERT INTO public."User"(id_game, discord_id) VALUES (1, 222); + From 530a9fb957436562c4997a6338f3e36674b56c37 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Fri, 10 Mar 2023 21:13:25 +0100 Subject: [PATCH 23/25] changed adding player and creature to db --- dnd-bot/dnd_bot/database/database_creature.py | 14 ++++---- dnd-bot/dnd_bot/database/database_player.py | 21 ++++++------ .../dnd_bot/logic/game/initialize_world.py | 33 ++++++++++--------- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/dnd-bot/dnd_bot/database/database_creature.py b/dnd-bot/dnd_bot/database/database_creature.py index f8899da7..1c6a9564 100644 --- a/dnd-bot/dnd_bot/database/database_creature.py +++ b/dnd-bot/dnd_bot/database/database_creature.py @@ -6,18 +6,20 @@ class DatabaseCreature: @staticmethod - def add_creature(c: Creature) -> int | None: - id_entity = DatabaseEntity.add_entity(c.name, c.x, c.y, 'sprite', c.id_game) + def add_creature(x: int = 0, y: int = 0, sprite: str = '', name: str = 'Creature', hp: int = 0, strength: int = 0, + dexterity: int = 0, intelligence: int = 0, charisma: int = 0, perception: int = 0, + initiative: int = 0, action_points: int = 0, level: int = 0, drop_money: int = 0, + id_game: int = 1) -> int | None: + id_entity = DatabaseEntity.add_entity(name, x, y, sprite, id_game) id_creature = DatabaseConnection.add_to_db('INSERT INTO public."Creature" (level, "HP", strength, dexterity, ' 'intelligence, charisma, perception, initiative, action_points, ' 'money, id_entity) VALUES' '(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', ( - c.level, c.hp, c.strength, c.dexterity, c.intelligence, - c.charisma, c.perception, - c.initiative, c.action_points, c.drop_money, id_entity), + level, hp, strength, dexterity, intelligence, + charisma, perception, + initiative, action_points, drop_money, id_entity), "creature") - c.id = id_creature return id_creature @staticmethod diff --git a/dnd-bot/dnd_bot/database/database_player.py b/dnd-bot/dnd_bot/database/database_player.py index 16b12194..501bfaaa 100644 --- a/dnd-bot/dnd_bot/database/database_player.py +++ b/dnd-bot/dnd_bot/database/database_player.py @@ -7,13 +7,16 @@ class DatabasePlayer: @staticmethod - def add_player(p: Player) -> int | None: - id_creature = DatabaseCreature.add_creature(p) - id_user = DatabaseUser.get_user_id_from_discord_id(p.discord_identity, p.id_game) + def add_player(x: int = 0, y: int = 0, sprite: str = '', name: str = 'Creature', hp: int = 0, strength: int = 0, + dexterity: int = 0, intelligence: int = 0, charisma: int = 0, perception: int = 0, + initiative: int = 0, action_points: int = 0, level: int = 0, discord_identity: int = 0, + alignment: str = '', backstory: str = '', id_game: int = 1) -> int | None: + id_creature = DatabaseCreature.add_creature(x, y, sprite, name, hp, strength, dexterity, intelligence, charisma, + perception, initiative, action_points, level, id_game) + id_user = DatabaseUser.get_user_id_from_discord_id(discord_identity, id_game) id_player = DatabaseConnection.add_to_db('INSERT INTO public."Player" (id_user, alignment, backstory, ' 'id_creature) VALUES (%s, %s, %s, %s)', - (id_user, p.alignment, p.backstory, id_creature)) - p.id = id_player + (id_user, alignment, backstory, id_creature)) return id_player @staticmethod @@ -21,8 +24,9 @@ def get_player(id_player) -> Player | None: player_tuple = DatabaseConnection.get_object_from_db('SELECT * FROM public."Player" WHERE id_player = (%s)', (id_player)) - creature_tuple = DatabaseConnection.get_object_from_db('SELECT * FROM public."Creature" WHERE id_creature = (%s)', - (player_tuple[5])) + creature_tuple = DatabaseConnection.get_object_from_db( + 'SELECT * FROM public."Creature" WHERE id_creature = (%s)', + (player_tuple[5])) entity_tuple = DatabaseConnection.get_object_from_db('SELECT * FROM public."Entity" WHERE id_entity = (%s)', (creature_tuple[11])) @@ -39,6 +43,3 @@ def get_player(id_player) -> Player | None: player.id = player_tuple[0] return player - - - diff --git a/dnd-bot/dnd_bot/logic/game/initialize_world.py b/dnd-bot/dnd_bot/logic/game/initialize_world.py index 22cf677c..4e425a7e 100644 --- a/dnd-bot/dnd_bot/logic/game/initialize_world.py +++ b/dnd-bot/dnd_bot/logic/game/initialize_world.py @@ -103,13 +103,10 @@ def load_entities(game, map_path): players_positions = InitializeWorld.spawn_players_positions(player_spawning_points, len(game.user_list)) for i, player_pos in enumerate(players_positions): entities[player_pos[1]].pop(player_pos[0]) - player = Player(x=player_pos[0], y=player_pos[1], - name=game.user_list[i].username, - discord_identity=game.user_list[i].discord_id, - game_token=game.token) - player.id_game = game.id - DatabasePlayer.add_player(player) - entities[player_pos[1]].insert(player_pos[0], player) + entities = InitializeWorld.add_player(x=player_pos[0], y=player_pos[1], + name=game.user_list[i].username, + discord_identity=game.user_list[i].discord_id, + game_token=game.token, entities=entities) game.entities = copy.deepcopy(entities) game.sprite = str(map_json['map']['img_file']) # path to raw map image @@ -130,12 +127,18 @@ def spawn_players_positions(spawning_points, num_players): @staticmethod def add_entity(entity_row, entity_class, x, y, game_token, game_id, entity_name): - new_entity = entity_class(x=x, y=y, game_token=game_token) - - if isinstance(entity_class, Creature): - DatabaseCreature.add_creature(new_entity) - else: - id_entity = DatabaseEntity.add_entity(entity_name, x, y, id_game=game_id) - new_entity.id = id_entity - entity_row.append(new_entity) + id_entity = DatabaseEntity.add_entity(entity_name, x, y, id_game=game_id) + entity_row.append(entity_class(id=id_entity, x=x, y=y, game_token=game_token)) return entity_row + + @staticmethod + def add_player(x: int = 0, y: int = 0, name: str = '', discord_identity: int = 0, + game_token: str = '', game_id: int = 0, entities=None) -> int | None: + p = Player(x=x, y=y, name=name, discord_identity=discord_identity, game_token=game_token) + id_player = DatabasePlayer.add_player(p.x, p.y, p.sprite, p.name, p.hp, p.strength, p.dexterity, + p.intelligence, p.charisma, p.perception, p.initiative, + p.action_points, p.level, p.discord_identity, p.alignment, + p.backstory, p.id_game) + p.id = id_player + entities[y].insert(x, p) + return entities From 960d7e6e9def907793a2825baada907001337a71 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Sat, 11 Mar 2023 12:48:55 +0100 Subject: [PATCH 24/25] fixed issues with adding player to db --- dnd-bot/dnd_bot/database/database_creature.py | 2 +- dnd-bot/dnd_bot/database/database_player.py | 6 ++++-- dnd-bot/dnd_bot/logic/game/initialize_world.py | 12 +++++++----- dnd-bot/dnd_bot/logic/prototype/entity.py | 5 ++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/dnd-bot/dnd_bot/database/database_creature.py b/dnd-bot/dnd_bot/database/database_creature.py index 1c6a9564..0f509d71 100644 --- a/dnd-bot/dnd_bot/database/database_creature.py +++ b/dnd-bot/dnd_bot/database/database_creature.py @@ -10,7 +10,7 @@ def add_creature(x: int = 0, y: int = 0, sprite: str = '', name: str = 'Creature dexterity: int = 0, intelligence: int = 0, charisma: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, level: int = 0, drop_money: int = 0, id_game: int = 1) -> int | None: - id_entity = DatabaseEntity.add_entity(name, x, y, sprite, id_game) + id_entity = DatabaseEntity.add_entity(name=name, x=x, y=y, sprite=sprite, id_game=id_game) id_creature = DatabaseConnection.add_to_db('INSERT INTO public."Creature" (level, "HP", strength, dexterity, ' 'intelligence, charisma, perception, initiative, action_points, ' 'money, id_entity) VALUES' diff --git a/dnd-bot/dnd_bot/database/database_player.py b/dnd-bot/dnd_bot/database/database_player.py index 501bfaaa..0c7d6175 100644 --- a/dnd-bot/dnd_bot/database/database_player.py +++ b/dnd-bot/dnd_bot/database/database_player.py @@ -11,8 +11,10 @@ def add_player(x: int = 0, y: int = 0, sprite: str = '', name: str = 'Creature', dexterity: int = 0, intelligence: int = 0, charisma: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, level: int = 0, discord_identity: int = 0, alignment: str = '', backstory: str = '', id_game: int = 1) -> int | None: - id_creature = DatabaseCreature.add_creature(x, y, sprite, name, hp, strength, dexterity, intelligence, charisma, - perception, initiative, action_points, level, id_game) + id_creature = DatabaseCreature.add_creature(x=x, y=y, sprite=sprite, name=name, hp=hp, strength=strength, + dexterity=dexterity, intelligence=intelligence, charisma=charisma, + perception=perception, initiative=initiative, + action_points=action_points, level=level, id_game=id_game) id_user = DatabaseUser.get_user_id_from_discord_id(discord_identity, id_game) id_player = DatabaseConnection.add_to_db('INSERT INTO public."Player" (id_user, alignment, backstory, ' 'id_creature) VALUES (%s, %s, %s, %s)', diff --git a/dnd-bot/dnd_bot/logic/game/initialize_world.py b/dnd-bot/dnd_bot/logic/game/initialize_world.py index 4e425a7e..ea24dea6 100644 --- a/dnd-bot/dnd_bot/logic/game/initialize_world.py +++ b/dnd-bot/dnd_bot/logic/game/initialize_world.py @@ -106,7 +106,7 @@ def load_entities(game, map_path): entities = InitializeWorld.add_player(x=player_pos[0], y=player_pos[1], name=game.user_list[i].username, discord_identity=game.user_list[i].discord_id, - game_token=game.token, entities=entities) + game_token=game.token, entities=entities, game_id=game.id) game.entities = copy.deepcopy(entities) game.sprite = str(map_json['map']['img_file']) # path to raw map image @@ -127,18 +127,20 @@ def spawn_players_positions(spawning_points, num_players): @staticmethod def add_entity(entity_row, entity_class, x, y, game_token, game_id, entity_name): - id_entity = DatabaseEntity.add_entity(entity_name, x, y, id_game=game_id) - entity_row.append(entity_class(id=id_entity, x=x, y=y, game_token=game_token)) + entity = entity_class(x=x, y=y, game_token=game_token) + id_entity = DatabaseEntity.add_entity(name=entity_name, x=x, y=y, id_game=game_id, sprite=entity.sprite_path) + entity.id = id_entity + entity_row.append(entity) return entity_row @staticmethod def add_player(x: int = 0, y: int = 0, name: str = '', discord_identity: int = 0, game_token: str = '', game_id: int = 0, entities=None) -> int | None: p = Player(x=x, y=y, name=name, discord_identity=discord_identity, game_token=game_token) - id_player = DatabasePlayer.add_player(p.x, p.y, p.sprite, p.name, p.hp, p.strength, p.dexterity, + id_player = DatabasePlayer.add_player(p.x, p.y, p.sprite_path, p.name, p.hp, p.strength, p.dexterity, p.intelligence, p.charisma, p.perception, p.initiative, p.action_points, p.level, p.discord_identity, p.alignment, - p.backstory, p.id_game) + p.backstory, id_game=game_id) p.id = id_player entities[y].insert(x, p) return entities diff --git a/dnd-bot/dnd_bot/logic/prototype/entity.py b/dnd-bot/dnd_bot/logic/prototype/entity.py index 71cb248f..369545e2 100644 --- a/dnd-bot/dnd_bot/logic/prototype/entity.py +++ b/dnd-bot/dnd_bot/logic/prototype/entity.py @@ -6,8 +6,7 @@ class Entity(DatabaseObject): """This class is the base class for all entities in the game like creatures and elements on the map""" - def __init__(self, id: int = 0, x: int = 0, y: int = 0, sprite=None, name: str = 'Entity', id_game: int = 0, - game_token: str = '', + def __init__(self, id: int = 0, x: int = 0, y: int = 0, sprite: str = '', name: str = 'Entity', game_token: str = '', skills=None, fragile: bool = False, look_direction: str = 'down'): """":param fragile: if entity can be moved or destroyed from its position""" super().__init__(id) @@ -17,11 +16,11 @@ def __init__(self, id: int = 0, x: int = 0, y: int = 0, sprite=None, name: str = self.y = y self.sprite = sprite self.name = name - self.id_game = id_game self.game_token = game_token self.skills = skills self.fragile = fragile self.look_direction = look_direction + self.sprite_path = sprite if sprite: self.sprite = cv.imread(sprite, cv.IMREAD_UNCHANGED) self.sprite = cv.resize(self.sprite, (50, 50), interpolation=cv.INTER_AREA) From 32b3b2b3e37c4ad390e1b5cfc861cf15a52d7944 Mon Sep 17 00:00:00 2001 From: kapselccc Date: Sat, 11 Mar 2023 13:43:24 +0100 Subject: [PATCH 25/25] prototype/creature: changed type of sprite --- dnd-bot/dnd_bot/logic/prototype/creature.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dnd-bot/dnd_bot/logic/prototype/creature.py b/dnd-bot/dnd_bot/logic/prototype/creature.py index 2118c450..50dd40ed 100644 --- a/dnd-bot/dnd_bot/logic/prototype/creature.py +++ b/dnd-bot/dnd_bot/logic/prototype/creature.py @@ -5,7 +5,7 @@ class Creature(Entity): """represents a creature""" - def __init__(self, x=0, y=0, sprite=None, name: str = 'Creature', hp: int = 0, strength: int = 0, + def __init__(self, x=0, y=0, sprite: str = '', name: str = 'Creature', hp: int = 0, strength: int = 0, dexterity: int = 0, intelligence: int = 0, charisma: int = 0, perception: int = 0, initiative: int = 0, action_points: int = 0, level: int = 0, drop_equipment: Equipment = None, drop_money: int = 0, items=None, game_token: str = ''):