This module depends on the following other modules:
Module:Unit/List
Module:Tooltip
Module:Tooltip/Types
Documentation for this module may be created at Module:Unit/doc
Implements{{Unit}}.
------------------------------------------------------------------------------
--
-- Module:Unit
--
-- This module implements Template:Unit.
------------------------------------------------------------------------------
local getArgs = require('Module:Arguments').getArgs
local ttUtils = require('Module:Tooltip')
local tech = require('Module:Technology').tech
local p = {};
local gameTT = 'color: #c2bdb8; font-style: normal; background: linear-gradient(to bottom,#47373a,#211b1c); border: solid #b19656; border-radius: 10px; width: max-content; padding: 5px;'
local uString = mw.ustring
local htmlCreate = mw.html.create
local frame = mw.getCurrentFrame():getParent() or mw.getCurrentFrame()
local formationRef = { name = 'ref', content = 'Scaled by percentage of formation composition', args = { name = 'formation scaling' } }
local function reftag(str)
return frame:extensionTag(formationRef)
end
function p.main(frame)
local args = getArgs(frame)
if args.table then
return p.table(args)
else
return p.unit(args)
end
end
function p.unit(args)
local index = tonumber(args.index)
local unitID = uString.lower(args[1])
local unitData = mw.loadData('Module:Unit/List')[unitID] --load unit data
-- catch unknown/error
if not unitData then
return '<span style="color:red; font-size:11px;">(unrecognized unit "' .. args[1] .. '" for [[Module:Unit]])</span>[[Category:Pages with unrecognized units]]'
end
local typeTable = {
['Infantry'] = "Land warfare",
['Artillery'] = "Land warfare",
['Cavalry'] = "Land warfare",
['Light Ships'] = "Naval warfare",
['Capital Ships'] = "Naval warfare",
['Support Vessels'] = "Naval warfare",
}
-- set tooltip display as icon and text, short-circuit tooltip if out of index
local displayText = uString.format("[[File:%s|%s|link=%s#%s|%s]] %s", unitData.icon, args.width or "24px", typeTable[unitData.type], unitData.link, args[1], args[1])
if (args.from == "unit") or (index == 0) then index = index + 1 end
if args.fromfrom == "unit" then index = 4 end
if type(index) ~= 'number' or index > 3 then -- any non number or index over 3 returns basic display
return args.from and ttUtils.tooltip(displayText,'','',{tagType='span'}) or displayText
end
local header = ttUtils.headerStyle('2LR', {
leftText=uString.format("[[File:%s|48px|link=%s#%s|%s]]", unitData.icon, typeTable[unitData.type], unitData.link, args[1]),
mainText=args[1],
rightText=unitData.type,
subText=unitData.tech and '<div>Unlocked by: ' .. tech{unitData.tech, index=(index + 1), from="unit", fromfrom=args.from}..'</div>' or "Always unlocked",
extra="align-items: center;"
})
local stats = '<div>Stats:<div style="margin-left:1em;">' .. ttUtils.effectList(unitData.stats) .. '</div></div>'
stats = uString.gsub(stats, "(<ref>)", reftag)
if unitData.upkeep and unitData.upkeep[1] then
stats = stats .. '<div>Upkeep:<div style="margin-left:1em;">' .. ttUtils.effectList(unitData.upkeep,{icon="Goods"}) .. '</div></div>'
else
stats = stats .. "<div>No upkeep goods</div>"
end
if unitData.upgrade.from and unitData.upgrade.from[1] then
stats = stats .. '<div>Can upgrade from:' .. ttUtils.ttList( { unit = unitData.upgrade.from }, { multiKey='', from='unit', index=index+2 } ) .. '</div>'
end
if unitData.upgrade.to and unitData.upgrade.to[1] then
stats = stats .. '<div>Can upgrade to:' .. ttUtils.ttList( { unit = unitData.upgrade.to }, { multiKey='', from='unit', index=index+2 } ) .. '</div>'
end
local body = '<hr>' .. tostring(htmlCreate('div'):cssText("display:grid; grid-template-columns: auto auto; gap:5px 10px;"):wikitext(stats))
return ttUtils.tooltip(displayText, header, body, {style=gameTT, index=index})
end
function p.table(args)
local index = tonumber(args.index) or 3
local unitID = uString.lower(args[1])
local unitData = mw.loadData('Module:Unit/List')[unitID] --load decree data
-- catch unknown/error
if not unitData then
return '<span style="color:red; font-size:11px;">(unrecognized unit "' .. args[1] .. '" for [[Module:Unit]])</span>[[Category:Pages with unrecognized units]]'
end
-- first column, icon and name
local s = uString.format("<span id='%s'></span>[[File:%s|48px|%s]] '''%s'''\n", mw.uri.anchorEncode(unitData.link), unitData.icon, args[1], args[1])
-- tech
s = uString.format('%s----\n* %s\n', s, unitData.tech and 'Unlocked by:\n** ' .. tech{ unitData.tech, index=(index + 1), from="unit" } or "Always unlocked")
-- upgrade from list
if unitData.upgrade.from and unitData.upgrade.from[1] then
s = s .. '----\n* Can upgrade from:' .. ttUtils.ttList( { unit = unitData.upgrade.from }, { multiKey='', listStyle="newline", indent = "**" } )
else
s = s .. '----\n* Can upgrade from:\n** None'
end
-- second column, unit stats
local stats = ttUtils.effectList(unitData.stats, {listStyle="newline",color={css=1}}) or '—'
stats = uString.gsub(stats, "(<ref>)", reftag)
-- third column, unit upkeep
local upkeep = unitData.upkeep and unitData.upkeep[1] and ttUtils.effectList(unitData.upkeep,{icon="Goods", listStyle="newline", color={css=1}}) or '—'
s = uString.format("%s\n|%s\n|%s", s, stats, upkeep)
return s
end
return p