bugfix250107.1
全站通知:

模块:周边

来自恋与深空WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:周边/doc创建

-- 这是mediawiki的lua模块
local p = {}
local getArgs = require('Module:Arguments').getArgs

-- 官方周边html
function p.main(frame)
    -- 参数获取
    local args = getArgs(frame)
    local goodsSeries = args["系列"]
    local goodsTheme = args["主题"]
    local goodsName = args["周边名字"]
    local defaultPrize = args["价格"]
    local goodsImg = args["图示"]
    local goodsStyle = args["款式"]
    local goodsType = args["类型"]

    -- {{imgKey="汇总", imgVal={"沈星回","祁煜"}}, {imgKey="沈星回", imgVal={"沈星回"}}, {imgKey="祁煜", imgVal={"祁煜"}}}
    -- 

    -- 参数处理  
    local parsedGoodsImg = parseGoodsImg(goodsImg)
    local parsedGoodsStyle = {}
    if goodsStyle then
        parsedGoodsStyle = parseGoodsStyle(goodsStyle, defaultPrize)
        local idx = #parsedGoodsImg

        for i, item in ipairs(parsedGoodsStyle) do
            if item.styleImg then
                local found = false
                for j, imgItem in ipairs(parsedGoodsImg) do
                    if item.styleImg == imgItem.imgKey then
                        item.styleIndex = j
                        imgItem.stylePrize = item.stylePrize
                        found = true
                        break
                    end
                end
                if not found then
                    idx = idx + 1
                    item.styleIndex = idx
                end
            else
                idx = idx + 1
                item.styleIndex = idx
            end
        end
    end    

    -- 左侧面板和右侧面板    
    local pageLeft = mw.html.create('div'):addClass("pageLeft col-md-7")
    local pageRight = mw.html.create('div'):addClass("pageRight col-md-5")
    local pageLTop = mw.html.create('div'):addClass("merchandise-Left")
    local pageLBottom = mw.html.create('div'):addClass("merchandise-Left-Bottom hidden-xs hidden-sm")
    local pageRTop = mw.html.create('div'):addClass("merchandise-Right"):tag('div'):addClass("merchandise-name")
        :wikitext(goodsName):done()
    local pageRBottom = mw.html.create('div'):addClass("merchandise-Right-Bottom")

    -- 左侧选项和左侧内容
    local tabNavL = mw.html.create('ul'):addClass('tab-nav')
    local tabContentL = mw.html.create('div'):addClass('tab-content')
    local tabNavR = mw.html.create('ul'):addClass('tab-nav')
    local tabContentR = mw.html.create('div'):addClass('tab-content')

    -- 生成左侧的tab按钮和tab内容
    for i, item in ipairs(parsedGoodsImg) do
        -- 左按钮和左大图
        local imgBtn = '[[file:' .. goodsName .. '-' .. item.imgKey .. '.png|link=|x80px]]'
        local imgPane = '[[file:' .. goodsName .. '-' .. item.imgKey .. '.png|x600px]]'
        tabNavL:node(tabBtn(i, imgBtn))
        tabContentL:node(tabPane(i, imgPane))
        -- 右价格
        if item.stylePrize then
            tabContentR:node(prizePane(i, item.stylePrize))
        else
            tabContentR:node(prizePane(i, defaultPrize))
        end
    end

    -- 生成右侧的tab按钮和tab内容
    for i, item in ipairs(parsedGoodsStyle) do
        if item.styleIndex > #parsedGoodsImg then
            -- 有图加图
            if item.styleImg and item.styleImg ~= '' and item.styleImg ~= nil then
                local imgPane = '[[file:' .. goodsName .. '-' .. item.styleImg .. '.png|x600px]]'
                tabContentL:node(tabPane(item.styleIndex, imgPane))
            end
            -- 价格
            tabContentR:node(prizePane(item.styleIndex, item.stylePrize))
        end

        -- 款式
        tabNavR:node(tabBtn(item.styleIndex, item.styleKey))
    end

    -- 往左侧面板增加详情图
    pageLBottom:wikitext("<br/>")
    for i = 1, 20 do
        local imgPath = goodsName .. '-详情' .. i .. '.png'
        if mw.title.new('File:' .. imgPath).exists then
            pageLBottom:wikitext('[[file:' .. imgPath .. '|680px]]')
        else
            break
        end
    end

    -- SMW
    frame:preprocess('{{#set:类型=' .. goodsType .. '}}')
    frame:preprocess('{{#set:系列=' .. goodsSeries .. '}}')
    frame:preprocess('{{#set:主题=' .. goodsTheme .. '}}')
    frame:preprocess('{{#set:周边名字=' .. goodsName .. '}}')
    frame:preprocess('{{#set:价格=' .. defaultPrize .. '}}') 

    for i, item in ipairs(parsedGoodsImg) do
    	frame:preprocess('{{#set:图示=' .. item.imgKey .. '}}')
        for _, character in ipairs(item.imgVal) do
        	local he = {'沈星回','黎深','祁煜','秦彻','夏以昼'}
            if not mw.ustring.match(table.concat(he, ','), character) then
                character = '其他'
            end

            frame:preprocess('{{#set:角色=' .. character .. '}}') 
            frame:preprocess('{{#set:' .. character .. '=' .. item.imgKey .. '}}')
        end
    end     

    -- local debugInfo = mw.html.create('pre'):addClass("col-md-12"):wikitext(mw.text.jsonEncode(characters))

    pageLTop:node(tabNavL):node(tabContentL)
    pageRTop:node(tabContentR):node(tabNavR)

    pageLeft:node(pageLTop):node(pageLBottom)
    pageRight:node(pageRTop):node(pageRBottom)

    return tostring(mw.html.create('div'):addClass("merchandise"):node(pageLeft):node(pageRight):node(debugInfo))
    -- return tostring(pageLeft..pageRight)
