Module:Card

local card = {}

-- Libraries of functions --

-- stands for High Frequency local HF = require('Module:HF') -- Parses invocation and template parameters, trims whitespace, and removes blanks. local getArgs = require('Dev:Arguments').getArgs -- Generates lists local L = require('Dev:List') -- The video game table local vgT = require('Module:Card/vgTable')

-- Gives the name of the current page. It's expensive, so we only do it once. -- The _G then makes it global, available to all page functions. _G.page_title = mw.title.getCurrentTitle.text _G.namespace = mw.title.getCurrentTitle.nsText

-- Parses template parameters from article, trims whitespace, and removes blanks. _G.t = getArgs(frame, { trim = true, removeBlanks = true, parentOnly = true }) -- The occasions where we need the same "variables" to process multiple fields are global. --- _G.level_rank = ( string.match( (t['type2'] or '').. (t['type3'] or '').. (t['type4'] or ''), 'Xyz') and 'Rank' ) or 'Level'

-- These two lines are equivalent -- if t_args['kr_sets'] or t_args['ko_sets'] then _G.kr_release = 1 end -- _G.kr_release = (t_args['kr_sets'] or t_args['ko_sets']) and 1 if t['ae_sets'] then _G.ae_release = 1 end _G.tc_release = t['tc_sets'] and 1 if t['tc_sets'] then _G.tc_release = 1 end if t['na_sets'] then _G.na_release = 1 end if t['eu_sets'] then _G.eu_release = 1 end if t['de_sets'] then _G.de_release = 1 end if t['it_sets'] then _G.it_release = 1 end if t['pt_sets'] then _G.pt_release = 1 end if t['sp_sets'] then _G.sp_release = 1 end if t['jp_sets'] or t['ja_sets'] then _G.ocg_jp = 1 end _G.ocg = (ocg_jp or '') .. (kr_release or '') .. (ae_release or '') .. (tc_release or '') if t['en_sets'] or na_release or eu_release or t['au_sets'] then _G.tcg_en = 1 end if t['fr_sets'] or t['fc_sets'] then _G.tcg_fr = 1 end _G.tcg = (tcg_en or '') .. (tcg_fr or '') .. (de_release or '') .. (it_release or '') .. (pt_release or '') .. (sp_release or '') _G.ocg_tcg = ocg .. tcg

-- -- Public functions (called from a Template or article) -- -- -- Primary image handler -- use inside function card.image_front(frame) local image = t['image'] -- local SMW = { } -- SMW['Card image'] = image -- mw.smw.set{ SMW } return ( image ) .. ( t['image2'] and '|Front' or '' ) end

-- Primary image handler -- use inside function card.image_back(frame) local type = t['type'] -- local SMW = { } local backs = { ['Strategy Card'] = 'StrategyCard-EN-Back.png', ['Tip Card'] = 'StrategyCard-EN-Back.png', ['FAQ Card'] = 'StrategyCard-EN-Back.png' }   local image_back = backs[type] or ('Back-'..((tcg and 'EN') or ( ocg_jp or tc_release and 'JP' ) or ( kr_release and 'KR' ) or ( ae_release and 'AE' ) or ( vg and 'TF-EN-VG' ) or 'EN' )..'.png') -- SMW['Card image'] = image -- mw.smw.set{ SMW } return ( t['card_back'] or image_back ) end

-- Machine translation link -- use inside -- "label" is optional, and defaults to 📃 function card.translate(frame) -- Parses invocation parameters, trims whitespace. local vars = getArgs(frame, { trim = true, frameOnly = true }) assert(vars[1], 'No language code given for translation link.') assert(vars[2], 'No text given for translation link.') local language, parameter, label = vars[1], vars[2], vars[3] or '📃' local text = t[parameter] or '' local URI = mw.uri.new( { protocol = 'https', host = 'translate.google.com', path = '/#'..language..'/en/'..mw.uri.encode(text) } ) local element = mw.html.create('span'):addClass('plainlinks'):addClass('sysop-show') :wikitext(HF.ExternalLink( tostring(URI), label )):allDone return tostring(element) end

