Example #1
0
def skip_turn(roomId):
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'PATCH':
        userId = request.cookies.get('userId')
        is_room_master = check_room_master(database, roomId, userId)
        if not is_room_master:
            return build_actual_response({"message": "Not room master"}, 400)
        valid_phase_shift = phase_shift(database, roomId)
        if not valid_phase_shift:
            return build_actual_response({"message": "Not valid phase shift"},
                                         400)
        return build_actual_response({"message": "Turn skipped"}, 200)
Example #2
0
def end_vote_phase(roomId):
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'PATCH':
        userId = request.cookies.get('userId')
        is_room_master = check_room_master(database, roomId, userId)
        if not is_room_master:
            return build_actual_response({"message": "Not room master"}, 400)

        valid_vote_result = end_votes(database, roomId)
        if not valid_vote_result:
            return build_actual_response({"message": "Not enough votes"}, 200)
        return build_actual_response({"message": "Valid execution"}, 200)
Example #3
0
def night_start(roomId):
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'PATCH':
        userId = request.cookies.get('userId')
        is_room_master = check_room_master(database, roomId, userId)
        if not is_room_master:
            return build_actual_response({"message": "Not room master"}, 400)
        voting_ended = night_start_write(database, roomId)
        if not voting_ended:
            return build_actual_response({"message": "Voting still ongoing"},
                                         400)
        return build_actual_response({"message": "Night started"}, 200)
Example #4
0
def detective_actions(roomId):
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'PATCH':
        userId = request.cookies.get('userId')
        is_detective, name = check_detective(database, roomId, userId)
        if not is_detective:
            return build_actual_response({"message": "Not detective"}, 400)
        is_valid_target = detect_action(database, roomId, name,
                                        request.form.get('targetId'))
        if not is_valid_target:
            return build_actual_response({"message": "Not valid target"}, 400)
        return build_actual_response({"message": "Target confirmed"}, 200)
Example #5
0
def game_start(roomId):
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'PATCH':
        userId = request.cookies.get('userId')
        is_room_master = check_room_master(database, roomId, userId)
        if not is_room_master:
            return build_actual_response({"message": "Not room master"}, 400)
        able_to_start = game_start_write(database, roomId)
        if not able_to_start:
            return build_actual_response({"message": "Not enough players"},
                                         400)
        return build_actual_response({"message": "Player roles shuffled"}, 200)
Example #6
0
def hang_action(roomId):
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'PATCH':
        userId = request.cookies.get('userId')
        is_room_master = check_room_master(database, roomId, userId)
        if is_room_master:
            return build_actual_response(
                {"message": "Room master cannot vote"}, 400)
        is_valid_target = vote(database, roomId, userId,
                               request.form.get('targetId'))
        if not is_valid_target:
            return build_actual_response(
                {"message": "Not valid target or already voted"}, 400)
        return build_actual_response({"message": "Vote Added"}, 200)
Example #7
0
def get_room_json():
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    check_new_ip(
        datetime.now(tz_TO).strftime('%Y-%m-%d %H:%M:%S'), visiting_ips,
        request.access_route[0])
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'GET':
        roomId = request.args.get('roomId')
        roomFound = False
        for i in database:
            if i.id == roomId:
                roomFound = True
                i.polling = True
                return build_actual_response(
                    json.dumps(i, indent=4, cls=RoomEncoder), 200)
        if roomId is None or roomFound is False:
            return build_actual_response({"message": "Not Found"}, 404)
Example #8
0
def join_room():
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'POST':
        userId = request.cookies.get('userId')
        roomId = request.form.get('roomId')
        join_option = request.form.get('option')
        print(join_option)
        if join_option == 'observe':
            return build_actual_response({"message": "Observer connection"},
                                         200,
                                         setCookie=True,
                                         cookie='observer')

        isValidRoom = False
        room = None
        new_player = Player(request.form.get('name'),
                            uuid.uuid4().hex, 'unassigned', 'alive', False)

        for i in database:
            if i.id == roomId:
                isValidRoom = True
                room = i

        if isValidRoom:
            if room.status != 'pre-game':
                return build_actual_response(
                    {"message": "Observer connection"}, 200)
            for i in room.players:
                if i.userId == userId:
                    return build_actual_response(
                        {"message": "Player reconnected"}, 200)
            room.players.append(new_player)
        else:
            return build_actual_response({"message": "Not Found"}, 404)
        return build_actual_response({"message": "Player created"},
                                     201,
                                     setCookie=True,
                                     cookie=new_player.userId)
Example #9
0
def create_room():
    LOG.info(request.access_route[0] + ' requested ' + request.url)
    global database
    if request.method == 'OPTIONS':
        return build_preflight_response()
    elif request.method == 'POST':
        database, new_room = write_new_room(database,
                                            request.form.get('numMafia'))
        return build_actual_response(
            {
                "message": "Room created",
                "roomId": new_room.id
            },
            201,
            setCookie=True,
            cookie=new_room.roomMaster)