Skip to content

Commit d7294e9

Browse files
committedFeb 1, 2025·
feat(py): Adding function to use area damage spell and fixing some procedures issues
1 parent 9c03bd3 commit d7294e9

File tree

6 files changed

+115
-43
lines changed

6 files changed

+115
-43
lines changed
 

‎src/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import os
2+
3+
# To work on windows
4+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
5+
16
from database.Database import Database
27
from default import populate_database
38
import sys

‎src/database/dml/procedures.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ DECLARE
272272
v_item_id INT;
273273
BEGIN
274274
-- Criar o item da poção
275-
INSERT INTO item (tipo, descricao, drop_inimigos_media, nome, peso, preco)
276-
VALUES ('Poção', p_descricao, p_drop_inimigos_media, p_nome, p_peso, p_preco)
275+
INSERT INTO item (tipo)
276+
VALUES ('Poção')
277277
RETURNING id INTO v_item_id;
278278

279279
-- Criar a poção
280-
INSERT INTO pocao (id, turnos, usado)
281-
VALUES (v_item_id, p_turnos, FALSE);
280+
INSERT INTO pocao (id, turnos, usado, descricao, drop_inimigos_media, nome, peso, preco)
281+
VALUES (v_item_id, p_turnos, FALSE, p_descricao, p_drop_inimigos_media, p_nome, p_peso, p_preco);
282282

283283
RETURN v_item_id;
284284
END;

‎src/database/dql/query.py

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# get regions and respective elements
21
from numpy import character
32
from logic.enemy import Enemy
43
from logic.character import Character
54

