37
37
38
38
class Character :
39
39
40
- def __init__ (self , conn , id = None ):
40
+ def __init__ (self , conn , id = None ):
41
41
self .conn = conn
42
42
self .id = None
43
43
self .nome = None
@@ -56,8 +56,8 @@ def __init__(self, conn, id = None):
56
56
if not id :
57
57
self .get_information ()
58
58
else :
59
- character_info = self .get_character_info (conn , id )
60
- self .id = character_info [0 ]
59
+ character_info = self .get_character_info (conn , id )
60
+ self .id = character_info [0 ]
61
61
self .sub_regiao_id = character_info [1 ]
62
62
self .nome = character_info [2 ]
63
63
self .elemento = character_info [3 ]
@@ -71,63 +71,87 @@ def __init__(self, conn, id = None):
71
71
self .inteligencia = character_info [11 ]
72
72
self .moedas = character_info [12 ]
73
73
self .nivel = character_info [13 ]
74
-
75
- def definy_initial_spells (self , conn ):
76
- try :
77
- spells = initial_spells [self .elemento ]
78
-
79
- with conn .cursor () as cur :
80
- for spell in spells :
81
- spell_id = spell [0 ]
82
- cur .execute (
83
- """
84
- SELECT aprender_feitico (%s, %s);
85
- """ , (self .id , spell_id )
86
- )
87
-
88
- debug (f"Initial Spells learned for '{ self .nome } ' with success" )
89
- except Exception as e :
90
- debug (f"Error: { e } " )
91
- raise
92
74
93
75
def get_information (self ):
94
76
print ("\n === Criação de Personagem === " )
95
77
self .nome = input ("Digite o nome do personagem: " )
96
-
78
+
97
79
def ask ():
98
80
return input (f"Escolha o elemento ({ ', ' .join (elements )} ): " ).lower ()
99
-
81
+
100
82
elemento = ask ()
101
83
lower_case_elements = [elemento .lower () for elemento in elements ]
102
84
while elemento not in lower_case_elements :
103
85
print ("Elemento inválido." )
104
86
elemento = ask ()
105
-
87
+
106
88
self .elemento = elemento .capitalize ()
107
- # self.definy_initial_spells(self.conn)
108
89
109
90
def add_database (self ):
110
91
try :
111
92
with self .conn .cursor () as cur :
112
93
cur .execute ("SELECT criar_personagem(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" , (self .sub_regiao_id , self .nome , self .elemento , self .conhecimento_arcano , self .vida , self .vida_maxima , self .xp , self .xp_total , self .energia_arcana , self .energia_arcana_maxima , self .inteligencia , self .moedas , self .nivel ))
113
- self .id = cur .fetchone ()[0 ]
114
- self .conn .commit ()
115
-
116
- debug (f"Character: Personagem '{ self .nome } ' adicionado com sucesso!" )
94
+ result = cur .fetchone ()
95
+ if result :
96
+ self .id = result [0 ]
97
+ self .conn .commit ()
98
+ debug (f"Character: Personagem '{ self .id } ' '{ self .nome } ' adicionado com sucesso!" )
99
+ else :
100
+ debug ("Character: Não foi retornado um ID após a criação do personagem." )
117
101
return self
118
102
119
103
except Exception as e :
120
104
debug (f"Character: Erro ao adicionar personagem: { e } " )
121
105
122
- def get_character_info (self , conn , id ):
106
+ def get_character_info (self , conn , id ):
123
107
with conn .cursor () as cur :
124
108
cur .execute ("""
125
109
SELECT *
126
110
FROM personagem
127
111
WHERE id = %s
128
112
""" , (id ,))
129
- result = cur .fetchone ()
113
+ result = cur .fetchone ()
130
114
return result
131
-
132
115
116
+ def create_inventory_if_not_exists (self , conn ):
117
+ with conn .cursor () as cur :
118
+ cur .execute (
119
+ "SELECT id FROM inventario WHERE personagem_id = %s" , (self .id ,)
120
+ )
121
+ result = cur .fetchone ()
122
+
123
+ if not result : # Se não existe, cria o inventário
124
+ cur .execute (
125
+ "INSERT INTO inventario (personagem_id) VALUES (%s) RETURNING id" ,
126
+ (self .id ,)
127
+ )
128
+ inventory_id = cur .fetchone ()[0 ]
129
+ conn .commit ()
130
+ print (f"✅ Inventário criado para { self .nome } (ID { inventory_id } )" )
131
+ else :
132
+ inventory_id = result [0 ]
133
+
134
+ return inventory_id
135
+
136
+
137
+ def define_initial_spells (self , conn ):
138
+ print (f"Definindo feitiços iniciais para { self .nome } ..." )
139
+ try :
140
+ inventory_id = self .create_inventory_if_not_exists (conn )
141
+ spells = initial_spells [self .elemento ]
142
+
143
+ with conn .cursor () as cur :
144
+ for spell in spells :
145
+ spell_id = spell [0 ]
146
+ # print(f"Inserindo feitiço {spell_id} no inventário {inventory_id}") # Debug
147
+ cur .execute (
148
+ """
149
+ INSERT INTO feitico_aprendido (inventario_id, feitico_id)
150
+ VALUES (%s, %s);
151
+ """ , (inventory_id , spell_id )
152
+ )
153
+ conn .commit ()
154
+ except Exception as e :
155
+ print (f"Erro ao definir feitiços iniciais: { e } " )
156
+ conn .rollback ()
133
157
0 commit comments