Esempio n. 1
0
def end_game(data, id):
    del data[id]['roles']
    del data[id]['phase']
    del data[id]['button_presses']
    del data[id]['execution_choices']
    functions.send_text(data[id]['numbers'][0],
                        [GAME_OVER + id + " name_no_spaces'"])
Esempio n. 2
0
def start_game(game_data):
    n = len(game_data['numbers'])
    game_data['roles'] = functions.setupGameState(n)
    game_data['phase'] = 0
    game_data['button_presses'] = [None for _ in range(n)]
    game_data["execution_choices"] = [None for _ in range(n)]
    functions.send_text(game_data['numbers'], [LIE_DETECTOR_DESC] * n)
Esempio n. 3
0
def execution_vote(game_data, body, player_id):
    name_regex_results = re.split(r'\s+|[ ,;.-]\s*', body)
    name_ind = None
    for name_possibility in name_regex_results:
        for i in range(len(game_data['names'])):
            if game_data['names'][i] == name_possibility:
                name_ind = i
    if name_ind is None:
        return BAD_AGENT_NAME
    agent_win = functions.excecution(player_id, name_ind,
                                     game_data["execution_choices"],
                                     game_data["roles"], game_data['numbers'])
    if agent_win is not None:
        bad_ind = game_data["roles"].index(1)
        if agent_win:
            messages = [AGENT_WIN for _ in range(len(game_data['numbers']))]
            messages[bad_ind] = SPY_LOSE
            functions.send_text(game_data['numbers'], messages)
        else:
            messages = [AGENT_LOSE for _ in range(len(game_data['numbers']))]
            messages[bad_ind] = SPY_WIN
            functions.send_text(game_data['numbers'], messages)
        return "GAME_OVER"
    else:
        return "You voted for Agent " + game_data['names'][name_ind]
Esempio n. 4
0
def start_game(game_data):
    """
    >>> start_game({'numbers': ['#1', '#2', '#3']})
    asd
    """
    game_data["total_choices"] = {}

    n = len(game_data['numbers'])
    game_data['roles'] = functions.setupGameState(n)
    game_data['names'] = functions.nameGenerator(n)
    game_data['phase'] = 0
    game_data['button_presses'] = {}
    functions.send_text(game_data['numbers'], [
        "If you would like to take the lie detector test then HQ will analyze the results and send them "
        +
        "back to those who took the test. But, the results are aggregated among all people who took the test for "
        + "privacy reasons. Text 'Take' or 'Don't Take'."
    ] * n)
Esempio n. 5
0
def determine_response(data, from_number, body):
    """
    Determine how to respond to a given text message,
    or None for no response.
    """
    game_id = get_game_id(data, from_number)
    if game_id is None:
        return response_not_in_game(data, from_number, body)
    game_data = data[game_id]

    # Setting up a game
    if body == 'start mission':
        return mission_start(game_data)
    elif body == 'abort':
        if 'phase' in game_data:
            return INVALID_ABORT_TIME
        remove_from_game(game_data, from_number)
        if len(game_data['names']) == 0:
            del data[game_id]
        return ABORT_MSG
    elif 'phase' not in game_data:
        return INVALID_COMMAND_PRE_MISSION

    phase = game_data['phase']
    player_id = game_data["numbers"].index(from_number)

    # Lie detector
    if phase == 0:
        return lie_detector(game_data, body, player_id)

    # Espionage
    if phase == 1 and body == 'next phase' and from_number == game_data[
            'numbers'][0]:
        functions.espionage(game_data['numbers'], game_data['roles'])
        game_data['phase'] = 2
        return ESPIONAGE_END + str(game_data['names'])

    # Mission
    if phase == 2 and from_number == game_data['numbers'][0]:
        return obtain_mission_list(game_data, body)

    if phase == 2.5 and from_number == game_data['numbers'][0]:
        if "y" in body:
            game_data['phase'] = 3
            functions.emergency_mission(game_data['roles'],
                                        game_data['mission_list'],
                                        game_data['numbers'])
            message = START_EXECUTION
            functions.send_text(game_data["numbers"],
                                [message] * len(game_data["numbers"]))
            return None
        else:
            game_data['phase'] = 2
            return MISSION_RE_ENTER

    # Execution
    if phase == 3:
        response_str = execution_vote(game_data, body, player_id)
        if response_str == "GAME_OVER":
            end_game(data, game_id)
        else:
            return response_str
    return None
