def load_from_db(self, _recursive=True): """ Fetch data from database, if _recursive is True fetch each steps too """ db = Database() result = db.select_one( ''' SELECT questNumber, questName FROM quest WHERE questId = ? ''', (self.m_id, )) if result: self.m_number = result[0] self.m_name = result[1] result = db.select_all( ''' SELECT questId, stepNumber FROM step WHERE step.questId = ? ORDER BY stepNumber ''', (self.m_id, )) if result: for row in result: self.add_step(Step( row[0], # questId row[1], # stepNumber )) if _recursive: for step in self.m_steps: step.load_from_db()
def register(_username, _password): """ Register new user in database with couple(username, password), return True if add, False if doesnt """ db = Database() if db.select_one('''SELECT username FROM user WHERE username LIKE ?''', (_username, )) is not None: return "Username already taken" else: pwh = str(bcrypt.hashpw(_password.encode('utf-8'), bcrypt.gensalt())) db.update("INSERT INTO user(username,password) VALUES(?,?)", (_username, pwh[2:(len(pwh) - 1)])) return User.login(_username, _password)
def load_from_db(self): """ Fetch data from database """ db = Database() result = db.select_one( ''' SELECT textOfTheStep, stepOptions FROM step WHERE questId = ? AND stepNumber = ? ''', (self.quest_id, self.m_number)) if result: self.text = result[0] self.options = result[1] return result
def login(_username, _password): """ Login with a check of the (username, password) couple in Database """ db = Database() result = db.select_one('''SELECT idUser, password FROM user WHERE username LIKE ?''', (_username, )) if result is not None: if result[1] != "": if bcrypt.checkpw(_password.encode('utf-8'), result[1].encode('utf-8')): return User(result[0], _username) else: return "Error : Invalid Password" else: return "Error : Invalid Username" else: return "Error : no user found"
def delete(_username, _password, user): """ Delete with a check of the password in Database """ db = Database() result = db.select_one('''SELECT idUser, password FROM user WHERE username LIKE ?''', (_username, )) if result is not None: if result[1] != "": if bcrypt.checkpw(_password.encode('utf-8'), result[1].encode('utf-8')): for hero in user.heroes: hero.delete() db.delete("DELETE FROM user WHERE username = ?", (_username,)) else: return "Error : Invalid Password" else: return "Error : Invalid Username" else: return "Error : no user found"
def load_from_db(self): """ Fetch data from database, if _recursive is True fetch each steps too """ db = Database() result = db.select_one( ''' SELECT lvl, weapon, armor, passive, sex, idUser, numQuest, numStep FROM hero WHERE nameOfTheHero = ? ''', (self.name,) ) if result is not None: self.lvl = result[0] self.weapon = result[1] self.armor = result[2] self.passive = result[3] self.sex = result[4] self.user_id = result[5] self.current_quest = result[6] self.current_step = result[7]
def load_to_db(self): """ Persist instance to database, if _recursive is True persist each steps too """ db = Database() if db.select_one("SELECT idHero FROM hero WHERE nameOfTheHero = ?", (self.name,)): db.update( ''' UPDATE hero SET lvl = ?,weapon = ?, armor = ?, passive = ?, sex = ?, idUser = ?,numQuest = ?, numStep = ? WHERE nameOfTheHero = ? ''', (self.lvl, self.weapon["name"], self.armor, self.passive, 1 if self.m_sex else 0, self.user_id, self.current_quest, self.current_step, self.name) ) else: db.add( ''' INSERT INTO hero(nameOfTheHero, lvl, weapon, armor, passive, sex, idUser, numQuest, numStep) VALUES(?,?,?,?,?,?,?,?,?) ''', (self.name, self.lvl, self.weapon["name"], self.armor, self.passive, 1 if self.m_sex else 0, self.user_id, self.current_quest, self.current_step) )
def check_hero_available(_name): """ Verify if a hero with _name already exist, return True if None, False il already exist """ db = Database() if not db.select_one('''SELECT nameOfTheHero FROM hero WHERE nameOfTheHero = ?''', (_name,)): return True return False