FANDOM


--[[
    Var list:
    (so I can keep track of what to use)
    -------
    # Data:
    ->  rg_list                 //  Region code list;
    ->  region_list             //  Region list;
    ->  ln_list                 //  Language code list;
    ->  language_list           //  Language list;
    ->  rarity_list             //  Rarity list;
    ----------
    # General:
    ->  PAGENAME                //  {{PAGENAME}};
    ->  NAMESPACE               //  {{NAMESPACE}};
    ->  _rg                     //  Region code;
    ->  _region                 //  Region name;
    ->  _ln                     //  Language code;
    ->  _language               //  Language name;
    ------
    # Set:
    ->  nameSet_parts           //  Aux var containing the parts for the set name by «(»;
    ->  _nameSet                //  Set name;
    ->  _nameSet_localized      //  Localized set name;
    ->  _qty                    //  Quantity value ({{{qty}}});
    ---------
    # Design:
    ->  header                  //  Page header (HTML);
    ->  category                //  Category;
    --------------
    # List (main):
    ->  cardList_input          //  List of cards passed;
    ->  cardList_array          //  Array of the above (by «\n»).
    ->  extra_column            //  If there's an extra column ({{{col}}});
    --------
    # Table:
    ->  _header                 //  {{{header}}};
    ->  _rarity                 //  {{{rarity}}};
    ->  out_t                   //  Table to dsiplay (HTML);
    -----------------
    # List (entries):
    ## Section:
    ->  _headerSection          //  For sections («!:»), the header info («header::»);
    ->  _raritySection          //  For sections («!:»), the rarity info («rarity::»);
    ->  _qtySection             //  For sections («!:»), the quantity info («qty::»);
    ## Standard values (passed and parsed directly: <number>; <name>; <rarity>; <qty>;):
    ->  _nameCard               //  Card name (<name>);
    ->  nameCard_strip          //  Aux var; strip «#» from the above;
    ->  nameCard_noDab          //  Aux var; removes dab text from «_nameCard»;
    ->  _nameCard_localized     //  Localized card name;
    ->  _numberCard             //  Card number (<number>);
    ->  _rarityCard             //  Card rarity (<rarity>) OR more general rarity values;
    ->  _typeCard               //  Card type (or category);
    ->  _qtyCard                //  Card quantity (<qty>) OR more general qty values;
    ## Alternate values (literal, separated by «//»):
    ->  _nameCard_alt           //  «name::»;
    ->  _nameCard_localized_alt //  «name-local::»;
    ->  _numberCard_alt         //  «number::»;
    ->  _rarityCard_alt         //  «rarity::»;
    ->  _typeCard_alt           //  «category::»;
    ->  _description            //  «description::»;
    ->  _extra                  //  «<lowercased_column_name>::», where the column name is «extra_column»;
    ## Processing:
    ->  cardList_array_parts    //  Array of «cardList_array» divided by «//»;
    ->  cardList_array_std      //  Array of [1] of the above, divided by «;»;
    ->  rarity_array            //  Array of the card rarities;
    ## Display vars:
    ->  display_nameCard
    ->  display_nameCard_localized
    ->  display_numberCard
    ->  display_rarityCard
    ->  display_typeCard
    ->  display_qtyCard
    --------
    # Flags:
    ->  notEnglish              //  Boolean;
    ->  italics                 //  Italicized name;
    ->  qty                     //  Boolean: exists {{{qty}}};
    ->  noAbbr                  //  Boolean: if {{{abbr}}} is set off;
    -----------
    # Counters:
    ->  colspan                 //  Number of columns (colspan);
    ---------
    # Return:
    ->  set_list                //  Info to display;
]]
----------
--  Start:
----------
local Values = {};
--  Load outside data and modules:
local data = mw.loadData( 'Module:Set list/data' );
local LN = require( 'Module:Set list/name' );   --  For Localized Name.
 
