Module:Page/Unit

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

local p = {}

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_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

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 profileList = mw.loadData("Module:Data/Unit/Profile") return profileList[iname] or '' 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 output = { '== Leader Skill ==', ' ',   }    local found = false for r=unit.rare,unit.raremax do       rareKey = 'ls'..(r+1) if unit[rareKey] and unit[rareKey] ~= '' then local skillPageKey = pageNameByType(unit[rareKey], 'Skill') local skillName = model.getLoc(skillPageKey, 'name') local skillExpr = model.getLoc(skillPageKey, 'expr') if skillName then found = true table.insert(output, ' '..p.rarityStar(r+1, 20)..' '..skillName..' '..skillExpr..'  ') end end end table.insert(output, ' ') return found and table.concat(output, "\n") or '' 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: ['..linkUtils.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: ['..linkUtils.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.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._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.unitLeaderSkill(unitData)) local jobList = require('Module:Unit/Job')._unitJobList table.insert(output, jobList(unitData)[1]) local evolve = require('Module:Unit/Evolve').unitEvolve table.insert(output, evolve(iname))

local enlightenment = require('Module:Unit/Enlightenment')._unitEnlightenmentList table.insert(output, enlightenment(iname)) local mementoList = require('Module:Unit/MementoList')._relatedMementoList table.insert(output, mementoList(iname))

local gearList = require('Module:Unit/GearList')._relatedGearList table.insert(output, gearList(iname)) table.insert(output, p.unitMasterSkill(unitData)) table.insert(output, p.unitStatBox(unitData)) table.insert(output, p.unitLimitBreak(unitData)) table.insert(output, p.unitQuestSkills(unitData.iname)) local unitSkins = require('Module:Unit/Skins')._unitSkinList table.insert(output, unitSkins(iname))

-- @TODO Check for categorization overrides. local flavor = mw.loadData("Module:Data/Unit/Flavor")[unitData.iname] if mw.title.getCurrentTitle.namespace == 0 then -- Unit table.insert(output, '') -- Gender local gender = enums.sexFromKey(unitData.sex) or 'Unknown' if gender == 'Unknown' then table.insert(output, '') else table.insert(output, '') end -- Summon type 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 -- Element if unitData.elem then table.insert(output, '') end -- Rarity if unitData.rare then table.insert(output, '') end if flavor then -- Origin origin = flavor['COUNTRY'] if origin ~= '─' and origin ~= '?' and origin ~= '―' then table.insert(output, '') elseif origin == '─' or origin == '―' then table.insert(output, '') end -- Blood Type blood = flavor['BLOOD'] if blood ~= '─' and blood ~= '?' and blood ~= '―' then table.insert(output, '') end -- Zodiac zodiac = flavor['ZODIAC'] if zodiac ~= '─' and zodiac ~= '?' and zodiac ~= '―' then table.insert(output, '') end -- Month of Birth birth = flavor['BIRTH'] if birth ~= '─' and birth ~= '?' and birth ~= '???' then table.insert(output, '') else table.insert(output, '') end end -- Jobs table.insert(output, jobList(unitData)[2]) end return table.concat(output, "\n") end

-- temporary change p.renderPage = p._unitPageContent

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

return p