欢迎查看本站wiki待办,一起参与施工!
如有建议或想法,也欢迎加入wiki学会洽谈区QQ群:471292177~目前急缺人手,详情请查看:学会会员招募中
欢迎查看本站wiki待办,一起参与施工!也欢迎加入wiki学会洽谈区QQ群:471292177~目前急缺人手,详情请查看:学会会员招募中
全站通知:

模块:SMW

来自苏丹的游戏WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

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

-- 参考了 https://rimworld.huijiwiki.com/wiki/模块:SMW , 其作者是 Lu 、 Duduluu
-- 新增以、作为分割符的功能
local p = {}

local getArgs = require('Module:Arguments').getArgs

-- 分割字符串的辅助函数
local function split(input, delimiter)
  local result = {}
  for match in (input .. delimiter):gmatch("(.-)" .. delimiter) do
    table.insert(result, match:match("^%s*(.-)%s*$")) -- 去掉首尾空格
  end
  return result
end

-- 用于查询一个值
function p.show(frame)
  if not (frame.args[1] and frame.args[2]) then
    return "模块:SMW show 需要参数1和2"
  end
  if frame.args[1] == '' or frame.args[2] == '' then
    return "模块:SMW show 需要非空参数1和2"
  end

  local result = frame:callParserFunction{name="#ask:[[" .. frame.args[1] .. "]]", args={
    "?" .. frame.args[2],
    mainlabel = "-",
    headers = "hide"
  }}

  if result and result ~= '' then
    return result
  elseif frame.args[3] then
    return frame.args[3]
  else
    return ''
  end
end

-- 用于设置单个属性
function p.set(frame)
  local args = getArgs(frame)
  local properties = {}
  for k, v in pairs(args) do
    if k and v then
      properties[k] = v
    end
  end

  local firstK, firstV = next(properties)
  if firstK and firstV then
    frame:callParserFunction('#set:' .. firstK .. '=' .. firstV, properties)
  end
end

-- 用于设置模板中的所有参数作为SMW属性
function p.setAllArgs(frame)
  local args = getArgs(frame)
  local properties = {}

  for k, v in pairs(args) do
    if type(k) == 'number' then
      -- 支持以双冒号分割键值
      local splitValues = mw.text.split(v, "::")
      if #splitValues == 2 then
        properties[splitValues[1]] = splitValues[2]
      else
        error("使用模块 SMW 的 setAllArgs 时,应当保证在参数的值中只有一个分隔符“::”,如“射速::360rpm”,但有参数为:" .. v)
      end
    else
      -- 检测是否包含逗号
      if v:find("、") then
        local values = split(v, "、")
        for _, singleValue in ipairs(values) do
          properties[k] = properties[k] or {}
          table.insert(properties[k], singleValue)
        end
      else
        properties[k] = v
      end
    end
  end

  -- 使用 frame:callParserFunction 对每个属性逐个设置
  for prop, val in pairs(properties) do
    if type(val) == "table" then
      for _, singleVal in ipairs(val) do
        frame:callParserFunction('#set:' .. prop .. '=' .. singleVal, {})
      end
    else
      frame:callParserFunction('#set:' .. prop .. '=' .. val, {})
    end
  end
end

return p