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.

161 lines
4.9 KiB

from random import randint
from graphics.layers import Sprite
from util.sqlite import Connection
class Material:
POTION = "POTION"
EPEE = "EPEE"
BATON = "BATON"
DAGUE = "DAGUE"
ARC = "ARC"
#TODO: inventaire mysql
class Inventaire:
def __init__(self, id, item=None):
self._id = id
self._potions = []
self.append(item)
self.append(Material.POTION)
def append(self, item_type):
conn = Connection()
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)
conn.close()
def remove(self, slot):
conn = Connection()
conn.db.execute("DELETE FROM inventaire WHERE personnage_id=? AND slot=?", (self._id, slot))
conn.commit()
conn.close()
def pop_potion(self):
if len(self._potions) == 0:
return None
v = self._potions[0]
del self._potions[0]
return v
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())
class Personnage:
def __init__(self, nom, cat, personnage_type):
expcoef = None
item = None
if cat == "Guerrier":
item = Material.EPEE
expcoef = 10
elif cat == "Magicien":
item = Material.BATON
expcoef = 10
elif cat == "Voleur":
item = Material.DAGUE
expcoef = 3
elif cat == "Elfe":
item = Material.ARC
expcoef = 8
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()
self.inventaire = Inventaire(self._id, item)
Sprite(3, cat, personnage_type)
def db_erase(self):
conn = Connection()
conn.db.execute("DELETE FROM personnages WHERE id=?;", (self._id,))
conn.close()
def __select_value(self, value):
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
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):
conn = Connection()
conn.db.execute("UPDATE personnages SET nom=? WHERE id=?;", (v, self._id))
conn.close()
def change_pdv(self, v):
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()
def change_exp(self, v):
conn = Connection()
if v > 0:
conn.db.execute("UPDATE personnages SET exp=exp+? WHERE id=?;", (v, self._id))
else:
raise ValueError("v attends un nombre positif")
conn.close()
def set_cat(self, v):
conn = Connection()
conn.db.execute("UPDATE personnages SET cat=? WHERE id=?;", (v, self._id))
conn.close()
def set_expcoef(self, v):
conn = Connection()
conn.db.execute("UPDATE personnages SET expcoef=? WHERE id=?;", (v, self._id))
conn.close()
def jet_attaque(self):
damage = randint(1, 20)
self.change_exp(self.get_expcoef())
return damage
def jet_defense(self):
damage = randint(1, 20)
self.change_exp(self.get_expcoef())
return damage
def affiche_caracteristiques(self):
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()}"
)
def affiche_inventaire(self):
return ["- "+item[0] for item in self.inventaire.get_items()]