Module:Infobox/parsing/sandbox: Difference between revisions

From Official Factorio Wiki
Jump to navigation Jump to search
(Init with current state)
 
(Tech trigger needs to be one string...)
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


local parsing = {}
local parsing = {}
local tech_trigger_text =
{
  ["mine-entity"] = "Mine:",
  ["build-entity"] = "Build:",
  ["craft-item"] = "Craft:",
  ["capture-spawner"] = "Capture any spawner.",
  ["create-space-platform"] = "Create space platform.",
  ["send-item-to-orbit"] = "Send to orbit:"
}
function parsing._technology_trigger(frame, str)
  local parts = util._split(str, ":")
  local text = frame:expandTemplate{title = "Translation", args = {tech_trigger_text[parts[1]]}}
  if parts[2] then
    return text .. " " .. parsing._item(frame, parts[2])
  end
  return text
end


function parsing._technology(frame, args)
function parsing._technology(frame, args)

Latest revision as of 11:25, 28 October 2024

Documentation for this module may be created at Module:Infobox/parsing/sandbox/doc

local util = require("Module:Util")

local parsing = {}

local tech_trigger_text =
{
  ["mine-entity"] = "Mine:",
  ["build-entity"] = "Build:",
  ["craft-item"] = "Craft:",
  ["capture-spawner"] = "Capture any spawner.",
  ["create-space-platform"] = "Create space platform.",
  ["send-item-to-orbit"] = "Send to orbit:"
}

function parsing._technology_trigger(frame, str)
  local parts = util._split(str, ":")
  local text = frame:expandTemplate{title = "Translation", args = {tech_trigger_text[parts[1]]}}
  if parts[2] then
    return text .. " " .. parsing._item(frame, parts[2])
  end
  return text
end

function parsing._technology(frame, args)
  if args[1] == "?" or util._empty_arg(args[1]) then
    return args[1]
  end

  local ret = {}
  local techs = util._split(args[1], "+")
  for _, tech in ipairs(techs) do
    local tech_parts = util._split(tech, ",")
    local tech_name = tech_parts[1] .. " (research)"
    if not util._page_exists(tech_name) then -- fall back to plain name if page with " (research)" doesn't exist
      tech_name = tech_parts[1]
    end

    local tech_level = tech_parts[2] or ""
    if tech_level == "" then -- fall back to level from tech name if none given manually
      local second_to_last_char = string.sub(tech_parts[1], -2, -2)
      if second_to_last_char == " " then
        tech_level = string.sub(tech_parts[1], -1) -- last character should be the level of the tech
      end
    end
    ret[#ret+1] = frame:expandTemplate{title = 'icon/special', args = {tech_name, tech_level, color=args.color or "999"}}
  end
  return table.concat(ret)
end

function parsing._item(frame, item_string)
  if item_string == "?" or util._empty_arg(item_string) then
    return item_string
  end

  local ret = {}
  local items = util._split(item_string, "+")
  for _, item in ipairs(items) do
    local icon_args = util._split(item, ",")
    icon_args[2] = icon_args[2] or ""
    ret[#ret+1] = frame:expandTemplate{title = 'icon/special', args = icon_args}
  end
  return table.concat(ret)
end

function parsing._crafting(frame, recipe)
  if recipe == "?" or util._empty_arg(recipe) then
    return recipe
  end
  
  local recipe_parts = util._split(recipe, "=")  
  local ingredients = parsing._crafting_part(frame, recipe_parts[1])
  
  local products = recipe_parts[2]
  if products then
    products = parsing._crafting_part(frame, products)
  else
    local item_name = frame:expandTemplate{title = 'No language suffix/No namespace'}
    products = frame:expandTemplate{title = 'icon/special', args = {item_name, "1"}}
  end
  
  return ingredients .. " → " .. products
end

function parsing._crafting_raw(frame, recipe)
  if recipe == "?" or util._empty_arg(recipe) then
    return recipe
  end

  local recipe_parts = util._split(recipe, "=")
  return parsing._crafting_part(frame, recipe_parts[1])
end

-- @param frame
-- @param str string A list of "item, count" separated by "+"
-- @return string @The resulting icons with a "+" between each icon
function parsing._crafting_part(frame, str)
  local ret = {}
  local items = util._split(str, "+")
  for _, item in ipairs(items) do
    local item_parts = util._split(item, ",")
    local item_count = item_parts[2] or "1" -- fall back to 1, not empty!
    item_count = frame:expandTemplate{title = 'Crop', args = {item_count}}
    ret[#ret+1] = frame:expandTemplate{title = 'icon/special', args = {item_parts[1], item_count}}
  end
  return table.concat(ret, "+")
end

return parsing