Example #1
0
def invites_get(request):
    if 'player_id' in request.GET:
        param_player_id = request.GET['player_id']
        connection_params = "?player_id=" + param_player_id
        response = connection_service("/invites/", None, connection_params,
                                      "GET")
        return Response(data=json.loads(response.content),
                        status=response.status_code)
    else:
        response = connection_service("/invites/", None, None, "GET")
        return Response(data=json.loads(response.content),
                        status=response.status_code)
Example #2
0
def create_game(invite_data):
    invite = invite_data['invite']
    sender_player_id = invite['sender_player_id']
    receiver_player_id = invite['receiver_player_id']
    player1 = {}
    player2 = {}
    player1['game_player'] = {
        "player_id": sender_player_id,
        "game_progress": 0,
        "score": 0
    }
    player2['game_player'] = {
        "player_id": receiver_player_id,
        "game_progress": 0,
        "score": 0
    }

    player1['game_round'] = []
    player2['game_round'] = []

    # Get the invite to get a hold of
    # match_name: string
    # question_duration: double

    player_status = []
    questions_list = []
    player_status.append(player1)
    player_status.append(player2)

    try:
        questions_list = get_questions_list()
        game_object = {
            "game": {
                "match_name": invite['match_name'],
                "question_duration": invite['question_duration'],
                "questions": questions_list,
                "player_status": player_status
            }
        }
        connection_service("/games/", game_object, None, "POST")
        return game_object
    except ConnectionError as e:
        error_message = generate_error_json(status.HTTP_404_NOT_FOUND,
                                            "No database connection found",
                                            None, None)
        response = JsonResponse(data=error_message,
                                status=status.HTTP_404_NOT_FOUND)
        return response
def players_post(request):
    decode_error_message = generate_error_json(status.HTTP_400_BAD_REQUEST, 'Json decode error',
                                               'Your body should probably look like the value in correct_data',
                                               CORRECT_PLAYER_OBJ)

    json_request = get_json_data_object(request, decode_error_message)
    if type(json_request) is Response:
        return json_request

    try:
        player_req = json_request['player']
        json_body = {
            "player": {
                "username": player_req['username'],
                "email": player_req['email'],
                "first_name": player_req['first_name'],
                "last_name": player_req['last_name'],
                "study_programme": player_req['study_programme'],
                "high_score": player_req['high_score']
            }
        }
        response = connection_service("/players/", json_body, None, "POST")
        return Response(data=json.loads(response.content), status=response.status_code)
    except KeyError:
        return Response(data=decode_error_message, status=status.HTTP_400_BAD_REQUEST)
Example #4
0
def get_questions_list():
    question_list = []
    used_question_idx = []

    response = connection_service("/questions/", None, None, "GET")

    response_content = json.loads(response.content.decode("utf-8"))

    questions = response_content['questions']

    number_of_questions = len(questions)

    # If there are not more than 18 questions in the db
    if number_of_questions > 18:
        question_range = 18
    else:
        question_range = number_of_questions

    for counter in range(question_range):
        while True:
            rnd = randint(0, number_of_questions - 1)
            if rnd not in used_question_idx:
                used_question_idx.append(rnd)
                break
        question_list.append(questions[rnd]['id'])
    return question_list
Example #5
0
def invites_post(request):
    decode_error_message = generate_error_json(
        status.HTTP_400_BAD_REQUEST, 'Json decode error',
        'Your body should probably look like the value in correct_data',
        CORRECT_INVITE_OBJ)

    json_request = get_json_data_object(request, decode_error_message)
    if type(json_request) is Response:
        return json_request

    invite = json_request['invite']
    # if invite['accepted']==

    json_body = {
        "invite": {
            "sender_player_id": invite['sender_player_id'],
            "receiver_player_id": invite['receiver_player_id'],
            "match_name": invite['match_name'],
            "question_duration": invite['question_duration'],
            "accepted": invite['accepted']
        }
    }
    response = connection_service("/invites/", json_body, None, "POST")
    return Response(data=json.loads(response.content),
                    status=response.status_code)
Example #6
0
def single_invite_put(request, invite_id):
    decode_error_message = generate_error_json(
        status.HTTP_400_BAD_REQUEST, 'Json decode error',
        'Your body should probably look like the value in correct_data',
        CORRECT_INVITE_OBJ)

    json_request = get_json_data_object(request, decode_error_message)
    # If the type of the return from get_json_data_object is a Response,
    # and not a json, there was an error
    if type(json_request) is Response:
        return json_request

    response = connection_service(f"/invites/{invite_id}/", json_request, None,
                                  "PUT")
    if type(response) != JsonResponse:
        response = create_game(json_request)
        return Response(data=response, status=status.HTTP_202_ACCEPTED)
    elif type(response) == JsonResponse:
        return response
def players_get(request):
    response = connection_service("/players/", None, None, "GET")
    return Response(data=json.loads(response.content), status=response.status_code)
def single_player_delete(request, player_id):
    response = connection_service(f"/players/{player_id}/", None, None, "DELETE")
    return Response(data=json.loads(response.content), status=response.status_code)
Example #9
0
def single_invite_delete(request, invite_id):
    response = connection_service(f"/invites/{invite_id}/", None, None,
                                  "DELETE")
    return Response(data=json.loads(response.content),
                    status=response.status_code)