Module:UnitPageContent/sandbox

-- local model  = require('Module:Data').model local render_unit_icon  = require('Module:Render/Unit')._icon local render_job_icon  = require('Module:Render/Job')._icon local render_item_icon  = require('Module:Render/Item')._icon local acdbLink = require('Module:LinkUtils').acdbLink local enums = require('Module:Data/Enums') local memoize = require('Module:Memoize').memoize local cargo = require('Module:CargoUtil') local util_table = require('Module:TableUtil')

local p = {} local h = {}

function h.quote(s) if not s then return end return ('%q'):format(s) end

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

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

function p.unitName(iname) return model.getLoc(pageNameByType(iname, 'Unit'), 'name') end

function p.getElemImg(elem) local elemName = elem and enums.elemFromKey(elem) or nil return (elemName and ()) or  end

function p.getUnitData(iname) return model.query("Unit", '_pageName, iname, ability, ai, atk, birth, 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, mmag, mmnd, mmp, mnd, mp, mspd, notsmn, piece, rare, raremax, recipe1, recipe2, recipe3, recipe4, server, sex, skill, skins, spd, tag, vce, weight', {where = 'iname="'..iname..'" and server = "gl"'})[1] or {} 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.rarityStar(rare, size) local full = '' local empty = ''

return string.rep(full, rare) .. string.rep(empty, 5 - rare) end p.rarityStar = memoize(p.rarityStar)

function p.limitBreakStar(limitBreak) local rare = math.floor(limitBreak / 5)

local full = '' local empty = ''

return string.rep(full, rare) .. string.rep(empty, 5 - rare) end

function p.unitProfile(iname) local profile = mw.loadData("Module:Data/Unit/Profile")[iname] if not profile then return end -- Maybe should include the rest of the profile data? return '== Profile ==\n' .. profile end

function p.unitMaxGateLevel(iname) local gateList = { { maxlevel = 97, name = 'Greed'}, { maxlevel = 95, name = 'Wrath'}, { maxlevel = 93, name = 'Gluttony'}, { maxlevel = 91, name = 'Lust'}, { maxlevel = 89, name = 'Sloth'}, { maxlevel = 87, name = 'Envy'}, }	local baseFileName = 'Module:Data/MasterParam/UnitEnlightenment'

for i, gateData in ipairs(gateList) do		local tempGateData = mw.loadData(baseFileName..'/'..gateData.name) if tempGateData[iname] then return gateData.maxlevel end end return nil end

function p.unitStatBox(unit) local maxLvl = 100 local statList = {'hp', 'mp', 'atk', 'def', 'mag', 'mnd', 'dex', 'spd', 'cri', 'luk'} local levelBrackets = {60, 65, 75, 80, 85} local maxGateLevel = p.unitMaxGateLevel(unit.iname) if maxGateLevel then table.insert(levelBrackets, tonumber(maxGateLevel)) end local output = { '== Stats ==', '{| class="wikitable unitStatBox"', '! Stat !! Min !! Max (at 100) !! Avg per Lv !! '..table.concat(levelBrackets, ' !! '), }	local validStats = 0 for i, stat in pairs(statList) do		local min = unit[stat] or 0 if min ~= 0 then validStats = validStats + 1 local max = unit['m'..stat] or min local perLevel = (max - min) / (maxLvl - 1) table.insert(output, '|-') table.insert(output, '| style="padding: 0" | ') table.insert(output, '| '..min) table.insert(output, '| '..max) table.insert(output, '| '..string.format("%.2f", perLevel)) for j, level in pairs(levelBrackets) do				table.insert(output, '| '..math.floor((level - 1) * perLevel + min)) end end end if (validStats == 0) then return '' end

table.insert(output, '|}') return table.concat(output, "\n") end

function p.unitLeaderSkill(unit)

local skills = {} for r = 1, 6 do local rareKey = 'ls' .. r		local iname = unit[rareKey] skills[r] = iname end if #skills == 0 then return end

for i, row in ipairs(cargo.query{		tables = 'Skill=SK, SkillLoc=SL',		join = 'SK.iname = SL.iname',		fields = {			'SK.iname = iname',			'SL.name = name',			'SL.expr = expr',		},		where = {			'SK.iname IN ('..util_table.concat(skills, ',', h.quote)..')',			'SK.server = "gl"',			'SL.lang = "english"',		},	}) do		skills[row.iname] = row end local output = mw.html.create output:tag('h2'):wikitext('Leader Skill') local dl = output:tag('dl') for rarity = unit.rare+1, unit.raremax+1 do		local skill = skills[skills[rarity]] dl:tag('dt'):wikitext(skill.name, ' (', rarity, '★)') dl:tag('dd'):wikitext(skill.expr) end return tostring(output) end

function p.unitMasterSkill(unit) if not unit.ability then return '' end local _pageName = AbilityFromKey(unit.ability)._pageName if not _pageName then return '' end

local output = {} local abilName = model.getLoc(_pageName, 'name') local abilExpr = model.getLoc(_pageName, 'expr') table.insert(output, '== Master Ability ==') table.insert(output, ';'..(abilName or unit.ability)) table.insert(output, ': '..(abilExpr or '')) local ma_quest = unit.ma_quest if ma_quest then table.insert(output, 'Acquired from Quest: ['..acdbLink(ma_quest) ..' '..ma_quest..']') end return table.concat(output, "\n") end

function p.unitQuestSkills(iname) local skillData = mw.loadData("Module:Data/MasterParam/QuestClearUnlockUnitData")[iname] if not skillData then return '' end mw.logObject(skillData, 'skillData')

