コード例 #1
0
def handler_leave(update, context):

    text, text_to_all = '', ''
    user_id = update.message.from_user.id
    room_id = server.get_current_room(user_id)

    if room_id:

        game = server.select_game(room_id)

        if not game:

            server.delete_user_from_room(user_id)

            text += 'You have left room number ' + str(room_id) + '.\n'

            if server.check_room_empty(room_id):
                server.delete_room(room_id)

                text += 'The room was empty with your departure, so it has been deleted.\n'
            else:
                text_to_all += get_user_name(user_id) + ' left the room.\n'

            server.commit()

        else:
            text += 'A game is being played in this room! Someone must /end it before anyone can leave.\n'

    else:
        text += 'You are not in any room right now!\n'

    update.message.reply_text(text, reply_markup=ReplyKeyboardRemove())
    send_message_to_room(room_id, text_to_all)
コード例 #2
0
def handler_begin(update, context):

    text, text_to_all = '', ''
    user_id = update.message.from_user.id
    room_id = server.get_current_room(user_id)

    if room_id:
        users = server.select_users_info_in_room(room_id)

        game = server.select_game(room_id)
        if not game:
            text_to_all += get_user_name(user_id) + ' has begun the game'
        else:
            text_to_all += get_user_name(user_id) + ' has rebegun the game'

        game = uno.Game()

        configs = server.get_room_configs(room_id)
        apply_room_configs(configs, game)

        game.begin(len(users))

        server.update_game(room_id, game)

        numbers = list(range(len(users)))
        random.shuffle(numbers)

        for for_player_number, for_user_id in users:
            server.update_player_number(room_id, for_user_id, numbers.pop())

        server.commit()

        send_message_to_room(room_id, text_to_all)

        def get_user_status_text(user_id):
            settings = get_and_apply_user_settings(user_id)
            return get_status_text(room_id, user_id, show_room_info=False)

        send_message_to_room(room_id,
                             get_user_status_text,
                             parse_mode=ParseMode.HTML)

    else:
        update.message.reply_text(
            "You cannot begin the game if you are not in a room! Try /new or /join <room number>",
            reply_markup=ReplyKeyboardRemove())
コード例 #3
0
def handler_join(update, context):

    text, text_to_all = '', ''
    user_id = update.message.from_user.id
    room_id = None

    if len(context.args) > 0:
        room_id = string_to_positive_integer(context.args[0])

        if room_id != None:

            current_room_id = server.get_current_room(user_id)
            room_exists = server.check_room_exists(room_id)
            game = None

            if current_room_id:
                text += 'You are already in room ' + str(
                    current_room_id) + '! You must /leave that room first.\n'

            if not room_exists:
                text += 'This room does not exist!\n'
            else:
                game = server.select_game(room_id)

            if game:
                text += 'A game is being played in this room! They must /end it before anyone can join.\n'

            if not current_room_id and room_exists and not game:
                server.insert_user_to_room(room_id, user_id)
                server.commit()

                text += 'Joined room ' + str(room_id) + '.\n'
                text_to_all += get_user_name(user_id) + ' joined the room.\n'

        else:
            text += 'This can\'t possibly be a room! Come on!\n'

    else:
        text += 'You have not said the room you want to join! Try /join <room number>\n'

    update.message.reply_text(text, reply_markup=ReplyKeyboardRemove())
    send_message_to_room(room_id, text_to_all)
コード例 #4
0
def get_status_text(room_id,
                    user_id,
                    show_room_info=True,
                    show_your_turn=False):

    text = ''

    if room_id:
        users = server.select_users_info_in_room(room_id)
        game = server.select_game(room_id)

        configs = server.get_room_configs(room_id)
        apply_room_configs(configs, game)

        if show_room_info:
            num_users = len(users)
            text += ('You are currently in room number ' + str(room_id) +
                     ', which has ' + str(num_users) + ' ' +
                     plural(num_users, 'user', 'users') + '.\n')

        if show_your_turn:
            text += 'It is your turn.\n'

        if game:
            if game.direction == -1:
                users.reverse()

        for for_player_number, for_user_id in users:

            for_user_name = get_user_name(for_user_id)

            if game:
                num_cards = len(game.player_cards[for_player_number])
                text += (str(for_player_number) + ': ' + for_user_name + ' (' +
                         str(num_cards) + ' ' +
                         plural(num_cards, 'card', 'cards') + ')')

                if game.winner == None:
                    if game.current_player == for_player_number:
                        text += ' &lt;- Current'
                    elif game.get_next_player() == for_player_number:
                        text += ' &lt;- Next'
                elif game.winner == for_player_number:
                    text += ' &lt;- Winner'

            else:
                text += '- ' + for_user_name

            text += '\n'

        if game:

            text += 'Current card: ' + unoparser.card_string(
                game.current_card) + '\n'
            if game.current_color != game.current_card.color:
                text += 'Chosen color: ' + unoparser.card_color_string(
                    game.current_color) + '\n'

            player_number = next((for_player_number
                                  for for_player_number, for_user_id in users
                                  if for_user_id == user_id))

            text += 'Your cards: '

            if len(game.player_cards[player_number]) != 0:
                text += unoparser.play_intent_list_string(
                    game.get_play_intents_cards(player_number))
            else:
                text += 'None!'

            text += '\n'

    else:
        text += 'You are currently not joined in any room.\n'

    return text
