diff --git a/game/personnage.py b/game/personnage.py index b7d45e8..2e8a165 100644 --- a/game/personnage.py +++ b/game/personnage.py @@ -12,17 +12,45 @@ class Material: #TODO: inventaire mysql class Inventaire: - def __init__(self, id, item): + def __init__(self, id, item=None): self._id = id - + self._slots = [] + self.append(item) + self.append(Material.POTION) - def append(self, item): + def append(self, item_type): conn = Connection() - conn.db.execute("INSERT INTO inventaire (personnage_id, nom) VALUES (?, ?);", (self._id, item)) + conn.db.execute("INSERT INTO inventaire (personnage_id, item_type) VALUES (?, ?);", (self._id, item_type)) + conn.commit() conn.db.execute("SELECT last_insert_rowid();") + self._slots.append(conn.db.fetchone()[0]) + conn.close() + + def remove(self, slot): + conn = Connection() + conn.db.execute("DELETE FROM inventaire WHERE personnage_id=? AND slot=?", (self._id, slot)) + conn.commit() self._id = conn.db.fetchone()[0] conn.close() + 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 @@ -122,4 +150,4 @@ class Personnage: ) def affiche_inventaire(self): - return ["- "+item for item in self.inventaire] + return ["- "+item[0] for item in self.inventaire.get_items()] diff --git a/jeu.db b/jeu.db index 44e1e03..40e81ab 100644 Binary files a/jeu.db and b/jeu.db differ diff --git a/main.py b/main.py index 69ecb7e..434a48c 100644 --- a/main.py +++ b/main.py @@ -23,10 +23,11 @@ if __name__ == "__main__": conn.db.execute( """CREATE TABLE IF NOT EXISTS inventaire ( - personnage_id INTEGER PRIMARY KEY, + personnage_id INTEGER, slot INTEGER PRIMARY KEY AUTOINCREMENT, - nom VARCHAR(50) NOT NULL, - FOREIGN KEY(personnage_id) REFERENCES personnages(id) + item_type VARCHAR(50) NOT NULL, + FOREIGN KEY (personnage_id) REFERENCES personnages(id) + UNIQUE (personnage_id, slot) )""") conn.close()