Module:GFLCalc:修订间差异
来自OGAS数据中枢
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第38行: | 第38行: | ||
end | end | ||
-- | -- 索引页可选:缺失或内容模型不是 JSON 时,退化为「别名即代码」 | ||
local function index() | local function index() | ||
local ok, idx = pcall(mw.loadData, 'Module:GFLCalc/index') | local ok, idx = pcall(mw.loadData, 'Module:GFLCalc/index') | ||
if ok and type(idx) == 'table' then return idx end | if ok and type(idx) == 'table' then return idx end | ||
return nil | |||
end | |||
-- 读取数据页:优先 mw.loadData;失败时直接读页面文本并解析 JSON | |||
local function fetch(page, code) | |||
local title = 'Module:GFLCalc/' .. page .. '/' .. code | |||
local ok, data = pcall(mw.loadData, title) | |||
if ok and type(data) == 'table' then return data 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", "") -- 去掉 BOM | |||
local ok2, parsed = pcall(mw.text.jsonDecode, text) | |||
if ok2 and type(parsed) == 'table' then return parsed end | |||
return nil | return nil | ||
end | end | ||
local function exists(page, code) | local function exists(page, code) | ||
return ( | return fetch(page, code) ~= nil | ||
end | end | ||
| 第60行: | 第77行: | ||
end | end | ||
-- 2) | -- 2) 索引里没有(或索引缺失/过期):直接把别名当代码试 | ||
local tries = { a, (a:gsub("%s+", "")), (a:gsub("^%l", string.upper)) } | local tries = { a, (a:gsub("%s+", "")), (a:gsub("^%l", string.upper)) } | ||
for _, c in ipairs(tries) do | for _, c in ipairs(tries) do | ||
| 第69行: | 第86行: | ||
local function load(page, code) | local function load(page, code) | ||
return fetch(page, code) | |||
end | end | ||
| 第184行: | 第199行: | ||
res = tonumber(a.res), adv = tonumber(a.adv), shape = tonumber(a.shape), | res = tonumber(a.res), adv = tonumber(a.adv), shape = tonumber(a.shape), | ||
}) | }) | ||
if v == nil then return | if v == nil then | ||
return p.explain(a[1]) | |||
end | |||
return tostring(v) | 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 | |||
if not (idx[a] or idx[a:lower()]) then | |||
return '<span class="gfl-error">GFLCalc:未知单位「' .. a .. '」</span>' | |||
end | |||
return ERR | |||
end | end | ||
2026年10月19日 (一) 22:11的版本
此模块的文档可以在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
-- 差值字符解码:编码表是 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 时,退化为「别名即代码」
local function index()
local ok, idx = pcall(mw.loadData, 'Module:GFLCalc/index')
if ok and type(idx) == 'table' then return idx end
return nil
end
-- 读取数据页:优先 mw.loadData;失败时直接读页面文本并解析 JSON
local function fetch(page, code)
local title = 'Module:GFLCalc/' .. page .. '/' .. code
local ok, data = pcall(mw.loadData, title)
if ok and type(data) == 'table' then return data 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", "") -- 去掉 BOM
local ok2, parsed = pcall(mw.text.jsonDecode, text)
if ok2 and type(parsed) == 'table' then return parsed end
return nil
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 = idx[a] or idx[a:lower()]
if 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 = d.mods[mod] or 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 or level > #arr then return nil end
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 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
if not (idx[a] or idx[a:lower()]) then
return '<span class="gfl-error">GFLCalc:未知单位「' .. a .. '」</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 = mw.loadData('Module:GFLCalc/index')
return tostring(idx['__dv__'] or '')
end
return p