Example #1
0
def move(index: int):
    game_state = json.loads(redis_client.get('game_state').decode())
    moved = False
    if not game_state['win']:
        values = game_state['values']
        empty_index = values.index('')
        movable_indexes = {empty_index - 4, empty_index + 4}
        if (empty_index + 1) % 4:
            movable_indexes.add(empty_index + 1)
        if (empty_index - 1) % 4 != 3:
            movable_indexes.add(empty_index - 1)
        if index in movable_indexes:
            values[empty_index] = values[index]
            values[index] = ''
            game_state['values'] = values
            moved = True
            game_state['move_count'] += 1
            if values[-1] == '':
                last_value = values[0]
                win = True
                for value in values[1:-1]:
                    if value < last_value:
                        win = False
                        break
                    last_value = value
                game_state['win'] = win
                if win and DB_ACTIVE:
                    result = Results(move_count=game_state['move_count'])
                    db.session.add(result)
                    db.session.commit()
    game_state = json.dumps(game_state)
    if moved:
        redis_client.set('game_state', game_state)
    return game_state
def get_userid(sessionId):
    if len(sessionId) > 1:
        sessionId = sessionId.replace("=", ":").replace("sessionid", "session")
        if redis_client.exists(sessionId):
            sessionValue = redis_client.get(sessionId)
            pickledObject = pickle.loads(sessionValue)
            if 'user_id' in pickledObject:
                return pickledObject['user_id']
            else:
                return ''
        else:
            return ''

    else:
        return ''
def check_permission(request):
    try:
        cookie = {'sessionid': request.cookies['sessionid']}
        session_id = 'session:' + request.cookies['sessionid']
        session_value = redis_client.get(session_id)
        pickled_object = pickle.loads(session_value)
        permission_objects = requests.get(env.scouting_url +
                                          "/api/scouting/knowage_permissions",
                                          cookies=cookie)
        permission_objects = json.loads(permission_objects.content)
        for permission_object in permission_objects["results"]:
            if permission_object["user"] == pickled_object['user_id']:
                return permission_object
        return False
    except Exception as e:
        logger.exception('Got exception when checking permission.')
        return False
def knowage_login():
    try:
        sessionid = request.headers.get('Cookie', ' ')
        session_id = 'session:' + request.cookies['sessionid']
        session_value = redis_client.get(session_id)
        pickled_object = pickle.loads(session_value)
        ## login check for engro user
        if 'sessionid' in sessionid:
            if pickled_object['user_id']:
                object = components.check_permission(request)
                ## knowage user check
                if object:
                    ## knowage user type check
                    if object["user_type"] == 'admin':
                        return login_request('demo_admin', 'demo_admin')
                    elif object['user_type'] == 'manager':
                        return login_request('demo_manager', 'demo_manager')
                    else:
                        return login_request('demo_user', 'demo_user')
                else:
                    return json.dumps({
                        "status_code": 200,
                        "message": "User not permitted"
                    })
            else:
                return json.dumps({
                    "status_code":
                    200,
                    "message":
                    "Your session has expired. Please Login again"
                })
        else:
            return json.dumps({
                "status_code": 200,
                "message": "User not logged in"
            })
    except ValueError as e:
        print(e)
Example #5
0
def game_info():
    info = redis_client.get('game_state') or None
    return info.decode() if info else {}
Example #6
0
def game_info():
    # Получим из Redis информацию о текущей игре
    info = redis_client.get('game_state') or None
    # Если информация получена, декодируем её и вернём в ответе на запрос,
    # Иначе, вернём пустой объект
    return info.decode() if info else {}
Example #7
0
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from config import redis_client
from app import app, db

app.config.from_object(str(redis_client.get('APP_SETTINGS').decode('utf-8')))

migrate = Migrate(app, db)
manager = Manager(app)

manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()
Example #8
0
def security():
    password = str(redis_client.get('FLUTTER_SECURITY').decode('utf-8'))
    pass_dict = {'password': password}
    return jsonify(pass_dict), 200