Module:Sandbox/MonkeysHK

-- local p = {}

local loader = require('Module:Loader') local ucfirst = string.ucfirst local getArgs = require('Module:Arguments').getArgs local data, aliases = loader.loadData('Pet/Data', 'Pet/Aliases') local string, table = loader.require('String', 'Table') local makeColor = require('Module:Color')._colorTemplates local colorAliases = require('Module:Color/Aliases') local makeCoins = require('Module:Currency')._coins local makeAbility = require('Module:Ability')._ability local rarityTier = require('Module:RarityTier') local statModule = require('Module:Statname') local rarityAliases = require('Module:RarityTier/Aliases') local checkType = require('Module:LibraryUtil').checkType local uitext = require('Module:UIText') local getFormatting = require('Module:UIText').getFormatting

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

local function rarityOrder(rarity) return rarityTier._getTier(rarity).order end

function p._petTooltip(pet) pet = pet:lower:gsub(' pet', '') pet = aliases[mw.text.trim(pet)] or 'not found' data = data[pet] local id = string.ucfirst(pet.." pet") local name = id   local title = "&7[Lvl 100] &6"..string.ucfirst(pet) local text = {} --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, }   local heldItem = data.heldItem if not stats then return error(string.format('Missing pet stats (Pet: %s)', pet), 2) end i = #rarities -- highest rarity local rarity = { letter = rarities:sub(i,i) } rarity.name = rarityTier._getTier(rarity.letter).name rarity.full = rarityAliases[rarity.letter:lower] rarity.num = rarityOrder(rarity.full) text = table.merge(text,		"&8"..(data.petType or "").." Pet",       p.constructTooltipStats(stats, rarity.full), --(stats, rarity)        p.processTooltipAbilities(ab, rarity.full, ab.amounts:sub(rarity.num, rarity.num)), --(ab, rarity, amount)        p.processTooltipHeldItems(heldItem, rarity.full), -- (heldItem, rarity)		{			"&eRight-click to add this pet to/your pet menu!",			"&l"..getFormatting(rarityTier._getTier(rarity.full).color, rarity.name:upper),		}	) text = table.concat(text, "//") return {name = name, title = title, text = text} end

function p.constructTooltipStats(stats, rarity) checkType('constructStats', 1, stats, 'table') rarity = rarityAliases[rarity] or rarity local list = {} for key, value in pairs(stats) do   	reqRarity = value.req and rarityAliases[value.req:lower] or nil if not value.req or rarityOrder(rarity) >= rarityOrder(reqRarity) then local num = (value[2] or 0) + 100 * value[1] table.push(list, ("&f%s: &a%s%s"):format( statModule._getstatdata(key).name, (num < 0 and '' or '+'), num )	       )	    end end return table.concat(list,'/') 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 type == "tooltip" and getFormatting(variable.color, variable.per_lvl) or makeColor(variable.color, variable.per_lvl) else return type == "tooltip" and getFormatting(variable.color, variable.base) or 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 return type == "tooltip" and getFormatting(variable.color, num) or makeColor(variable.color, num) end

function p.processSTAT(str, type) if str:find('STAT_[%u-]+') then str = str:gsub('STAT_([%u-]+)', function(s)			local dt = statModule._getstatdata(s)       	return type == "tooltip" and getFormatting(dt.color, dt.character.." "..dt.name) or statModule._getStatName(s)        end) end if str:find('STATs_([%u-]+)') then str = str:gsub('STATs_([%u-]+)', function(s)			local dt = statModule._getstatdata(s)       	return type == "tooltip" and getFormatting(dt.color, dt.character.." "..dt.nameShort) or statModule._getStatName(s, nil, true)        end) end return str end

function p.processRARITY(str, type) if str:find('RARITY_%u+') then str = str:gsub('RARITY_(%u+)', function(s)			local dt = rarityTier._getTier(s)			return type == "tooltip" and getFormatting(dt.color, dt.name) or rarityTier._link(s)		end) end return str end

function p.replaceCoins(str, type) return str:gsub('COINS', type == "tooltip" and "&6coins" or 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, type) checkType('formatFakeAbility', 1, s, 'string') return s:gsub('%{ABILITY%}(.+)%{/ABILITY%}', function(s)		return type == "tooltip" and "&6"..s or makeAbility(s)	end) end

function p.xmlColorConvert(s, type) checkType('xmlColorConvert', 1, s, 'string') 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            local c = colorAliases[string.trim(tag):lower:gsub('%s',' ')]            return c and (type == "tooltip" and getFormatting(c, str) or makeColor(c, str)) or '<'..tag..'>'..str..''        end    ) end

local function processVars(desc, _type, vars) desc = p.xmlColorConvert(desc, _type) desc = p.processSTAT(desc, _type) desc = p.processRARITY(desc, _type) desc = p.replaceCoins(desc, _type) desc = p.formatFakeAbility(desc, _type) if vars then local MAX = 10 -- process double digits first for j = 10, MAX, 1 do			if desc:find('VAR'..tostring(j)) then desc = desc:gsub('VAR'..tostring(j), p.processVariables(vars[j], _type, pet)) end end -- process single digit for j = 1, 9, 1 do			if desc:find('VAR'..tostring(j)) then desc = desc:gsub('VAR'..tostring(j), p.processVariables(vars[j], _type, pet)) end end end -- fullstop handler if _type ~= "tooltip" then if desc:match("%w$") then desc = desc..'.' end else if desc:match(".$") then desc = desc:sub(1,#desc-1) end end return desc end

function p.processTooltipAbilities(ab, rarity, amount, pet) checkType('processAbilities', 1, ab, 'table') checkType('processAbilities', 2, rarity, 'string') --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 desc = processVars(ab.descs[i], "tooltip", ab.variables[rarity][i]) table.push(list, '&6'..ab.names[i]..'/&f'..desc) end return list end

function p.processTooltipHeldItems(heldItem, rarity) checkType('processAbilities', 1, heldItem, 'table', true) checkType('processAbilities', 2, rarity, 'string') --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 desc if heldItem and rarity == heldItem.req then desc = processVars(heldItem.desc, "tooltip") else return '' end return '&6Held Item: '..heldItem.name..'/'..desc end

return p