- 欢迎光临Satisfactory国区BWIKI。请登录您的B站账号,方可使用页面编辑与评论功能。

全站通知:

模块:ItemTooltip

来自幸福工厂WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:ItemTooltip/doc创建

local cargo = mw.ext.cargo
local p = {}

local template = '<div style="line-height:1">' ..
                  '<div style="font-size:medium; display:inline-table; align-items:center">%s' ..
                    '<div class="tooltip" style="display:inline-block;">' ..
                      '[[Image:%s.png|%spx|link=%s]]' ..
                      '<div class="tooltip-block">' ..
                      '%s' ..
                    '</div>' ..
                    '</br>' ..
                    '<div style="font-size:x-small; text-align:right">%s</div>' ..
                  '</div>' ..
                '</div>'

local template_tooltip = '<div class="title">%s</div>' ..
                            '<div class="tooltip-body">' ..
                              '<div class="description">%s</div><hr>' ..
                              '<div>' ..
                                '%s' ..
                              '</div>' ..
                            '</div>' ..
                         '</div>'

local template_item = '<div class="recipe-item"><div>[[Image:%s.png|28px]]</div><span class="amount">%s</span></div>'
local template_recipe = '<div class="tooltip-recipe">' ..
                          '<div class="recipe-name">%s:</div>' ..
                          '<div class="recipe-row">' ..
                            '%s' ..
                            '<span class="arrow">&#x25B6;</span>' ..
                            '%s' ..
                          '</div>' ..
                          '%s' ..
                        '</div>'

local function compileRecipe(recipe, targetItemName)
  local ipm = 0
  
  local ingredients = ''
  for i=1, 8, 1 do
    local item = recipe["ingredient" .. i]
    if item ~= nil and #item > 0 then
      local amount = recipe["quantity" .. i] or 1
      ingredients = ingredients .. string.format(template_item, item, amount)
    end
  end
  
  local products = ''
  for i=1, 4, 1 do
    local item = recipe["product" .. i]
    if item ~= nil and #item > 0 then
      local amount = recipe["productCount" .. i] or 1
      products = products .. string.format(template_item, item, amount)
	  
	  if tonumber(recipe.craftingTime) ~= nil and item == targetItemName then
	    ipm = 60 / recipe.craftingTime * tonumber(amount)
	  end
	end
  end
  
  local productionRate = ''
  if ipm > 0 then
    productionRate = '<div class="production-rate">Production Rate: <span>' .. ipm .. ' / min</span></div>'
  end
  
  return string.format(template_recipe,
      recipe.recipeName,
      ingredients,
      products,
      productionRate
  )
end

local function getRecipes(itemName)
  local tables = 'crafting_recipes'
  local fields = 'recipeName, craftingTime, product=product1, product2, product3, product4, productCount=productCount1, productCount2, productCount3, productCount4, ingredient1, quantity1, ingredient2, quantity2, ingredient3, quantity3, ingredient4, quantity4, ingredient5, quantity5, ingredient6, quantity6, ingredient7, quantity7, ingredient8, quantity8'
  local args = { where = '(product="' .. itemName .. '" or product2="' .. itemName .. '" or product3="' .. itemName .. '" or product4="' .. itemName .. '") and (experimental is null or experimental=False)', 
                 orderBy = 'mainRecipe DESC, alternateRecipe ASC' }
  local recipes = cargo.query(tables, fields, args)
  
  local recipesList = ''
  for i=1, #recipes, 1 do
    recipesList = recipesList .. compileRecipe(recipes[i], itemName)
  end
  
  return recipesList
end

function p.getTooltip(frame)
  local itemName = frame.args.item or frame.args[1]
  local amount = frame.args.amount or frame.args[2] or nil
  local size = frame.args.size or frame.args[3] or 40
  
  local count = amount and amount .. ' ×&nbsp;' or ''
  
  local tooltipHtml = string.format(template_tooltip,
      itemName,
      --description, -- no items table for now so no description
      '',
      getRecipes(itemName)
  )
  
  return string.format(template,
      count,
      itemName,
      size,
      itemName,
      tooltipHtml,
      itemName
  )
end

return p