模块:ItemInfo

来自歌颂之丘
Xiyan讨论 | 贡献2024年6月8日 (六) 15:03的版本 (修正错误偏移)
跳转到导航 跳转到搜索

可在模块:ItemInfo/doc创建此模块的帮助文档

local p = {}

local be = require 'Module:BattleEffect'
local ci = require 'Module:CardInfo'

function p.loadItemInfoByIndex(game, category, index)
    -- 获取索引(综合数据)
    local matIdx = mw.loadJsonData('数据:' .. game .. '/' .. category .. '/索引')
    local itemBase = matIdx[index]
    local status, data = pcall(mw.loadJsonData, '数据:' .. game .. '/' .. category .. '/' .. index)
    if status then
        return table.append(itemBase, data)
    else
        return itemBase
    end
end

-- 根据物品Id获取物品信息
function p.loadItemById(game, id)
    id = tonumber(id)
    if game == 'AT1' then
        -- id 区间检查

        if id >= 226 and id <= 226 + 339 then
            -- 葛拉斯诺晶体
            return p.loadItemInfoByIndex(game, '葛拉斯诺晶体', id - 225)
        end
        return nil, '获取数据失败,无效物品ID: ' .. id
    elseif game == 'AT2' then
    elseif game == 'AT3' then
    end
    return nil, '获取数据失败,所指定的游戏不存在'
end

local function getNameById(game, id)
    local item, err = p.loadItemById(game, id)
    if err then return err end
    return item.name
end

local function recryFilter(recry)
    local game = 'AT1'
    local ret = {}
    if recry.s_prod == '-1' then
        ret.s_prod = nil
    else
        ret.s_prod = getNameById(game, recry.s_prod)
    end
    if recry.a_prod == '-1' then
        ret.a_prod = nil
    else
        ret.a_prod = getNameById(game, recry.a_prod)
    end
    if recry.b_prod == '-1' then
        ret.b_prod = nil
    else
        ret.b_prod = getNameById(game, recry.b_prod)
    end
    if recry.c_prod == '-1' then
        ret.c_prod = nil
    else
        ret.c_prod = getNameById(game, recry.c_prod)
    end
    return ret
end

-- 生成材料物品基本信息的方法,使用模板“物品信息”
function p.at1MaterialItemInfo(index)
    local item = p.loadItemInfoByIndex('AT1', '材料物品', index)

    local filteredRecrystalisation = recryFilter(item.recry)

    local arguments = {
        ["物品名"] = item.name,
        ["图像"] = item.img,
        ["原文"] = item.ori,
        ["类别"] = '材料',
        ["子类别"] = '采集',
        ["卖价"] = item.base.sell_price,
        ["游戏"] = 'AT1',     
        -- 重结晶S=filteredRecrystalisation.s_prod,
        -- 重结晶A=filteredRecrystalisation.a_prod,
        -- 重结晶B=filteredRecrystalisation.b_prod,
        ["重结晶C"] = filteredRecrystalisation.c_prod,
        ["其他来源"] = item.alt_src,
        ["笔记"] = item.desc
    }
    if item.src ~= nil then
        for i = 1, 5 do
            if not item.src[i] then
                arguments['来源' .. i .. '/类别'] = item.src[i].type
                arguments['来源' .. i .. '/详细'] = item.src[i].desc
            end
        end
    end 
    if item.item_use_this ~= nil then
        for i = 1, 8 do
            arguments['合成用途' .. i] = item.item_use_this[i]
        end
    end 

    return mw.getCurrentFrame():expandTemplate {
        title = '物品信息',
        args = arguments
    }
end

function p.listMaterialsInAt1()
    result = {}
    for i = 1, 34 do
        table.insert(result, p.at1MaterialItemInfo(i))
    end
    return table.concat(result, '\n')
end

p.gcType = {'#b70000', '#0d57cb', '#139a00', '#9f0087'}