-- Render ultimate type -- use inside function card.type(frame) -- Parses template parameters, trims whitespace, and removes blanks. local t = getArgs(frame, { trim = true, removeBlanks = true, parentOnly = true }) local out = {} -- local SMW = {} local type1, type2, type3, type4 = t['type'] or '', t['type2'] or nil, t['type3'] or nil, t['type4'] or nil local type1U = mw.getContentLanguage:ucfirst(type1) local switch = { [] = "",       ['Equip Card'] = HF.Link(type1U)..HF.SMWP('Card type', type1, ), ['Tip Card'] = HF.Link(type1U)..HF.SMWP('Card type', type1, ''), ['Strategy Card'] = HF.Link(type1U)..HF.SMWP('Card type', type1, ''), ['FAQ Card'] = HF.Link(type1U)..HF.SMWP('Card type', type1, ''), ['Illustration Card'] = HF.Link(type1U)..HF.SMWP('Card type', type1, ''), ['Counter'] = HF.Link(type1U)..HF.SMWP('Card type', type1, ''), }   local switch2 = { ['Flip'] = "Flip monster", ['Spirit'] = "Spirit monster", ['Union'] = "Union monster", ['Toon'] = "Toon monster", ['Gemini'] = "Gemini monster", ['Tuner'] = "Tuner monster", ['Dark Tuner'] = "Dark Tuner monster", }   if t['type'] or type2 or type3 or type4 then if type1:match('%(') then type1_prefix = type1:match('(%a*)%(') else type1_prefix = type1 end table.insert(out,            switch[type1] or HF.Link(type1U, type1_prefix)..HF.SMWP('Type', type1, '') ) -- if switch[type1] then SMW['Card type'] = type1 else SMW['Type'] = type1 end if type2 then table.insert(out, HF.Link( switch2[type2] or type2..' Monster', type2)) end if type3 then table.insert(out, HF.Link( switch2[type3] or type3..' Monster', type3)..HF.SMWP('Type3', type3, '')) -- SMW['Type3'] = type3 end if type4 then table.insert(out, HF.Link( type4..' Monster', type4)..HF.SMWP('Type4', type4, '')) -- SMW['Type4'] = type4 end end -- mw.smw.set( SMW ) return table.concat(out, ' / ') end

-- use inside function card.card_type (frame) -- Parses template parameters, trims whitespace, and removes blanks. local t = getArgs(frame, { trim = true, removeBlanks = true, parentOnly = true }) if t['type2'] == "Token" then token = 1 else token = nil end if t['atk'] or t['def'] or t['level'] or t['rank'] or t['link_arrows'] or token == 1 then monster = 1 else monster = nil end local out, SMW = {} if monster or t['card_type'] then local m = (t['card_type'] or 'Monster')..' Card' table.insert(out, HF.Link( m, (t['card_type'] or 'Monster'))) if t['card_type'] == 'Speed Spell' then table.insert(out, ' ') elseif not monster then table.insert(out, '') end table.insert(out, HF.SMWP('Card type', m, )..HF.SMWP('Card type (short)',..(t['card_type'] or 'Monster').., )) --SMW['Card type'] = m      --SMW['Card type (short)'] = ..(t.card_type or 'Monster').. if monster then table.insert(out, HF.SMWP('Card category', 'Monster Card', '')) --SMW['Card category'] = 'Monster Card' end end -- mw.smw.set( SMW ) return table.concat(out) end

-- use inside function card.level (frame) -- Parses template parameters, trims whitespace, and removes blanks. local t = getArgs(frame, { trim = true, removeBlanks = true, parentOnly = true }) local stars = t['level'] or t['rank'] or nil local out, SMW = {} if stars then table.insert(out, HF.Link(level_rank..' '..stars..' Monster Cards', stars)) if ocg_tcg or vg then table.insert(out,                string.rep( '', ( tonumber(stars) or 1 ) )        ) end table.insert(out,           HF.SMWP(( tonumber(stars) and  or '?' )..' '..level_rank, stars, )..            HF.SMWP(level_rank..' string', stars, )..            HF.SMWP(( tonumber(stars) and  or '?' )..'Stars', stars, )..            HF.SMWP('Stars string', stars, )        ) -- SMW[( tonumber(stars) and '' or '?' )..' '..level_rank] = stars -- SMW[level_rank..' string'] = stars -- SMW[( tonumber(stars) and '' or '?' )..'Stars'] = stars -- SMW['Stars string'] = stars -- mw.smw.set( SMW ) return table.concat(out) end end

