打开/关闭菜单
28
6680
50
1.4万
OGAS数据中枢
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:GFLCalc/panel

来自OGAS数据中枢
弃权者​(留言 | 贡献)2026年10月19日 (一) 22:35的版本 (gflcalc: use mw.loadJsonData for JSON pages)

此模块的文档可以在Module:GFLCalc/panel/doc创建

--[[
Module:GFLCalc/panel —— 人形属性面板(带等级/好感滑条与编队选择)

由 Template:GFLCalc 调用:
    {{GFLCalc|M4A1}}
    {{GFLCalc|M4A1|100|100}}        -- 代码 | 初始等级 | 初始好感

本模块只做三件事:
  1. 用 mw.loadJsonData 取该人形的数据页(旧版本回退到 mw.loadData / 文本解析);
  2. 把各等级数值序列打包成 JSON,塞进 <pre class="gfl-data" hidden>;
  3. 输出 HTML 骨架(滑条、编队下拉、属性表)。
真正的联动计算在 MediaWiki:Common.js 的 gflcalc-widget 里完成。

编队规则(仅影响生命,生命 = 单编 × 编队数):
    1–9   → 1
    10–29 → 1–2
    30–69 → 1–3
    70–89 → 1–4
    90–100→ 1–5
    >100 或 已改造(mod 1/2/3) → 只能 5
]]

local p = {}

-- 同一页面里可能有多个人形面板;Widget(JS)只需要包含一次。
-- Scribunto 在单次页面解析中复用同一个 Lua 状态,所以模块级变量可以当“每页只发一次”的开关。
local widgetEmitted = false

local STATS = { "hp", "damage", "accuracy", "evasion", "rof" }
local LABEL = { hp = "生命", damage = "火力", accuracy = "命中",
                evasion = "回避", rof = "射速" }
local MOD_MAX = { [0] = 100, [1] = 110, [2] = 115, [3] = 120 }

local function trim(s)
	return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end

-- 读取 JSON 页:优先 mw.loadJsonData;它是唯一直接支持 JSON 内容模型的接口。
-- mw.loadData 走 require,在 MediaWiki 1.45 只接受 Scribunto 模块。
local function read_json(title)
	if type(mw.loadJsonData) == "function" then
		local ok, d = pcall(mw.loadJsonData, title)
		if ok and type(d) == "table" then return d end
	end

	local ok, d = pcall(mw.loadData, title)
	if ok and type(d) == "table" then return d end

	if not mw.title or not mw.title.new then return nil end
	local t = mw.title.new(title)
	if not t or not t.exists then return nil end
	local text = t:getContent()
	if not text or text == "" then return nil end
	text = text:gsub("^\239\187\191", "")
	local ok2, parsed = pcall(mw.text.jsonDecode, text)
	if ok2 and type(parsed) == "table" then return parsed end
	return nil
end

local function fetch(code)
	return read_json('Module:GFLCalc/data/' .. code)
end

local function resolve(alias)
	local a = trim(alias)
	if a == "" then return nil end

	-- 索引页可选:缺失/模型不对时退化为「别名即代码」
	local idx = read_json('Module:GFLCalc/index')
	if type(idx) == "table" then
		return idx[a] or idx[a:lower()]
	end
	local tries = { a, (a:gsub("%s+", "")), (a:gsub("^%l", string.upper)) }
	for _, c in ipairs(tries) do
		if c ~= "" and fetch(c) ~= nil then
			return c
		end
	end
	return nil
end

