Пример #1
0
def game_message(message):

    game_control().delete(message.from_user.id)
    game_control().create(message.from_user.id)


    logging.info(message.from_user.first_name+' started the game.')

    info = 'Welcome to the world of Laindor!\nTo exit the game type /quit or /exit.\nTo see your characters status type /status.'+\
    '\nTo see game ratings type /rating_game'
    bot.send_message(message.chat.id, info)
    game_main.create_character(bot, message, True)
Пример #2
0
def show_status(bot, message):
    game_user = game_control()
    game_user.load(message.from_user.id)

    if message.from_user.id == game_user.user_id and game_user.character.name != "____no_name____":
        character_text = 'Name: '+game_user.character.name+\
        '\nRace: '+game_user.character.race+'\nClass: '+game_user.character.role+'\nStrength: '+str(game_user.character.strength)+\
        '\nIntellect: '+str(game_user.character.intellect)+'\nEndurance: '+str(game_user.character.endurance)+\
        '\nHealth: '+str(game_user.character.hp)+'\nScore: '+str(game_user.character.score)+'\nGold: '+str(game_user.character.inventory.gold)

        damage_type = str(Damage(
            game_user.character.damage_type)).split('.')[1].lower()
        character_text += '\nDamage: {0}-{1} {2}'.format(
            game_user.character.damage_min, game_user.character.damage_max,
            damage_type)

        character_text += '\nProtection: '
        for i in range(len(game_user.character.protection)):
            if game_user.character.protection[i] != 0:
                damage_type = str(Damage(
                    game_user.character.damage_type)).split('.')[1].lower()
                character_text += '{0}% {1}'.format(
                    int(game_user.character.protection[i] * 100), damage_type)

        bot.send_message(message.chat.id, character_text)
        return 0
    else:
        bot.send_message(message.chat.id, 'First create your character')
        return 0
Пример #3
0
def close_shop(bot, call):
    game_user = game_control()
    game_user.load(call.from_user.id)

    conn = sqlite3.connect('game_folder/shop.bd')
    c = conn.cursor()
    c.execute('DELETE FROM items WHERE user_id = ?', [call.from_user.id])
    conn.commit()
    conn.close()

    bot.edit_message_text(chat_id=game_user.user_id,
                          message_id=call.message.message_id,
                          text='See you later, traveler!')
    game_user.in_shop = False
    game_user.save(game_user.user_id)
    return 0
Пример #4
0
def sell_menu(bot, call):
    game_user = game_control()
    game_user.load(call.from_user.id)
    #game_user.character.load_from_repository(game_user.user_id, folder='game_folder/')

    if 'Back' in call.data.split('->'):
        text, keyboard = main_menu(game_user)
        bot.edit_message_text(chat_id=game_user.user_id,
                              message_id=call.message.message_id,
                              text=text,
                              reply_markup=keyboard,
                              parse_mode='Markdown')
        return 0

    backpack = game_user.character.show_backpack()
    prices = calculate_buying_price(game_user.character.show_backpack())
    if call.data != 'sell' and call.data.split('->')[1] in backpack:
        sell = call.data.split('->')[1]
        for i in range(len(backpack)):
            if sell == backpack[i]:
                game_user.character.inventory.gold += prices[i]

                item = sell.split(',')[0]
                if item[0] == '(':
                    item = item.split(')')[1]
                    item = item[1:]

                for j in range(len(game_user.character.backpack)):
                    if item == game_user.character.backpack[j].description:
                        game_user.character.backpack.pop(j)
                        break
                break
        else:
            raise
        game_user.save(call.from_user.id)
        bot.send_message(game_user.user_id,
                         'You recieved *{0} gold*'.format(prices[i]),
                         parse_mode='Markdown')

    backpack = game_user.character.show_backpack()
    prices = calculate_buying_price(game_user.character.show_backpack())
    backpack += ['Back']

    backpack_text = [' '] * len(backpack)

    keyboard = telebot.types.InlineKeyboardMarkup(True)
    for i in range(len(backpack)):
        if i != (len(backpack) - 1):
            backpack_text[i] = backpack[i] + ' ({0} gold)'.format(prices[i])
        else:
            backpack_text[i] = backpack[i]
        button = telebot.types.InlineKeyboardButton(backpack_text[i],
                                                    callback_data='sell->' +
                                                    backpack[i])
        keyboard.row(button)
    text = 'Have you found anything interesting in your adventures?\nYou have *{0} gold*'.format(
        game_user.character.inventory.gold)
    bot.edit_message_text(chat_id=game_user.user_id,
                          message_id=call.message.message_id,
                          text=text,
                          reply_markup=keyboard,
                          parse_mode='Markdown')
    return 0