Esempio n. 6
0
def determine_response(data, from_number, body):
    """
    Determine how to respond to a given text message,
    or None for no response.
    >>> determine_response({'123':{'numbers': ['#1', '#2', '#3']}}, '#1', 'start mission')
    "You are already enlisted in mission id1. Wait for the person who started the mission to text 'Start mission' or exit by texting 'Abort'"
    """
    game_id = get_game_id(data, from_number)
    if game_id is None:
        print(data)
        # Not currently in a game
        if body == "begin enlisting":
            game_id = str(random.randint(0, 100000))
            data[game_id] = {'numbers': [from_number]}
            return "Began Enlisting for " + game_id + ". Tell others to join by texting 'enlist me " \
                   + game_id + "' to this number without quotes. Start the mission by texting 'Start Mission'"
        elif ' '.join(body.split(" ")[:2]) == "enlist me":
            game_id = ''.join(body.split(" ")[2:])
            print(data)
            if game_id in data:
                add_to_game(data[game_id], from_number)
                return "Successfully joined mission " + game_id
            else:
                return "This game id was not found."
        else:
            return NO_GAME_MESSAGE
    # Setting up a game
    if ' '.join(
            body.split(" ")[:2]) == "enlist me" or body == 'begin enlisting':
        return "You are already enlisted in mission " + game_id + ". " + IN_MISSION
    elif body == 'start mission':
        start_game(data[game_id])
    elif body == 'abort':
        remove_from_game(data[game_id], from_number)
        return "You have left the mission."

    game_data = data[game_id]
    phase = game_data['phase']

    if phase == 0:
        if body == 'take' or body == "don't take":
            done = functions.button(game_data['button_presses'],
                                    game_data['numbers'], from_number, body,
                                    game_data['roles'])
            if done:
                game_data['phase'] = 1
                return None
            else:
                return "Submitted"
        else:
            functions.send_text()
            return "Please type in 'take' or 'don't take'."
    if phase == 1 and body == 'next phase':
        functions.espionage(game_data['numbers'], game_data['roles'])
        game_data['phase'] = 2
        return None

    player_id = game_data["numbers"].index(from_number)
    if phase == 3:
        message = "Now beginning the excecution, submit your vote by Agent Name"
        functions.send_text(game_data["numbers"],
                            [message] * len(game_data["numbers"]))
        game_data['phase'] = 3.5
        return None
    if phase == 3.5:
        role = game_data['roles'][player_id]
        choice = body  # expects a name
        results, revote = functions.excecution(role, choice,
                                               game_data["total_choices"],
                                               game_data["names"],
                                               game_data["roles"])
        if results is not None:
            while revote:
                if revote:
                    message = "There is too much disagreement, try voting again"
                    functions.send_text(game_data["numbers"],
                                        len(game_data["numbers"] * [message]))
                    results, revote = functions.excecution(
                        role, choice, game_data["total_choices"],
                        game_data["names"], game_data["roles"])
                    continue
                ind = game_data["roles"].index(1)
                bad_number = game_data["numbers"][ind]
                good_numbers = [
                    num for num, num_ind in zip(num_ind, game_data["numbers"],
                                                range(game_data["numbers"]))
                    if num_ind != ind
                ]

                if results:
                    message = "Sorry Comrad, You've been busted"
                    functions.send_text(bad_number, message)

                    message = "Congrats Agents, You caught 'em"
                    functions.send_text(
                        good_numbers,
                        [message for m in range(len(good_numbers))])
                else:
                    message = "Congrats Comrad, You know too much"
                    functions.send_text(bad_number, message)

                    message = "Agents Nooo, The sleeper has gotten away"
                    functions.send_text(
                        good_numbers,
                        [message for m in range(len(good_numbers))])
                phase += 1
                results, revote = functions.excecution(
                    role, choice, game_data["total_choices"],
                    game_data["names"], game_data["roles"])
        end_game()

    # phase 3: mission
    if phase == 2 and from_number == game_data['numbers'][0]:
        # get the mission list
        mission_list = body
        mission_list = mission_list.replace(",", "")
        mission_list = mission_list.replace(";", "")
        mission_list = mission_list.replace("/", "")
        mission_list = mission_list.split()
        game_data['mission_list'] = mission_list
        # TODO be more flexible with inputs
        mission_names = ' '.join([str(elem) for elem in mission_list])
        functions.send_text(
            game_data["numbers"],
            ["Possible Agents:" for i in range(len(game_data["numbers"]))])
        functions.send_text(
            game_data["numbers"],
            [game_data["names"] for i in range(len(game_data["numbers"]))])
        game_data['phase'] = 2.25
        return "Is this the mission you'd like: " + mission_names + "? Respond (Y/N)"

    if phase == 2.25 and from_number == game_data['numbers'][0]:
        functions.send_text(
            game_data["numbers"],
            [game_data["names"] for i in range(len(game_data["numbers"]))])
        if "y" in body.lower():
            functions.emergency_mission(game_data['roles'],
                                        game_data['mission_list'],
                                        game_data['names'])
            game_data['phase'] = 3
            functions.emergency_mission(game_data['roles'],
                                        game_data['mission_list'],
                                        game_data['names'])
        if "n" in body.lower():
            game_data['phase'] = 2
            return "Please try again, send a list of the names you'd like to go on the mission"

    return None