模块:Templates
来自星露谷物语扩展百科
更多操作
local p = {}
-- 表格行拼接(普通)
function p.row(frame)
local args = frame:getParent().args
local cells = {}
local count = 1
for i, value in ipairs(args) do
if value ~= 'row' then
cells[count] = '<td>' .. value .. '</td>'
count = count + 1
end
end
return '<tr>' .. table.concat(cells) .. '</tr>'
end
-- 表格行拼接(GiftRow)
function p.itemRow(frame)
local args = frame:getParent().args
local cells = {}
local count = 1
for i, value in ipairs(args) do
if value ~= 'row' then
if count == 1 then
cells[count] = '<td>{{Name|' .. value .. '}}</td>'
count = count + 1
cells[count] = '<td>{{Description|' .. value .. '}}</td>'
else
cells[count] = '<td>' .. value .. '</td>'
end
count = count + 1
end
end
return frame:preprocess('<tr>' .. table.concat(cells) .. '</tr>')
end
-- 检查页面是否存在
function p.checkPageExistence(frame)
local pageName = frame.args[1]
if mw.title.new(pageName).exists then
return "exists" -- return string.format('[[%s]]', pageName)
else
return "notexists" -- return pageName
end
end
-- 获取上层版本号
function p.getVerP(frame)
local versionMapping = {
["1.07a"] = "1.0",
["1.07"] = "1.0",
["1.06"] = "1.0",
["1.051b"] = "1.0",
["1.051"] = "1.0",
["1.05"] = "1.0",
["1.04"] = "1.0",
["1.03"] = "1.0",
["1.02"] = "1.0",
["1.01"] = "1.0",
["1.0"] = "1.0",
}
local version = frame.args[1] or "1.6"
if versionMapping[version] then
return "版本历史/1.0"
end
if version == "1.11" then
return "版本历史/1.1"
end
local major, minor = version:match("^(%d+)%.(%d+)")
if major and minor then
return "版本历史/" .. major .. "." .. minor
end
return "版本历史"
end
-- 获取主版本号
function p.getVer(frame)
local version = frame.args[1] or "stable"
local versions = {
stable = "1.6",
beta = "1.6",
wegame = "1.5.4"
}
return versions[version] or version
end
-- 移除内链
function p.removeLinks(frame)
local text = frame.args[1] or ""
text = text:gsub("%[%[([^\]]+)%]%]", "%1")
return text
end
-- SVE Quote 兼容性处理
function p.quoteSVE(frame)
local quote = frame.args[1] or ""
quote = quote:match("^%s*(.-)%s*$")
quote = quote:gsub('^%s*["]*(.-)["]*%s*$', '%1')
quote = quote:gsub('^%s*“*(.-)”*%s*$', '%1')
quote = quote:gsub('^%s*“(.-)”%s*$', '%1')
quote = quote:gsub('^%s*”(.-)“%s*$', '%1')
return quote
end
-- 获取首条链接
function p.getFirstLink(frame)
local source = frame.args.source or ""
if source == "" then return "" end
local first_link = source:match("%[%[([^%]]-)]%]")
return first_link or ""
end
-- 鱼塘 header 生成
function p.generateHeaders(frame)
local args = frame.args
local count = tonumber(args[1]) or 0
local result = {}
for i = 1, count do
table.insert(result, string.format('<th data-sort-type="number">%d</th>', i))
end
return table.concat(result)
end
-- 鱼塘 row 生成
function p.generateRow(frame)
local args = mw.text.split(frame.args[1], ",")
local result = {}
local i = 1
table.insert(result, string.format('<td data-sort-value="%s">%s</td>', args[i + 1], args[i]))
i = i + 2
while i <= #args - 3 do
local colspan = tonumber(args[i])
local content = args[i + 1] or "/"
table.insert(result, string.format('<td colspan="%d">%s</td>', colspan, content))
i = i + 2
end
table.insert(result, string.format('<td>%s</td>', args[i]))
table.insert(result, string.format('<td>%s</td>', args[i + 1]))
return "<tr>" .. table.concat(result) .. "</tr>"
end
-- 隐藏列按钮生成
function p.generateButtons(frame)
local n = tonumber(frame.args[1])
local result = ""
for i = 1, n do
result = result .. '<li class="btn btn-default control-column" role="button" data-column="' .. i .. '">第 ' .. i .. ' 列</li>'
end
return result
end
-- 鱼类一览 分类格式化
function p.formatCategories(frame)
local categories = frame.args.categories or ''
local result = {}
for category in mw.text.gsplit(categories, ',') do
local displayText = category:match("%[%[:分类:[^|]+|(.-)%]%]")
if displayText then
table.insert(result, mw.text.trim(displayText))
else
table.insert(result, mw.text.trim(category))
end
end
return table.concat(result, ", ")
end
-- 鱼类一览 检查地点
function p.checkLocations(frame)
local input = frame.args[1] or ""
local keywords = { "突变虫穴", "巫婆沼泽", "农场", "夜市" }
local result = {}
for _, keyword in ipairs(keywords) do
if input:find(keyword, 1, true) then
table.insert(result, keyword)
end
end
return table.concat(result, ", ")
end
-- 鱼类一览 检查是否全天
function p.checkAllDay(frame)
local input = frame.args[1] or ""
if input:find("全天") then
return "全天"
end
if input:find("任意") then
return "全天"
else
return "非全天"
end
end
-- ExtractStats
-- 从 {{Name}} 模板文本中提取特定属性的数值
-- 示例1:基础使用
-- {{#invoke:Templates|getStatValue|{{Name|Defense|+5}}{{Name|Immunity|+8}}|Immunity}}
-- 结果:8
-- 示例2:提取其他属性
-- {{#invoke:Templates|getStatValue|{{Name|Defense|+5}}{{Name|Immunity|+8}}|Defense}}
-- 结果:5
-- 示例3:处理多位数值
-- {{#invoke:Templates|getStatValue|{{Name|Immunity|+500}}|Immunity}}
-- 结果:500
function p.getStatValue(frame)
local text = frame.args[1] or '' -- 第一个参数是要搜索的文本
local stat = frame.args[2] or '' -- 第二个参数是要查找的属性名
-- 使用模式匹配,用 stat 变量构建匹配模式
local value = string.match(text, stat..".-(%+(%d+))")
return value or ''
end
-- Calcedibility
-- 用于计算可食用物品的能量值和生命值。
-- 使用了可食用性进行计算;可以用于输出未格式化的原始数字(用于表格排序的 data-sort-value),也可以输出用于实际显示的数字。
--ceh = calculate edibility (energy/health)
function p.ce(frame)
local item = string.lower(frame.args.im)
local edibility = tonumber(frame.args.ed)
local quality = tonumber(frame.args.q)
local ulang = string.upper(frame.args.ll)
local result, formattedresult, temp, length
if edibility == 0 then return 0 end
if item == "energy" then
result = math.floor(math.ceil(edibility*2.5) + edibility*quality)
else
result = math.floor(math.floor(math.ceil(edibility*2.5) + edibility*quality)*0.45)
end
formattedresult = mw.language.getContentLanguage():formatNum(result)
return formattedresult
end
-- Tcartprice
-- 用于计算给定物品出现在旅行货车时可能的价格范围。
function p.tprice(frame)
local price = frame.args["price"]
local separator = frame.args["separator"]
local gold = frame.args["gold"]
local space = string.lower(frame.args["spacearoundseparator"])
local ulang = string.upper(mw.language.getContentLanguage():getCode())
local formattedprice = "[[File:Gold.png|18px|link=]]"
if (string.lower(price) == "furniture") then
formattedprice = formattedprice .. "250"
if space == "true" then formattedprice = formattedprice .. " " .. separator .. " "
else formattedprice = formattedprice .. separator
end
formattedprice = formattedprice .. tostring(mw.language.getContentLanguage():formatNum(2500))
else
local lowprice = tonumber(price) * 3
local highprice = tonumber(price) * 5
local fmlowprice = tostring(mw.language.getContentLanguage():formatNum(lowprice))
local fmhighprice = tostring(mw.language.getContentLanguage():formatNum(highprice))
if (lowprice <= 100) then
formattedprice = formattedprice .. "100"
else
formattedprice = formattedprice .. fmlowprice
end
if space == "true" then formattedprice = formattedprice .. " " .. separator .. " "
else formattedprice = formattedprice .. separator
end
if (highprice <= 1000) then
formattedprice = formattedprice .. tostring(mw.language.getContentLanguage():formatNum(1000))
else
formattedprice = formattedprice .. fmhighprice
end
end
return formattedprice .. gold
end
-- Calcgrangepoints
-- 用于计算在星露谷展览会展览给定物品会获得的分数。
function p.cgp(frame)
--Template must call Calcsellprice and send result here
local price = tonumber(frame.args.p)
--quality must be 0, 1, 2, or 4
local quality = tonumber(frame.args.q)
local totalpoints = 0
totalpoints = quality + 1
if (price >= 20) then totalpoints = totalpoints + 1 end
if (price >= 90) then totalpoints = totalpoints + 1 end
if (price >= 200) then totalpoints = totalpoints + 1 end
if (price >= 300 and quality < 2) then
totalpoints = totalpoints + 1 end
if (price >= 400 and quality < 1) then
totalpoints = totalpoints + 1 end
return totalpoints
end
return p