def hangman_start(channel_id):
    """ Given channel_id, start a game of hangman. """

    # If hangman game already active in this channel, raise InputError.
    for game in HANGMANS:
        if game['channel_id'] == channel_id:
            raise error.InputError(description="""There is already a game of hangman active
                                                  in this channel""")

    # Bot is called
    bot_id = hangmanbot.call_bot(channel_id)

    # Bot sends a message notifying channel that hangman game has started
    msg = "A game of hangman has been started in this channel"
    message_parser.parse_message(bot_id['bot_id'], channel_id, msg)

    # Generate a word that should be used
    words = []
    with open("/usr/share/dict/words") as wordfile:
        words = wordfile.readlines()
    word = random.choice(words).strip()

    # Add the hangman game to the list HANGMANS
    new_game = {"channel_id": channel_id,
                "word": word,
                "state": word,
                "num_guesses": 0,
                "guesses": []}
    HANGMANS.append(new_game)
Beispiel #2
0
def message_send(token, channel_id, message):
    """
    Grab information from the frontend chat bar and send it.
    """

    u_id = get_current_user(token)

    if is_user_channel_member(channel_id, u_id) is False:
        raise AccessError(description='The authorised user has not ' +
                          'joined the channel they are trying to post to')

    # raise error if the message is longer than 1000 or empty string is given.
    if len(message) > 1000:
        raise InputError(description='Message is more than 1000 characters')

    if message == '':
        raise InputError(description='Cannot send an empty message')

    # Now parse the message
    message_id = message_parser.parse_message(u_id, channel_id, message)
    check_command(channel_id, message)

    return {
        "message_id": message_id,
    }
Beispiel #3
0
    def testmessage_parser(self):
        cmd, payload = message_parser.parse_message('update game round 1')
        self.assertEqual(cmd, 'update')
        self.assertEqual(payload, ('round', 1, 'game'))

        cmd, payload = message_parser.parse_message('update game field .,0,1')
        self.assertEqual(payload, ('field', [0, 1, 2], 'game'))

        cmd, payload = message_parser.parse_message(
            'update alex living_cells 2')
        self.assertEqual(payload, ('living_cells', 2, 'alex'))

        cmd, payload = message_parser.parse_message('update alex move 2,3')
        self.assertEqual(payload, ('move', '2,3', 'alex'))

        cmd, payload = message_parser.parse_message('settings field_width 20')
        self.assertEqual(payload, ('field_width', 20))
def hangman_print_state(game):
    """ Print the state of the given hangman game.
    Bot is called in this function. """

    bot_id = hangmanbot.call_bot(game['channel_id'])

    # If game won
    if game['state'] == '':
        msg = f"Congratultions! You correctly guessed that the word was {game['word']}"
        message_parser.parse_message(bot_id['bot_id'], game['channel_id'], msg)
        hangman_end(game)

    # If game lost
    elif game['num_guesses'] == 10:
        msg = f"Game Lost! The correct word was {game['word']}"
        message_parser.parse_message(bot_id['bot_id'], game['channel_id'], msg)
        hangman_end(game)

    # If game is to continue
    # Print the appropriate details of the game
    else:
        state = ''
        for char in game['word']:
            if char in game['guesses']:
                state += char
            else:
                state += '_'
        state += '\n'

        num_guesses = f"You have {9 - game['num_guesses']} incorrect guesses left\n"
        guesses = "Guessed Characters: " + str(game['guesses'])

        msg = state + num_guesses + guesses
        message_parser.parse_message(bot_id['bot_id'], game['channel_id'], msg)
Beispiel #5
0
    def readLine(self, line: str) -> T.Tuple[str, T.Any]:
        # Parse incoming message
        cmd, payload = message_parser.parse_message(line)

        if cmd is None:
            cmd = ''

        if cmd == 'settings':
            key, value = payload
            self.settings[key] = value

        elif cmd == 'update':
            # Update state with game state
            self.state = self.update_state(self.state, cmd, payload)

        elif cmd == 'action':
            # When an action is called, set the bot id to your ID
            self.state = self.state.using(
                activePlayer=self.settings['your_botid'] + 1)

        return cmd, payload