Пример #5
0
def buy_menu(bot, call):
    game_user = game_control()
    game_user.load(call.from_user.id)

    if 'back' in call.data.split('<-'):
        text, keyboard = main_menu(game_user)
        bot.edit_message_text(chat_id=game_user.user_id,
                              message_id=call.message.message_id,
                              text=text,
                              reply_markup=keyboard,
                              parse_mode='Markdown')
        return 0

    conn = sqlite3.connect('game_folder/shop.bd')
    c = conn.cursor()
    c.execute('SELECT * FROM items WHERE user_id = ?', [game_user.user_id])
    items = c.fetchall()
    conn.close()

    if len(items) == 0:
        keyboard = items_for_selling(game_user.user_id)
    else:
        keyboard = telebot.types.InlineKeyboardMarkup(True)
        result_items = []
        items_lines = []
        prices = []
        for i in range(len(items)):
            temp = Item()
            temp.load(items[i][1][:-1])
            line = temp.inventory_line()

            result_items.append(temp)
            items_lines.append(temp.description[:25])

            price = int(items[i][2])
            prices.append(price)
            price = ' ({0} gold)'.format(price)
            callback_data = 'buy<-' + temp.description[:25]
            button = telebot.types.InlineKeyboardButton(
                line + price, callback_data=callback_data)
            keyboard.row(button)

        if call.data != 'buy' and call.data.split('<-')[1] in items_lines:
            idx = items_lines.index(call.data.split('<-')[1])
            if game_user.character.inventory.gold - prices[idx] >= 0:
                game_user.character.inventory.gold -= prices[idx]
                game_user.character.add_to_backpack([result_items[idx]])
                result_items.pop(idx)
                prices.pop(idx)

                keyboard = telebot.types.InlineKeyboardMarkup(True)
                for i in range(len(result_items)):
                    line = result_items[i].inventory_line()
                    price = ' ({0} gold)'.format(prices[i])
                    callback_data = 'buy<-' + result_items[i].description
                    button = telebot.types.InlineKeyboardButton(
                        line + price, callback_data=callback_data)
                    keyboard.row(button)
            else:
                bot.send_message(game_user.user_id,
                                 'You don\'t have enough money')
                return 0

    button = telebot.types.InlineKeyboardButton('Back',
                                                callback_data='buy<-back')
    keyboard.row(button)
    text = 'Let\'s see how I can help you\nYou have *{0} gold*'.format(
        game_user.character.inventory.gold)
    game_user.save(call.from_user.id)
    bot.edit_message_text(chat_id=game_user.user_id,
                          message_id=call.message.message_id,
                          text=text,
                          reply_markup=keyboard,
                          parse_mode='Markdown')
    return 0
