模块:State

Documentation for this module may be created at Module:State/doc

Implements {{State}}.

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

local getArgs = require('Module:Arguments').getArgs
local iconify = require('Module:Iconify').iconify
local stateTrait = require('Module:State trait').state_trait
local yesno = require('Module:Yesno')
local inArray = require('Module:TableTools').inArray
local p = {};

function p.main(frame)
    local args = getArgs(frame)
    return p.state(args)
end

function p.state(args)

    local newline = "\n|"
    local stateData = mw.loadData('Module:State/List')[args.state]

    if not stateData then
        return '<span style="color:red; font-size:11px;">(unrecognized state "' .. args.state .. '" for [[Module:State]])</span>[[Category:Pages with unrecognized states]]'
    end

    -- initialize return string with state name; args.state is parameter 1 in {{state}}
    local s = args.state

    -- set info formatting, default to 'state status' type
    local width = "36px"
    local image = false
    if yesno(args.cores) then
        width = "border|24px" -- 'hack' iconify to add border like {{flag}}
        image = true -- used in info iconify to set icon from info directly
    end
    --[[
    -- state info, comma separated pseudo-table in template call, with a separate parameter for formatting as state status icons or "core" flags
    -- args.info is parameter 2 in {{state}}
    -- args.core is parameter "cores" in {{state}}
    --]]
    local info = args.info or ""
    s = s .. newline
    if info ~= "" then
        info = mw.text.split(info,",")
        for _, v in ipairs(info) do
            v = mw.text.split(v, "_")
            -- Allow using claims icon in 'core' type, must be last
            if yesno(args.cores) and string.lower(v[1]) == "claims" then
                image = false
                width = "24px"
            end
            if image then image = v[1]..".png" end
            s = string.format("%s\n* %s", s, iconify({icon=v[1], group="State status", image=image, width=width, text="0"}))
            if v[2] and v[2] ~= "" then
                s = string.format("%s %s", s, iconify({icon="Capital", group="State status", width="20px", text="0"}))
            end
        end
    end

    --[[
    -- state homelands, defined table in state data
    --]]
    s = s .. newline
    for _, v in ipairs(stateData["homelands"]) do
        s = string.format("%s\n* %s",s,v)
    end

    -- strategic region, optional; args.region is parameter "region" in {{state}}
    if yesno(args.region) then
        s = string.format("%s%s %s",s,newline,stateData["region"])
    end

    -- arable land amount
    s = string.format("%s\n| %s",s,stateData["arable_land"])

    -- arable resources, optionally multiColumn'd; args.multiColumn is parameter "mc" in {{state}}
    -- keep equivalent with {{multiColumn}} with two columns
    s = s .. newline
    if yesno(args.multiColumn) then
        s = s .. '<div style="columns:100px 2; column-gap: 10px;-webkit-columns:100px 2; -webkit-column-gap: 10px;-moz-columns:100px 2; -moz-column-gap: 10px;">'
    end
    local agOrder = {
        'Maize Farms',         'Millet Farms',
        'Rice Farms',          'Rye Farms',
        'Wheat Farms',         'Livestock Ranches',
        'Banana Plantations',  'Coffee Plantations',
        'Cotton Plantations',  'Dye Plantations',
        'Opium Plantations',   'Silk Plantations',
        'Sugar Plantations',   'Tea Plantations',
        'Tobacco Plantations', 'Vineyard'
    };
    for _, ag in ipairs(agOrder) do -- cycle standardized order
        if inArray(stateData["arable_resources"], ag) then -- check the type is actually present
            ag = mw.ustring.gsub(ag, "e?s$","") -- singularize ag buildings
            s = mw.ustring.format("%s\n* %s",s,iconify({icon=ag,group="building",width="30px"}))
        end
    end
    if yesno(args.multiColumn) then s = s .. '</div>' end

    -- non-ag resources, cycles invoked list; can miss resources in state; args.resources is parameter 3 in {{state}}
    s = s .. newline
    local resTable = stateData["resources"]
    for res in mw.text.gsplit(args.resources,",") do
        s =  string.format("%s %s ||", s, resTable[res] or "—")
    end
    s = s:sub(1, -4) -- trim last ' ||'

    -- state traits
    local traits = stateData["traits"]
    s = s .. newline
    if traits[1] ~= nil then
        for _, v in ipairs(traits) do
            s = mw.ustring.format("%s%s\n",s,stateTrait(v))
        end
        s = s:sub(1, -2) -- trim last newline
    end
    return s
end

return p