Module:图鉴2

来自植物大战僵尸百科
跳转到导航 跳转到搜索
{{🛈}}模組文檔[查看] [編輯] [歷史] [清除缓存]此文档嵌入自Module:图鉴2/文档

本模組用於{{植物圖鑑2}}和{{殭屍圖鑑2}},用法見其文檔。

模組內容

almanac

本模組的返回值。

almanac.plant(frame)
從框架對象frame接收參數並傳遞至almanac.main,返回其結果。
almanac.zombie(frame)
從框架對象frame接收參數並傳遞至almanac.main,返回其結果。
almanac.main(subtype, args)
接收圖鑑類型subtype和參數表args,返回圖鑑結果。

_almanac

非供外部調用的內容。
local almanac = {} -- 模組內容
local _almanac = {} -- 過程用內容

local getArgs = require('Dev:Arguments').getArgs
local ts = require('Module:参数').loadData('Module:图鉴2/参数')

local function invoke(subtype, ...)
    local wrappers = { ... }
    return function(frame)
        local args = getArgs(frame, wrappers)
        return almanac.main(subtype, args)
    end
end

almanac.plant = invoke('plant', 'Template:植物图鉴2')

almanac.zombie = invoke('zombie', 'Template:僵尸图鉴2')

function almanac.main(subtype, args)
    local almanacWrapper = mw.html.create('div'):addClass('almanac2 almanac2-' .. subtype) -- 外殼,已加入類別
    local index = { __index = almanacWrapper, __tostring = getmetatable(almanacWrapper).__tostring }
    local titleText = ts:parameter('標題', args) or '' -- 標題文字
    local notReleased = ts:parameter('測試', args) -- 測試

    function almanacWrapper:title() -- 加入標題
        local title = self:tag('div'):addClass('almanac2-title'):cssText(ts:parameter('樣式', args)):wikitext(titleText)
        if string.len(titleText) > 24 then -- 標題過長,用於行動版樣式(無行內)
            title:addClass('almanac2-title--long')
        end
        return self
    end

    function almanacWrapper:image() -- 加入圖片,返回圖片部分
        local image = setmetatable(self:tag('div'):addClass('almanac2-image'), index)
        if notReleased then -- 測試內容不加入實際圖片內容
            image.noImage = true
            return image
        end

        local imageName = ts:parameter('圖片', args) or titleText .. (args['再'] and '2' or '') .. '图鉴.png'
        local imageTitle = mw.title.new(imageName, 'File')
        local imageText
        if imageTitle and imageTitle.file.exists then
            imageText = '[[' .. imageTitle.fullText .. ']]'
        else
            image:addClass('almanac2-image--broken')
            imageText = '[' .. tostring(mw.uri.fullUrl('Special:Upload', 'wpDestFile=' .. (imageTitle and imageTitle.text or imageName))) .. ' 上传图片]'
            image.noImage = true
        end
        return image:wikitext(imageText)
    end

    function almanacWrapper:level()
        if self.noImage or ts:parameter('無級', args) then
            return self
        end
        self:tag('div'):addClass('almanac2-level'):wikitext('1級')
        return self
    end

    function almanacWrapper:findMore() -- 加入「尋找更多」
        local findMoreType = args['更多']
        if ts:compare('無', findMoreType or '') then
            return self
        end
        local findMore = self:tag('div'):addClass('almanac2-button almanac2-find-more mobile-hidden'):wikitext('寻找更多')
        if findMoreType then
            findMore:tag('div'):addClass('almanac2-find-more--tooltip')
                :wikitext(ts:msg(findMoreType, titleText) or ts:msg('世界', titleText, findMoreType))
        else
            findMore:addClass('almanac2-find-more--broken')
        end
        return self
    end

    function almanacWrapper:mainWrapper() -- 加入主要部分,返回主要部分
        return setmetatable(self:tag('div'):addClass('almanac2-main'), index)
    end

    function almanacWrapper:descriptionWrapper() -- 加入描述部分,返回描述部分
        return setmetatable(self:tag('div'):addClass('almanac2-description'), index)
    end

    function almanacWrapper:stats(data, messages) -- 加入資料部分
        local stats = self:tag('div'):addClass('almanac2-data')
        local i = 1
        local isFamilySep -- 家族是切分下來的,用於無腳本樣式
        while data[i] do
            local line = stats:tag('div'):addClass('almanac2-data--line')
            if data[i] == '家族' or data[i + 1] == '家族' then
                line:addClass('almanac2-data--family-line')
                if isFamilySep then
                    line:addClass('almanac2-data--family-line--separated')
                end
                local item = line:tag('div'):addClass('almanac2-data--item'):addClass('almanac2-data--family')
                item:tag('div'):addClass('almanac2-data--image'):wikitext('[[File:家族.png|36px|link=]]')
                item:tag('div'):addClass('almanac2-data--label'):wikitext('家族')
                item:tag('div'):addClass('almanac2-data--data'):wikitext(data[i + 2])
                break
            end
            for _ = 1, 2 do
                local imageName = data[i]
                local label = data[i + 1]
                if imageName == '家族' or label == '家族' then
                    isFamilySep = true
                    break
                end
                if label == '' then
                    label = imageName
                elseif imageName == '' then
                    imageName = label
                end
                imageName = ts:msg({ key = messages, sub = imageName }) or imageName .. '.png'
                local item = line:tag('div'):addClass('almanac2-data--item')
                item:tag('div'):addClass('almanac2-data--image'):wikitext('[[File:' .. imageName .. '|36px|link=]]')
                item:tag('div'):addClass('almanac2-data--label'):wikitext(label)
                item:tag('div'):addClass('almanac2-data--data'):wikitext(data[i + 2])
                i = i + 3
                if not data[i] then
                    break
                end
            end
        end
        return self
    end

    function almanacWrapper:description() -- 描述部分內容
        self.typeName = mw.loadData('Module:二代类别名称对照')[titleText]
        self.original = mw.loadData('Module:二代图鉴原文')[self.typeName] or { '空' }
        if type(self.original) ~= 'table' then
            self.original = { '空' }
        end

        local header = ts:parameter('簡介', args)
        local description = ts:parameter('說明', args)
        if not header and not description and not args[1] then
            self.notTranslated = true
        end

        header = header or self.original.header and mw.text.tag('span', { lang = 'en' }, self.original.header)
        self:tag('div'):addClass('almanac2-description--header'):wikitext(header)

        if args[1] then
            self:keywords(args, ':')
        elseif self.original.keywords then
            self:keywords(self.original.keywords, ': '):attr('lang', 'en')
        end

        description = description or self.original.description and mw.text.tag('span', { lang = 'en' }, self.original.description)
        self:tag('div'):addClass('almanac2-description--flavor'):wikitext(description)

        return self
    end

    function almanacWrapper:originalDescription() -- 原文,返回內容爲整個結果
        if self.notTranslated or self.original and self.original[1] then
            return self:allDone()
        end
        local originalWrapper = mw.html.create('div'):addClass('almanac2-original mw-collapsible mw-collapsed')
        originalWrapper:node(mw.text.tag('div', {class='almanac2-original--caption'}, '英文原文'))

        local original = setmetatable(originalWrapper:tag('div'):addClass('mw-collapsible-content'):attr('lang', 'en'), index)
        original:tag('div'):addClass('almanac2-description--header'):wikitext(self.original.header)
        if self.original.keywords then
            original:keywords(self.original.keywords, ': ')
        end
        original:tag('div'):addClass('almanac2-description--flavor'):wikitext(self.original.description)
        return self:wrapResult(originalWrapper)
    end

    function almanacWrapper:keywords(keywordsTable, colon) -- 關鍵字部分,返回關鍵字部分
        local keywords = self:tag('div'):addClass('almanac2-description--keywords')
        local i = 1
        while keywordsTable[i] do
            if i ~= 1 then
                keywords:tag('br')
            end
            keywords
                :tag('span'):addClass('almanac2-description--keyword'):wikitext(keywordsTable[i], colon):done()
                :tag('span'):addClass('almanac2-description--stat'):wikitext(keywordsTable[i + 1] or '')
            i = i + 2
        end
        return keywords
    end

    function almanacWrapper:wrapResult(originalWrapper)
        return setmetatable({ almanacWrapper, originalWrapper, type = 'almanacResult' }, { __tostring = self.resultTostring })
    end

    function almanacWrapper.resultTostring(result)
        if type(result) ~= 'table' or result.type ~= 'almanacResult' then
            error('almanacWrapper.resultTostring:錯誤的類型,輸入應爲almanacWrapper:originalDescription返回的內容')
        end
        return tostring(result[1]) .. tostring(result[2])
    end

    return _almanac[subtype](almanacWrapper, args)