Пример #6
0
def inventory_buttons(bot, call):
    game_user = game_user = game_control()
    game_user.load(call.from_user.id)

    if game_user.complete and call.data in game_user.character.show_backpack():
        game_user.last_status = 0
        item = call.data.split(',')[0]
        item = game_user.character.take_from_backpack(item)
        item.transfer_to_storage(game_user.user_id,
                                 game_user.user_id,
                                 folder='game_folder/')
        end_game(bot, call, game_user
                 )  # I send call here instead of message, but it should work
        game_user.save(game_user.user_id)
        return 1

    if call.data.lower() == 'close' and game_user.last_status == 1:
        game_user.last_status = 0
        keyboard = move_keyboard(game_user)
        game_user.save(game_user.user_id)
        bot.delete_message(chat_id=game_user.user_id,
                           message_id=call.message.message_id)
        bot.send_message(game_user.user_id,
                         'Backpack closed',
                         reply_markup=keyboard)
        return 0

    backpack = game_user.character.show_backpack()
    callback_data = []
    for i in backpack:
        callback_data.append(i[:50])

    if call.data in callback_data and game_user.last_status == 1:
        idx = callback_data.index(call.data)
        item = backpack[idx].split(',')[0]
        if item[0] == '(':
            item = item.split(')')[1]
            item = item[1:]
        if item == 'scroll of teleportation':
            game_user.is_being_teleported = True
            game_user.last_status = 0
            text = 'Enter the number of the room to which you wish to be teleported'
            bot.send_message(game_user.user_id, text)
            game_user.save(game_user.user_id)
            return 0
        elif item == 'map':
            game_user.has_map = True
            for i in range(len(game_user.character.backpack)):
                if game_user.character.backpack[i].description == 'map':
                    game_user.character.backpack.pop(i)
                    break

            text = 'Now you have a map, you will know the whole layout!'
            buffer = game_folder.generate_dungeon.draw_dungeon(
                game_user.dungeon, [game_user.x, game_user.y],
                game_user.user_id,
                folder='game_folder/',
                map_found=game_user.has_map)
            bot.send_photo(game_user.user_id, photo=buffer)
        else:
            text = game_user.character.use(item)

        bot.send_message(game_user.user_id, text)
        game_user.last_status = 1

        backpack = game_user.character.show_backpack()
        callback_data = []
        for i in backpack:
            callback_data.append(i[:50])

        if game_user.complete == False:
            backpack += ['Close']
            callback_data += ['close']

        keyboard = telebot.types.InlineKeyboardMarkup(True)
        for i in range(len(backpack)):
            button = telebot.types.InlineKeyboardButton(
                backpack[i], callback_data=callback_data[i])
            keyboard.row(button)
        text = game_user.character.name + '\'s backpack:'
        bot.edit_message_text(chat_id=game_user.user_id,
                              message_id=call.message.message_id,
                              text=text,
                              reply_markup=keyboard)
        game_user.save(game_user.user_id)
        return 0
    else:
        bot.send_message(call.from_user.id,
                         'Backpack menu is inactive right now.')
        return 0
