Module:GFLCalc:修订间差异
来自OGAS数据中枢
更多操作
无编辑摘要 |
小 gflcalc: clearer error for missing data pages |
||
| (未显示同一用户的5个中间版本) | |||
| 第1行: | 第1行: | ||
--[[ | --[[ | ||
Module:GFLCalc —— 少女前线 人形 / 铁血 面板数值取数模块 | Module:GFLCalc —— 少女前线 人形 / 铁血 面板数值取数模块 | ||
只做三件事:查数据页、按等级索引、按好感倍率收尾取整。 | |||
没有任何计算公式、没有系数表——数值全部由数据页提供。 | |||
用法(模板里): | 用法(模板里): | ||
| 第29行: | 第32行: | ||
local function ceil9(v) | local function ceil9(v) | ||
return math.ceil(v - 1e-9) | return math.ceil(v - 1e-9) | ||
end | |||
-- JSON 对象里形如 "0" / "3" 的键,经 FormatJson 解码后会变成整数键。 | |||
-- 统一按「原键 → 数字键 → 字符串键」查找,兼容两种解码结果。 | |||
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 | |||
-- FormatJson 会把 {"0": ...} 这种单键对象当成 0 基序列,重排成 [1]。 | |||
-- 有 "0" 和 "3" 两个键时不会重排,所以两种都要兼容。 | |||
local function series_of(mods, mod) | |||
local s = keyed(mods, mod) | |||
if s then return s end | |||
if tonumber(mod) == 0 then | |||
return keyed(mods, 1) | |||
end | |||
return nil | |||
end | end | ||
| 第38行: | 第66行: | ||
end | end | ||
-- | -- 读取 JSON 页。MediaWiki 1.45 的 mw.loadData 走 require, | ||
local | -- 而 require 只接受 Scribunto 模型;JSON 页必须用 mw.loadJsonData。 | ||
-- 后两项是旧版本 / 特殊站点的兼容回退。 | |||
if | local json_cache = {} | ||
end | local function read_json(title) | ||
if json_cache[title] ~= nil then | |||
return json_cache[title] or nil | |||
end | |||
if type(mw.loadJsonData) == 'function' then | |||
local | local ok, data = pcall(mw.loadJsonData, title) | ||
if ok and type(data) == 'table' then | |||
json_cache[title] = data | |||
return data | |||
end | |||
end | |||
local ok, data = pcall(mw.loadData, title) | local ok, data = pcall(mw.loadData, title) | ||
if ok and type(data) == 'table' then return data end | if ok and type(data) == 'table' then | ||
json_cache[title] = data | |||
return data | |||
end | |||
if not mw.title or not mw.title.new then | |||
json_cache[title] = false | |||
return nil | |||
end | |||
local t = mw.title.new(title) | local t = mw.title.new(title) | ||
if not t or not t.exists then return nil end | if not t or not t.exists then | ||
json_cache[title] = false | |||
return nil | |||
end | |||
local text = t:getContent() | local text = t:getContent() | ||
if not text or text == '' then return nil end | if not text or text == '' then | ||
json_cache[title] = false | |||
return nil | |||
end | |||
text = text:gsub("^\239\187\191", "") -- 去掉 BOM | text = text:gsub("^\239\187\191", "") -- 去掉 BOM | ||
local ok2, parsed = pcall(mw.text.jsonDecode, text) | local ok2, parsed = pcall(mw.text.jsonDecode, text) | ||
if ok2 and type(parsed) == 'table' then return parsed end | if ok2 and type(parsed) == 'table' then | ||
json_cache[title] = parsed | |||
return parsed | |||
end | |||
json_cache[title] = false | |||
return nil | |||
end | |||
-- 索引页可选:缺失或内容模型不是 JSON 时,退化为「别名即代码」 | |||
local function index() | |||
local idx = read_json('Module:GFLCalc/index') | |||
if type(idx) == 'table' then return idx end | |||
return nil | return nil | ||
end | |||
-- 读取数据页 | |||
local function fetch(page, code) | |||
return read_json('Module:GFLCalc/' .. page .. '/' .. code) | |||
end | end | ||
| 第73行: | 第137行: | ||
local idx = index() | local idx = index() | ||
if idx then | if idx then | ||
local hit = idx | local hit = keyed(idx, a) or keyed(idx, a:lower()) | ||
if hit then return hit end | -- 索引可能过期:指向的数据页不存在时继续按代码回退。 | ||
if hit and exists("data", hit) then return hit end | |||
end | end | ||
| 第98行: | 第163行: | ||
-- ---------------------------------------------------------------- 人形 | -- ---------------------------------------------------------------- 人形 | ||
local function doll_value(d, stat, level, mod) | local function doll_value(d, stat, level, mod) | ||
local series = d.mods | local series = series_of(d.mods, mod) or keyed(d.mods, '0') | ||
if not series then return nil end | if not series then return nil end | ||
if stat == 'armor' then return series.armor end | if stat == 'armor' then return series.armor end | ||
local arr = series[stat] | local arr = series[stat] | ||
if not arr then return nil end | if not arr then return nil end | ||
if level < 1 | if level < 1 then return nil end | ||
-- mw.loadJsonData 返回只读代理表,Lua 5.1 下 #arr 不可靠,直接取值。 | |||
return arr[level] | return arr[level] | ||
end | end | ||
| 第172行: | 第238行: | ||
local mod = clamp(opts.mod, 0, 3, 0) | local mod = clamp(opts.mod, 0, 3, 0) | ||
-- 数据里只有两套:'0' 未改造、'3' 已改造(MOD1/2/3 共用)。 | -- 数据里只有两套:'0' 未改造、'3' 已改造(MOD1/2/3 共用)。 | ||
local key = (mod >= 1 and d.mods | local key = (mod >= 1 and keyed(d.mods, '3') ~= nil) and '3' or '0' | ||
local lvmax = MOD_MAX[mod] or 100 | local lvmax = MOD_MAX[mod] or 100 | ||
if key == '0' then lvmax = 100 end | if key == '0' then lvmax = 100 end | ||
| 第219行: | 第285行: | ||
return '<span class="gfl-error">GFLCalc:没有填写单位</span>' | return '<span class="gfl-error">GFLCalc:没有填写单位</span>' | ||
end | end | ||
local hit = keyed(idx, a) or keyed(idx, a:lower()) | |||
if not hit then | |||
return '<span class="gfl-error">GFLCalc:未知单位「' .. a .. '」</span>' | return '<span class="gfl-error">GFLCalc:未知单位「' .. a .. '」</span>' | ||
end | |||
if not exists("data", hit) then | |||
return '<span class="gfl-error">GFLCalc:数据页读取失败「' .. tostring(hit) .. '」</span>' | |||
end | end | ||
return ERR | return ERR | ||
| 第250行: | 第320行: | ||
--- 数据版本:{{#invoke:GFLCalc|dv}} | --- 数据版本:{{#invoke:GFLCalc|dv}} | ||
function p.dv() | function p.dv() | ||
local idx = | local idx = index() | ||
if not idx then return ERR end | |||
return tostring(idx['__dv__'] or '') | return tostring(idx['__dv__'] or '') | ||
end | end | ||
return p | return p | ||
2026年10月19日 (一) 22:46的最新版本
此模块的文档可以在Module:GFLCalc/doc创建
--[[
Module:GFLCalc —— 少女前线 人形 / 铁血 面板数值取数模块
只做三件事:查数据页、按等级索引、按好感倍率收尾取整。
没有任何计算公式、没有系数表——数值全部由数据页提供。
用法(模板里):
{{#invoke:GFLCalc|stat|M4A1|100|damage}}
{{#invoke:GFLCalc|stat|M4A1|100|damage|favor=100|mod=3}}
{{#invoke:GFLCalc|stat|Aegis_SWAP|13|accuracy|res=4|adv=2|shape=2|favor=96}}
{{#invoke:GFLCalc|brief|Aegis_SWAP|4|health}} -- 列表页用精简数据
{{#invoke:GFLCalc|num|Aegis_SWAP|4}} -- 解析度对应的编队数
]]
local p = {}
local ERR = '' -- 取不到时返回的内容(可改成 'N/A')
local FAVOR_STATS = { damage = true, accuracy = true, evasion = true }
-- 改造档位的等级上限(MOD1/2/3 共用同一套「改造」数据,只差上限)
local MOD_MAX = { [0] = 100, [1] = 110, [2] = 115, [3] = 120 }
local function favor_ratio(f)
if f < 10 then return 0.95 end
if f < 90 then return 1.00 end
if f < 140 then return 1.05 end
if f < 190 then return 1.10 end
return 1.15
end
local function ceil9(v)
return math.ceil(v - 1e-9)
end
-- JSON 对象里形如 "0" / "3" 的键,经 FormatJson 解码后会变成整数键。
-- 统一按「原键 → 数字键 → 字符串键」查找,兼容两种解码结果。
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
-- FormatJson 会把 {"0": ...} 这种单键对象当成 0 基序列,重排成 [1]。
-- 有 "0" 和 "3" 两个键时不会重排,所以两种都要兼容。
local function series_of(mods, mod)
local s = keyed(mods, mod)
if s then return s end
if tonumber(mod) == 0 then
return keyed(mods, 1)
end
return nil
end
-- 差值字符解码:编码表是 33..126 但跳过 34(") 与 92(\\),避免 JSON 转义
local function delta_of(byte)
if byte < 34 then return byte - 33 end
if byte < 92 then return byte - 34 end
return byte - 35
end
-- 读取 JSON 页。MediaWiki 1.45 的 mw.loadData 走 require,
-- 而 require 只接受 Scribunto 模型;JSON 页必须用 mw.loadJsonData。
-- 后两项是旧版本 / 特殊站点的兼容回退。
local json_cache = {}
local function read_json(title)
if json_cache[title] ~= nil then
return json_cache[title] or nil
end
if type(mw.loadJsonData) == 'function' then
local ok, data = pcall(mw.loadJsonData, title)
if ok and type(data) == 'table' then
json_cache[title] = data
return data
end
end
local ok, data = pcall(mw.loadData, title)
if ok and type(data) == 'table' then
json_cache[title] = data
return data
end
if not mw.title or not mw.title.new then
json_cache[title] = false
return nil
end
local t = mw.title.new(title)
if not t or not t.exists then
json_cache[title] = false
return nil
end
local text = t:getContent()
if not text or text == '' then
json_cache[title] = false
return nil
end
text = text:gsub("^\239\187\191", "") -- 去掉 BOM
local ok2, parsed = pcall(mw.text.jsonDecode, text)
if ok2 and type(parsed) == 'table' then
json_cache[title] = parsed
return parsed
end
json_cache[title] = false
return nil
end
-- 索引页可选:缺失或内容模型不是 JSON 时,退化为「别名即代码」
local function index()
local idx = read_json('Module:GFLCalc/index')
if type(idx) == 'table' then return idx end
return nil
end
-- 读取数据页
local function fetch(page, code)
return read_json('Module:GFLCalc/' .. page .. '/' .. code)
end
local function exists(page, code)
return fetch(page, code) ~= nil
end
local function resolve(alias)
local a = mw.text.trim(tostring(alias or ''))
if a == '' then return nil end
-- 1) 先查索引(支持别名)
local idx = index()
if idx then
local hit = keyed(idx, a) or keyed(idx, a:lower())
-- 索引可能过期:指向的数据页不存在时继续按代码回退。
if hit and exists("data", hit) then return hit end
end
-- 2) 索引里没有(或索引缺失/过期):直接把别名当代码试
local tries = { a, (a:gsub("%s+", "")), (a:gsub("^%l", string.upper)) }
for _, c in ipairs(tries) do
if c ~= "" and exists("data", c) then return c end
end
return nil
end
local function load(page, code)
return fetch(page, code)
end
local function clamp(v, lo, hi, default)
v = tonumber(v) or default
if v < lo then return lo end
if v > hi then return hi end
return v
end
-- ---------------------------------------------------------------- 人形
local function doll_value(d, stat, level, mod)
local series = series_of(d.mods, mod) or keyed(d.mods, '0')
if not series then return nil end
if stat == 'armor' then return series.armor end
local arr = series[stat]
if not arr then return nil end
if level < 1 then return nil end
-- mw.loadJsonData 返回只读代理表,Lua 5.1 下 #arr 不可靠,直接取值。
return arr[level]
end
-- ---------------------------------------------------------------- 铁血
local function sv_value(d, stat, level, res, adv, shape)
local blk = d.s[stat]
if not blk then return nil end
shape = (shape == 0) and 1 or shape -- 形态 0 与 1 同值
local idx = res * 25 + (adv - 1) * 5 + (shape - 1) + 1
local v = blk.b[idx]
if not v then return nil end
local off = (idx - 1) * 99
for i = 1, level - 1 do
v = v + delta_of(string.byte(blk.d, off + i))
end
return v
end
-- ---------------------------------------------------------------- 精简页
local DOLL_BRIEF_IDX = { hp = 1, damage = 2, accuracy = 3, evasion = 4, rof = 5, armor = 6 }
local SV_BRIEF_IDX = { health = 1, damage = 2, rof = 3, accuracy = 4, evasion = 5 }
local function brief_value(d, stat, level, opts)
if d.k == 'doll' then
local mod = clamp(opts.mod, 0, 3, 0)
local key = (mod >= 1 and d.v['3|1'] ~= nil) and '3' or '0'
local lvmax = (key == '0') and 100 or (MOD_MAX[mod] or 100)
level = clamp(level, 1, lvmax, 1)
local arr = d.v[key .. '|' .. tostring(level)]
if not arr then return nil end
local i = DOLL_BRIEF_IDX[stat]
return i and arr[i] or nil
end
local tier = d.tier or 1
local defadv = (tier == 1) and 3 or (tier == 2 and 2 or 1)
local res = clamp(opts.res, 0, 4, 0)
local adv = clamp(opts.adv, 1, 5, defadv)
local shape = clamp(opts.shape, 1, 5, 1)
local arr = d.v[res .. '|' .. adv .. '|' .. shape .. '|' .. tostring(level)]
if not arr then
arr = d.v[res .. '|' .. defadv .. '|1|' .. tostring(level)] -- 回落到默认状态
end
if not arr then return nil end
local i = SV_BRIEF_IDX[stat]
return i and arr[i] or nil
end
-- ---------------------------------------------------------------- 核心
--- 供其他 Lua 模块直接调用
-- @param alias 代码/英文名
-- @param level 等级
-- @param stat 属性(hp/damage/accuracy/evasion/rof/armor)
-- @param opts { favor=, mod=, res=, adv=, shape=, brief= }
function p.value(alias, level, stat, opts)
opts = opts or {}
local code = resolve(alias)
if not code then return nil end
stat = tostring(stat or 'hp'):lower()
local d = load(opts.brief and 'brief' or 'data', code)
if not d then return nil end
local v
if opts.brief then
v = brief_value(d, stat, clamp(level, 1, 120, 1), opts)
elseif d.k == 'doll' then
local mod = clamp(opts.mod, 0, 3, 0)
-- 数据里只有两套:'0' 未改造、'3' 已改造(MOD1/2/3 共用)。
local key = (mod >= 1 and keyed(d.mods, '3') ~= nil) and '3' or '0'
local lvmax = MOD_MAX[mod] or 100
if key == '0' then lvmax = 100 end
v = doll_value(d, stat, clamp(level, 1, lvmax, 1), key)
else
local tier = d.tier or 1
local defadv = (tier == 1) and 3 or (tier == 2 and 2 or 1)
v = sv_value(d, stat, clamp(level, 1, 100, 1),
clamp(opts.res, 0, 4, 0),
clamp(opts.adv, 1, 5, defadv),
clamp(opts.shape, 1, 5, 1))
end
if v == nil then return nil end
if FAVOR_STATS[stat] then
v = ceil9(v * favor_ratio(tonumber(opts.favor) or 50))
end
return v
end
--- {{#invoke:GFLCalc|stat|别名|等级|属性|favor=…|mod=…|res=…|adv=…|shape=…}}
function p.stat(frame)
local a = frame.args
local v = p.value(a[1], tonumber(a[2]) or 1, a[3], {
favor = tonumber(a.favor), mod = tonumber(a.mod),
res = tonumber(a.res), adv = tonumber(a.adv), shape = tonumber(a.shape),
})
if v == nil then
return p.explain(a[1])
end
return tostring(v)
end
--- 出错时给出可读原因(缺索引 / 缺数据页 / 别名无效)
function p.explain(alias)
local a = mw.text.trim(tostring(alias or ''))
local idx = index()
if not idx then
if a ~= '' and exists("data", a) then
return tostring(p.value(a, 1, 'hp') or ERR)
end
return '<span class="gfl-error">GFLCalc:读不到 Module:GFLCalc/index'
.. '(页面不存在,或内容模型不是 JSON)</span>'
end
if a == '' then
return '<span class="gfl-error">GFLCalc:没有填写单位</span>'
end
local hit = keyed(idx, a) or keyed(idx, a:lower())
if not hit then
return '<span class="gfl-error">GFLCalc:未知单位「' .. a .. '」</span>'
end
if not exists("data", hit) then
return '<span class="gfl-error">GFLCalc:数据页读取失败「' .. tostring(hit) .. '」</span>'
end
return ERR
end
--- 精简数据:{{#invoke:GFLCalc|brief|别名|等级|属性|…}}
function p.brief(frame)
local a = frame.args
local v = p.value(a[1], tonumber(a[2]) or 1, a[3], {
favor = tonumber(a.favor), mod = tonumber(a.mod),
res = tonumber(a.res), adv = tonumber(a.adv), shape = tonumber(a.shape),
brief = true,
})
if v == nil then return ERR end
return tostring(v)
end
--- 解析度对应的编队数:{{#invoke:GFLCalc|num|Aegis_SWAP|4}}
function p.num(frame)
local a = frame.args
local code = resolve(a[1])
if not code then return ERR end
local d = load('data', code)
if not d or not d.num then return ERR end
local res = clamp(a[2], 0, 4, 0)
return tostring(d.num[res + 1])
end
--- 数据版本:{{#invoke:GFLCalc|dv}}
function p.dv()
local idx = index()
if not idx then return ERR end
return tostring(idx['__dv__'] or '')
end
return p