local paramNames = {'hp', 'mp', 'atk', 'crit', 'def', 'agi', 'fdef', 'idef', 'tdef', 'wdef', 'fatk', 'iatk', 'tatk', 'watk', 'dmg_max', 'burst_spd', 'gd_resist', 'harmonics'}
local paramDisplay = {'HP', 'MP', '攻击力', '暴击率', '防御力', '敏捷', '炎抗性', '冰抗性', '雷抗性', '风抗性', '炎攻击', '冰攻击', '雷攻击', '风攻击', '附加伤害', '爆发速率', 'GD抗性', '谐调强化'}
local function checkAndMakeParamList(item)
    result = {}
    for i, v in ipairs(paramNames) do
        if item[v] ~= '0' then
            table.insert(result, '* ' .. paramDisplay[i] .. ':' .. item[v])
        end

    end
    return table.concat(result, '\n')
end

function p.generateGrathnodeCrystalList()
    local result = {}
    for i = 1, 339 do
        local item = p.loadItemInfoByIndex('AT1', '葛拉斯诺晶体', i)

        local type = tonumber(item.type)
        -- 之后要替换为图片及标题的::before元素,提供更好的视觉效果
        local icon = '<span style="background-color: ' .. p.gcType[type + 1] .. '; padding: 0 .25em; color: white">' .. item.level .. '</span>'
        local section = '==' .. item.name .. '==\n' .. item.desc
        local html = mw.html.create('table'):addClass('wikitable')

        html:tag('tr')
            :tag('th'):wikitext('类型'):done()
            :tag('th'):wikitext('品质'):done()
            :tag('th'):wikitext('GD'):done()
            :tag('th'):wikitext('价格'):done()
            :tag('th'):wikitext('稀有度'):done()
            :tag('th'):wikitext(be.getBattleEffectById('AT1', item.eff_key)):done()
        
        html:tag('tr')
            :tag('th'):wikitext(icon):done()
            :tag('td'):wikitext(item.quality):done()
            :tag('td'):wikitext(item.gd):done()
            :tag('td'):wikitext(string.sub(item.price, 0, -2)):done()
            :tag('td'):wikitext(item.rarity):done()
            :tag('td'):wikitext(item.eff_val):done()
        
        local list = checkAndMakeParamList(item)

        table.insert(result, section .. tostring(html) .. '\n' .. list)
    end        
    return table.concat(result, '\n')
end

local function makeArgumentOfParameter(args, para)
    for i, v in ipairs(paramNames) do
        args[paramDisplay[i]] = para[v]
    end
end

function p.at1EquipInfo(type, index)
    local item = p.loadItemInfoByIndex('AT1', type, index)
    
    local filteredRecrystalisation = recryFilter(item.recry)

    local arguments = {
        ["物品名"] = item.name,
        ["图像"] = item.img,
        ["原文"] = item.ori,
        ["类别"] = '装备',
        ["子类别"] = type,
        ["卖价"] = item.base.sell_price,
        ["游戏"] = 'AT1',     
        ["重结晶S"] = filteredRecrystalisation.s_prod,
        ["重结晶A"] = filteredRecrystalisation.a_prod,
        ["重结晶B"] = filteredRecrystalisation.b_prod,
        ["重结晶C"] = filteredRecrystalisation.c_prod,
        ["其他来源"] = item.alt_src,
        ["笔记"] = item.desc
    }

    if item.base.synthesisable == '1' then
        arguments["配方"] = ci.getRecipeCardNameById(item.base.rc_id)
    end

    makeArgumentOfParameter(arguments, item.para)

    slots = ''
    for i = 1, 4 do
        if item.powered_slots[i] == '1' then
            slots = slots .. i
        end
    end
    arguments["强化槽位"] = slots

    return mw.getCurrentFrame():expandTemplate {
        title = '物品信息',
        args = arguments
    }
end

function p.listArmorsInAt1()
    result = {}
    for i = 1, 29 do
        table.insert(result, p.at1EquipInfo('防具', i))
    end
    return table.concat(result, '\n')
end

function p.listWeaponsInAt1()
    result = {}
    for i = 1, 35 do
        table.insert(result, p.at1EquipInfo('武器', i))
    end
    return table.concat(result, '\n')
end

function p.listAccessoriesInAt1()
    result = {}
    for i = 1, 20 do
        table.insert(result, p.at1EquipInfo('饰品', i))
    end
    return table.concat(result, '\n')
end

return p