diff --git a/JDR_SQL.py b/JDR_SQL.py index 96ac268..211be95 100644 --- a/JDR_SQL.py +++ b/JDR_SQL.py @@ -5,7 +5,7 @@ import time #Création de la table Personnage conn = sqlite3.connect('JeuDeRole.db') c = conn.cursor() -c.execute('''CREATE TABLE if not exists Perso(num_joueur INT, nom TEXT, pdv INT, exp INT, categorie TEXT)''') +c.execute('''CREATE TABLE if not exists Perso (nom TEXT PRIMARY KEY, pdv INT, exp INT, categorie TEXT)''') conn.commit() class Personnage: @@ -79,17 +79,23 @@ P1 = Personnage(nomP1, catP1) P2 = Personnage(nomP2, catP2) -J1 = (1, nomP1, 20, 1, catP1) -J2 = (2, nomP2, 20, 1, catP2) +J1_1 = (20, 1, catP1, nomP1) +J2_1 = (20, 1, catP2, nomP2) +J1_2 = (nomP1, 20, 1, catP1) +J2_2 = (nomP2, 20, 1, catP2) liste = c.execute("SELECT nom FROM Perso") -if nomP1 in liste : - - c.execute("UPDATE Perso SET num_joueur = (?), nom = (?), pdv = (?), exp = (?), categorie = (?)", J1) - c.execute("UPDATE Perso SET num_joueur = (?), nom = (?), pdv = (?), exp = (?), categorie = (?)", J2) +conn.commit() +if (nomP1,) in liste : + c.execute("UPDATE Perso SET pdv = (?), exp = (?), categorie = (?) WHERE nom = (?)", J1_1) +else: + c.execute("INSERT INTO Perso VALUES (?, ?, ?, ?)", J1_2) +conn.commit() +if (nomP2,) in liste: + c.execute("UPDATE Perso SET pdv = (?), exp = (?), categorie = (?) WHERE nom = (?)", J2_1) else: - c.execute("INSERT INTO Perso VALUES (?, ?, ?, ?, ?)", J1) - c.execute("INSERT INTO Perso VALUES (?, ?, ?, ?, ?)", J2) + c.execute("INSERT or REPLACE INTO Perso VALUES (?, ?, ?, ?)", J2_2) conn.commit() +#https://stackoverflow.com/questions/35415469/sqlite3-unique-constraint-failed-error print("-----------------------------") while P1.pdv > 0 and P2.pdv > 0: @@ -101,24 +107,24 @@ while P1.pdv > 0 and P2.pdv > 0: print(nomP1, "attaque", nomP2, "!!!", nomP2,"vient de perdre", -perte_pdv, "point(s) de vie.") P2.change_pdv(perte_pdv) if P2.pdv > 0: - pdv2 = (P2.pdv) - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 2", (pdv2,)) + pdv2 = (P2.pdv, nomP2,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv2) conn.commit() else: - pdv2 = 0 - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 2", (pdv2,)) + pdv2 = (0, nomP2,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv2) conn.commit() else: perte_pdv = -randint(1,4) print(nomP2, "arrive à se défendre de l'attaque de", nomP1,"!!!", nomP1,"vient de perdre", -perte_pdv, "point(s) de vie.") P1.change_pdv(perte_pdv) if P1.pdv > 0: - pdv1 = (P1.pdv) - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 1", (pdv1,)) + pdv1 = (P1.pdv, nomP1,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv1) conn.commit() else: - pdv1 = 0 - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 1", (pdv1,)) + pdv1 = (0, nomP1,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv1) conn.commit() P1.affiche_caracteristiques() P2.affiche_caracteristiques() @@ -126,9 +132,6 @@ while P1.pdv > 0 and P2.pdv > 0: if P1.pdv > 0 and P2.pdv <= 0: P1.change_exp(1) print("Le joueur", nomP1, "a gagné.") - valeur = 0 - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 1", (valeur,)) - conn.commit() break elif P1.pdv <= 0 and P2.pdv > 0: P2.change_exp(1) @@ -136,10 +139,11 @@ while P1.pdv > 0 and P2.pdv > 0: break - print("-----------------------------") + print("-----------------------------------") if P1.pdv > 0 and P2.pdv > 0: input("~~~Veuillez appuyer sur Entrée~~~") - print(" ") + print("-----------------------------------") + print(" ") print("---Phase 2 du tour (", nomP2,"attaque)---") @@ -150,24 +154,24 @@ while P1.pdv > 0 and P2.pdv > 0: print(nomP2, "attaque", nomP1, "!!!", nomP1,"vient de perdre", -perte_pdv, "point(s) de vie.") P1.change_pdv(perte_pdv) if P1.pdv > 0: - pdv1 = (P1.pdv) - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 1", (pdv1,)) + pdv1 = (P1.pdv, nomP1,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv1) conn.commit() else: - pdv1 = 0 - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 1", (pdv1,)) + pdv1 = (0, nomP1,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv1) conn.commit() else: perte_pdv = -randint(1,4) print(nomP1, "arrive à se défendre de l'attaque de", nomP2, "!!!", nomP2,"vient de perdre", -perte_pdv, "point(s) de vie.") P2.change_pdv(perte_pdv) if P2.pdv > 0: - pdv2 = (P2.pdv) - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 2", (pdv2,)) + pdv2 = (P2.pdv, nomP2,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv2) conn.commit() else: - pdv2 = 0 - c.execute("UPDATE Perso SET pdv = (?) WHERE num_joueur = 2", (pdv2,)) + pdv2 = (0, nomP2,) + c.execute("UPDATE Perso SET pdv = (?) WHERE nom = (?)", pdv2) conn.commit() P1.affiche_caracteristiques() P2.affiche_caracteristiques() @@ -178,9 +182,10 @@ while P1.pdv > 0 and P2.pdv > 0: P2.change_exp(1) print("Le joueur", nomP2, "a gagné.") - print("-----------------------------") + print("-----------------------------------") if P1.pdv > 0 and P2.pdv > 0: input("~~~Veuillez appuyer sur Entrée~~~") + print("-----------------------------------") print(" ") conn.close() \ No newline at end of file