模块:测试调用数据

可在模块:测试调用数据/doc创建此模块的帮助文档

local p = {}
local db = mw.loadData('模块:角色属性数据')
local dataList = db.char_self_data
local elements = {"HP", "SP", "ATK", "DEF", "MAG", "MDEF", "CRT", "SPD"}

p["测试调用"] = function(frame)
    local query_num = frame.args['角色编号']
    local html = mw.html.create()

    -- 开始表格
    html:tag('table')
    :addClass('wikitable')
    :css('width', '100%')

    -- 表头
    html:tag('tr')
    :tag('th'):wikitext('NAME'):done()
    :tag('th'):wikitext('Base'):done()
    :tag('th'):wikitext('MAX'):done()
    :done()

    if query_num then
        for _, char in ipairs(dataList) do
            if query_num == char.owner_order then
                for _, element in ipairs(elements) do
                    local maxAttr = "MAX" .. element
                    local baseAttr = "Base" .. element

                    -- 获取属性值,如果不存在则设为空白字符串
                    local maxAttrValue = char.Data_jp[maxAttr] or ""
                    local baseAttrValue = char.Data_jp[baseAttr] or ""

                    -- 输出每行并追加到html对象
                    html:tag('tr')
                    :tag('td'):wikitext(element):done()
                    :tag('td'):wikitext(baseAttrValue):done()
                    :tag('td'):wikitext(maxAttrValue):done()
                end
            end
        end
    end

    -- 结束表格
    html:done()

    return tostring(html)
end

return p