function Values.list( frame )
    --  Get arguments:
    --  { '1' or 'list', 'region', 'set', 'qty', 'abbr', 'col', 'header', 'rarity' }
    local parameters = frame.args;
 
    --  Load outside data:
    local rg_list       = data.rg_list;
    local region_list   = data.region_list;
    local ln_list       = data.ln_list;
    local language_list = data.language_list;
    local rarity_list   = data.rarity_list;
    --  {{PAGENAME}}
    local PAGENAME = mw.title.getCurrentTitle().text;
    --  {{NAMESPACE}}
    local NAMESPACE = mw.title.getCurrentTitle().nsText;
 
    ----------------------------------------
    --  Define vars related to the set info:
    ----------------------------------------
 
    --  Region and language:
    local _rg = parameters['region'] and parameters['region']:lower();
    local _region = '';
    if _rg == nil or _rg == '' then
        --_region = string.match( PAGENAME, '%-(%a*)' ):lower();  -- a value is needed always!
        return '<strong class="error">Error: A «region» is always needed!</strong>';
    else
        _region = region_list[_rg] or region_list['default'];
    end
    local flag_notEnglish = true;
    local flag_italics = 'italic';
    local _ln = ln_list[_rg];
    local _language = language_list[_ln];
    if _ln == 'en' then
        flag_notEnglish = false;
    end
    if _ln == 'ja' or _ln == 'zh' or _ln == 'ko' then
        flag_italics = 'normal';
    end
 
    --  Set name:
    local _nameSet = parameters['set'];
    if _nameSet == nil or _nameSet == '' then
        local nameSet_parts = mw.text.split( PAGENAME, ' %(' );
        if nameSet_parts[3] == nil or nameSet_parts[3] == '' then
            _nameSet = nameSet_parts[1];  -- Set name doesn't contain dab text.
        else
            _nameSet = nameSet_parts[1] ..' ('.. nameSet_parts[2];  -- Set name contains dab text.
        end
    end
    local _nameSet_localized = LN.localized_name( _nameSet, _language );
 
    --  Set abbreviation:
    local _abbr = parameters['abbr'] and parameters['abbr']:lower();
    local flag_noAbbr = false;
    if _abbr == 'no' or _abbr == 'none' or _abbr == 'off' or _abbr == 'false' or _abbr == '0' then
        flag_noAbbr = true;
    end
 
    -- Quantity (main):
    local _qty = parameters['qty'];  -- $qty-value
    local flag_qty = false;
    if not(_qty == nil or _qty == '') then
        flag_qty = true;
        _qty = tonumber(_qty) or 1;
    end
 
    -----------------------
    --  Design page header:
    -----------------------
    --  Header:
    local header = mw.html.create( 'div' ):css('text-align', 'center')
        :tag('span'):css{
                            ['font-size']   = '120%',
                            ['font-weight'] = 'bold',
                            ['font-style']  = 'italic'
                    }
            :wikitext('[[' .._nameSet.. ']]')
        :done()
        if flag_notEnglish and _nameSet_localized ~= '' and _nameSet_localized then
            header:tag('br'):done()
            :tag('span'):css{
                                ['font-weight'] ='bold',
                                ['font-style']  = flag_italics
                            }
                :wikitext(_nameSet_localized)
            :done()
        end
        header:tag('br'):done()
        :wikitext(_region)
    :allDone()
    --   Category:
    local category = string.format('[['..'Category:%s %s]]', _region, NAMESPACE)
 
    ---------------------------
    --  Process the list array:
    ---------------------------
    --  General:
    local cardList_input  = parameters['list'] or parameters['1'] or parameters[1]; -- @INPUT
    local cardList_array  = mw.text.split( cardList_input, '\n' );
    local extra_column    = parameters['col'];
    local counter_colspan = 4;
        if flag_notEnglish then counter_colspan = 5 end;
        if flag_qty then counter_colspan = counter_colspan + 1 end;
        if flag_noAbbr then counter_colspan = counter_colspan - 1 end;
        if extra_column and extra_column ~= '' then counter_colspan = counter_colspan + 1 end;
 
    --  Build table:
    local _header = parameters['header'];
    local _rarity = parameters['rarity'];
    local out_t   = mw.html.create( 'table' )
        :addClass('wikitable'):addClass('sortable'):addClass('card-list')
        if _header and _header ~= '' then   -- If {{{header}}}.
            out_t:tag('caption'):wikitext(_header):done()
        end
        out_t:tag('tr')
            if not(flag_noAbbr) then
                out_t:tag('th'):attr('scope','col'):wikitext('Card number'):done()
            end
            if flag_notEnglish then
                out_t:tag('th'):attr('scope','col'):wikitext('English name'):done()
                     :tag('th'):attr('scope','col'):wikitext(_language..' name'):done()
            else
                out_t:tag('th'):attr('scope','col'):wikitext('Name'):done()
            end
            out_t:tag('th'):attr('scope','col'):wikitext('Rarity'):done()
                 :tag('th'):attr('scope','col'):wikitext('Category'):done()
            if flag_qty then
                out_t:tag('th'):attr('scope','col'):wikitext('Qty'):done()
            end
            if extra_column and extra_column ~= '' then
                out_t:tag('th'):attr('scope','col'):wikitext(extra_column):done()
            end
        out_t:done() -- Close <tr>
 
    --  Process list results:
    local _headerSection, _raritySection, _qtySection;  -- Section info (don't need to be reset).
    for _, value in ipairs(cardList_array) do
        -- Redefine values:
        local _nameCard, _nameCard_localized, _numberCard, _rarityCard, _typeCard, _qtyCard; -- Standard info.
            local nameCard_strip, nameCard_noDab; -- Aux.
        local _nameCard_alt, _nameCard_localized_alt, _numberCard_alt, _rarityCard_alt, _typeCard_alt, 
              _description, _extra;  -- Alternate info.
        local display_nameCard, display_nameCard_localized, display_numberCard,
              display_rarityCard, display_typeCard, display_qtyCard; -- Display info.
 
        -- Parse:
        if value:match('^%s*!:') then
            -- It's a section; contains section info.
            _headerSection = value:match('header::(.-);')
                          or value:match('header::(.+)' );
            _raritySection = value:match('rarity::(.-);')
                          or value:match('rarity::(.+)' );
            _qtySection    = value:match(   'qty::(.-);')
                          or value:match(   'qty::(.+)' );
 
            if _headerSection then
                out_t:tag('tr')
                    :tag('th'):attr('scope','col'):attr('colspan',counter_colspan):wikitext(_headerSection):done()
                :done()
            end
        elseif value:match('^(%s*)$') then
            -- Nothing: empty line.
        else
            local cardList_array_parts = mw.text.split( value, '//' );
            --[[
                cardList_array_parts[1] has the standard values ($values-standard);
                cardList_array_parts[2] has the alternate values ($values-alternate);
            ]]
            -- Deal with standard values:
            local cardList_array_std = mw.text.split( cardList_array_parts[1], ';' );
            if flag_noAbbr then
                _nameCard   = mw.text.trim( cardList_array_std[1] or '' );
                _rarityCard = mw.text.trim( cardList_array_std[2] or '' );
                _qtyCard    = mw.text.trim( cardList_array_std[3] or '' );
            else
                _numberCard = mw.text.trim( cardList_array_std[1] or '' );
                _nameCard   = mw.text.trim( cardList_array_std[2] or '' );
                _rarityCard = mw.text.trim( cardList_array_std[3] or '' );
                _qtyCard    = mw.text.trim( cardList_array_std[4] or '' );
            end
            --  Polish the card name:
            --  (
                nameCard_strip = mw.ustring.gsub( _nameCard, '#', '' ); -- Remove «#» from card names.
                nameCard_noDab = mw.text.split( _nameCard, ' %(' )[1];  -- Remove dab text.
            --  )
            if flag_notEnglish then 
                _nameCard_localized = LN.localized_name( nameCard_strip, _language );
            end
            _typeCard           = frame:expandTemplate{ title = 'Card type', args = { nameCard_strip } };
 
            -- Deal with alternate values:
            if cardList_array_parts[2] then
                _nameCard_alt           = cardList_array_parts[2]:match('name::(.-);')
                                       or cardList_array_parts[2]:match('name::(.+)' );
                _nameCard_localized_alt = cardList_array_parts[2]:match('name%-local::(.-);')
                                       or cardList_array_parts[2]:match('name%-local::(.+)' );
                _numberCard_alt         = cardList_array_parts[2]:match('number::(.-);')
                                       or cardList_array_parts[2]:match('number::(.+)' );
                _rarityCard_alt         = cardList_array_parts[2]:match('rarity::(.-);')
                                       or cardList_array_parts[2]:match('rarity::(.+)' );
                _typeCard_alt           = cardList_array_parts[2]:match('category::(.-);')
                                       or cardList_array_parts[2]:match('category::(.+)' );
                _description            = cardList_array_parts[2]:match('description::(.-);')
                                       or cardList_array_parts[2]:match('description::(.+)' );
                if extra_column and extra_column ~= '' then
                    _extra              = cardList_array_parts[2]:match(extra_column:lower()..'::(.-);')
                                       or cardList_array_parts[2]:match(extra_column:lower()..'::(.+)' );
                end
            end
            -- Display values:
            -- # Card name:
            if _nameCard_alt then
                display_nameCard = _nameCard_alt;
            elseif _nameCard and _nameCard ~= '' then
                display_nameCard = '"[[' ..nameCard_strip.. '|' ..nameCard_noDab.. ']]"';
            else
                display_nameCard = '';
            end
            -- # Card localized name:
            if _nameCard_localized_alt then
                display_nameCard_localized = _nameCard_localized_alt;
            else
                if _nameCard_localized and _nameCard_localized ~= '' and _ln == "ja" or _ln == "zh" then
                    display_nameCard_localized = '「' .._nameCard_localized.. '」' or '';
                elseif _nameCard_localized and _nameCard_localized ~= '' then
                    display_nameCard_localized = '"' .._nameCard_localized.. '"' or '';
                end
            end
            -- # Card number:
            if not(flag_noAbbr) then
                if _numberCard_alt then
                    display_numberCard = _numberCard_alt;
                else
                    if _numberCard:match('?') or _numberCard == '' then
                        display_numberCard = _numberCard; -- Unknown or empty; don't link.
                    else
                        display_numberCard = '[[' .._numberCard.. ']]'; -- Link.
                    end
                end
            end
            -- # Card rarity:
            if _rarityCard_alt then
                display_rarityCard = _rarityCard_alt;
            else
                local rarity_array;
                if _rarityCard and _rarityCard ~= '' then
                    rarity_array = mw.text.split( _rarityCard, ',' );
                elseif _raritySection and _raritySection ~= '' then
                    rarity_array = mw.text.split( _raritySection, ',' );
                elseif _rarity and _rarity ~= '' then
                    rarity_array = mw.text.split( _rarity, ',' );
                end
                if rarity_array then
                    for _, rarity in ipairs(rarity_array) do
                        display_rarityCard = (display_rarityCard or '').. '[[' ..(rarity_list[mw.ustring.gsub(mw.text.trim(rarity):lower(), ' rare', '')] or rarity_list['default']).. ']]<br />';
                    end
                else
                    display_rarityCard = ''; -- Display blank.
                end
            end
            -- # Card type:
            display_typeCard = _typeCard_alt or _typeCard;
            -- # Card qty:
            if _qtyCard_alt then
                display_qtyCard = _qtyCard_alt;
            elseif _qtyCard and _qtyCard ~= '' then
                display_qtyCard = tonumber(_qtyCard) or 1;
            elseif _qtySection and _qtySection ~= '' then
                display_qtyCard = tonumber(_qtySection) or 1;
            elseif _qty and _qty ~= '' then
               display_qtyCard = _qty;
            end
 
            -- Add to table:
            out_t:tag('tr')
                if not(flag_noAbbr) then
                    out_t:tag('td'):wikitext(display_numberCard):done()
                end
                out_t:tag('td'):wikitext(display_nameCard..' '..(_description or '')):done()
                if flag_notEnglish then
                    out_t:tag('td'):wikitext(display_nameCard_localized):done()
                end
                out_t:tag('td'):wikitext(display_rarityCard):done()
                     :tag('td'):wikitext(display_typeCard):done()
                if flag_qty then
                    out_t:tag('td'):wikitext(display_qtyCard):done()
                end
                if _extra and _extra ~= '' then
                    out_t:tag('td'):wikitext(_extra):done()
                end
            out_t:done()
        end
    end
 
    out_t:allDone() -- Close <table>.
 
    -----------------
    --  Return value:
    -----------------
    local set_list = '';
    if NAMESPACE ~= '' then
        set_list = tostring(header)..category;
    end
    set_list = set_list..tostring(out_t);
 
    --  End
    -------
 
    --  For debug.
    local debug = mw.html.create( 'ul' )
        :tag('li'):tag('code'):wikitext('PAGENAME'):done():wikitext(': '..PAGENAME):done()
        :tag('li'):tag('code'):wikitext('NAMESPACE'):done():wikitext(': '..NAMESPACE):done()
        :tag('li'):tag('code'):wikitext('_rg'):done():wikitext(': '.._rg):done()
        :tag('li'):tag('code'):wikitext('_region'):done():wikitext(': '.._region):done()
        :tag('li'):tag('code'):wikitext('_ln'):done():wikitext(': '.._ln):done()
        :tag('li'):tag('code'):wikitext('_language'):done():wikitext(': '.._language):done()
        :tag('li'):tag('code'):wikitext('_nameSet'):done():wikitext(': '.._nameSet):done()
        :tag('li'):tag('code'):wikitext('_nameSet_localized'):done():wikitext(': '..tostring(_nameSet_localized)):done()
        :tag('li'):tag('code'):wikitext('_qty'):done():wikitext(': '..tostring(_qty)):done()
        :tag('li'):wikitext('<b>Flags</b>'):done()
        :tag('li'):tag('code'):wikitext('flag_notEnglish'):done():wikitext(': '..tostring(flag_notEnglish)):done()
        :tag('li'):tag('code'):wikitext('flag_italics'):done():wikitext(': '..flag_italics):done()
        :tag('li'):tag('code'):wikitext('flag_qty'):done():wikitext(': '..tostring(flag_qty)):done()
        :tag('li'):tag('code'):wikitext('flag_noAbbr'):done():wikitext(': '..tostring(flag_noAbbr)):done()
        :tag('li'):wikitext('<b>List</b>'):done()
        :tag('li'):tag('code'):wikitext('extra_column'):done():wikitext(': '..tostring(extra_column)):done()
        :tag('li'):tag('code'):wikitext('counter_colspan'):done():wikitext(': '..counter_colspan):done()
        :tag('li'):tag('code'):wikitext('cardList_input'):done():wikitext(': '..tostring(cardList_input)):done()
    :done()
 
    return set_list;
end
 
return Values;

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.