Module:Unit/Evolve

local model = require('Module:Data').model local render_item_icon  = require('Module:Render/Item')._icon local memoize = require('Module:Memoize').memoize

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

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.unitEvolve(unit) unit = p.getUnitData(unit) if not unit.rare or unit.rare >= unit.raremax then return '' end local output = { '== Evolution ==', '{| class="wikitable" style="width:calc(100% - 20px); min-width:500px; border:1px solid black; background:e0e0e0"', '|- style="height:35px; font-weight:bold; font-size:16px; text-align:center; background:#d1af59"', '| style="width:100px"|Evolves To', '| Materials', '| style="width:60px"|Cost' } 	local evoFound = false for r=unit.rare,unit.raremax-1 do 		local recipeKey = 'recipe'..(r+1) -- querying the recipe each time the loop comes back local iname = unit[recipeKey] local data = model.query('Recipe','cost,counts,mats',{where = 'iname="'..iname..'"'}) local data = data[1] or {cost='???',counts={},mats={}} local mats = data.mats local counts = data.counts local cost = data.cost if iname then evoFound = true table.insert(output, '|-') table.insert(output, '| style="text-align:center"|'.. p.rarityStar(r+2, 20)) table.insert(output, '| ') for i, mat in ipairs(mats) do 				table.insert(output, render_item_icon({mat, count=counts[i]})) end table.insert(output, ' ') table.insert(output, '| style="text-align:center"|'..cost..' '..'') end end table.insert(output, '|}') return evoFound and table.concat(output, "\n") or '' end

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

return p