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

    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 game:
            server.update_game(room_id, None)
            server.commit()

            send_message_to_room(
                room_id,
                get_user_name(user_id) + ' has ended the game')

        else:
            update.message.reply_text("But there is no game going on!",
                                      reply_markup=ReplyKeyboardRemove())

    else:
        update.message.reply_text(
            "You cannot end the game if you are not in a room! Try /new or /join <room number>",
            reply_markup=ReplyKeyboardRemove())
コード例 #2
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)
コード例 #3
0
def handler_chat(update, context):

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

    message = update.message.text[len('/chat '):]

    command_chat(update, user_id, room_id, message)
コード例 #4
0
def handler_status(update, context):

    user_id = update.message.from_user.id

    settings = get_and_apply_user_settings(user_id)
    text = get_status_text(server.get_current_room(user_id), user_id)

    bot.send_message(user_id,
                     text,
                     parse_mode=ParseMode.HTML,
                     reply_markup=ReplyKeyboardRemove())
コード例 #5
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())
コード例 #6
0
def handler_new(update, context):

    text = ''
    user_id = update.message.from_user.id
    current_room_id = server.get_current_room(user_id)

    if current_room_id == None:
        room_id = server.insert_room()
        server.insert_user_to_room(room_id, user_id)
        server.commit()

        text += 'Created and joined room ' + str(room_id) + '.\n'

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

    update.message.reply_text(text, reply_markup=ReplyKeyboardRemove())
コード例 #7
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)
コード例 #8
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())
コード例 #9
0
def handler_configs(update, context):

    text, text_to_all = '', ''
    reply_markup = ReplyKeyboardRemove()

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

    if room_id:

        configs = server.get_room_configs(room_id)

        if len(context.args) == 0:

            text += 'Current configurations of room ' + str(room_id) + ':\n'

            for config in server.all_configs:
                default = server.all_configs[config][0]
                text += config + ': ' + str(configs.get(config,
                                                        default)) + '\n'

            reply_markup = ReplyKeyboardMarkup(
                [[("/configs " + x)] for x in list(server.all_configs.keys())],
                input_field_placeholder="Choose a configuration...",
                one_time_keyboard=True)

        elif len(context.args) == 1:

            config = context.args[0].lower()

            if config in server.all_configs:
                default = server.all_configs[config][0]
                text += config + ': ' + str(configs.get(config,
                                                        default)) + '\n'
                text += 'Possible values: ' + ", ".join(
                    server.all_configs[config]) + '\n'
            else:
                text += 'This configuration does not exist!\n'

        elif len(context.args) >= 2:

            config = context.args[0].lower()
            value = context.args[1].lower()

            if config in server.all_configs:
                if value in server.all_configs[config]:

                    server.update_room_config(room_id, config, value)
                    server.commit()

                    send_message_to_room(
                        room_id,
                        get_user_name(user_id) + ' set room configuration ' +
                        config + ' to ' + value + '\n')

                    return

                else:
                    text += 'This value is not allowed for this configuration!\n'
            else:
                text += 'This configuration does not exist!\n'

    else:
        text += 'You cannot change room configuration if you are not in a room!\n'

    if text:
        update.message.reply_text(text, reply_markup=reply_markup)