end

function p.goodsMain(frame)
    local name = frame.args[1] --图示
    local pre = frame.args[2]  --图片前缀
    local type = frame.args[3] --类型:btn,content
    local single = frame.args['单人'] --单人角色
    
    local defaultClass,dom,imgSize,link
    if type == 'btn' then
        defaultClass = 'd-tab-title btn'
        dom = 'span'
        imgSize = '35px'
        link = '|link='
    else
        defaultClass = 'tab-content content'
        dom = 'div'
        imgSize = '300px'
        link= ''
    end

    local nameArr = mw.text.split(name, ',')
    local returnStr = ''
    local showSummary = not (single and #nameArr > 1)
    local debugInfo = mw.html.create('div')
    local active = true
    for i, item in ipairs(nameArr) do
        item = mw.text.trim(item)
        local class = defaultClass
        if showSummary or item ~= "汇总" then
            if active then
                class = defaultClass .. ' active'
                active = false
            elseif type == 'content' then
                class = defaultClass .. ' hidden'
            end
            local text = '[[file:' .. pre .. item .. '.png|' .. imgSize .. link .. ']]'
            returnStr = returnStr .. tostring(mw.html.create(dom):addClass(class):wikitext(text))
        end
    end

    return returnStr
end

function p.test(frame)
    local imgPath = '金币2.png'
    local title = mw.title.new('File:' .. imgPath)
    local exists = title and title.exists

    if exists then
        return ("文件存在")
    else
        return ("文件不存在")
    end
end

-- 处理参数:款式
function parseGoodsStyle(goodsStyle, defaultPrize)
    local result = {}

    if not goodsStyle or goodsStyle == "" then
        return result
    end

    local items = mw.text.split(goodsStyle, ',')

    for _, item in ipairs(items) do
        local parts = mw.text.split(item, '-')
        local styleKey = parts[1]
        local styleImg = (#parts >= 2) and parts[2] or nil
        local stylePrize = (#parts >= 3) and parts[3] or defaultPrize
        table.insert(result, {
            styleKey = styleKey,
            styleImg = styleImg,
            stylePrize = stylePrize
        })
    end

    return result

end

-- 处理参数:图示
function parseGoodsImg(goodsImg)
    local result = {}

    if not goodsImg or goodsImg == "" then
        return result
    end

    local items = mw.text.split(goodsImg, ',')

    for _, item in ipairs(items) do
        local parts = mw.text.split(item, '-')
        if #parts == 2 then
            local imgKey = parts[1]
            local imgVal = mw.text.split(parts[2], '+')
            table.insert(result, {
                imgKey = imgKey,
                imgVal = imgVal
            })
        else
            local imgKey = item
            table.insert(result, {
                imgKey = imgKey,
                imgVal = {}
            })
        end
    end

    -- 填充汇总的值
    for _, item in ipairs(result) do
        if item.imgKey == "汇总" and #item.imgVal == 0 then
            for _, otherItem in ipairs(result) do
                if otherItem.imgKey ~= "汇总" then
                    table.insert(item.imgVal, otherItem.imgKey)
                end
            end
        end
    end

    -- 填充其他项的值
    for _, item in ipairs(result) do
        if item.imgKey ~= "汇总" and #item.imgVal == 0 then
            table.insert(item.imgVal, item.imgKey)
        end
    end

    return result
end

-- 返回tab-btn的Dom元素
function tabBtn(index, content)

    local className = 'tab-btn'
    if index == 1 then
        className = className .. ' active'
    end

    local tBtn = mw.html.create('li'):addClass(className):tag('div'):attr('data-target',
        '#merchandise-item-切换' .. index):attr('data-toggle', 'tab'):attr('aria-expanded', 'false'):wikitext(content)
        :done():done()

    return tBtn
end

-- 返回tab-pane的Dom元素
function tabPane(index, content)
    local className = 'tab-pane'
    if index == 1 then
        className = className .. ' active'
    end

    local tPane = mw.html.create('div'):addClass(className):attr('id', 'merchandise-item-切换' .. index):wikitext(
        content)
    return tPane
end

-- 返回价格的Dom元素
function prizePane(index, prize)
    local className = 'tab-pane merchandise-price'
    if index == 1 then
        className = className .. ' active'
    end

    local pPane = mw.html.create('div'):addClass(className):attr('id', 'merchandise-item-切换' .. index)

    if prize == '免费' then
        pPane:wikitext(prize)
    else
        pPane:tag('span'):attr('id', 'price-pre'):wikitext('¥'):done():wikitext(prize)
    end

    return pPane
end

return p

-- function p.html(frame)
-- 	local goodsSeries = frame.args["系列"]
-- 	local goodsTheme = frame.args["主题"]
-- 	local goodsName = frame.args["周边名字"]
-- 	local defaultPrize = frame.args["价格"]
-- 	local goodsImg = frame.args["图示"]
-- 	local goodsStyle = frame.args["款式"]
-- 	local goodsType = frame.args["类型"]
-- 	return goodsStyle
-- end