-- Module:碧蓝航线Equips
-- Made with ♥ by User:Leranjun
-- This module implements {{tl|碧蓝航线装备links}} {{tl|碧蓝航线装备图标}}.
-- Please refrain from invoking this module directly.
local p = {}
local getArgs = require("Module:Arguments").getArgs
local DATA = mw.loadData("Module:碧蓝航线Equips/data")
local COLORS = {
[1] = "#999",
[2] = "#999",
[3] = "#33f",
[4] = "#c3c",
[5] = "#c90",
[6] = "#f39"
}
local color_border = {
["1"] = "#b3b3b3",
["2"] = "#38c8ff",
["3"] = "#9a6cf1",
["4"] = "#ffce3b",
["5"] = "#f7c1cf"
}
local color_back = {
["1"] = "linear-gradient(-45deg,#d0d0d0,#D3D3D3,#A9A9A9)",
["2"] = "linear-gradient(-45deg,#4169E1,#aae6fd,#00BFFF)",
["3"] = "linear-gradient(-45deg,#9932CC,#B399FF,#9932CC)",
["4"] = "linear-gradient(-45deg,#E6B800,#FFD700,#E6B800)",
["5"] = "linear-gradient(-45deg,#fd99dc,#c5a2f5,#a2bef5,#a2e0f5,#93f2ff,#a1ffe2,#a4ffb1)"
}
local function throwError(message)
return '[[Category:碧蓝航线需要修正装备链接的页面]]<strong class="error">错误:' .. message .. '</strong>'
end
function p.main(frame)
return p._main(getArgs(frame))
end
function p._main(args)
local pre = args.outerprefix or ""
local suf = args.outersuffix or ""
local r = ""
local i = 1
while (args[i]) do
if (i ~= 1) then
r = r .. " • "
end
r = r .. pre
local t = mw.text.split(args[i], "T")
if (not args.notech) and (#t < 2 or (not tonumber(t[#t]))) then
r = r .. throwError('装备"' .. args[i] .. '"不包含tech值')
else
local tech = (not args.notech) and tonumber(t[#t]) -- Tech will be false if args.notech is true
local beau = args.notech and args[i] or mw.ustring.gsub(args[i], "T%d-$", "") -- Beautified name (w/o tech)
if (not DATA[beau]) then
r = r .. throwError('装备"' .. args[i] .. '"不存在')
elseif (tech and (not DATA[beau][tech])) then
r = r .. throwError('装备"' .. beau .. '"的tech值' .. tech .. "不存在")
else
local link = args["link" .. args[i]] or DATA[beau].link or beau
local rare = DATA[beau][tech] or -1
if (args.notech) then
for k, v in pairs(DATA[beau]) do
if (tonumber(k)) then
rare = math.max(rare, v) -- Use max rarity from table
end
end
end
r =
r ..
"[[碧蓝航线:" ..
link ..
'|<span style="color:' ..
COLORS[rare] .. ';">' .. (args.notech and beau or args[i]) .. "</span>]]"
end
r = r .. suf
end
i = i + 1
end
return r
end
local function safe(v) return tostring(v or "") end
-- 解析 size(接受 number、"75"、"75px"、" 75 px " 等),返回 number(像素)
local function parse_size(raw)
if not raw then return 50 end
if type(raw) == "number" then return raw end
local s = tostring(raw)
s = s:match("^%s*(.-)%s*$") or s
local numstr = s:match("([%d]+%.?%d*)")
local n = tonumber(numstr)
if n then return n end
return 50
end
-- 四舍五入到整数
local function iround(n)
return math.floor((n or 0) + 0.5)
end
-- 主渲染接口:与原模板参数兼容
function p.render(frame)
frame = frame or mw.getCurrentFrame()
local args = frame.args or {}
-- 原模板对稀有度使用的是 {{{2|{{{稀有度}}}}}} —— 先检查位置参数2,再检查命名参数稀有度
local rarity = safe(args[2] or args["稀有度"] or "")
-- size 与 unit
local size_raw = args.size or ""
local size = parse_size(size_raw) -- number
local unit = safe(args.unit ~= "" and args.unit or "px") -- 保留 unit 参数,默认 px
-- file 名:优先 file 参数,否则用第1个位置参数
local file_base = safe(args.file ~= "" and args.file or args[1] or "")
-- 构造文件名与显示名(与原模板一致)
local fileName = "BLHX 装备 " .. file_base .. ".png"
-- 颜色和背景(如果 rarity 非 1..5,回退为默认)
local borderColor = color_border[rarity] or "#d0d0d0"
local backImage = color_back[rarity] or "#d0d0d0"
-- css 外部透传
local css = args.css or ""
-- 数量角标
local num = safe(args.num or "")
-- T 参数与 link 参数:如果有 T 则展开装备 links 模板
local T = args.T
local linkValue = args.link
-- 构造 inner style(确保 size 用整数)
local size_px = iround(size)
local inner_style = string.format("width:%d%s;height:%d%s;border:2px solid %s;box-shadow:1px 1px 8px #696969;display:inline-block;border-radius:10px;background-image:%s;overflow:hidden",
size_px, unit, size_px, unit, borderColor, backImage)
-- 构造 File 嵌入的 wikitext([[File:...|NNpx|link=...]])
-- 仅在 linkValue 非空时加 link= 参数;否则不写 link=
local file_wikitext
if linkValue and linkValue ~= "" then
-- link 参数通常需要去掉首尾空白
local linkTarget = mw.text.trim(tostring(linkValue))
file_wikitext = string.format('[[File:%s|%d%s|link=%s]]', fileName, size_px, unit, mw.text.nowiki(linkTarget))
else
file_wikitext = string.format('[[File:%s|%d%s]]', fileName, size_px, unit)
end
-- badge(数量)位置与字体大小计算
local badge_bottom = iround(size * 0.0375)
local badge_right = iround(size * 0.0375)
local badge_font = iround(size / 5)
-- 组装最终 wikitext
local parts = {}
-- 外层容器(保留原来的 display/position/style 并注入外传 css)
local outer_style_attr = mw.text.nowiki(css)
table.insert(parts, string.format('<span style="display:inline-block;position:relative;vertical-align:middle;line-height:100%%;margin:2px;%s">', outer_style_attr))
-- 内层显示容器
table.insert(parts, string.format('<span style="%s">', mw.text.nowiki(inner_style)))
-- 文件显示(直接插入 wikitext)
table.insert(parts, file_wikitext)
table.insert(parts, '</span>') -- close inner
-- 数量角标(如果存在)
if num ~= "" then
-- 用 mw.text.nowiki 包裹 num 内容以防破坏结构
local badge = string.format(
'<span style="bottom:%d%s;right:%d%s;position:absolute;white-space:nowrap;color:#000!important;text-shadow:-1px -1px #FFF,1px -1px #FFF,-1px 1px #FFF,1px 1px #FFF,-2px -2px 4px #FFF,2px -2px 4px #FFF,-2px 2px 4px #FFF,2px 2px 4px #FFF;font-weight:bold;font-size:%d%s">%s</span>',
badge_bottom, unit, badge_right, unit, badge_font, unit, mw.text.nowiki(num)
)
table.insert(parts, badge)
end
table.insert(parts, '</span>') -- close outer
-- 如果存在 T 参数,按原模板调用碧蓝航线装备links 模板:
if T and tostring(T) ~= "" then
-- 构造第一个位置参数为: 參數1 + "T" + T(与原模板语义一致)
local first_param = (args[1] or "") .. "T" .. tostring(T)
-- 构造命名参数 key: "link" .. args[1] .. "T" .. T = linkValue
local named_key = "link" .. (args[1] or "") .. "T" .. tostring(T)
local invoke_args = { [1] = first_param }
if linkValue and linkValue ~= "" then
invoke_args[named_key] = linkValue
end
-- 展开模板并追加(frame:expandTemplate 用 table 形式)
local links_expanded = frame:expandTemplate{ title = "碧蓝航线装备links", args = invoke_args }
table.insert(parts, links_expanded)
end
return table.concat(parts)
end
return p