Module:TowerCard
Documentation for this module may be created at Module:TowerCard/doc
local p = {}
local ICON = {
coins = '[[File:CoinV2.png|24px|link=|Coins]]',
gems = '[[File:GemV3.png|24px|link=|Gems]]',
robux = '[[File:RobuxIcon.svg|24px|link=|Robux]]',
none = '',
}
ICON.coin, ICON.gem = ICON.coins, ICON.gems
local RAAAAAAAH = {
starter = 'colour-common', intermediate = 'colour-uncommon', advanced = 'colour-rare',
hardcore = 'colour-hardcoremode', evolved = 'colour-rare', golden = 'colour-golden',
exclusive = 'colour-ultimate', unavailable = 'colour-event',
}
local SKIN = {
common = 'colour-common', uncommon = 'colour-uncommon', rare = 'colour-rare',
legendary = 'colour-hardcoremode', ultimate = 'colour-ultimate', golden = 'colour-golden',
event = 'colour-event', exclusive = 'colour-event', removed = 'colour-removed',
}
local lower, trim = mw.ustring.lower, mw.text.trim
local function val(v)
if v and v ~= '' then return v end
end
local function costs(a, s)
local list, cost = {}, val(a['cost' .. s])
if cost then
for part in mw.text.gsplit(cost, '+', true) do
local amount, currency = trim(part):match('^(%S+)%s*(.*)$')
if amount then list[#list + 1] = { amount, currency } end
end
else
local price = 'price' .. s
list[1] = { a[price] or '', a['currency' .. s] }
if val(a[price .. '_b']) then
list[2] = { a[price .. '_b'], a['currency' .. s .. '_b'] }
end
end
return list
end
local function method(row, a, s)
local m = row:tag('span'):addClass('tower-card-method')
local unlock = val(a['unlock' .. s])
if unlock then
m:tag('span'):addClass('tower-card-amount'):wikitext(unlock)
return
end
local list = costs(a, s)
local combo = #list > 1
if combo then m:addClass('combo') end
for _, c in ipairs(list) do
local box = combo and m:tag('span'):addClass('tower-card-cost') or m
box:wikitext(ICON[lower(val(c[2]) or 'coins')] or ICON.coins)
:tag('span'):addClass('tower-card-amount'):wikitext(c[1])
end
end
local function visibleLen(s)
s = mw.text.killMarkers(s)
:gsub('<[^>]*>', '')
:gsub('%[%[[^%]|]*|([^%]]*)%]%]', '%1')
:gsub('%[%[([^%]]*)%]%]', '%1')
:gsub('%[%a+://%S+%s+([^%]]*)%]', '%1')
:gsub("'''?", '')
return mw.ustring.len(trim(mw.text.decode(s)))
end
function p._card(a, skin)
local name = trim(a.name or a[1] or '')
if name == '' then return '' end
skin = skin or lower(val(a.type) or '') == 'skin'
local tier = val(a.tier) or (skin and 'Common' or 'Starter')
local tkey = lower(tier)
local colour = (skin and SKIN or RAAAAAAAH)[tkey] or 'colour-common'
local link = val(a.link) or name
local note, req = val(a.note), val(a.req)
local card = mw.html.create('div')
:addClass('tower-card')
:addClass('tier-' .. tkey)
:addClass(colour)
:addClass(skin and 'is-skin' or nil)
:addClass(note and 'has-note' or nil)
:addClass(req and 'has-req' or nil)
card:tag('span'):addClass('tower-card-link'):wikitext('[[', link, '|​]]')
card:tag('div'):addClass('tower-card-render')
:wikitext('[[File:', val(a.image) or (name .. 'Icon.png'), '|256px|link=|alt=', name, ']]')
card:tag('div'):addClass('tower-card-name'):css('--tc-len', visibleLen(name)):wikitext(name)
card:tag('div'):addClass('tower-card-tier'):wikitext(val(a.label) or tier)
if req then
local reqs = card:tag('div'):addClass('tower-card-reqs')
reqs:tag('span'):addClass('tower-card-req')
:tag('span'):addClass('ego-i i-lock-fill'):attr('aria-hidden', 'true'):done()
:wikitext(req)
if val(a.req2) then reqs:tag('span'):addClass('tower-card-req'):wikitext(a.req2) end
end
local row, n = card:tag('div'):addClass('tower-card-price'), 1
method(row, a, '')
for s = 2, 5 do
if val(a['price' .. s]) or val(a['unlock' .. s]) or val(a['cost' .. s]) then
n = n + 1
method(row, a, s)
end
end
row:addClass('n' .. n)
if note then
card:tag('span'):addClass('tower-card-info')
:attr({ tabindex = '0', role = 'button', ['aria-label'] = 'Notes for ' .. name })
:tag('span'):addClass('ego-i i-info-lg'):attr('aria-hidden', 'true')
card:tag('div'):addClass('tower-card-note'):wikitext(note)
end
return tostring(card)
end
function p.heeho(frame) return p._card(frame:getParent().args) end
function p.skin(frame) return p._card(frame:getParent().args, true) end
return p