5+
# get regions and respective elements
66
def regions(conn):
77
with conn.cursor() as cur:
88
cur.execute(
@@ -146,21 +146,47 @@ def list_item_inventory(conn, character_id):
146146
cur.execute("""
147147
SELECT id FROM inventario WHERE personagem_id = %s AND tipo = 'Mochila'
148148
""", (character_id,))
149-
149+
150150
inventarios = cur.fetchall()
151-
151+
152152
items = []
153153
for inventario in inventarios:
154154
inventario_id = inventario[0]
155155
cur.execute("""
156-
SELECT item.nome, item.descricao, COUNT(item_instancia.id) as quantidade
157-
FROM item_instancia
158-
JOIN item ON item_instancia.item_id = item.id
159-
WHERE item_instancia.inventario_id = %s
160-
GROUP BY item.nome, item.descricao
156+
-- Seleciona os itens que estão na mochila do personagem
157+
SELECT
158+
i.tipo,
159+
CASE
160+
WHEN i.tipo = 'Poção' THEN p.nome
161+
WHEN i.tipo = 'Pergaminho' THEN pe.nome
162+
WHEN i.tipo = 'Acessório' THEN a.nome
163+
ELSE 'Desconhecido'
164+
END AS nome,
165+
CASE
166+
WHEN i.tipo = 'Poção' THEN p.descricao
167+
WHEN i.tipo = 'Pergaminho' THEN pe.descricao
168+
WHEN i.tipo = 'Acessório' THEN a.descricao
169+
ELSE 'Sem descrição'
170+
END AS descricao,
171+
COUNT(ii.id) AS quantidade
172+
FROM item_instancia ii
173+
JOIN item i ON ii.item_id = i.id
174+
LEFT JOIN pocao p ON i.id = p.id
175+
LEFT JOIN pergaminho pe ON i.id = pe.id
176+
LEFT JOIN acessorio a ON i.id = a.id
177+
WHERE ii.mochila_id = %s
178+
GROUP BY
179+
i.tipo,
180+
p.nome,
181+
pe.nome,
182+
a.nome,
183+
p.descricao,
184+
pe.descricao,
185+
a.descricao
161186
""", (inventario_id,))
162-
187+
163188
items.extend(cur.fetchall())
189+
164190
return items
165191

166192
def get_civilian_info(conn, npc_name):
@@ -281,7 +307,7 @@ def update_mp(conn, character_id, new_energia_arcana):
281307
cur.execute(
282308
f"""
283309
UPDATE personagem
284-
SET energia_arcana = GREATEST(0, energia_arcana - {new_energia_arcana})
310+
SET energia_arcana = GREATEST(0, {new_energia_arcana})
285311
WHERE id = {character_id}
286312
"""
287313
)
@@ -299,12 +325,9 @@ def update_combat(conn , enemies: Enemy, character: Character):
299325
with conn.cursor() as cursor:
300326
for enemy in enemies:
301327
cursor.execute(
302-
f"""
303-
SELECT atualizar_combate (
304-
{character.id},
305-
{enemy.id},
306-
{character.vida},
307-
{enemy.vida}
308-
)"""
328+
"""
329+
SELECT atualizar_combate(%s, %s, %s, %s)
330+
""",
331+
(character.id, enemy.id, int(character.vida), int(enemy.vida))
309332
)
310333
conn.commit()

‎src/default.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def populate_database(db: Database):
6161
#enemie_storage.enemie_storage(db)
6262
#quest.quests(db)
6363
#
64-
##potions
65-
#potions.potions(db)
66-
#potion_effect.potion_effect(db)
64+
#potions
65+
potions.potions(db)
66+
potion_effect.potion_effect(db)
6767

6868

6969
db.conn.commit()

‎src/logic/character.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ def add_initial_items(self, conn):
164164

165165
with conn.cursor() as cur:
166166
cur.execute("""
167-
INSERT INTO item_instancia (item_id, inventario_id)
167+
INSERT INTO item_instancia (item_id, mochila_id)
168168
VALUES (%s, %s)
169169
RETURNING item_id;
170170
""", (elixir_da_vida_id, inventory_id))
171171

172172
cur.execute("""
173-
INSERT INTO item_instancia (item_id, inventario_id)
173+
INSERT INTO item_instancia (item_id, mochila_id)
174174
VALUES (%s, %s)
175175
RETURNING item_id;
176176
""", (elixir_da_vida_id, inventory_id))
177177

178178
cur.execute("""
179-
INSERT INTO item_instancia (item_id, inventario_id)
179+
INSERT INTO item_instancia (item_id, mochila_id)
180180
VALUES (%s, %s)
181181
RETURNING item_id;
182182
""", (mana_liquida_id, inventory_id))

‎src/logic/combat.py

+60-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ast import For
12
from typing import List
23
import random
34
from colorama import Fore, Style
@@ -87,13 +88,16 @@ def get_spell(self) -> tuple:
8788
selected_spell = spells[option_i - 1]
8889
_, _, _, energia_arcana, *_ = selected_spell
8990

90-
91+
# Energia_arcana != None
92+
energia_arcana = energia_arcana if energia_arcana is not None else 0
93+
9194
if energia_arcana > self.character.energia_arcana:
9295
print(Fore.RED + "Energia Arcana insuficiente!" + Style.RESET_ALL)
9396
display.press_enter()
9497
continue
9598
# To-do: Add to query
96-
self.character.energia_arcana = query.update_mp(self.conn, self.character.id, energia_arcana)
99+
self.character.energia_arcana -= energia_arcana
100+
self.character.energia_arcana = query.update_mp(self.conn, self.character.id, self.character.energia_arcana)
97101
break
98102

99103
return selected_spell
@@ -108,7 +112,7 @@ def apply_spell_effect(self, spell) -> None:
108112
# ..
109113
# To-do: debug test...
110114
# ..
111-
debug(tipo, energia_arcana)
115+
debug(f" tipo do feitiço: {tipo}, qtd_energia: {energia_arcana}")
112116
display.press_enter()
113117

114118
if tipo == 'Dano':
@@ -117,9 +121,10 @@ def apply_spell_effect(self, spell) -> None:
117121

118122

119123
elif tipo == 'Dano de área':
120-
enemy = self.select_enemy()
121-
debug('to-do: area damage')
122-
display.press_enter()
124+
# enemy = self.select_enemy()
125+
# debug('to-do: area damage')
126+
# display.press_enter()
127+
self.apply_area_damage_spell(spell)
123128

124129

125130
elif tipo == 'Cura':
@@ -128,19 +133,30 @@ def apply_spell_effect(self, spell) -> None:
128133
# Functionality:
129134
# Returns true if enemy was killed.
130135
def apply_damage_spell(self, spell, enemy) -> bool:
131-
damage = spell[3]
136+
damage = spell[4]
132137

133138
damage *= self.advantage_element(self.character.elemento, enemy.elemento)
134139

135140
enemy.vida = min(enemy.vida - damage, 0)
136141

137142
# To-do: put this into a interface file.
138143
print(f"{Fore.BLUE} Foi conjurado {spell[0]} causando {damage} de dano! {Style.RESET_ALL}")
139-
140144
return enemy.vida <= 0
141145

142146
# Functionality:
143147
# Apply area damage spell
148+
def apply_area_damage_spell(self, spell):
149+
nome, _, _, _, dano, qtd_inimigos_afetados = spell
150+
print(f"{Fore.MAGENTA} {nome} foi conjurado! {Style.RESET_ALL}")
151+
152+
# filter enemies alive and random chose enemies
153+
alive_enemies = [enemy for enemy in self.enemies if enemy.vida > 0]
154+
affected_enemies = random.sample(alive_enemies, min(qtd_inimigos_afetados, len(alive_enemies)))
155+
156+
for enemy in affected_enemies:
157+
final_damage = dano * self.advantage_element(self.character.elemento, enemy.elemento)
158+
enemy.vida = max(enemy.vida - final_damage, 0)
159+
print(f"{Fore.BLUE} {enemy.nome} recebeu {final_damage} de dano! {Style.RESET_ALL}")
144160

145161
# Functionality:
146162
# Apply recover from healing spell
@@ -157,6 +173,20 @@ def enemy_delay(self):
157173
print(Style.BRIGHT + Fore.YELLOW + "..." + Style.RESET_ALL)
158174
time.sleep(1)
159175

176+
# Functionality
177+
# Enemy turn
178+
def enemy_turn(self):
179+
print(Style.BRIGHT + Fore.YELLOW + "Os inimigos estão atacando!!!" + Style.RESET_ALL)
180+
time.sleep(1)
181+
182+
for enemy in self.enemies:
183+
if enemy.vida > 0:
184+
damage = random.randint(3, 10)
185+
damage *= self.advantage_element(enemy.elemento, self.character.elemento)
186+
self.character.vida = max(self.character.vida - damage, 0)
187+
188+
print(f"{Fore.RED} {enemy.nome} te atacou e causou {damage} de dano! {Style.RESET_ALL}")
189+
160190
# Functionality:
161191
# Select and returns an enemy.
162192
def select_enemy(self) -> Enemy:
@@ -170,6 +200,19 @@ def select_enemy(self) -> Enemy:
170200
], False)
171201
return self.enemies[enemy_i - 1]
172202

