Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interface for saving and loading a game #206

Merged
merged 14 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database
10 changes: 10 additions & 0 deletions dnd-bot/dnd_bot/database/database_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,13 @@ def update_object_in_db(query: str = '', parameters: tuple = None, element_name:
print(f'db: error updating {element_name} {err}')

DatabaseConnection.connection.commit()

@staticmethod
def update_multiple_objects_in_db(queries: List[str], parameters_list: List[tuple]) -> None:
for query, parameters in list(zip(queries, parameters_list)):
try:
DatabaseConnection.cursor.execute(query, parameters)
except ProgrammingError as err:
print(f'db: error updating multiple objects {err}')

DatabaseConnection.connection.commit()
19 changes: 19 additions & 0 deletions dnd-bot/dnd_bot/database/database_creature.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def update_creature(id_creature: int = 0, hp: int = 0, level: int = 0, money: in
id_entity = DatabaseCreature.get_creature_id_entity(id_creature)
DatabaseEntity.update_entity(id_entity, x, y, look_direction)

@staticmethod
def update_creature_query(id_creature: int = 0, hp: int = 0, level: int = 0, money: int = 0, experience: int = 0,
action_points: int = 0) -> tuple[str, tuple]:
return 'UPDATE public."Creature" SET level = (%s), "HP" = (%s), money = (%s), experience = (%s), action_points = (%s) WHERE ' \
'id_creature = (%s)', (level, hp, money, experience, action_points, id_creature)

@staticmethod
def get_creature(id_creature: int = 0) -> dict | None:
query = f'SELECT * FROM public."Creature" WHERE id_creature = (%s)'
Expand All @@ -65,6 +71,19 @@ def get_creature(id_creature: int = 0) -> dict | None:
creature[key] = value
return creature

@staticmethod
def get_creature_by_id_entity(id_entity: int = 0) -> dict | None:
query = f'SELECT * FROM public."Creature" WHERE id_entity = (%s)'
db_t = DatabaseConnection.get_object_from_db(query, (id_entity,), "Creature")
creature = {'id_creature': db_t[0], 'level': db_t[1], 'hp': db_t[2], 'strength': db_t[3], 'dexterity': db_t[4],
'intelligence': db_t[5], 'charisma': db_t[6], 'perception': db_t[7], 'initiative': db_t[8],
'action_points': db_t[9], 'money': db_t[10], 'id_entity': db_t[11], 'experience': db_t[12],
'id_equipment': db_t[13], 'class': db_t[14], 'max_hp': db_t[15], 'initial_action_points': db_t[16]}
entity = DatabaseEntity.get_entity(creature['id_entity'])
for key, value in entity.items():
creature[key] = value
return creature

@staticmethod
def get_creature_id_entity(id_creature: int = 0) -> int | None:
return DatabaseConnection.get_object_from_db(
Expand Down
21 changes: 19 additions & 2 deletions dnd-bot/dnd_bot/database/database_entity.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from dnd_bot.database.database_connection import DatabaseConnection


Expand All @@ -21,15 +23,30 @@ def add_entity_query(name: str = "", x: int = 0, y: int = 0, id_game: int = None
@staticmethod
def update_entity(id_entity: int = 0, x: int = 0, y: int = 0, look_direction: str = "RIGHT") -> None:
DatabaseConnection.update_object_in_db('UPDATE public."Entity" SET x = (%s), y = (%s), look_direction = (%s) WHERE id_entity = (%s)',
(x, y, look_direction, id_entity), "Entity")
(x, y, look_direction.upper(), id_entity), "Entity")

@staticmethod
def update_entity_query(id_entity: int = 0, x: int = 0, y: int = 0, look_direction: str = "RIGHT") -> tuple[str, tuple]:
return 'UPDATE public."Entity" SET x = (%s), y = (%s), look_direction = (%s) WHERE id_entity = (%s)', (x, y, look_direction.upper(), id_entity)

@staticmethod
def get_entity(id_entity: int) -> dict | None:
query = f'SELECT * FROM public."Entity" WHERE id_entity = (%s)'
db_t = DatabaseConnection.get_object_from_db(query,(id_entity,), "Entity")
db_t = DatabaseConnection.get_object_from_db(query, (id_entity,), "Entity")
return {'id_entity': db_t[0], 'name': db_t[1], 'x': db_t[2], 'y': db_t[3],
'id_game': db_t[4], 'description': db_t[5], 'look_direction': db_t[6]}

@staticmethod
def get_all_entities(id_game: int) -> List[dict] | None:
query = f'SELECT * FROM public."Entity" WHERE id_game = (%s)'
db_l = DatabaseConnection.get_multiple_objects_from_db(query, (id_game,), "Entities")
return [{'id_entity': db_t[0], 'name': db_t[1], 'x': db_t[2], 'y': db_t[3],
'id_game': db_t[4], 'description': db_t[5], 'look_direction': db_t[6]} for db_t in db_l]

@staticmethod
def get_entity_query(id_entity: int) -> tuple[str, tuple]:
return 'SELECT * FROM public."Entity" WHERE id_entity = (%s)', (id_entity,)

@staticmethod
def get_entity_skills(id_entity: int = 0) -> list | None:
pass
3 changes: 3 additions & 0 deletions dnd-bot/dnd_bot/database/database_equipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def update_equipment(id_equipment: int = 0, helmet: int = None, chest: int = Non
def get_equipment(id_equipment: int = 0) -> dict | None:
query = f'SELECT * FROM public."Equipment" WHERE id_equipment = (%s)'
db_l = DatabaseConnection.get_object_from_db(query, (id_equipment,), "Equipment")
if db_l is None:
return None

return {'id_equipment': id_equipment, 'helmet': db_l[1], 'chest': db_l[2], 'leg_armor': db_l[3],
'boots': db_l[4], 'left_hand': db_l[5], 'right_hand': db_l[6], 'accessory': db_l[7]}

22 changes: 22 additions & 0 deletions dnd-bot/dnd_bot/database/database_event.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from dnd_bot.database.database_connection import DatabaseConnection


Expand Down Expand Up @@ -30,3 +32,23 @@ def get_game_events(id_game) -> list | None:
def update_event(id_event: int, status: str = '') -> None:
query = f'UPDATE public."Event" SET status = (%s) WHERE id_event = (%s)'
DatabaseConnection.update_object_in_db(query, (status, id_event), "Event")

@staticmethod
def __update_event_query(id_event: int, status: str = '') -> tuple[str, tuple]:
return f'UPDATE public."Event" SET status = (%s) WHERE id_event = (%s)', (status, id_event)

@staticmethod
def update_multiple_events(event_ids: List[int], status_list: List[str]):
"""
update events based on the provided ids, with statuses that correspond in order to the ids
"""
queries = []
parameters_list = []
for id_event, status in zip(event_ids, status_list):
query, parameters = DatabaseEvent.__update_event_query(id_event, status)
queries.append(query)
parameters_list.append(parameters)
DatabaseConnection.update_multiple_objects_in_db(queries, parameters_list)



3 changes: 2 additions & 1 deletion dnd-bot/dnd_bot/database/database_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ def find_game_by_token(token: str) -> dict | None:
def get_game(id_game: int) -> dict | None:
query = f'SELECT * FROM public."Game" WHERE id_game = (%s)'
db_t = DatabaseConnection.get_object_from_db(query, (id_game,), "Game")
return {'id_game': db_t[0], 'token': db_t[1], 'id_host': db_t[2], 'game_state': db_t[3], 'campaign_name': db_t[4]}
return {'id_game': db_t[0], 'token': db_t[1], 'id_host': db_t[2], 'game_state': db_t[3],
'campaign_name': db_t[4], 'active_creature': db_t[5]}
3 changes: 3 additions & 0 deletions dnd-bot/dnd_bot/database/database_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def add_item(name: str = "") -> int | None:
def get_item(id_item) -> dict | None:
query = f'SELECT * FROM public."Item" WHERE id_item = (%s)'
db_t = DatabaseConnection.get_object_from_db(query, (id_item,), "Item")
if db_t is None:
return None

return {'id_item': db_t[0], 'name': db_t[1]}

@staticmethod
Expand Down
Loading