Пример #7
0
def create_character(bot, message, start):
    game_user = game_control()
    game_user.load(message.from_user.id)

    game_user.creating_character = True
    status_change = True

    conn = sqlite3.connect('game_folder/character_save.bd')
    c = conn.cursor()
    c.execute('SELECT * FROM characters WHERE user_id = ? LIMIT 1',
              [game_user.user_id])
    temp = c.fetchall()
    conn.close()

    if len(temp) == 1:
        game_user.veteran_character = True
        game_user.user_id = message.from_user.id
        game_user.character.load_from_repository(game_user.user_id,
                                                 folder='game_folder/')
        if game_user.character.role == 'warrior':
            game_user.choose_class(Warrior())
        if game_user.character.role == 'mage':
            game_user.character.damage_type = Damage.ARCANE.value
            game_user.choose_class(Mage())
        game_user.save(game_user.user_id)

        keyboard = telebot.types.ReplyKeyboardMarkup(True)
        keyboard.row('Yes', 'No')
        bot.send_message(game_user.user_id,
                         'You have the following saved character:',
                         reply_markup=keyboard)
        show_status(bot, message)
        bot.send_message(
            game_user.user_id,
            'Do you want to play as this character (yes) or create a new one (no)?\nIf you choose to create a new one, old character will be deleted.'
        )
        return 0

    if start:
        bot.send_message(game_user.user_id, 'Enter character name:')
        game_user.save(game_user.user_id)
    else:
        text = message.text
        if text.lower() in ['human', 'elf', 'dwarf', 'ork'
                            ] and game_user.last_status == 1:
            text = text.lower()
            game_user.character.race = text

            if text == 'elf':
                game_user.character.strength = 45
                game_user.character.intellect = 55

            if text == 'dwarf':
                game_user.character.strength = 45
                game_user.character.endurance = 55

            if text == 'ork':
                game_user.character.strength = 55
                game_user.character.intellect = 45

            game_user.save(game_user.user_id)

            choose_role_text = 'Next select a class:'
            keyboard_role = telebot.types.ReplyKeyboardMarkup(True)
            keyboard_role.row('warrior', 'mage')
            bot.send_message(game_user.user_id,
                             choose_role_text,
                             reply_markup=keyboard_role)
        elif text.lower() in ['warrior', 'mage'
                              ] and game_user.last_status == 1:
            text = text.lower()
            game_user.character.role = text
            game_user.character.set_max_hp()

            if text == 'warrior':
                game_user.character.strength += 5
                game_user.choose_class(Warrior())

            if text == 'mage':
                game_user.character.intellect += 5
                game_user.choose_class(Mage())

            game_user.last_status = 0
            character_created_text = 'Congratulations, your character is created!\nName: '+game_user.character.name+\
            '\nRace: '+game_user.character.race+'\nClass: '+game_user.character.role+'\nStrength: '+str(game_user.character.strength)+\
            '\nIntellect: '+str(game_user.character.intellect)+'\nEndurance: '+str(game_user.character.endurance)+\
            '\nHealth: '+str(game_user.character.hp)
            game_user.save(game_user.user_id)

            keyboard_1 = telebot.types.ReplyKeyboardMarkup(True)
            keyboard_1.row('Next')
            bot.send_message(game_user.user_id,
                             character_created_text,
                             reply_markup=keyboard_1)
            status_change = False
            logging.info(message.from_user.first_name + ' created a character')
        elif game_user.last_status == 0:
            if game_user.character.name == '____no_name____':
                if len(text) > 15:
                    bot.send_message(game_user.user_id,
                                     'Name cannot be longer than 15 letters.')
                    return status_change

                game_user.character.set_name(text)
                game_user.last_status = 1
                game_user.save(game_user.user_id)

                choose_race_text = 'Now choose race:'
                keyboard_race = telebot.types.ReplyKeyboardMarkup(True)
                keyboard_race.row('human', 'elf', 'dwarf', 'ork')
                bot.send_message(game_user.user_id,
                                 choose_race_text,
                                 reply_markup=keyboard_race)
            else:
                bot.send_message(game_user.user_id,
                                 'Your character died during birth.')
                game_user.delete(game_user.user_id)
        else:
            bot.send_message(game_user.user_id,
                             'Use only options available on buttons.')

    return status_change
Пример #8
0
def clean_history(message):
    game_control().delete(message.from_user.id)
