Mobius Final Fantasy Wiki
Advertisement

local  p = {}

util = require( 'Module:Utils' )
box = require( 'Module:Infobox' )
style = require( 'Module:Infobox/CSS' )
auto = require( 'Module:AutoAbilities' )
COLS = "2"
skills = {}
local lang = mw.getContentLanguage()
local title = mw.title.getCurrentTitle()
local NAMESPACE = title.namespace
local PAGENAME = title.text

function p.main( frame )
    local pframe = frame:getParent()
    local args = pframe.args
    local config = frame.args

    local autocat = false
    local cats = {}
    if NAMESPACE == 0 or args.demospace == 'main' then
        autocat = true
    end
    local isScroll = mw.ustring.find(PAGENAME, "(scroll)", 1, true) ~= nil
    local isMaterial = mw.ustring.lower(args.type) == "material"

    t = p.parseArgs( args )
    boxes = {}
    -- Remove margin of the table
    style['table'].margin = "0"

    for i, v in ipairs(t) do
        html = {}
        -- Set box colors
        p.color( v.element:lower() )
        html = {
            [1] = {
                ['bbottom'] = false,
                { p.image( v.image ), attr = { colspan = COLS } }
            },
            [2] = {
                { "Job Type", css = style.th },
                { p.formatJobtype(v['type']), css = style.td }
            },
            [3] = {
                { "Element", css = style.th },
                { p.element( v.element ), css = style.td }
            },
            [4] = {
                { "Skillseeds", css = style.th },
                { p.formatSeed( v.seeds ), css = style.td }
            },
            [5] = {
                ['type'] = "section",
                { "Ability", css = style.section, attr = {colspan='2'} }
            },
            [6] = {
                { "Name", css = style.th },
                { p.abilityLink( v.ability ), css = style.td }
            },
            [7] = {
                ['type'] = "group", n = 3, ['bbottom'] = true,
                { "<div style='font-size:15px;'>'''Orbs'''</div>" .. p.orbs( v.orbs ) },
                { "<div style='font-size:15px;'>'''Range'''</div>" .. v.range },
                { "<div style='font-size:15px;'>'''Max Lv.'''</div>" .. v.maxlv },
            },
            [8] = {
                ['type'] = "group", n = 3,
                { "<div style='font-size:15px;'>'''Attack'''</div>" .. v.attack },
                { "<div style='font-size:15px;'>'''Break Power'''</div>" .. v.breakpower },
                { "<div style='font-size:15px;'>'''Crit Chance'''</div>" .. v.crit },
            }
        }

        if ( (v.extraskills ~= "" and v.extraskills ~= nil) or next(skills) ~= nil ) then
            html[#html+1] = {
                ['type'] = "section",
                { "Extra Skills", css = style.section, attr = { colspan = COLS } }
            }
            html[#html+1] = {
                { p.extraSkills( v.extraskills ), attr = { colspan = COLS } }
            }
        end
        if ( v.auto ~= "" and v.auto ~= nil ) then
            html[#html+1] = {
                ['type'] = "section",
                { "Auto-Abilities", css = style.section, attr = { colspan = COLS } }
            }
            html[#html+1] = {
                { p.autoAbilities( v.auto ), attr = { colspan = COLS } }
            }
        end
        if( v.augment ~= "" and v.augment ~= nil ) then
            -- TODO: Utilize the plus (+) symbolizing Fast Learner cards.
            local star, plus = string.match(v.star, '(-?%d+)(%+?)')
            star = tonumber( star ) + 1
            html[#html+1] = {
                ['type'] = "section",
                { "Augment to " .. util.stars( star ), css = style.section, attr = { colspan = COLS } }
            }
            html[#html+1] = {
                { p.augment( v.augment ), attr = { colspan = COLS } }
            }
        end

        boxes[i] = box.build ( util.sortTable(html), style, COLS )
    end
    
    -- the root html node
    local root = mw.html.create()

    -- tabbed infoboxes
    local tabs = mw.html.create( 'div' )
    tabs    :css( 'margin', "10px 10px 20px 20px" )
            :attr( 'class', 'cardtabs' )
            :wikitext( p.tabber( frame ) )
            :allDone()
    root:node(tabs)

    -- description quote
    local info = args.info or args.description or ''
    if info ~= '' then
        local quote_node = mw.html.create()
        local quote = frame:expandTemplate{title="quote", args={info, "In-game description"}}
        quote_node:newline():wikitext(quote)
        root:node(quote_node)
    end

    -- auto category
    if autocat then
        cats[#cats+1] = '[[Category:Cards]]'

        local jobtype = mw.ustring.lower(args['type'] or '')
        local jobtype_cats = {
            warrior = '[[Category:Warrior Cards]]',
            mage = '[[Category:Mage Cards]]',
            meia = '[[Category:Mage Cards]][[Category:Meia Cards]]',
            ranger = '[[Category:Ranger Cards]]',
            sarah = '[[Category:Ranger Cards]][[Category:Sarah Cards]]',
            monk = '[[Category:Monk Cards]]',
            support = '[[Category:Healer Cards]][[Category:Support Cards]]',
            healer = '[[Category:Healer Cards]][[Category:Support Cards]]',
            material = '[[Category:Material Cards]]',
        }
        cats[#cats+1] =  jobtype_cats[jobtype] or ''
        local ele = mw.ustring.lower(args.element or '')
        local ele_cats = {
            none = '[[Category:Non-Elemental]]',
            fire = '[[Category:Fire Element]]',
            water = '[[Category:Water Element]]',
            wind = '[[Category:Wind Element]]',
            earth = '[[Category:Earth Element]]',
            light = '[[Category:Light Element]]',
            dark = '[[Category:Dark Element]]',
            life = '[[Category:Life Element]]',
            prismatic = '[[Category:Prismatic Element]]',
        }
        cats[#cats+1] = ele_cats[ele] or ''
        local abi = args.abi or ''
        if ability ~= '' then
            local abis = util.explode('//', abi)
            cats[#cats+1] = '[[Category:'.. abis[#abis] ..' Cards]]'
        end
        local auto = args.auto or ''
        if auto == '' and not isScroll and not isMaterial then
            cats[#cats+1] = '[[Category:Cards without auto-abilities]]'
        end
        if abi ~= '' then
            local stars = util.explode('//', args.star or '')
            local augments = util.explode('//', args.augment or '')
            for i=2,#stars do
                local s=stars[i] or ''
                local a=augments[i-1] or ''
                if s~="" and a=="" then
                    cats[#cats+1] = '[[Category:Cards missing augment]]'
                    break
                end
            end
        end
        root:wikitext(table.concat(cats,""))
    end

    return tostring( root )
end

function p.tabber( frame )
    local param = ""
    for i, box in ipairs( boxes ) do
        param = param .. t[i].star .. "=" .. box
        if i == #boxes then break end
        param = param .. "|-|"
    end
    return frame:extensionTag( 'tabber', param )
end

--[[ frame.args are named parameters with several values separated
by a // separator. This function creates tables of key, values.
Example : star = "1 // 2 // 3"; job = "Mage"; attack = "10 // 20 // 30"
gives {
        { star = 1, job = "Mage", attack = "10" }
        { star = 2, job = "Mage", attack = "20" }
        { star = 3, job = "Mage", attack = "30" }
    }
]]
function p.parseArgs( args )
    local t = {}
    local n = 1
    for k, v in pairs( args ) do
        params = util.explode( '//', v )
        if #params > n then n = #params end
        for i=1, #params do
            if not t[i] then t[i] = {} end
            t[i][k] = mw.text.trim( params[i] )
        end
    end
    --[[ if a value is lacking, get the previous one
    except for image, extraskills, upgrade mats ]]
    for k, v in pairs( args ) do
        if ( k ~= 'extraskills' and k ~= 'image' and k ~= 'augment' ) then
            for i=1, n do
                if not t[i][k] then t[i][k] = t[i-1][k] end
            end
        end
    end
    return t
end

-- Change box colors based on the element 
function p.color( element )
    if element == "fire" then
        style.th['background-color'] = "#f44336"
        style.section['background-color'] = "#b71c1c"
        style.color.color = "#f44336"
    elseif element == "water" then
        style.th['background-color'] = "#448aff"
        style.section['background-color'] = "#0d47a1"
        style.color.color = "#2196f3"
    elseif element == "earth" then
        style.th['background-color'] = "#795548"
        style.section['background-color'] = "#3e2723"
        style.color.color = "#795548"
    elseif element == "wind" then
        style.th['background-color'] = "#8bc34a"
        style.th.color = "#212121"
        style.section['background-color'] = "#33691e"
        style.color.color = "#8bc34a"
    elseif element == "dark" then
        style.th['background-color'] = "#673ab7"
        style.section['background-color'] = "#311b92"
        style.color.color = "#673ab7"
    elseif element == "light" then
        style.th['background-color'] = "#ffff8d"
        style.th.color = "#212121"
        style.section['background-color'] = "#f57f17"
        style.color.color = "#ffc107"
    elseif element == "life" then
        style.th['background-color'] = "#f48fb1"
        style.th.color = "#212121"
        style.section['background-color'] = "#d81b60"
        style.color.color = "#f06292"
    elseif element == "prismatic" then
        style.th['background-color'] = "#cddc39"
        style.section['background-color'] = "#0d47a1"
    end
end

-- Format image, displays title if no image
function p.image( img )
    if ( img ~= nil and img ~= "" ) then
        return "[[File:" .. img .. "|350px]]"
    else
        local div = mw.html.create( 'div' )
        div    :wikitext( util.cleanTitle( mw.title.getCurrentTitle().text ) )
            :css( { ['font-size'] = "20px", ['background-color'] = "#212121", 
            color = "white", ['text-align'] = "center" } )
            :css( style.td )
        return tostring( div )
    end
end

-- Format skillseeds display
function p.formatSeed( data )
    if not data then data = "" end
    local t = util.explode( ',', data )
    local elmnt = {}
    for k, v in pairs(t) do
        elmnt[k] = util.explode( 'x', mw.text.trim(v) )
    end
    local seeds = ''
    for k, v in pairs(elmnt) do
        seeds = seeds .. p.seed( mw.text.trim( v[1] ) ) .." x " .. mw.text.trim( v[2] ) .. "  "
    end
    return seeds
end

function p.formatJobtype( jobtype )
    local t = lang:lc( jobtype )
    local icon, name
    if t == 'warrior' then
        icon = 'File:Warrior_Icon.png'
        name = 'Warrior'
    elseif t == 'mage' then
        icon = 'File:Mage_Icon.png'
        name = 'Mage'
    elseif t == 'ranger' then
        icon = 'File:Ranger_Icon.png'
        name = 'Ranger'
    elseif t == 'monk' then
        icon = 'File:Monk_Icon.png'
        name = 'Monk'
    elseif t == 'support' or t == 'healer' then
        icon = 'File:All_Icon.png'
        name = 'Support'
    elseif t == 'material' then
        icon = 'File:Material_Icon.png'
        name = 'Material'
    elseif t == 'meia' then
        icon = 'File:Meia_Icon.png'
        name = 'Mage'
    else
        return jobtype
    end
    return '[[' .. icon .. '|link=|x20px]]&nbsp;' .. name
end

function p.seed(elmt)
    return "[[File:Seed_" .. elmt .. ".png|link=|x20px]]"
end

-- Format orbs
function p.orbs( orb )
    local data = util.explode( 'x', orb )
    return "{{Orb|" .. data[1] .. "}} x" .. mw.text.trim( data[2] )
end

-- Format element
function p.element( element )
    local elmt = "{{Orb|" .. element .. "}} "
    elmt = elmt .. util.colorText( util.startMaj( element ), element )
    return elmt
end

-- Add element color to ability name
function p.abilityLink( name )
	local name = name
	if name == nil or name == '' then return '' end
    local span = mw.html.create( 'span' )
    span:css( style.color ):wikitext( name )
    return "[[" .. name .. "|" .. tostring(span) .. "]]"
end

-- Format extra skills display
function p.extraSkills( data )
    local es = mw.html.create( 'div' )
    es:css( style.td )
    if ( data ~= nil and data ~= "" ) then
        for i, v in ipairs( util.explode( ';', data ) ) do
            table.insert( skills, v )
        end
    end
    for i, v in ipairs( skills ) do
        local skill = util.explode( ' ', mw.text.trim(v), 2 )
        local span = mw.html.create( 'span' )
        span:css( { position = "absolute", right = "0", ["padding-left"] = "15px" } )
        span:wikitext( "A.Lv. " .. skill[1] )
        local div = mw.html.create( 'div' )
        local text = skill[2]
        if text:sub( 1, 1 ) == "*" then
            text = text:sub( 2 )
            skills[i] = ""
        end
        div:css( "position", "relative" ):wikitext( "{{link|" .. text .. "}}" ):node( span )
        es:node (div )
    end
    -- Clean skills table by removing empty string
    local n, skillsNew = 1, {}
    for i, v in ipairs( skills ) do
        if v ~= "" then
            skillsNew[n] = v
            n = n + 1
        end
    end
    skills = skillsNew
    return tostring(es)
end

-- Format auto abilities
function p.autoAbilities( data )
    local data = data or ""
    local div = mw.html.create( 'div' )
    local text = ""
    local abilities = util.explode( ',', data )
    for i, v in ipairs( abilities ) do
        text = text .. "<div>" .. auto.formatString( v ) .. "</div>"
    end
    div:css( style.td ):wikitext( text )
    return tostring( div )
end

-- Format upgrade mats
function p.augment( data )
    local data = data or ""
    local div = mw.html.create( 'div' )
    local text = ""
    local mats = util.explode( ',', data )
    for i, v in ipairs( mats ) do
        local mat = util.explode( 'x', v )
        local title = "{{MaterialLink|" .. util.cleanTitle( util.trim(mat[1]) ) .. "}}"
        if mat[2] then
            text = text .. "<div>" .. title .. " x" .. mat[2] .. "</div>"
        else
            text = text .. "<div>" .. title .. "</div>"
        end
    end
    div:css( style.td ):wikitext( text )
    return tostring( div )
end

return p
Advertisement