203+
# Functionality
204+
# check if combat has terminated
205+
def check_combat_end(self):
206+
if self.character.vida <= 0:
207+
print(Fore.RED + "Você foi derrotado..." + Style.RESET_ALL)
208+
return True
209+
210+
if all(enemy.vida <= 0 for enemy in self.enemies):
211+
print(Fore.GREEN + "Todos os inimigos foram derrotados!" + Style.RESET_ALL)
212+
return True
213+
214+
return False
215+
173216
# Logic:
174217
# True: if the player ran away or won the combat
175218
# False: if the player died.
@@ -221,17 +264,18 @@ def init(self):
221264

222265
debug(self.character.energia_arcana)
223266
display.press_enter()
224-
225-
# to-do:
226-
query.update_combat(self.conn, self.enemies, self.character)
227-
228267

229268
elif option == "Usar Poção":
230269
debug("to-do: usar poção")
231270
display.press_enter()
232-
233-
# ---
234-
# ....
235-
# To-do: enemies turn.
271+
272+
query.update_combat(self.conn, self.enemies, self.character)
273+
if self.check_combat_end():
274+
return True
236275

276+
self.enemy_turn()
277+
278+
if self.check_combat_end():
279+
return True
280+
237281
return True

0 commit comments

Comments
 (0)
Please sign in to comment.