end

function _almanac.plant(almanacWrapper, args)
    local function preprocessData()
        local data = ts:parameter('資料', args) or ''
        local recharge = ts:parameter('恢復時間', args)
        if recharge and not string.find(data, '恢[復复][時时][間间]') then
            data = '恢复时间 !! ' .. recharge .. (data ~= '' and ' ! ' .. data or '')
        end
        local cost = ts:parameter('陽光花費', args)
        if cost and not string.find(data, '[陽阳]光花[費费]') then
            data = '阳光花费 !! ' .. cost .. (data ~= '' and ' ! ' .. data or '')
        end
        local family = ts:parameter('家族', args)
        if family and not string.find(data, '家族') then
            data = (data ~= '' and data .. ' ! ' or '') .. '家族 !! ' .. family
        end
        return data ~= '' and mw.text.split(data, '%s*[!>]%s*') or {}
    end

    return almanacWrapper
        :title()
        :image()
            :level()
            :done()
        :findMore()
        :mainWrapper()
            :descriptionWrapper()
                :stats(preprocessData(), '植物圖片')
                :description()
                :originalDescription()
end

function _almanac.zombie(almanacWrapper, args)
    local function preprocessData()
        local data = ts:parameter('資料', args)
        return data and mw.text.split(data, '%s*[!>]%s*') or {
            '强度', '强度', ts:parameter('強度', args) or '',
            '速度', '速度', args['速度'] or ''
        }
    end

    return almanacWrapper
        :title()
        :image()
            :done()
        :mainWrapper()
            :stats(preprocessData(), '殭屍圖片')
            :descriptionWrapper()
                :description()
                :originalDescription()
end

return almanac