Ejemplo n.º 1
0
def start(u_id, channel_id, message, message_id):
    """
    Checks if no standups are in progess and initiates hangman
    """
    # Check can start
    validation.check_can_start_hangman(channel_id)

    msg = message['message']
    buffer = len('/hangman start ')
    word = msg[buffer:].lower()
    #word = ''.join(msg.split(' ')[2:]).lower()

    # Check valid word
    validation.check_valid_word(word)

    # Updates hangman info with that word
    info = data.get_hangman_info(channel_id)
    update_info(info, u_id, message_id, word)

    # Prints hangman starting message
    message['message'] = print_start(info)
    data.add_message(message, channel_id)

    message_info = data.get_message(channel_id, message_id)
    message_info['is_pinned'] = True
    return {'message_id': message_id}
Ejemplo n.º 2
0
def stop(u_id, channel_id):
    """
    Stops current game of hangman
    """
    info = data.get_hangman_info(channel_id)

    # Check if hangman is currently active
    validation.check_active_hangman(channel_id)

    # Check if user is owner of channel or status message
    validation.check_stop_permission(u_id, channel_id)

    data.remove_message(info['status_message'], channel_id)
    info['is_active'] = False
Ejemplo n.º 3
0
def check_stop_permission(u_id, channel_id):
    """
    Checks if user has permission to use '/hangman stop'

    Parameters:
        u_id(int): Identifier used for users
        channel_id(int): Identifier used for channels

    Returns:
        Nothing if user has permission
        Raises InputError if user does not have permission
    """
    channel = data.get_channel_info(channel_id)
    hang_info = data.get_hangman_info(channel_id)
    if u_id not in channel['owners'] and hang_info['u_id'] != u_id: # pragma: no cover
        raise InputError(description="User does not have permission to use command")
Ejemplo n.º 4
0
def check_if_hangman(channel_id, message):
    """
    Checks if channel is in 'hangman' mode
    and if message is a guess

    Parameters:
        channel_id(int): The id of channel

    Returns:
        True if guess is valid
        False if message does not start with /guess
        InputError if message is a guess, but hangamn is not active
    """
    hang_info = data.get_hangman_info(channel_id)
    if not hang_info['is_active'] and message.startswith('/guess '):
        raise InputError(description="Hangman is not active")
    return message.startswith('/guess ')
Ejemplo n.º 5
0
def check_not_status_message(message_id):
    """
    Checks to make sure that the targeted message isn't the pinned 
    hangman message with the game state
    Parameters:
        message_id(int): unique identifier of message
    returns:
        error if status message is pinned message
        otherwise nothing
    """
    channel_id = data.get_channel_from_message(message_id)
    # Message does not exist
    if channel_id is None:
        return None
    hang_info = data.get_hangman_info(channel_id)
    if hang_info['status_message'] == message_id:
        raise InputError(description="Can't edit/delete hangman status message")
Ejemplo n.º 6
0
def check_start_hangman(channel_id, message):
    """
    Checks whether message will/should start a hangman session

    Parameters:
        channel_id(int): The id of channel
        message(str): Contents of the message about to be sent

    Returns:
        True if hangman is not active and message will start it
        False if message is not '/hangman start'
        Raise InputError if message will start hangman, but hangman is already active
    """
    if message.startswith('/hangman start'):
        hangman_info = data.get_hangman_info(channel_id)
        if hangman_info['is_active']:
            raise InputError(description='A hangman session is already active')
        return True
    return False
Ejemplo n.º 7
0
def guess(u_id, channel_id, message):
    """
    Takes in a guess, if correct, execute correct guess, if not, execute incorrect guess
    """
    hang_info = data.get_hangman_info(channel_id)

    # Check to make sure user didn't start hangman session
    validation.check_guesser_not_creator(u_id, channel_id)

    # Check if valid guess
    validation.check_valid_guess(message)

    # Check if guess is correct, incorrect or repeated
    letter = get_guess(message)

    if letter in hang_info['revealed_word']:
        raise InputError(description='Cannot guess already revealed letters')
    hang_info['guesses'] += 1
    if letter in hang_info['word']:
        execute_correct_guess(hang_info, letter)
    else:
        execute_incorrect_guess(hang_info, letter)
    print_updated_status(hang_info)