local function jsonEncode(obj)
	if mw.text and mw.text.jsonEncode then
		local ok, out = pcall(mw.text.jsonEncode, obj)
		if ok then return out end
	end
	-- 兜底:只支持本模块用到的结构(数字数组 / 字符串 / 布尔 / 表)
	local function enc(v)
		local t = type(v)
		if t == "number" then
			if v ~= v or v == math.huge then return "null" end
			return tostring(math.floor(v) == v and v or v)
		elseif t == "boolean" then
			return tostring(v)
		elseif t == "string" then
			return '"' .. v:gsub('[\\"]', '\\%0'):gsub('[\n\r\t]', " ") .. '"'
		elseif t == "table" then
			if #v > 0 then
				local parts = {}
				for i = 1, #v do parts[i] = enc(v[i]) end
				return "[" .. table.concat(parts, ",") .. "]"
			end
			local parts = {}
			for k, x in pairs(v) do
				parts[#parts + 1] = enc(tostring(k)) .. ":" .. enc(x)
			end
			return "{" .. table.concat(parts, ",") .. "}"
		end
		return "null"
	end
	return enc(obj)
end

local function series(d, key)
	local s = d.mods[key]
	if not s then return nil end
	local out = {}
	for _, stat in ipairs(STATS) do out[stat] = s[stat] end
	out.armor = s.armor
	return out
end

function p.render(frame)
	local args = frame.args
	if next(args) == nil then args = frame:getParent().args end

	local alias = args[1] or args.code or args["代码"]
	local code = resolve(alias)
	if not code then
		return '<span class="gfl-error">GFLCalc:未知单位「' .. tostring(alias) .. '」</span>'
	end

	local d = fetch(code)
	if not d then
		return '<span class="gfl-error">GFLCalc:缺少数据页 ' .. code .. '</span>'
	end

	local hasMod = d.mods["3"] ~= nil
	local favor = tonumber(args[3] or args.favor) or 100

	-- 根据初始等级推断改造档位(未改造 1–100,MOD1 100–110,MOD2 110–115,MOD3 115–120)
	local MOD_RANGE = { [0] = { 1, 100 }, [1] = { 100, 110 }, [2] = { 110, 115 }, [3] = { 115, 120 } }
	local want = tonumber(args[2] or args.level) or 100
	local mod = 0
	if hasMod then
		if want > 100 then
			if want <= 110 then mod = 1
			elseif want <= 115 then mod = 2
			else mod = 3 end
		end
	else
		mod = 0
	end
	local lo, hi = MOD_RANGE[mod][1], MOD_RANGE[mod][2]
	local lv = math.max(lo, math.min(want, hi))

	local payload = {
		code = code,
		name = d.n or code,
		type = d.t,
		crit = d.c,
		ap = d.ap,
		hasMod = hasMod,
		modMax = { hasMod and 120 or 100 },
		series = { ["0"] = series(d, "0"), ["3"] = series(d, "3") },
		level = lv,
		mod = mod,
		favor = favor,
	}
	local json = jsonEncode(payload)

	local root = mw.html.create("div")
		:addClass("gfl-panel")
		:attr("data-gfl-panel", "1")
		:attr("data-code", code)
		:attr("data-hasmod", hasMod and "1" or "0")

	local head = root:tag("div"):addClass("gfl-head")
	head:tag("span"):addClass("gfl-name"):wikitext(d.n or code)
	head:tag("span"):addClass("gfl-meta"):wikitext(
		string.format("类型 %s · 暴击 %s%% · 穿甲 %s",
			tostring(d.t), tostring(d.c), tostring(d.ap)))
	head:tag("button"):attr("type", "button"):addClass("gfl-max")
		:wikitext("满状态")

	local ctl = root:tag("div"):addClass("gfl-controls")

	local lvRow = ctl:tag("label"):addClass("gfl-ctl")
	lvRow:tag("span"):addClass("gfl-ctl-name"):wikitext("等级")
	-- 轨道始终从 1 开始;可用下限放在 data-floor 里,由脚本负责夹住
	lvRow:tag("input"):attr("type", "range"):attr("class", "gfl-level")
		:attr("min", 1):attr("max", hi):attr("step", 1):attr("value", lv)
		:attr("data-floor", lo)
		:attr("title", string.format("可用等级 %d–%d", lo, hi))
	lvRow:tag("input"):attr("type", "number"):addClass("gfl-num")
		:addClass("gfl-level-num")
		:attr("min", lo):attr("max", hi):attr("step", 1)
		:attr("value", lv):attr("title", "可直接输入等级")

	local favorRow = ctl:tag("label"):addClass("gfl-ctl")
	favorRow:tag("span"):addClass("gfl-ctl-name"):wikitext("好感")
	favorRow:tag("input"):attr("type", "range"):attr("class", "gfl-favor")
		:attr("min", 0):attr("max", 200):attr("step", 1)
		:attr("value", math.max(0, math.min(favor, 200)))
	favorRow:tag("input"):attr("type", "number"):addClass("gfl-num")
		:addClass("gfl-favor-num")
		:attr("min", 0):attr("max", 200):attr("step", 1)
		:attr("value", math.max(0, math.min(favor, 200)))
		:attr("title", "0–200")

	-- 改造滑条:没有改造数据的人形整行不渲染
	if hasMod then
		local MOD_LABEL = { "未改造", "MOD1", "MOD2", "MOD3" }
		local modRow = ctl:tag("label"):addClass("gfl-ctl gfl-modwrap")
		modRow:tag("span"):addClass("gfl-ctl-name"):wikitext("改造")
		modRow:tag("input"):attr("type", "range"):attr("class", "gfl-mod")
			:attr("min", 0):attr("max", 3):attr("step", 1):attr("value", mod)
		modRow:tag("output"):addClass("gfl-mod-out"):wikitext(MOD_LABEL[mod + 1])
	end

	-- 编队滑条:轨道固定 1–5,可用上限写 data-ceil,滑块始终贴右
	local squadRow = ctl:tag("label"):addClass("gfl-ctl")
	squadRow:tag("span"):addClass("gfl-ctl-name"):wikitext("编队")
	squadRow:tag("input"):attr("type", "range"):attr("class", "gfl-squad")
		:attr("min", 1):attr("max", 5):attr("step", 1):attr("value", 1)
		:attr("data-ceil", 5)
	squadRow:tag("input"):attr("type", "number"):addClass("gfl-num")
		:addClass("gfl-squad-num")
		:attr("min", 1):attr("max", 5):attr("step", 1):attr("value", 1)
		:attr("title", "1–5")

	local tbl = root:tag("table"):addClass("gfl-stats")
	local tbody = tbl:tag("tbody")
	local function row(key, label, extra)
		local tr = tbody:tag("tr")
		tr:tag("th"):wikitext(label)
		local td = tr:tag("td")
		td:tag("span"):addClass("gfl-val"):attr("data-stat", key):wikitext("—")
		if extra then td:tag("span"):addClass("gfl-note"):attr("data-note", key) end
	end
	row("hp", "生命", true)
	row("damage", "火力", false)
	row("accuracy", "命中", false)
	row("evasion", "回避", false)
	row("rof", "射速", false)
	row("armor", "护甲", false)

	root:tag("pre"):addClass("gfl-data"):attr("hidden", "hidden"):wikitext(json)
	local hint = root:tag("div"):addClass("gfl-hint")
	hint:wikitext("编队仅影响生命:生命 = 单编 × 编队数;" ..
		"90 级以上最多 5 编队,改造后固定 5 编队。")
	hint:tag("span"):addClass("gfl-lockhint")

	local html = tostring(root)
	if not widgetEmitted then
		widgetEmitted = true
		local ok, widget = pcall(frame.callParserFunction, frame, '#widget', 'GFLCalcPanel')
		if ok and type(widget) == "string" then
			html = html .. widget
		end
	end
	return html
end

return p