Module:Unit/Job

-- local model  = require('Module:Data').model local linkUtils = require('Module:LinkUtils') local jobPageName = require('Module:Data/Extra/JobPageName')

local p = {}

function p.getUnitData(iname) return model.query("Unit", "_pageName, iname, ability, ai, atk, birth_id, cri, def, dex, elem, height, hero, hp, img, jobsets, ls1, ls2, ls3, ls4, ls5, ls6, luk, ma_lv, ma_quest, ma_rarity, mag, matk, mcri, mdef, mdex, mdl, mhp, mluk, mmnd, mmp, mnd, mp, mspd, notsmn, piece, rare, raremax, recipe1, recipe2, recipe3, recipe4, sex, skill, skins, spd, tag, vce, weight", {where = 'iname="'..iname..'" and server = "gl"'})[1] or {} end

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

function p.jobName(iname) return model.getLoc(pageNameByType(iname, 'Job'), 'name') end

function p.getJobSetData(iname) return model.query("JobSet", "_pageName, cjob, iname, job, ljob1, llv1, lplus, lrare, target_unit, nil", {where = 'iname="'..iname..'" and server = "gl"'})[1] or {} end

function p.getJobSetUpgradeData(targetUnit, targetJobSet, targetJob) return model.query("JobSet", "_pageName, cjob, iname, job, ljob1, llv1, lplus, lrare, target_unit, nil", {where = 'cjob="'..targetJobSet..'" and ljob1 = "'..targetJob..'" and target_unit = "'..targetUnit..'" and server = "gl"'})[1] or {} end

function p.getJobData(iname) return model.query("Job", "_pageName, iname", {where = 'iname="'..iname..'" and server = "gl"'})[1] or {} end

function p.jobIcon(iname) local jobpage = mw.ext.cargo.query('Pages', 'iname', {where = 'type = "Job" AND _pageName = "' .. iname .. '" OR iname = "' .. iname .. '"'})[1] if jobpage then iname = jobpage.iname end local data = model.query('Job','_pageName,server,ac2d,mdl',{where='iname="'..iname..'"'})[1] or {} local icon = 'ItemIcon,IT_UNKNOWN' if data.ac2d and data.ac2d ~= '' then icon = 'JobIconM,'..data.ac2d elseif data.mdl and data.mdl ~= '' then icon = 'JobIconM,'..data.mdl end local name = model.getLoc(data._pageName, 'name') local link = nil if data._pageName ~= nil then link = (mw.ext.cargo.query('Pages','_pageName',{where='type="Job" and iname="'..iname..'"'})[1] or {_pageName = jobPageName[iname] or linkUtils.wikiSafeName(name)})._pageName or nil end imageOutput = ' '..(name and name ~=  and (link and ..name..'') or name) return imageOutput end

function p._unitJobList(unit) local jobList = {} local jobCats = {} local jobFound = false local jobUpgradeFound = false local job2ndLevelFound = false for i, jobSet in pairs(unit.jobsets) do       local jobSetData = p.getJobSetData(jobSet) if jobSetData.job then local jobData = p.getJobData(jobSetData.job) if jobData.iname then local tempJob = {} jobFound = true -- Insert initial job table.insert(tempJob, p.jobIcon(jobData.iname)) table.insert(jobCats, '') -- Find out if there's a job upgrade local jobSetUpgradeData = p.getJobSetUpgradeData(unit.iname, jobSet, jobData.iname) local jobUpgradeData = nil if jobSetUpgradeData.job then jobUpgradeData = p.getJobData(jobSetUpgradeData.job) end if jobUpgradeData and jobUpgradeData.iname then jobUpgradeFound = true -- Insert first level job upgrade table.insert(tempJob, p.jobIcon(jobUpgradeData.iname)) table.insert(jobCats, '')

-- Find out if there's another job upgrade local jobSetUpgradeData2 = p.getJobSetUpgradeData(unit.iname, jobSetUpgradeData.iname, jobUpgradeData.iname)

local jobUpgradeData2 = nil if jobSetUpgradeData2.job then jobUpgradeData2 = p.getJobData(jobSetUpgradeData2.job) end

-- Insert 2nd level job upgrade if jobUpgradeData2 and jobUpgradeData2.iname then job2ndLevelFound = true table.insert(tempJob, p.jobIcon(jobUpgradeData2.iname)) table.insert(jobCats, '') end end table.insert(jobList, tempJob) end end end -- Exit if no valid job was found if jobFound == false then return '' end -- Adjust output based on number of columns local jobColCount = 2 local width = '100%' if jobUpgradeFound then if job2ndLevelFound then width = '33%' else width = '50%' end end local output = { '== Jobs ==', '{| class="wikitable" style="width:calc(100% - 285px); min-width:400px; text-align:center; border:1px solid black"', '|- style="height:30px; border-bottom:1px solid black; background:#ff4d4d;"', -- Aligning the different columns in the div tag '| ',       ' Initial Job ', }

if jobUpgradeFound then table.insert(output, ' Job Change 1 ') jobColCount = jobColCount + 1 end

if job2ndLevelFound then table.insert(output, ' Job Change 2 ') jobColCount = jobColCount + 1 end table.insert(output, ' ') for i, tempJob in ipairs(jobList) do       table.insert(output, '|-') -- aligning the items table.insert(output, '| ') for j=1,jobColCount do           if tempJob[j] then table.insert(output, ' '.. tempJob[j]..' ') end end table.insert(output, ' ') end table.insert(output, '|}') return {table.concat(output, "\n"), table.concat(jobCats, '')} end

function p.test(frame) local args = require("Module:Arguments").getArgs(frame) local unitData = p.getUnitData(args[1]) return p.unitJobList(unitData)[1] end

return p