Module:Page/Job

From The Alchemist Code Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Page/Job/doc

local render_job_icon  = require('Module:Render/Job')._icon
local model   = require('Module:Data').model

local pageNameByType = function(iname, pageType)
	if iname == nil then return nil end
    return 'Data:Game/MasterParam/'..pageType..'/' .. string.gsub(iname, '_', ' ')
end

local p = {}

function p.jobSource(iname)
    local cargo_query = mw.ext.cargo.query
    local job_data = cargo_query('EventObtainableJob', '_pageName', {where = 'job_iname="'..iname..'"'})
    
    return (#job_data > 0 and job_data[1] and job_data[1]['_pageName']) or nil
end

function p.jobInfoBox(job)
    local jobSource = p.jobSource(job.iname)
    if jobSource and jobSource ~= nil and jobSource ~= '' then
        jobSource = "|-\n"..'| colspan="4" style="text-align:center;" | '.."'''Source:''' [["..jobSource.."]]\n"
    else
        jobSource = ''
    end
    
    local output = {
        '{| class="wikitable jobInfoBox" style="float:right; margin-left: 15px;margin-bottom:0;"',
        '| style="padding:0;width:128px;white-space: nowrap" | '..render_job_icon({job.iname, data=job, size='large', name='none'}),
        '! style="padding:12px;font-size:140%;" | '..job.name..'<span style="font-size:65%"><br/>Job</span>',
        '|-',
        '| colspan="2" style="padding:0" |',
        '{| class="wikitable jobInfoBoxInner" style="margin: 0; width: 100%"'..jobSource,
        '|-',
        '| Move',
        '| '..(job.jmov or ' 0'),
        '| Jump',
        '| '..(job.jjmp or ' 0'),
        '|-',
        '| Avoid',
        '| '..(job.avoid or ' 0'),
        '| Initial Jewels',
        '| '..(100 + (job.inimp or 0))..'%',
        '|-',
        '| HP',
        '| '..(job.hp or ' 0'),
        '| Jewels',
        '| '..(job.mp or ' 0'),
        '|-',
        '| PATK',
        '| '..(job.atk or ' 0'),
        '| PDEF',
        '| '..(job.def or ' 0'),
        '|-',
        '| MATK',
        '| '..(job.mag or ' 0'),
        '| MDEF',
        '| '..(job.mnd or ' 0'),
        '|-',
        '| DEX',
        '| '..(job.dex or ' 0'),
        '| AGI',
        '| '..(job.spd or ' 0'),
        '|-',
        '| CRIT',
        '| '..(job.cri or ' 0'),
        '| LUCK',
        '| '..(job.luk or ' 0'),
        '|}',
        '|}',
    }
    
    return table.concat(output, "\n")
end

function p.jobAbilityOutput(job)
    local abilityOutput = {}
    
    if job.fixabl and job.fixabl ~= '' then
        local abilityPageName = pageNameByType(job.fixabl, 'Ability')
        local abilityName = model.getLoc(abilityPageName, 'name')
        local abilityDesc = model.getLoc(abilityPageName, 'expr')

        table.insert(abilityOutput, '=== '..abilityName..' ===')
        table.insert(abilityOutput, abilityDesc)
        table.insert(abilityOutput, require("Module:AbilitySkill")._AbilitySkillList(job.fixabl))
    end
    
    local jobEquipList = mw.loadData("Module:Data/MasterParam/JobEquip")
    
    if jobEquipList[job.iname] then
        for level,equipData in pairs(jobEquipList[job.iname]) do
            if equipData.learn1 and equipData.learn1 ~= '' then
                local abilityPageName = pageNameByType(equipData.learn1, 'Ability')
                local abilityName = model.getLoc(abilityPageName, 'name')
                local abilityDesc = model.getLoc(abilityPageName, 'expr')

                table.insert(abilityOutput, '=== '..abilityName..' ===')
                table.insert(abilityOutput, abilityDesc)

                if level > 2 then
                    table.insert(abilityOutput, '; Learned at Rank '..(level - 1))
                end

                table.insert(abilityOutput, require("Module:AbilitySkill")._AbilitySkillList(equipData.learn1))
            end
        end
    end
    
    return table.concat(abilityOutput, "\n")
end

function p.aiPriorityOutput(ai)
    if ai == nil then return '' end

    local enums   = require('Module:Data/Enums')

    local renderSkillPriority = function(root, skillKeys)
        if skillKeys == nil or #skillKeys < 1 then return end

        root:tag('h3'):wikitext('Skill Priority')
        local ol = root:tag('ol')
    
        for index, key in ipairs(skillKeys) do
            ol:tag('li'):wikitext((enums.SkillCategory[key] or key))
        end

        root:wikitext("\n\n")
    end

    local renderBuffPriority = function(root, buffKeys, selfOnly)
        if buffKeys == nil or #buffKeys < 1 then return end

        root:tag('h3'):wikitext('Buff Priority')
    
        if selfOnly and selfOnly == 1 then
            root:tag('b'):wikitext("Self Buffs only")
            root:wikitext("\n\n")
        end
    
        local ol = root:tag('ol')
    
        for index, key in ipairs(buffKeys) do
            ol:tag('li'):wikitext((enums.statNameFromType(key) or key))
        end

        root:wikitext("\n\n")    
    end

    local renderConditionPriority = function(root, condKeys)
        if condKeys == nil or #condKeys < 1 then return end

        root:tag('h3'):wikitext('Condition Priority')
        local ol = root:tag('ol')
    
        for index, key in ipairs(condKeys) do
            ol:tag('li'):wikitext(enums.enchantTypeNameFromKey(key))
        end
    
        root:wikitext("\n\n")
    end

    local root = mw.html.create()
    
    root:tag('h2'):wikitext('AI Priority')
    
    renderSkillPriority(root, ai.skil_prio)
    renderBuffPriority(root, ai.skil_prio, ai.buff_self)
    renderConditionPriority(root, ai.cond_prio)
    
    return tostring(root)
end


local renderPage = function(iname, frame)
    local job = model.query("Job", "_pageName, iname, ac2d, mdl, type, wepmdl, master, origin, role, ai, artifact, fixabl, atk, atkskl, avoid, cri, def, dex, hp, inimp, jjmp, jmov, luk, mag, mnd, mp, spd", {where = 'iname="'..iname..'"'})
    job = job[1]
    local getLoc = function(param) return model.getLoc(job._pageName, param) end

    local output = {}
    
    job.name = getLoc('name')
    
    -- If the job inherits from another job, output that information
    if job.origin and job.origin ~= '' and job.origin ~= job.iname then
        table.insert(output, job.name..' is a variation of the '..render_job_icon({job.origin, size='small'})..' job.')
    end
    
    -- Output job info box
    table.insert(output, p.jobInfoBox(job))
    
    -- Output job mastery bonus
    if job.master and job.master ~= '' then
        table.insert(output, '== Job Mastery Bonus ==')
        table.insert(output, require("Module:JobMaster")._jobMasterBonusTable(iname))
    end
    
    -- Output attack skill
    if job.atkskl and job.atkskl ~= '' then
        local attackSkillPage = pageNameByType(job.atkskl, 'Skill')
        
        local attackSkillName = model.getLoc(attackSkillPage, 'name')
        local attackSkillDesc = model.getLoc(attackSkillPage, 'expr')

        table.insert(output, '== Attack ==')        
        table.insert(output, ';'..attackSkillName)
        table.insert(output, ': '..attackSkillDesc)
    end
    
    -- Job/Page/Abilities
    table.insert(output, '== Abilities ==')
    table.insert(output, p.jobAbilityOutput(job))
    
    -- Job/Page/JobUnits
    table.insert(output, '== Units With This Job ==')
    table.insert(output, require("Module:JobUnits")._jobUnitList(iname))
    
    -- Job/Page/RelatedJobs
    table.insert(output, '== Related Jobs ==')
    table.insert(output, require("Module:JobRelationship")._relatedJobList(iname))

    -- Job/Page/Equip
    table.insert(output, '== Equipment ==')
    table.insert(output, require("Module:JobEquipmentList")._jobEquipmentList(iname))
    
    -- AI
    if job.ai and job.ai ~= '' then
        local ai = model.query("AI", "iname, buff_prio, cond_prio, skil_prio, buff_self", {where = 'iname="'..job.ai..'"'}) or {}
        if ai and ai[1] then
            table.insert(output, p.aiPriorityOutput(ai[1]))
        end
    end
  
    -- Category
    table.insert(output, '[[Category:Jobs]]')
    
    return table.concat(output, "\n")
end

local renderFuncs = {}
renderFuncs.Icon = render_job_icon

local renderTransclude = function(iname, args)
  local type = mw.text.trim(args[1])
  args[1] = iname
  return (renderFuncs[type] or renderFuncs.Name)(args)
end

p.renderPage = renderPage

function p._main(iname, args)
  if args.isPage then
    return renderPage(iname)
  else
    return renderTransclude(iname, args)
  end
end

return p