local TextInfoFromKey = function(iname) local infoMap = { SK='Skill', AB='Ability', }		local prefix = iname:sub(1, 2) if not infoMap[prefix] then return {} end local item = model.query(infoMap[prefix], '_pageName,iname', {where = 'iname="'..iname..'" and server = "gl"'})[1] or {} if not item._pageName then return {} end return { name = model.getLoc(item._pageName, 'name'), expr = model.getLoc(item._pageName, 'expr'), }	end

local safeLink = require('Module:LinkUtils').wikiSafeName local output = {'== Quest Rewarded Skills =='} local t = {} for i, skill in ipairs(skillData) do		local row = {} local newSkill = TextInfoFromKey(skill.new_id) if newSkill and newSkill.name then table.insert(row, ('=== '.. newSkill.name .. ' ==='))			if newSkill.expr then table.insert(row, (': '.. newSkill.expr)) end -- Handle cases where the skill is an upgrade if skill.add == 0 and skill.old_id and skill.old_id ~= '' then local oldSkill = TextInfoFromKey(skill.old_id) if oldSkill and oldSkill.name then table.insert(row, "\n: Replaces:  " .. oldSkill.name .. " ") if oldSkill.expr then table.insert(row, (':: '..oldSkill.expr)) end end end

for questKey, questName in pairs(skill.qids) do				table.insert(row, ('\nAcquired from Quest: ['..acdbLink(questKey) ..' '..safeLink(questName)..']')) end end t[table.concat(row, '\n')] = 1 end for k, v in pairs(t) do		output[#output+1] = k	end

return table.concat(output, "\n") end

function p.unitJobList(unit) local jobList = {} 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 limit break table.insert(tempJob, p.limitBreakStar(jobSetData.lplus or 0)) -- Insert initial job table.insert(tempJob, render_job_icon({jobData.iname, size='small'})) -- 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, render_job_icon({jobUpgradeData.iname, size='small'}))

-- 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, render_job_icon({jobUpgradeData2.iname, size='small'})) 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 output = { '== Available Jobs ==', '{| class="wikitable" style="background:#f9eee4"', '|- style="background:#f8dbc4"', '!Limit Break', '!Default Job', }

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

if job2ndLevelFound then table.insert(output, '!2nd Level Job Upgrade') jobColCount = jobColCount + 1 end for i, tempJob in ipairs(jobList) do		table.insert(output, '|-') for j=1,jobColCount do			if tempJob[j] then table.insert(output, '|' .. tempJob[j]) else table.insert(output, '| ') end end end table.insert(output, '|}') return table.concat(output, "\n") end

function p.unitLimitBreak(unit) local piece = unit.piece or '' if piece == '' then return '' end local output = {} table.insert(output, '== Limit Break ==') table.insert(output, render_item_icon({piece})) return table.concat(output, "\n") end

function p.unitEvolve(unit) if not unit.rare or unit.rare >= unit.raremax then return '' end

local output = {} local evoFound = false table.insert(output, '== Evolution ==') local render_recipe_table = require('Module:Render/Recipe')._table for r=unit.rare,unit.raremax do		local recipeKey = 'recipe'..(r+1) if unit[recipeKey] then evoFound = true table.insert(output, p.rarityStar(r+2, 32)) table.insert(output, render_recipe_table({unit[recipeKey]})) end end return evoFound and table.concat(output, "\n") or '' end

function p.unitPageContent(frame) local args = require('Module:Arguments').getArgs(frame, {		parentFirst = true,	}) return p._unitPageContent(args[1]) end

function p._unitPageContent(iname) local unitData = p.getUnitData(iname)

if not unitData.iname then return '' end local output = {} table.insert(output, p.unitProfile(unitData.iname)) table.insert(output, p.unitStatBox(unitData)) table.insert(output, p.unitLeaderSkill(unitData)) table.insert(output, p.unitMasterSkill(unitData)) table.insert(output, p.unitQuestSkills(unitData.iname)) table.insert(output, p.unitJobList(unitData)) local mementoList = require('Module:UnitMementoList')._relatedMementoList table.insert(output, mementoList(iname))

local gearList = require('Module:UnitGearList')._relatedGearList table.insert(output, gearList(iname)) table.insert(output, p.unitLimitBreak(unitData)) table.insert(output, p.unitEvolve(unitData))

local enlightenment = require('Module:UnitEnlightenment')._unitEnlightenmentList table.insert(output, enlightenment(iname)) local unitSkins = require('Module:UnitSkins')._unitSkinList table.insert(output, unitSkins(iname)) table.insert(output, '== External links ==') local name = (cargo.query{tables='UnitLoc', fields='name', where='iname="'..unitData.iname..'" AND lang="english"'}[1] or {}).name or "Unit page" table.insert(output, '* ['..acdbLink(unitData.iname)..' '..name..'] on AlchemistCodeDB')

-- @TODO Check for categorization overrides. if mw.title.getCurrentTitle.namespace == 0 then table.insert(output, '') if unitData.elem then table.insert(output, '') end if unitData.birth_id then table.insert(output, '') end local gender = enums.sexFromKey(unitData.sex) or 'Unknown' if gender == 'Unknown' then table.insert(output, '') else table.insert(output, '') end local extra = mw.loadData("Module:Data/Extra/Unit")[unitData.iname] if extra and extra.source then local source = extra.source if source:find("Unreleased") then table.insert(output, '') end if source:find("Free") then table.insert(output, '') if source:find("Arena Coin") then table.insert(output, '') table.insert(output, '') end elseif source:find("Time Limited Summon") then table.insert(output, '') elseif source:find("Limited Summon") then table.insert(output, '') elseif source:find("Summon Coins") then table.insert(output, '') elseif source:find("Summon") then table.insert(output, '') end end end return table.concat(output, "\n") end

function p.test local iname = 'UN_V2_L_UROB' return p._unitPageContent(iname) end

return p