コード例 #1
0
def set_craft_possible_tier(bot, update, user_data):
    mes = update.callback_query.message
    data = update.callback_query.data
    player = Player.get_player(update.callback_query.from_user.id)
    guild = Guild.get_guild(player.guild)
    guild_stock = guild.get_stock() if guild is not None else {}
    stock = merge_int_dictionaries(player.stock.copy(), guild_stock.copy())
    parse = re.search("craft_possible_tier_(\\d+)", data)
    if parse is None:
        bot.answerCallbackQuery(
            callback_query_id=update.callback_query.id,
            show_alert=True,
            text="Произошла ошибка. Попробуйте начать сначала.")
        return
    tier = user_data.get("craft_possible_tier")
    new_tier = int(parse.group(1))
    if tier == new_tier:
        new_tier = None
        pop_from_user_data_if_presented(user_data, "craft_possible_tier")
    else:
        user_data.update({"craft_possible_tier": new_tier})
    res = get_possible_text(stock, tier=new_tier)
    buttons = InlineKeyboardMarkup(get_possible_buttons(new_tier))
    try:
        bot.editMessageText(chat_id=mes.chat_id,
                            message_id=mes.message_id,
                            text=res,
                            reply_markup=buttons,
                            parse_mode='HTML')
    except Exception:
        logging.error(traceback.format_exc())
    bot.answerCallbackQuery(callback_query_id=update.callback_query.id,
                            text="Готово!")
コード例 #2
0
def craft_possible(bot, update):
    player = Player.get_player(update.message.from_user.id)
    guild = Guild.get_guild(player.guild)
    guild_stock = guild.get_stock() if guild is not None else {}
    stock = merge_int_dictionaries((player.stock or {}).copy(),
                                   guild_stock.copy())
    res = get_possible_text(stock)
    buttons = InlineKeyboardMarkup(get_possible_buttons(None))
    bot.send_message(chat_id=update.message.chat_id,
                     text=res,
                     parse_mode='HTML',
                     reply_markup=buttons)
コード例 #3
0
def count_craft(craft_item: dict,
                craft_name: str,
                need_count: int,
                stock: dict,
                guild_stock: dict,
                withdraw: dict,
                buy: dict,
                to_craft: dict,
                current_offset: str,
                depth: int = 0,
                depth_limit: int = None,
                force_deep: bool = False,
                explicit: bool = True,
                last_one: bool = False):
    depth += 1
    if craft_item is None:
        craft_type = "simple"
        search_name = craft_name[0].capitalize() + craft_name[1:]
        craft_code = get_resource_code_by_name(search_name)
        if craft_code is None:
            craft_code = get_item_code_by_name(search_name)
    else:
        craft_type = craft_item.get("type")
        craft_code = str(craft_item.get("code"))
    player_count, guild_count = stock.get(craft_code,
                                          0), guild_stock.get(craft_code, 0)
    total_count = player_count + guild_count
    if craft_type == "simple" or (not force_deep
                                  and total_count >= need_count):
        # Добавляем ресурс в список на выдачу, и вычитаем из стока, чтобы не посчитать далее дважды
        enough = True
        if player_count < need_count:
            withdraw_count = min(guild_count, need_count - player_count)
            increase_or_add_value_to_dict(withdraw, craft_code, withdraw_count)
            pop_from_user_data_if_presented(stock, craft_code)
            decrease_or_pop_value_from_dict(guild_stock, craft_code,
                                            withdraw_count)

            buy_count = need_count - total_count
            if buy_count > 0:
                enough = False
                increase_or_add_value_to_dict(buy, craft_code, buy_count)
        else:
            decrease_or_pop_value_from_dict(stock, craft_code, need_count)
        if (enough and not explicit) or check_depth(depth, depth_limit):
            return ""
        return "{}{}".format(
            current_offset,
            format_resource_string(craft_name,
                                   craft_code,
                                   player_count,
                                   guild_count,
                                   total_count,
                                   need_count,
                                   depth=depth,
                                   need_separator=current_offset != "",
                                   close_separator=last_one))
    res = ""
    if not force_deep or depth == depth_limit:
        res += "{}{}\n".format(
            current_offset,
            format_resource_string(craft_name,
                                   craft_code,
                                   player_count,
                                   guild_count,
                                   total_count,
                                   need_count,
                                   depth=depth,
                                   need_separator=current_offset != "",
                                   close_separator=last_one))

        lvl_name = "level_{}".format(depth)
        cur_lvl = to_craft.get(lvl_name)
        if cur_lvl is None:
            cur_lvl = {}
            to_craft.update({lvl_name: cur_lvl})
        increase_or_add_value_to_dict(cur_lvl, craft_code,
                                      need_count - total_count)

    if not force_deep and craft_code is not None:
        pop_from_user_data_if_presented(stock, craft_code)
        increase_or_add_value_to_dict(withdraw, craft_code, guild_count)
        pop_from_user_data_if_presented(guild_stock, craft_code)

    resources_to_craft = list(craft_item.get("recipe").items())
    for i, (resource_name, count) in enumerate(resources_to_craft):
        current_withdraw = {}
        new_res = count_craft(get_craft_by_name(resource_name),
                              resource_name,
                              count * need_count,
                              stock,
                              guild_stock,
                              current_withdraw,
                              buy,
                              to_craft,
                              current_offset +
                              (LEVEL_OFFSET if not force_deep else ""),
                              depth=depth,
                              depth_limit=depth_limit,
                              explicit=explicit,
                              last_one=i == len(resources_to_craft) - 1)
        merge_int_dictionaries(withdraw, current_withdraw)

        res += ("<a href=\"/g_withdraw {}\">{}</a>\n".format(
            " ".join([
                "{} {}".format(code, count)
                for code, count in current_withdraw.items()
            ]), new_res) if current_withdraw is not None else
                "{}\n".format(new_res)) if new_res != "" else ""
        if res[-2:] == "\n\n":
            res = res[:-1]
    return res[:-1] if not check_depth(depth, depth_limit) else ""