Module:UnitStats
Appearance
Documentation for this module may be created at Module:UnitStats/doc
-- Renders unit statistics from [[Module:Data/Units]].
--
-- This module is hand-written. Module:Data/Units is generated by
-- scripts/export-wiki-data.cjs, which refuses to write anything outside the
-- Module:Data/ prefix, so this file is never overwritten by the export.
local p = {}
local data = mw.loadData( 'Module:Data/Units' )
local RESOURCES = { 'money', 'food', 'metal', 'petrol', 'uranium' }
-- "100 money, 20 metal", or "free" when a unit costs nothing.
local function formatResources( bundle )
if not bundle then
return nil
end
local parts = {}
for _, name in ipairs( RESOURCES ) do
local amount = bundle[name]
if amount and amount > 0 then
parts[#parts + 1] = string.format( '%s %s', amount, name )
end
end
if #parts == 0 then
return 'free'
end
return table.concat( parts, ', ' )
end
-- Emits nothing at all for a missing value, so empty rows never show up.
local function row( label, value )
if value == nil or value == '' then
return ''
end
return string.format( '|-\n! %s\n| %s\n', label, value )
end
--- Stat box for one tier of one unit.
-- Usage: {{#invoke:UnitStats|infobox|code=light_infantry|tier=1}}
function p.infobox( frame )
local code = frame.args.code
local tier = tonumber( frame.args.tier ) or 1
local unit = data[code]
if not unit then
return string.format(
'<strong class="error">Unknown unit code: %s</strong>',
tostring( code )
)
end
local stats = unit.tiers[tier]
if not stats then
return string.format(
'<strong class="error">%s has no tier %s</strong>',
unit.name, tostring( tier )
)
end
local cargo = ( stats.cargo_volume or 0 ) > 0 and stats.cargo_volume or nil
return table.concat( {
'{| class="wikitable" style="float:right; margin:0 0 1em 1em; min-width:18em"\n',
string.format( '|+ %s — tier %d\n', unit.name, tier ),
row( 'Domain', unit.domain ),
row( 'Attack', stats.attack ),
row( 'Defense', stats.defense ),
row( 'Speed', stats.speed ),
row( 'Range', stats.range ),
row( 'Volume', stats.volume ),
row( 'Cargo capacity', cargo ),
row( 'Build time', stats.build_time ),
row( 'Cost', formatResources( stats.cost ) ),
row( 'Upkeep', formatResources( stats.upkeep ) ),
'|}',
} )
end
return p