def add_profile(id_, skill, color, ab): g.prfl = ProfilesDB(get_db()) g.skll = SkillsDB(get_db()) if g.prfl.addProfile(id_, color) and g.skll.addFirstSkill( id_, skill, ab): return True return False
def messages(): dbase = MessagesDB(get_db()) if request.method == 'POST': user_id = request.form['user_id'] message = request.form['message'] #print('form data massage:', request.form) if not check_cookies(): return jsonify({'error': 'in cookies'}) # if not dbase.isAuthValid(user_id, code): # return jsonify( {"success": False, "error": "not authorized"} ) #if dbase.userVerificationWhenSendingMessage(user_id, code): if dbase.addMessageInDB(user_id, message): return jsonify({"success": True}) else: return jsonify({"success": False, "error": "added failed"}) return jsonify({"success": False, "error": "verification failed"}) elif request.method == 'GET': data = [] for m in dbase.getMessages(): print('mm:', m) mes = {'user_id': m[0], 'text': m[1], 'time': m[2]} data.append(mes) return jsonify(data) return jsonify({"success": False})
def get_sectors(): db1 = CreaturesDB(get_db()) db2 = SectorsDB(get_db()) db3 = SkillsDB(get_db()) data = [] user_data = {} sectors_data = db2.getSectors() sectors_data = sorted(sectors_data) for sector in sectors_data: creatures = [] all_creatures = db1.getSectorCreatures(sector[0]) if all_creatures: for user_id in all_creatures: if user_id: a = db1.getCreatureDataByUserId(user_id[0]) skills = db3.getSkills(user_id[0]) if skills: user_data = { 'user_id': user_id[0], 'amount': a[0], 'skills': skills[0] } creatures.append(user_data) else: creatures = [] b = { 'id': sector[0], 'position_top': sector[1], 'position_left': sector[2], 'food': sector[3], 'type': sector[4], 'creatures': creatures } data.append(b) response = flask.jsonify(data) response.headers.add('Access-Control-Allow-Origin', '*') return response
def addMAIN(): user_id = request.form['user_id'] sector_id = request.form['sector_id'] code = request.form['code'] db = EvolDataBase(get_db()) if not db.isAuthValid(user_id, code): return jsonify({"success": False, "error": "not authorized"}) #print('first func -', add_user_to_sector(user_id, sector_id, code)) #print('second func -', has_user_enough_amount_in_neighbors(sector_id, user_id)) if add_user_to_sector(user_id, sector_id, code): return jsonify({"success": True}) #elif has_user_enough_amount_in_neighbors(sector_id, user_id): # return jsonify( {"success": True} ) return jsonify({"success": False, "error": "cannot add user to sector"})
def login(login): #if login: print('login in APP:', login) g.data = UsersDB(get_db()) user_id = g.data.getUserId(login) if user_id: user_id = user_id[0] else: return False user_hpsw = g.data.getUserPsw(user_id) if user_hpsw: user_hpsw = user_hpsw[0] else: return False code = g.data.addAuthUser(user_id) #print('code', code) if user_id and user_hpsw and code: return user_id, user_hpsw, code return False
def getNeigborId(left, top): g.data = SectorsDB(get_db()) id_sector = g.data.getNeigborId(left, top) if id_sector: return id_sector else: return False
def getUserAmountInNeighbors(id_sector, id_user): g.data = SectorsDB(get_db()) ngbrs = g.data.getUserAmountInNeighbors(id_sector, id_user) if ngbrs: return ngbrs else: return False
def getSectors(): g.data = SectorsDB(get_db()) sectors = g.data.getSectors() if sectors: return sectors else: return False
def add_sector(position_top, position_left, food, type_): g.data = SectorsDB(get_db()) if g.data.addSector(position_top, position_left, int(food), type_): return True return False
def deleteUser(user_id): g.data = UsersDB(get_db()) delete = g.data.deleteUser(user_id) if delete: return delete else: return False
def registration(name, hash, login, email): g.data = UsersDB(get_db()) if g.data.addUser(name, hash, login, email): return True return False
def add_skill(id_, skill, ab): g.skll = SkillsDB(get_db()) if g.skll.addSkill(id_, skill, ab): return True return False
def before_request(): global dbase db = get_db() dbase = SectorsDataBase(db)
def getUserCode(user_id): g.data = UsersDB(get_db()) code = g.data.getUserCode(user_id) if code: return code return False
def get_skills(id_): g.skll = SkillsDB(get_db()) res = g.skll.getSkills(id_) if res: return res return False
def getUserIdbyLogin(login): g.data = UsersDB(get_db()) user_id = g.data.getUserIdbyLogin(login) if user_id: return user_id return False
def check_auth_user(user_login): g.data = UsersDB(get_db()) if g.data.checkAuthUser(user_login): return True return False
def getUsers(): g.data = UsersDB(get_db()) users = g.data.getUsers() if users: return users return False
def getAllSectorsPositions(): g.data = SectorsDB(get_db()) positions = g.data.getAllSectorsPositions() if positions: return positions else: return False
def getSectorFood(id_sector): g.data = SectorsDB(get_db()) food = g.data.getSectorFood(id_sector) if food: return food else: return False
def addUserCreaturesAmount(id_sector, id_user, amount): g.data = CreaturesDB(get_db()) if g.data.addUserCreaturesAmount(id_sector, id_user, amount): return True else: return False
def getCreatures(): g.data = CreaturesDB(get_db()) res = g.data.getCreatures() if res: return res else: return False
def auth(user_id, code): g.data = UsersDB(get_db()) if g.data.isAuthValid(user_id, code): return True return False
def getUserCreaturesAmount(self, id_user, id_sector): with app.app_context(): g.data = CreaturesDB(get_db()) amount = g.data.getUserCreaturesAmount(id_user, id_sector) if amount: return amount else: return False