Module:Building

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

------------------------------------------------------------------------------
-- 
--                                 Module:Building
-- 
-- This module implements Template:Building.
------------------------------------------------------------------------------

local p = {};
local iconify = require('Module:Iconify').iconify
local getArgs = require('Module:Arguments').getArgs
local ttUtils = require('Module:Tooltip')
p = require('Module:Tooltip/Types')

function p.main(frame)
    local args = getArgs(frame)
    if args.table then
        return p.table(args)
    else
        return p.building(args)
    end
end

--[=[
function p.building(args)
    local index = tonumber(args.index)
    local buildID = mw.ustring.lower(args[1])
    local buildData = mw.loadData('Module:Building/List')[buildID] --load building data

    -- catch unknown/error
    if not buildData then
        return '<span style="color:red; font-size:11px;">(unrecognized building "' .. args[1] .. '" for [[Module:Building]])</span>[[Category:Pages with unrecognized buildings]]'
    end

    local function tchelper(first, rest)
       return first:upper()..rest:lower()
    end

    local canLoc = mw.ustring.gsub(buildData.canLoc, "(%a)([%w_']*)", tchelper)

    local fromText
    if args.from == 'pm' then fromText = 'Used by' end

    -- set tooltip display as icon and text, short-circuit tooltip if out of index 
    local displayText = iconify{icon=args[1], group="building", link="List of buildings#"..canLoc,mod=fromText}
    if type(index) ~= 'number' or index > 3 then -- any non number or index over 3 returns basic display
        return ttUtils.tooltip(displayText)
    end

    -- set tooltip style, imitates in-game tooltips
    local tooltipStyle = 'color: #c2bdb8; background: linear-gradient(to bottom,#47373a,#211b1c); border: solid #b19656; border-radius: 10px; width: max-content; padding: 5px;'

    -- set up header subtitle, with building basic info
    local constr = mw.ustring.format([[%s <span title="Construction">Constr.</span> '''%s''']], "[[File:State status construction.png|16px|link=Construction]]", buildData.cost)
    local infra = mw.ustring.format([[%s <span title="Infrastructure">Infra.</span> '''%s''']], "[[File:State status infrastructure.png|16px|link=Infrastructure]]", buildData.infra)
    local urban = mw.ustring.format([[<span title="Urbanization">Urban.</span> '''%s''']], buildData.urb)
    local subtitle = mw.ustring.format("<span>%s | %s | %s</span>", constr, infra, urban)

    -- start header with float left icon
    local header = ttUtils.headerStyle('2LR', {
        leftText=iconify{icon=canLoc, group="building", width="48px", text=0, link="List of buildings#"..canLoc},
        rightText=buildData.category,
        mainText=canLoc,
        subText=subtitle
    })

    -- start body with reqs and blockers
    local body = '<hr>'
    if buildData.reqs and buildData.notes then
        body = body .. '<div style="display:grid; grid-template-columns: auto auto; gap:5px;">'
        body = body .. '<div>Requires:<div style="margin-left:1em;">' .. ttUtils.ttList(buildData.reqs, {index=(index + 1), from="building"})..'</div></div>'
        local notes = ''
        for _, v in ipairs(buildData.notes) do
            notes = mw.ustring.format("%s<div>%s</div>",notes,v)
        end
        body = body .. '<div>Additional info:<div style="margin-left:1em;">' .. notes..'</div></div>'
        body = body .. '</div><hr>'
    elseif buildData.reqs then
        body = body .. 'Requires:<div style="margin-left:1em;">' .. ttUtils.ttList(buildData.reqs, {index=(index + 1), from="building"})..'</div><hr>'
    elseif buildData.notes then
        local notes = ''
        for _, v in ipairs(buildData.notes) do
            notes = mw.ustring.format("%s<div>%s</div>",notes,v)
        end
        body = body .. 'Additional info:<div style="margin-left:1em;">' .. notes..'</div><hr>'
    end

    -- no recursive tooltips please
    local pmIndex = index + 1
    if args.from == "pm" then pmIndex = 4 end

    -- continue tooltip for PMs, grid and flexbox to align them neatly
    if buildData.pm then
        body = body .. '<div style="display:grid; grid-template-columns: auto auto; gap:5px 10px;">'
        for _,pmg in ipairs(buildData.pm) do
            body = body .. '<div style="display:flex; flex-direction: column; align-items: flex-start">'
            if pmg.group then
               body = body .. tostring(mw.html.create('div'):cssText("color:white; font-weight:bold;"):wikitext(pmg.group))
            end
            for _,u in ipairs(pmg) do
                body = body..p.productionMethod{u,index=pmIndex,from="building"}
            end
            body = body .. '</div>'
        end
        body = body .. '</div>'
    end

    return ttUtils.tooltip(displayText, header, body, {style=tooltipStyle, index=index})
end
--]=]

function p.table(args)
    local buildID = mw.ustring.lower(args[1])
    local buildData = mw.loadData('Module:Building/List')[buildID] --load building data

    if not buildData then
        return '<span style="color:red; font-size:11px;">(unrecognized building "' .. args[1] .. '" for [[Module:Building]])</span>[[Category:Pages with unrecognized buildings]]'
    end
    local s = iconify{icon = args[1], group = "building", width="64px"}

    local reqs = ''
    if buildData.reqs then
        reqs = '\n' .. ttUtils.ttList(buildData.reqs, {index=1, listStyle="newline", from="building"})
    end

    s = mw.ustring.format("%s || %s\n|%s\n| %s || %s || %s\n|", s, buildData.group, reqs, buildData.cost, buildData.urb, buildData.infra)

    if buildData.pm then
        for _, v in ipairs(buildData.pm) do
            for _, u in ipairs(v) do
                s = mw.ustring.format("%s\n* %s", s, p.productionMethod{u, index=1, from="building"})
            end
        end
    end
    
    s = s .. "\n|"

    if buildData.notes then
        for _, v in ipairs(buildData.notes) do
            s = mw.ustring.format("%s\n* %s", s, v)
        end
    end

    return s
end

return p