Module:Law

天津风不是天津的风讨论 | 贡献2025年7月9日 (三) 08:03的版本 (merge from official wiki)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

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

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

function p.main(frame)
    local args = getArgs(frame)
    if args.table then
        return '<span style="color:red;">WIP</span>' --p.table(args)
    else
        return p.law(args)
    end
end
--[=[
function p.law(args)
    local index = tonumber(args.index)
    local lawID = uString.lower(args[1])
    local lawData = mw.loadData('Module:Law/List')[lawID] --load pm data

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

    local loc = lawData.loc
    -- set tooltip display as icon and text, short-circuit tooltip if out of index 
    --local displayText = uFormat("[[File:Law %s.png|%s|link=%s#%s|%s]] %s", uString.lower(loc), args.width or '24px', lawData.category, loc, loc, loc)
    local displayText = iconify{icon=loc, group="Law", width=args.width or '24px', link=lawData.category.."#"..loc, extra=loc}
    if args.fromfrom == 'law' then index = 4 end
    if type(index) ~= 'number' or index > 3 then -- any non number or index over 3 returns basic display
        if not args.from then
            return displayText
        else
            return ttUtils.tooltip(displayText)
        end
    end

    -- set up header subtitle, with building for pm, tooltip building if pm called directly
    subtitle = '<span>Law group: ' .. lawData.group .. '</span>'

    local header = ttUtils.headerStyle('2LR', {
        --leftText=uFormat("[[File:Law %s.png|48px|link=%s#%s|%s]]", uString.lower(loc), lawData.category, loc, loc),
        leftText=iconify{icon=loc, group="Law", width="48px", text=0, link=lawData.category.."#"..loc, extra=loc},
        rightText=lawData.category,
        mainText=loc,
        subText=subtitle
    })

    -- start body with reqs and blockers
    local body = '<hr>'
    local reqs, blockers
    if lawData.reqs and size(lawData.reqs) > 0 then
        reqs = '<div>Requires:' .. ttUtils.ttList(lawData.reqs, {index=(index + 1), from="law", fromfrom=args.from})..'</div>'
    end
    if lawData.blockers and size(lawData.blockers) > 0 then
        blockers = '<div>Blocked by:' .. ttUtils.ttList(lawData.blockers, {index=(index+1), from="law", fromfrom=args.from}) .. '</div>'
    end
    if reqs or blockers then
        reqs, blockers = reqs or '', blockers or ''
        body = body .. '<div style="display:grid; grid-template-columns: auto auto; gap:5px;">' .. reqs .. blockers .. '</div><hr>'
    end

    local effects = ''
    -- iterate effects
    if lawData.effects and lawData.effects[1] then
        effects = effects .. '<div>Effects:<div style="margin-left:1em;">' .. ttUtils.effectList(lawData.effects) .. '</div></div>'
    end
    if lawData.enact and lawData.enact[1] then
        effects = effects .. '<hr><div>When enacted:<div style="margin-left:1em;">' .. ttUtils.effectList(lawData.enact) .. '</div></div>'
    end
    if effects ~= '' then
        body = body .. tostring(htmlCreate('div'):wikitext(effects))
    else
        body = body .. "No effects"
    end
    if lawData.desc then
        body = body .. '<hr><div style="font-size:smaller; font-style:italic; line-height:1.2em; max-width:350px">' .. lawData.desc .. '</div>'
    end
    return ttUtils.tooltip(displayText, header, body, {style=gameTT, index=index})
end
--]=]

-- Temp copied from building module, not usable yet
function p.table(args)
    local lawID = mw.ustring.lower(args[1])
    local lawData = mw.loadData('Module:Law/List')[lawID] --load building data

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

    local reqs = ''
    if lawData.reqs then
        reqs = '\n' .. ttUtils.ttList(lawData.reqs, {index=1, listStyle="newline"})
    end
    if lawData.blockers then
        reqs = reqs .. '\nNone of:\n' .. ttUtils.ttList(lawData.blockers, {index=1, listStyle="newline"})
    end
    s = mw.ustring.format("%s || %s || %s || %s || %s || %s", s, reqs, lawData.effects, lawData.stances)

    return s
end

return p