Пример #9
0
def game(bot, message):
    game_user = game_control()
    game_user.load(message.from_user.id)
    logging.info(message.from_user.first_name + ' is playing')

    if game_user.creating_character:
        if message.text.lower() == 'yes' and game_user.veteran_character:
            game_user.creating_character = False
            game_user.last_status = 1  # sending player to shop
            game_user.save(game_user.user_id)

            keyboard = telebot.types.ReplyKeyboardMarkup(True)
            keyboard.row('Next')
            bot.send_message(game_user.user_id,
                             'Adventures of ' + game_user.character.name +
                             ' continues!',
                             reply_markup=keyboard)
            return 0
        if message.text.lower() == 'no' and game_user.veteran_character:
            game_user.character.delete_from_repository(game_user.user_id,
                                                       folder='game_folder/')
            game_user.delete(game_user.user_id)
            game_user.create(game_user.user_id)
            create_character(bot, message, True)
            return 0
        if game_user.veteran_character:
            bot.send_message(
                game_user.user_id,
                'Answer with yes (continue as this character) or no (start with a new one)'
            )
            return 0

        status_change = create_character(bot, message, False)
        try:
            game_user.load(game_user.user_id)
        except:
            return 1
        game_user.creating_character = status_change

        if status_change == False:
            # trying to get some item from storage
            temp = Item()
            result = temp.transfer_from_storage(game_user.user_id,
                                                folder='game_folder/')
            if result:
                game_user.character.add_to_backpack([temp])
                text = 'You have recieved an item left for you by another player.'+\
                '\nIt\'s '+temp.description
                keyboard = telebot.types.ReplyKeyboardMarkup(True)
                keyboard.row(u'\U0001F44D', 'Into the dungeon')
                bot.send_message(game_user.user_id,
                                 text,
                                 reply_markup=keyboard)
        game_user.save(game_user.user_id)
        return 0

    if len(game_user.dungeon) == 0:

        # rewarding for transfered item
        if len(game_user.character.backpack
               ) != 0 and game_user.veteran_character == False:
            effect = game_user.character.backpack[0].effect.split(',')
            game_user.character.backpack[0].effect = effect[0]
            item_type = game_user.character.backpack[0].type.split(',')
            game_user.character.backpack[0].type = item_type[0]
            game_user.save(game_user.user_id)
            if message.text == u'\U0001F44D':
                add_likes(bot, message, int(effect[1]), int(item_type[1]),
                          numpy.random.randint(low=46, high=55))
            if message.text.lower() == 'into the dungeon':
                add_likes(bot, message, int(effect[1]), int(item_type[1]),
                          numpy.random.randint(low=26, high=35))

        # visiting shop
        if game_user.veteran_character and game_user.last_status == 1:
            game_user.last_status = 0
            game_user.in_shop = True
            game_user.save(game_user.user_id)
            open_shop(bot, game_user, folder='game_folder/')
            return 0

        if game_user.in_shop:
            bot.send_message(
                game_user.user_id,
                'If you want to continue, press close in the shop window')
            return 0

        # generating dungeon
        oppening, creators, doom = game_folder.generate_dungeon.main(
            game_user.character,
            game_user.user_id,
            folder='game_folder/',
            veteran=game_user.veteran_character)
        game_user.load(
            game_user.user_id)  # now dungeon part was added to the database

        game_user.creators = creators
        game_user.doom = doom
        game_user.save(game_user.user_id)
        bot.send_message(game_user.user_id, oppening)

        buffer = game_folder.generate_dungeon.draw_dungeon(
            game_user.dungeon, [game_user.x, game_user.y],
            game_user.user_id,
            folder='game_folder/',
            map_found=game_user.has_map)
        keyboard = move_keyboard(game_user)
        bot.send_photo(game_user.user_id, photo=buffer, reply_markup=keyboard)
        bot.send_message(game_user.user_id,
                         game_user.dungeon[game_user.in_room_now].description)
        return 0

    if game_user.is_fighting:
        game_user = fight(bot, message, game_user)
        game_user.save(game_user.user_id)
        if game_user.dead:
            return 1
        else:
            return 0

    if game_user.is_being_teleported:
        try:
            game_user.teleport(int(message.text))
            text = 'Winds of magic swirl around you as you finish reading the scroll. '+\
            'You open your eyes in completly different place. '+game_user.dungeon[game_user.in_room_now].description

            buffer = game_folder.generate_dungeon.draw_dungeon(
                game_user.dungeon, [game_user.x, game_user.y],
                game_user.user_id,
                folder='game_folder/',
                map_found=game_user.has_map)
            bot.send_photo(game_user.user_id, photo=buffer)

            game_user.check_completion()
            if game_user.complete:
                if len(game_user.character.show_backpack()) != 0:
                    text = 'Congratulations! You have completed the dungeon.'+\
                    ' Do you want to help other players and leave one of your items for future adventurers?'
                    keyboard = telebot.types.ReplyKeyboardMarkup(True)
                    keyboard.row('Yes', 'No')
                    bot.send_message(game_user.user_id,
                                     text,
                                     reply_markup=keyboard)
                    game_user.save(game_user.user_id)
                    return 0
                else:
                    end_game(bot, message, game_user)
                    game_user.save(game_user.user_id)
                    return 1

            game_user.save(game_user.user_id)
            keyboard = move_keyboard(game_user)
            bot.send_message(game_user.user_id, text, reply_markup=keyboard)

            if game_user.is_fighting:
                game_user = fight(bot, message, game_user)
                game_user.save(game_user.user_id)
                if game_user.dead:
                    return 1
        except:
            import traceback
            bot.send_message(game_user.user_id,
                             'Enter the number of the room on the map')
            traceback.print_exc()
        return 0

    if game_user.complete:
        if message.text.lower() == 'yes':
            game_user.last_status = 1
            open_inventory(bot, game_user)
            game_user.save(game_user.user_id)
            return 0
        elif message.text.lower() == 'no':
            end_game(bot, message, game_user)
            return 1
        else:
            bot.send_message(game_user.user_id, 'Select one of the buttons')

    # blocks move, look around, search, backpack buttons if backpack is open
    if game_user.last_status == 1:
        bot.send_message(game_user.user_id,
                         'You need to close the backpack to do that')
        return 0

    if message.text.lower() == 'look around':
        bot.send_message(
            game_user.user_id,
            game_user.dungeon[game_user.in_room_now].look_around())
        return 0

    if message.text.lower() == 'search':
        items, text = game_user.dungeon[game_user.in_room_now].loot_room()
        if len(items) != 0:
            game_user.character.add_to_backpack(items)
        game_user.save(game_user.user_id)
        bot.send_message(game_user.user_id, text)
        return 0

    if message.text.lower() == 'backpack':
        game_user.last_status = 1
        open_inventory(bot, game_user)
        game_user.save(game_user.user_id)
        return 0

    moves = game_user.determine_possible_moves()
    if message.text in moves:
        game_user.move(message.text)
        game_user.check_completion()
        n = game_user.how_many_rooms_left()

        if game_user.complete:
            if len(game_user.character.show_backpack()) != 0:
                game_user.save(game_user.user_id)
                text = 'Congratulations! You have completed the dungeon.'+\
                ' Do you want to help other players and leave one of your items for future adventurers?'
                keyboard = telebot.types.ReplyKeyboardMarkup(True)
                keyboard.row('Yes', 'No')
                bot.send_message(game_user.user_id,
                                 text,
                                 reply_markup=keyboard)
                return 0
            else:
                end_game(bot, message, game_user)
                return 1

        buffer = game_folder.generate_dungeon.draw_dungeon(
            game_user.dungeon, [game_user.x, game_user.y],
            game_user.user_id,
            folder='game_folder/',
            map_found=game_user.has_map)
        keyboard = move_keyboard(game_user)
        bot.send_photo(game_user.user_id, photo=buffer, reply_markup=keyboard)
        bot.send_message(
            game_user.user_id,
            game_user.dungeon[game_user.in_room_now].description +
            '\nYou have ' + str(n) + ' more unexplored rooms.')

        if game_user.is_fighting:
            game_user = fight(bot, message, game_user)
            game_user.save(game_user.user_id)
            if game_user.dead:
                return 1
        else:
            game_user.save(game_user.user_id)
            return 0

    logging.info("Recieved: " + message.text)