Module:GFLCalc/panel:修订间差异
来自OGAS数据中枢
更多操作
imported>GFLCalc gflcalc data import dv=sha256:e510650bd49bf03c |
小 gflcalc: build panel controls client-side to bypass HTML sanitizer |
||
| (未显示2个用户的7个中间版本) | |||
| 第7行: | 第7行: | ||
本模块只做三件事: | 本模块只做三件事: | ||
1. 用 mw.loadData | 1. 用 mw.loadJsonData 取该人形的数据页(旧版本回退到 mw.loadData / 文本解析); | ||
2. 把各等级数值序列打包成 JSON,塞进 <pre class="gfl-data" hidden>; | 2. 把各等级数值序列打包成 JSON,塞进 <pre class="gfl-data" hidden>; | ||
3. 输出 HTML 骨架(滑条、编队下拉、属性表)。 | 3. 输出 HTML 骨架(滑条、编队下拉、属性表)。 | ||
| 第34行: | 第34行: | ||
local function trim(s) | local function trim(s) | ||
return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", "")) | return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", "")) | ||
end | |||
-- JSON 里的 "0" / "3" 键解码后可能是整数键,两种都试。 | |||
local function keyed(t, k) | |||
if type(t) ~= "table" then return nil end | |||
local v = t[k] | |||
if v ~= nil then return v end | |||
local n = tonumber(k) | |||
if n ~= nil then | |||
v = t[n] | |||
if v ~= nil then return v end | |||
end | |||
return t[tostring(k)] | |||
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 | end | ||
| 第39行: | 第78行: | ||
local a = trim(alias) | local a = trim(alias) | ||
if a == "" then return nil end | if a == "" then return nil end | ||
local idx = | |||
-- 索引页可选:缺失/模型不对时退化为「别名即代码」 | |||
local idx = read_json('Module:GFLCalc/index') | |||
if type(idx) == "table" then | |||
local hit = keyed(idx, a) or keyed(idx, a:lower()) | |||
if hit and fetch(hit) then return hit end | |||
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 | end | ||
| 第76行: | 第127行: | ||
local function series(d, key) | local function series(d, key) | ||
local s = d.mods[key | local s = keyed(d.mods, key) | ||
-- 单键 {"0": ...} 会被 FormatJson 重排成 [1] | |||
if not s and tonumber(key) == 0 then s = keyed(d.mods, 1) end | |||
if not s then return nil end | if not s then return nil end | ||
local out = {} | local out = {} | ||
for _, stat in ipairs(STATS) do out[stat] = s[stat] end | -- 代理表没有可靠的 # 长度;逐项拷贝成普通数组,供 JSON 编码和 JS 使用。 | ||
local function copy_array(arr) | |||
local copy = {} | |||
if type(arr) ~= "table" then return copy end | |||
local i = 1 | |||
while arr[i] ~= nil do | |||
copy[i] = arr[i] | |||
i = i + 1 | |||
end | |||
return copy | |||
end | |||
for _, stat in ipairs(STATS) do out[stat] = copy_array(s[stat]) end | |||
out.armor = s.armor | out.armor = s.armor | ||
return out | return out | ||
| 第94行: | 第158行: | ||
end | end | ||
local | local d = fetch(code) | ||
if | if not d then | ||
return '<span class="gfl-error">GFLCalc:缺少数据页 ' .. code .. '</span>' | return '<span class="gfl-error">GFLCalc:缺少数据页 ' .. code .. '</span>' | ||
end | end | ||
local hasMod = d.mods | local hasMod = keyed(d.mods, "3") ~= nil | ||
local favor = tonumber(args[3] or args.favor) or 100 | local favor = tonumber(args[3] or args.favor) or 100 | ||
| 第133行: | 第197行: | ||
local json = jsonEncode(payload) | local json = jsonEncode(payload) | ||
-- MediaWiki 的 Sanitizer 会转义 button/input/label/output。 | |||
-- 这里只输出白名单内的容器和隐藏 JSON;控件全部由 Widget JS 创建。 | |||
local root = mw.html.create("div") | local root = mw.html.create("div") | ||
:addClass("gfl-panel") | :addClass("gfl-panel") | ||
| 第138行: | 第204行: | ||
:attr("data-code", code) | :attr("data-code", code) | ||
:attr("data-hasmod", hasMod and "1" or "0") | :attr("data-hasmod", hasMod and "1" or "0") | ||
root:tag("pre"):addClass("gfl-data"):wikitext(json) | |||
root:tag("pre"):addClass("gfl-data | |||
local html = tostring(root) | local html = tostring(root) | ||
| 第227行: | 第218行: | ||
return p | return p | ||
2026年10月19日 (一) 22:52的最新版本
此模块的文档可以在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 里的 "0" / "3" 键解码后可能是整数键,两种都试。
local function keyed(t, k)
if type(t) ~= "table" then return nil end
local v = t[k]
if v ~= nil then return v end
local n = tonumber(k)
if n ~= nil then
v = t[n]
if v ~= nil then return v end
end
return t[tostring(k)]
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
local hit = keyed(idx, a) or keyed(idx, a:lower())
if hit and fetch(hit) then return hit end
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 = keyed(d.mods, key)
-- 单键 {"0": ...} 会被 FormatJson 重排成 [1]
if not s and tonumber(key) == 0 then s = keyed(d.mods, 1) end
if not s then return nil end
local out = {}
-- 代理表没有可靠的 # 长度;逐项拷贝成普通数组,供 JSON 编码和 JS 使用。
local function copy_array(arr)
local copy = {}
if type(arr) ~= "table" then return copy end
local i = 1
while arr[i] ~= nil do
copy[i] = arr[i]
i = i + 1
end
return copy
end
for _, stat in ipairs(STATS) do out[stat] = copy_array(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 = keyed(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)
-- MediaWiki 的 Sanitizer 会转义 button/input/label/output。
-- 这里只输出白名单内的容器和隐藏 JSON;控件全部由 Widget JS 创建。
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")
root:tag("pre"):addClass("gfl-data"):wikitext(json)
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