Module:Pet

-- local p = {}

local string = require('Module:String') local table = require('Module:Table') local ucfirst = string.ucfirst local getArgs = require('Module:Arguments').getArgs local data = require('Module:Pet/Data') local aliases = require('Module:Pet/Aliases') local makeStat = require('Module:Statname')._getStatName local makeColor = require('Module:Color')._colorTemplates local makeCoins = require('Module:Currency')._coins local makeAbility = require('Module:Ability')._ability local makeRarity = require('Module:RarityTier')._link local getRarityData = require('Module:RarityTier')._getTier local rarityAliases = require('Module:RarityTier/Aliases') local checkType = require('Module:LibraryUtil').checkType

local curtitle = mw.title.getCurrentTitle local pagename = curtitle.prefixedText local fullpagename = curtitle.fullText

local rarityList = { nums = { common = 1, uncommon = 2, rare = 3, epic = 4, legendary = 5, mythic = 6, } }

function p.petStatsTable(frame) local args = getArgs(frame) local pet = args[1] or args["p"] or args["pet"] or (curtitle.namespace == 0 and (fullpagename:find("Pet") and fullpagename or 'ERROR') or 'ERROR') if pet == 'ERROR' then return string.error('Pet name expected, got none') end return frame:preprocess(string.pcall(p._petStatsTable, pet)) end

function p._petStatsTable(pet) local data = mw.loadData('Module:Pet/Data') local aliases = mw.loadData('Module:Pet/Aliases') pet = pet:lower:gsub(' pet', '') pet = aliases[mw.text.trim(pet)] or 'not found' data = data[pet] --create various variables local rarities = (data.rarities):upper:gsub('[^%a]', '') local stats = data.stats local ab = { amounts = data.abilities.amounts and data.abilities.amounts:gsub('[^%d]', '') or '112234', names = data.abilities.name, descs = data.abilities.desc, bonus_descs = data.abilities.bonus_desc, variables = data.abilities.variables, }   if not stats then return error(string.format('Missing pet stats (Pet: %s)', pet), 2) end local tabs = {} for i = 1, #rarities, 1 do       --================================ -- construct the table --================================       -- headers -       local ved_buttons = ' v • e • d' local wikitable = mw.html.create('table'):addClass('wikitable'):css({['width']='100%'}) wikitable:tag('tr') :tag('th'):wikitext('Base Stats ⓘ '):css({['width']='50%'}):done :tag('th'):wikitext('Level 100 '..ved_buttons):css({['width']='50%'}):done :done local rarity = { letter = rarities:sub(i,i) }       rarity.name = getRarityData(rarity.letter).name rarity.full = rarityAliases[rarity.letter:lower] rarity.num = rarityList.nums[rarity.full] -       -- base stats -       wikitable:tag('tr') :tag('td'):wikitext(table.concat{               p.constructStats(stats, 'base', rarity.letter), --(stats, _type)                p.processAbilities(ab, 'base', rarity.full, ab.amounts:sub(rarity.num, rarity.num), pet) --(ab, type, rarity, amount, pet)            }):done -           -- level 100 stats -           :tag('td'):wikitext(table.concat{                p.constructStats(stats, '100', rarity.letter), --(stats, _type)                p.processAbilities(ab, '100', rarity.full, ab.amounts:sub(rarity.num, rarity.num), pet) --(ab, type, rarity, amount, pet)            }):done :done -       -- bonus stats -       wikitable:tag('tr') :tag('th'):attr({ colspan=2 }):wikitext('Leveling Bonuses '):done :done wikitable:tag('tr') :tag('td'):attr({ colspan=2 }):wikitext(table.concat{               p.constructStats(stats, 'bonus', rarity.letter), --(stats, _type)                p.processAbilities(ab, 'bonus', rarity.full, ab.amounts:sub(rarity.num, rarity.num), pet) --(ab, type, rarity, amount, pet)            }):done tabs[#tabs+1] = table.concat{ '|-|', rarity.name, '=', tostring(makeRarity(rarity.full)), ' stats:', tostring(wikitable) } end return table.concat{' ', table.concat(tabs), ' '} end

function p.constructStats(stats, _type, rarity) checkType('constructStats', 1, stats, 'table') checkType('constructStats', 2, _type, {'string', 'nil'}) rarity = rarityAliases[rarity] or rarity local list = {} for key, value in pairs(stats) do   	if not value.req or value.req == rarity then local num if _type == '100' then num = (value[2] or 0) + 100 * value[1] elseif _type == 'bonus' then num = value[1] else num = (value[2] or 0) end table.push(list, string.wrapHtml({ tostring(makeStat(key)), (num < 0 and ': ' or ': +'), num, (_type == 'bonus' and ' per level' or '') }, 'li')) end end return table.concat{ _type == '100' and 'Stats:' or (           _type == 'bonus'             and 'Bonus stats:'            or 'Base stats:'        ), string.wrapHtml(list, 'ul') } end

function p.processVariables(variable, type, pet) -- check for errors if not variable.color then error(string.format('Variable argument missing (color) (Pet: %s)', pet), 2) end if variable.is_roman then if variable.per_lvl then return makeColor(variable.color, variable.per_lvl) else return makeColor(variable.color, variable.base) end end -- check for errors again if not variable.per_lvl and variable.per_lvl ~= 0 then return error(string.format('Variable argument missing (per_lvl) (Pet: %s)', pet), 2) end --perform calculations local num if type and type == '100' or type == 100 then num = (variable.base or 0) + 100 * variable.per_lvl elseif type and type == 'bonus' then num = variable.per_lvl else num = (variable.base or 0) end if variable.round_to_full then num = math.floor(num) end --add suffix num = tostring(num)..(variable.suffix or '') --colorize num = makeColor(variable.color, num) return num end

