You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.9 KiB

2 years ago
from random import randint
from graphics.layers import Sprite
2 years ago
from util.sqlite import Connection
2 years ago
class Material:
POTION = "POTION"
EPEE = "EPEE"
BATON = "BATON"
DAGUE = "DAGUE"
ARC = "ARC"
2 years ago
#TODO: inventaire mysql
class Inventaire:
2 years ago
def __init__(self, id, item=None):
2 years ago
self._id = id
self._potions = []
2 years ago
self.append(item)
self.append(Material.POTION)
2 years ago
2 years ago
def append(self, item_type):
2 years ago
conn = Connection()
2 years ago
conn.db.execute("INSERT INTO inventaire (personnage_id, item_type) VALUES (?, ?);", (self._id, item_type))
conn.commit()
if item_type == "POTION":
conn.db.execute("SELECT last_insert_rowid();")
slot = conn.db.fetchone()[0]
self._potions.append(slot)
2 years ago
conn.close()
def remove(self, slot):
conn = Connection()
conn.db.execute("DELETE FROM inventaire WHERE personnage_id=? AND slot=?", (self._id, slot))
conn.commit()
2 years ago
conn.close()
def pop_potion(self):
if len(self._potions) == 0:
return None
v = self._potions[0]
del self._potions[0]
return v
2 years ago
def has(self, item_type):
conn = Connection()
conn.db.execute("SELECT slot FROM inventaire WHERE personnage_id=? AND item_type=?", (self._id, item_type))
conn.commit()
l = len(conn.db.fetchall())
conn.close()
return False if l == 0 else True
def get_items(self):
conn = Connection()
conn.db.execute("SELECT item_type FROM inventaire WHERE personnage_id=?;", (self._id,))
values = conn.db.fetchall()
conn.close()
return values
def __len__(self):
return len(self.get_items())
2 years ago
class Personnage:
def __init__(self, nom, cat, personnage_type):
2 years ago
expcoef = None
2 years ago
item = None
2 years ago
if cat == "Guerrier":
2 years ago
item = Material.EPEE
2 years ago
expcoef = 10
2 years ago
elif cat == "Magicien":
2 years ago
item = Material.BATON
2 years ago
expcoef = 10
2 years ago
elif cat == "Voleur":
2 years ago
item = Material.DAGUE
2 years ago
expcoef = 3
2 years ago
elif cat == "Elfe":
2 years ago
item = Material.ARC
2 years ago
expcoef = 8
2 years ago
conn = Connection()
conn.db.execute(f"INSERT INTO personnages (nom, pdv, exp, cat, expcoef) VALUES (?,?,?,?,?);", (nom, 20, 1, cat, expcoef))
conn.commit()
conn.db.execute("SELECT last_insert_rowid();")
self._id = conn.db.fetchone()[0]
conn.close()
2 years ago
2 years ago
self.inventaire = Inventaire(self._id, item)
2 years ago
Sprite(3, cat, personnage_type)
2 years ago
def db_erase(self):
conn = Connection()
conn.db.execute("DELETE FROM personnages WHERE id=?;", (self._id,))
conn.close()
2 years ago
2 years ago
def __select_value(self, value):
2 years ago
conn = Connection()
conn.db.execute("SELECT "+value+" FROM personnages WHERE id=?;", (self._id,))
value = conn.db.fetchone()
conn.close()
return value[0] if value is not None else None
2 years ago
def get_nom(self):
return self.__select_value("nom")
def get_pdv(self):
return self.__select_value("pdv")
def get_exp(self):
return self.__select_value("exp")
def get_cat(self):
return self.__select_value("cat")
def get_expcoef(self):
return self.__select_value("expcoef")
def set_nom(self, v):
2 years ago
conn = Connection()
conn.db.execute("UPDATE personnages SET nom=? WHERE id=?;", (v, self._id))
conn.close()
2 years ago
def change_pdv(self, v):
2 years ago
conn = Connection()
conn.db.execute("UPDATE personnages SET pdv = CASE WHEN pdv - ? <= 0 THEN 0 ELSE pdv - ? END WHERE id=?;", (v, v, self._id))
conn.close()
2 years ago
def change_exp(self, v):
2 years ago
conn = Connection()
2 years ago
if v > 0:
2 years ago
conn.db.execute("UPDATE personnages SET exp=exp+? WHERE id=?;", (v, self._id))
2 years ago
else:
raise ValueError("v attends un nombre positif")
2 years ago
conn.close()
2 years ago
def set_cat(self, v):
2 years ago
conn = Connection()
conn.db.execute("UPDATE personnages SET cat=? WHERE id=?;", (v, self._id))
conn.close()
2 years ago
def set_expcoef(self, v):
2 years ago
conn = Connection()
conn.db.execute("UPDATE personnages SET expcoef=? WHERE id=?;", (v, self._id))
conn.close()
2 years ago
2 years ago
def jet_attaque(self):
damage = randint(1, 20)
2 years ago
self.change_exp(self.get_expcoef())
2 years ago
return damage
def jet_defense(self):
damage = randint(1, 20)
2 years ago
self.change_exp(self.get_expcoef())
2 years ago
return damage
def affiche_caracteristiques(self):
2 years ago
return (f"Nom: {self.get_nom()}",
f"Type de classe: {self.get_cat()}",
f"Vie: {self.get_pdv()}",
f"Expérience: {self.get_exp()}",
f"XPCOEF: {self.get_expcoef()}"
2 years ago
)
def affiche_inventaire(self):
2 years ago
return ["- "+item[0] for item in self.inventaire.get_items()]