コード例 #5
0
def handler_text_message(update, context):

    user_id = update.message.from_user.id
    room_id = server.get_current_room(user_id)

    message = update.message.text

    # Check if it is a chat message
    if (len(message) > 0 and message[0] == "."):
        command_chat(update, user_id, room_id, message[1:])
        return

    if room_id:

        game = server.select_game(room_id)
        player_number = server.select_player_number(room_id, user_id)

        if game:

            configs = server.get_room_configs(room_id)
            apply_room_configs(configs, game)

            # Check if someone has already won the game
            if game.winner != None:
                winner_user_id = server.select_user_id_from_player_number(
                    room_id, game.winner)
                update.message.reply_text(
                    get_user_name(winner_user_id) +
                    ' already won this game! You cannot play anymore. Try /begin',
                    reply_markup=ReplyKeyboardRemove())
                return

            # Check if is the current player
            if game.current_player != player_number:
                current_user_id = server.select_user_id_from_player_number(
                    room_id, game.current_player)
                update.message.reply_text(
                    'It is not your turn! The current player is ' +
                    get_user_name(current_user_id),
                    reply_markup=ReplyKeyboardRemove())
                return

            # Try to parse the user text
            try:
                play = unoparser.parse_play(message)

            except unoparser.InputParsingError as e:
                update.message.reply_text('That is not how you play! ' +
                                          str(e) + ' And try reading /help',
                                          reply_markup=ReplyKeyboardRemove())
                return

            bluffed_player = game.previous_player

            # Execute the play
            play_result = game.play(player_number, play)

            # If failed, send reason
            if not play_result.success:
                fail_reason = unoparser.fail_reason_string(
                    play_result.fail_reason)
                update.message.reply_text(fail_reason,
                                          reply_markup=ReplyKeyboardRemove())
                return

            if play_result.draw_pile_has_emptied:
                send_message_to_room(
                    room_id,
                    'The draw pile does not have enough cards, cards from the discard pile have been shuffled into the draw pile.',
                    reply_markup=ReplyKeyboardRemove())

            # Store game in database
            server.update_game(room_id, game)
            server.commit()

            # Send info messages

            current_user_id = server.select_user_id_from_player_number(
                room_id, game.current_player)

            user_name = get_user_name(user_id)

            if play_result.action == uno.ACTION_CALL_BLUFF:
                bluffed_user_id = server.select_user_id_from_player_number(
                    room_id, bluffed_player)
                bluffed_user_name = get_user_name(bluffed_user_id)
            else:
                bluffed_user_name = None

            # For all users in room...
            for room_user_id in server.select_users_ids_in_room(room_id):
                settings = get_and_apply_user_settings(room_user_id)

                # Send made play
                play_number_text = ''
                if settings.get('show_play_number', 'false') == 'true':
                    play_number_text = '#' + str(
                        game.current_play_number) + ': '

                play_result_text = play_number_text + unoparser.play_result_string(
                    play_result, user_name, bluffed_user_name)
                bot.send_message(room_user_id,
                                 play_result_text,
                                 reply_markup=ReplyKeyboardRemove())

                # Send if someone won
                if game.winner != None:
                    bot.send_message(room_user_id,
                                     user_name + ' won.',
                                     reply_markup=ReplyKeyboardRemove())
                    continue

                # Send status to current player
                if room_user_id == current_user_id:
                    text = get_status_text(room_id,
                                           room_user_id,
                                           show_your_turn=True,
                                           show_room_info=False)
                    bot.send_message(room_user_id,
                                     text,
                                     parse_mode=ParseMode.HTML,
                                     reply_markup=ReplyKeyboardRemove())

        else:
            update.message.reply_text('There is no game going on! Try /begin',
                                      reply_markup=ReplyKeyboardRemove())

    else:
        update.message.reply_text(
            'You cannot play if you are not in a room! Try /new or /join <room number>',
            reply_markup=ReplyKeyboardRemove())