-- Formats card sets in rows -- Detects if the content uses a wrapper template, or is data -- use inside function card.cardset_format (frame) local vars = getArgs(frame, { trim = true, frameOnly = true }) local out = {} -- no logic yet table.insert(out, _cardset_envelope(vars)) return table.concat(out) end

function card.search_categories (frame) -- Parses invocation parameters, trims whitespace. local vars = getArgs(frame, { trim = true, frameOnly = true }) -- Parses template parameters, trims whitespace, and removes blanks. local t = getArgs(frame, { trim = true, removeBlanks = true, parentOnly = true }) local parameter = assert(vars[1], 'No parameter is specified.') local value = t[parameter] local property = vars[2] local out = {} -- output

local test = mw.text.split(value, '%*') local hash = {} local raw_result = {} local list_items = {} for _,v in ipairs(test) do      if (not hash[v] and v ~= '') then table.insert( raw_result, v ) hash[v] = true end end -- unique results are now in table 'raw_result' if #raw_result > 0 then for i, v in ipairs( raw_result ) do           v_unlinked = ( v:match( '%[%[:?(.-)[|%]]' ) or HF.trim( v ) ) table.insert( list_items,                HF.Link( v_unlinked, ( v_unlinked:match('(.+)%(') or v_unlinked ) ).. HF.SMWP('Support', v_unlinked, '') )           -- table.insert( SMW, 'Support='..v_unlinked )        end        local list = L.makeList( 'horizontal', list_items )        table.insert(out, tostring(list))        if parameter == 'summoning' and t['monster'] and (not t['token']) then            if (not value:lower:match('nomi')) and                 not (value:lower:match('cannot special summon') or value:lower:match('cannot be special summoned')) then                    table.insert( out, HF.SMWP('Summoning', 'Can be Special Summoned', ) )                    -- table.insert( SMW, 'Summoning=Can be Special Summoned' )                if not ( value:lower:match('semi[ -]?nomi') or                    value:lower:match('special summon-only monster') or                    t['monster_type']) then                        table.insert( out, HF.SMWP('Summoning', 'Can always be Special Summoned', ) )                        -- table.insert( SMW, 'Summoning=Can always be Special Summoned' ) end end end elseif parameter == 'summoning' and t['monster'] and (not t['token']) then table.insert( out, HF.SMWP('Summoning', 'Can be Special Summoned', '')) -- table.insert( SMW, 'Summoning=Can be Special Summoned' ) if not t['monster_type'] then table.insert( out, HF.SMWP('Summoning', 'Can always be Special Summoned', '') ) -- table.insert( SMW, 'Summoning=Can always be Special Summoned' ) end end

-- mw.smw.set( SMW ) return table.concat(out) end

function card.other end

function card.external_links end

function card.sorting end

function card.semantics end

function card.vg_table (frame) -- Parses template parameters, trims whitespace, and removes blanks. local t = getArgs(frame, { trim = true, removeBlanks = true, parentOnly = true }) return vgT._table(t) end

- -- Internal functions (used in this and other Modules) -- - -- Create an envelope for existing tables function _cardset_envelope (inv_args) local envelope = mw.html.create('div') :addClass('mw-collapsible'):addClass('mw-collapsed') :attr('data-expandtext','Show'):attr('data-collapsetext','Hide') :wikitext(inv_args[1]):allDone return tostring(envelope) end

-- If card set content passed is only data, build a table around it function _cardset_table end

-- Build table rows for _cardset_table function _cardset_row end

- -- Output (send it back to whatever called it) -- - return card