Exemplo n.º 1
0
def run_script():
    '''
        Run's script inside own container
    '''
    user = check_user()  #
    if type(user) == dict:
        return jsonify(user)

    requires = ["filename", "filetext"]  # #
    failed = bad_request(requires)
    if failed is not None:
        return jsonify(failed)

    filename = request.json['filename']  # # #
    text = request.json['filetext']
    row = 0
    col = 0

    user.touch()
    container = nest.load_container(user)
    file_obj = create_file(filename, text, row, col)
    result = nest.run_file(user, file_obj)

    if result["has_heart"] == False:
        user.form = 'ghost'
    elif result["has_heart"] == None:
        pass
    else:
        user.form = user.character

    db.save()
    # # # #
    return jsonify({"result": result, "user": return_user(user)})
Exemplo n.º 2
0
def collect():
    ''' Execute inside user container and update database '''
    user = check_user()  #
    if type(user) == dict:
        return jsonify(user)

    requires = ["fileid"]  # #
    failed = bad_request(requires)
    if failed is not None:
        return jsonify(failed)

    fileid = request.json['fileid']  # # #
    if user.form == 'ghost':
        return jsonify({"msg": "you're a ghost", "error": True})

    script = db.get("Script", fileid)
    if script is None:
        return jsonify({"msg": "script not found", "error": True})

    filename = script.filename
    text = script.filetext
    row = script.row
    col = script.col
    author = db.get("User", script.user_id)

    if author.id == user.id:
        return jsonify({"msg": "script is yours", "error": True})

    if script.has_collected(user.id):
        return jsonify({
            "msg": "you've already collected this script",
            "error": True
        })

    is_bad_file = script.material >= 20
    container = nest.load_container(user)
    file_obj = create_file(filename, text, row, col)
    result = nest.run_file(user, file_obj)

    if result["has_heart"] == None or result["has_heart"] == False:
        user.form = 'ghost'
        if is_bad_file:
            author.add_material(user.pay_material(script.material))
    else:
        user.form = user.character
        script.collect(user.id)
        if is_bad_file:
            user.add_material(script.material)
        else:
            user.add_material(script.material)
            author.add_material(script.material)

    result['script'] = return_script(user, script)
    result['material'] = script.material
    user.touch()
    db.save()
    # # # #
    return jsonify({"user": return_user(user), "result": result})
Exemplo n.º 3
0
def test():
    requires = ["filename", "filetext"]  # #
    failed = bad_request(requires)
    if failed is not None:
        return jsonify(failed)

    filename = request.json['filename']  # # #
    text = request.json['filetext']
    row = request.json['row']
    col = request.json['col']
    file_obj = create_file(filename, text, row, col)
    print("filename: {}\nfiletext: {} file_obj: {}".format(
        filename, text, file_obj))
    result = nest.test_file(file_obj)
    # # # #
    return jsonify({"result": result})
Exemplo n.º 4
0
def edit():
    '''
        Edit a dropped script
    '''
    user = check_user()  #
    if type(user) == dict:
        return jsonify(user)

    requires = ["fileid", "filename", "filetext"]  # #
    failed = bad_request(requires)
    if failed is not None:
        return jsonify(failed)

    fileid = request.json.get("fileid")
    script = db.get("Script", fileid)  # # #
    if script is None:
        return jsonify({"msg": "script not found", "error": True})

    author = db.get("User", script.user_id)
    if author.id != user.id:
        return jsonify({"msg": "script is not yours", "error": True})

    filename = request.json.get("filename")
    text = request.json.get("filetext")
    row = script.row
    col = script.col

    file_obj = create_file(filename, text, row, col)
    result = nest.test_file(file_obj)

    script.filename = filename
    script.filetext = text
    script.material = result['material']
    result['script'] = return_script(user, script)

    user.touch()
    db.save()
    # # # #
    return jsonify({"result": result, "user": return_user(user)})
Exemplo n.º 5
0
def drop():
    ''' Test file and save it to database '''
    user = check_user()  #
    if type(user) == dict:
        return jsonify(user)

    requires = ["filename", "filetext", "row", "col"]  # #
    failed = bad_request(requires)
    if failed is not None:
        return jsonify(failed)

    if user.form == 'ghost':  # # #
        return jsonify({"msg": "you're a ghost", "error": True})

    filename = request.json['filename']
    text = request.json['filetext']
    row = request.json['row']
    col = request.json['col']

    user.material += 1
    file_obj = create_file(filename, text, row, col)
    result = nest.test_file(file_obj)
    material = result['material']
    script = db.create("Script",
                       user_id=user.id,
                       material=material,
                       filename=filename,
                       filetext=text,
                       filetype=file_obj['filetype'],
                       row=row,
                       col=col,
                       location=user.location)
    result['script'] = return_script(user, script)
    user.touch()
    db.save()
    # # # #
    return jsonify({"result": result, "user": return_user(user)})
Exemplo n.º 6
0
def heal_container(container):
    '''puts heart into container'''
    c_name = container.name

    filename = "heart"
    text = ""
    with open(filename, 'r') as fd:
        text = fd.read()
    row = 0
    col = 0

    file_obj = shell.create_file("1", filename, text, row, col)
    file_id = file_obj['fileid']
    file_name = file_obj['filename']
    file_type = file_obj['filetype']

    responding = check_container(c_name)
    if responding:
        shell.copy_file(c_name, file_id, file_name)
        has_heart = shell.extract_heart(c_name)
    else:
        has_heart = None

    return {"has_heart": has_heart}