예제 #1
0
def loggout():
    post_data = request.get_json()
    if not post_data:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid format, check your input and try again"
        })
    
    if not 'token' in post_data :
        return jsonify({"""
            "result" : "error",
            "comment" : "Please follow the current template {"token" : "*************"}"
            """
        })
    
    user_id = db.get_user_id_by_token(post_data['token'])
    if not user_id:
        return jsonify( {
            "result" : "error",
            "comment" : "Invalid token, try again"
        })
    
    db.delete_token(user_id)
    return jsonify({
        "result" : "ok",
        "comment": "You are successfully logged out"
    })    
예제 #2
0
def join_battle():
    post_data = request.get_json()
    if not post_data:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid format, check your input and try again"
        })      
    
    if not 'token' in post_data:
        return jsonify({"""
            "result" : "error",
            "comment" : "Please follow the current template {"token" : "*****"}"
            """
        })

    user_id = db.get_user_id_by_token(post_data['token'])
    if not user_id:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid token"
        })

    user_enemy_name = db.find_enemy(user_id)
    if not user_enemy_name:
        return jsonify({
            "result" : "error",
            "comment" : "Cannot find battle partner"
        })
    
    return jsonify({
        "result" : "ok",
        "comment" : f"Your enemy is: {user_enemy_name}" 
    })
예제 #3
0
def battle_status():
    post_data = request.get_json()
    if not post_data:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid format, check your input and try again"
        })      
    
    if not 'token' in post_data:
        return jsonify({"""
            "result" : "error",
            "comment" : "Please follow the current template {"token" : "*****"}"
            """
        })

    user_id = db.get_user_id_by_token(post_data['token'])
    if not user_id:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid token"
        })

    user_enemy_name = db.get_user_enemy(user_id)
    if not user_enemy_name:
        return jsonify({
            "result" : "ok",
            "status" : "You are not in the battle"
        })

    return jsonify({
        "result" : "ok",
        "status" : f"You are fighting against {user_enemy_name}" 
    })
예제 #4
0
def statistics():
    post_data = request.get_json()
    if not post_data:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid format, check your input and try again"
        })
    
    if not 'user_name' in post_data or not 'token':
        return jsonify({"""
            "result" : "error",
            "comment" : "Please follow the current template {"user_name" : "John",
            "token" : "*****"}"
            """
        })

    if not db.get_user_id_by_token(post_data['token']):
        return jsonify({
            "result" : "error",
            "comment" : "Invalid token"
        })
    
    user_id = db.get_user_id_by_name(post_data['user_name'])
    if not user_id:
        return jsonify({
            "result" : "error",
            "comment" : "Cannot check statistics, user does not exists"
        })
    

    return jsonify({
        "result" : "ok",
        "statistics" : db.get_user_stats(user_id).to_dict()
    })
예제 #5
0
def battle_action():
    post_data = request.get_json()
    if not post_data:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid format, check your input and try again"
        })      
    
    if 'token' not in post_data or 'action' not in post_data:
        return jsonify({
            "result" : "error",
            "comment" : "Please follow the current template {'token' : '*****', 'action' : 'your_action'}"
            
        })
    
    user_id = db.get_user_id_by_token(post_data['token'])
    if not user_id:
        return jsonify({
            "result" : "error",
            "comment" : "Invalid token"
        })

    user_enemy_name = db.get_user_enemy(user_id)
    if not user_enemy_name:
        return jsonify({
            "result" : "error",
            "comment" : "You are not in the battle"
        })
    current_turn_user_id = db.get_current_turn_user_id(user_id)
    if current_turn_user_id != user_id:
        return jsonify({
            "result": "error",
            "comment": "It is not your turn"
        })

    action = post_data['action']
    if action not in ('up', 'down', 'left', 'right'):
        return jsonify({
            "result": "error",
            "comment": "Invalid action. Available actions: 'up', 'down', 'left', 'right'"
        })
  
    new_position, battle_won = db.make_action(user_id, action)
    if battle_won:
        comment = 'You won the battle!'
    else:
        commet = f'Action made successfuly. Your new position is [{new_position.x}, {new_position.y}]'
    return jsonify({
        "result": "ok",
        "comment": comment,
    })