function p.processSTAT(str) if str:find('STAT_[%u-]+') then str = str:gsub('STAT_([%u-]+)', function(s) return makeStat(s) end) end if str:find('STATs_([%u-]+)') then str = str:gsub('STATs_([%u-]+)', function(s) return makeStat(s, nil, true) end) end return str end

function p.processRARITY(str) if str:find('RARITY_%u+') then str = str:gsub('RARITY_(%u+)', function(s) return makeRarity(s) end) end return str end

function p.replaceCoins(str) return str:gsub('COINS', tostring(makeCoins)) end

function p.recursiveGsub(val, t)   for i = 1,#t,1 do        val = val:gsub(t[i][1], t[i][2]) end return val end

function p.formatFakeAbility(s) checkType('formatFakeAbility', 1, s, 'string') return s:gsub('%{ABILITY%}(.+)%{/ABILITY%}', function(s) return makeAbility(s) end) end

function p.xmlColorConvert(s) checkType('xmlColorConvert', 1, s, 'string') local colors = { ['green']='green', ['black']='black', ['blue']='blue', ['aqua']='aqua', ['bronze']='bronze', ['orange']='orange', ['darkgray']='darkgray', ['darkgreen']='darkgreen', ['darkpurple']='darkpurple', ['darkred']='darkred', ['gold']='gold', ['red']='red', ['lightpurple']='lightpurple', ['silver']='silver', ['turquoise']='turquoise', ['white']='white', ['yellow']='yellow', ['pink']='pink', }   return s:gsub(        '(.-)',        function(tag, str, endTag)             tag = tag:lower:gsub('_', )            endTag = endTag:lower:gsub('_', )            if tag and (not endTag or endTag == "")                    then error(string.format('XML color tags syntax error: No closing tag found for tag ', tag), 4)                elseif tag:lower ~= endTag:lower                    then error( string.format(                           'XML color tags syntax error: Expected closing tag for \'\' is \'\', got ',                            tag,                            tag,                            endTag                        ), 4)           end            return colors[tag] and makeColor((colors[tag] or color):lower, str) or table.concat{'<', tag, '>', str, ''}        end    ) end

function p.processAbilities(ab, type, rarity, amount, pet) checkType('processAbilities', 1, ab, 'table') checkType('processAbilities', 2, type, 'string') checkType('processAbilities', 3, rarity, 'string') local rarity_num = rarityList.nums[rarity] --if not ab.names[amount] then error(string.format('Missing ability name #%s', amount), 2) end --if not ab.descs[amount] then error(string.format('Missing ability description #%s', amount), 2) end local list = {} for i=1, amount, 1 do       if type == '100' then local desc = ab.descs[i] desc = p.xmlColorConvert(desc) desc = p.processSTAT(desc) desc = p.processRARITY(desc) desc = p.replaceCoins(desc) desc = p.formatFakeAbility(desc) if desc:find('VAR1') then desc = desc:gsub('VAR1', p.processVariables(ab.variables[rarity][i][1], type, pet)) end if desc:find('VAR2') then desc = desc:gsub('VAR2', p.processVariables(ab.variables[rarity][i][2], type, pet)) end if desc:sub(#desc,#desc) ~= '.' then desc = desc..'.' end table.push(list,                    string.wrapHtml({ makeAbility(ab.names[i]), ' - ', desc }, 'li')           ) elseif type == 'bonus' then local desc = ab.bonus_descs[i] or 'no leveling bonus' desc = p.xmlColorConvert(desc) desc = p.processSTAT(desc) desc = p.processRARITY(desc) desc = p.replaceCoins(desc) desc = p.formatFakeAbility(desc) if desc:find('VAR1') then desc = desc:gsub('VAR1', p.processVariables(ab.variables[rarity][i][1], type, pet)) end if desc:find('VAR2') then desc = desc:gsub('VAR2', p.processVariables(ab.variables[rarity][i][2], type, pet)) end if desc:sub(#desc,#desc) ~= '.' then desc = desc..'.' end table.push(list,               string.wrapHtml({ makeAbility(ab.names[i]), ': ', desc }, 'li')           ) else local desc = ab.descs[i] desc = p.xmlColorConvert(desc) desc = p.processSTAT(desc) desc = p.replaceCoins(desc) desc = p.formatFakeAbility(desc) desc = p.processRARITY(desc) if desc:find('VAR1') then desc = desc:gsub('VAR1', p.processVariables(ab.variables[rarity][i][1], type, pet)) end if desc:find('VAR2') then desc = desc:gsub('VAR2', p.processVariables(ab.variables[rarity][i][2], type, pet)) end if desc:find('VAR3') then desc = desc:gsub('VAR3', p.processVariables(ab.variables[rarity][i][3], type, pet)) end if desc:sub(#desc,#desc) ~= '.' then desc = desc..'.' end table.push(list,                string.wrapHtml({ makeAbility(ab.names[i]), ' - ', desc }, 'li')           ) end end return table.concat{ makeAbility('Abilities:'), string.wrapHtml{ list, '', { ["list-style-type"]="circle"; ["line-height"]="inherit"; }       }    } end

return p