User:Weslon/sandbox/Module:Essence Crafting Table

— local getArgs = require('Module:Arguments').getArgs local string = require('Module:String') local makeRarity = require('Module:RarityTier')._link local makeColor = require('Module:Color')._colorTemplates local makeStat = require('Module:Statname')._getStatName local coins = require('Module:Currency')._coins local centerText = require('Module:String').centerText local p = {} - -- Template:Essence crafting table -- -- Creates a table that formats the cost to essence-craft items - function p.reforgeStoneTable(frame) local args = getArgs(frame) local rTiers = { 'common', 'uncommon', 'rare', 'epic', 'legendary', 'mythic', }   local rTiersShort = { 'c', 'u', 'r', 'e', 'l', 'm', }   local statsMap = { 'damage', 'bonus_attack_speed', 'intelligence', 'speed', 'health', 'defense', 'strength', 'crit_damage', 'crit_chance', 'sea_creature_chance', 'true_defense', }   local statsMapShort = { 'dmg', 'bas', 'int', 'spd', 'hp', 'def', 'str', 'cd', 'cc', 'scc', 'td', }   local statsListShowPerc = { false, -- damage true, -- bonus_attack_speed false, -- intelligence true, -- speed false, -- health false, -- defense false, -- strength true, -- crit_damage true, -- crit_chance true, -- sea_creature_chance false, -- true_defense }   --Configuration for the size of the table local config = { lenX = #rTiers or 6, lenY = #statsMapShort or 10, }   local infoNeededVals = { ['in']=true, ['infoneeded']=true, ['ineeded']=true, ['infon']=true, ['moreinfoneeded']=true, ['moreinfo']=true, ['info needed']=true, ['more info needed']=true, ['more info']=true, ['info n']=true, }   local confirmVals = { ['confirm']=true, ['c']=true, ['con']=true, ['conf']=true, ['check']=true, ['not confirmed']=true, ['unconfirmed']=true, ['notconfirmed']=true, }   local srcNeededVals = { ['source needed']=true, ['src needed']=true, ['src n']=true, ['sn']=true, ['source n']=true, ['source?']=true, ['souce']=true, ['s?']=true, ['s']=true, }   local blankCellVals = { ['n/a']=true, ['no']=true, ['n']=true, ['none']=true, ['f']=true, ['fal']=true, ['false']=true, ['0']=true, ['-0']=true, ['+0']=true, ['-0%']=true, ['+0%']=true, ['0%']=true, ['']=true, ['unapplicable']=true, }   local function isNormal(arg) if not infoNeededVals[arg:lower] and not blankCellVals[arg:lower] then return true else return false end end local function processOtherArg(arg) if infoNeededVals[arg:lower] then ret = string.infoNeeded elseif blankCellVals[arg:lower] then ret = string.blankCell else ret = arg end return ret end local function extractEndNotice(arg) if arg:match('%s*;%s*') then arg, endNotice = arg:match('(.+)%s*;%s*(.*)') else endNotice = '' end if endNotice ~= '' then if confirmVals[endNotice:lower] then endNotice = '' elseif srcNeededVals[endNotice:lower] then endNotice = '' else endNotice = '' end end return arg, endNotice end local function processCostArgs(i) local argCase1 = args["cost_"..rTiers[i]] local argCase2 = args["cost_"..rTiersShort[i]] local argStorage = argCase1 or argCase2 if args["cost"] then local tmp = mw.text.split(args["cost"], '%s*,%s*') arg = tmp[i] else arg = argStorage or "" end return arg end local function processStatArgs(i) local argShort = args['stats_'..statsMap[i]] local argNormal = args['stats_'..statsMapShort[i]] if argNormal then tmp = argNormal elseif argShort then tmp = argShort else tmp = '' end if tmp ~= '' then ret = (mw.text.split(tmp, '%s*\,%s*')) for j=1, config.lenX, 1 do                       if not ret[j] or ret[j] == '' then ret[j] = string.blankCell end end else ret = {} for j=1, config.lenX ,1 do                   local argCase1 = args[statsMapShort[i]..'_'..rTiers[j]] local argCase2 = args[statsMapShort[i]..'_'..rTiersShort[j]] local argCase3 = args[statsMap[i]..'_'..rTiers[j]] local argCase4 = args[statsMap[i]..'_'..rTiersShort[j]] local argRet = argCase2 or argCase1 or argCase3 or argCase4 ret[j] = argRet if not ret[j] then ret[j] = string.blankCell end end end return ret end local wikitable = mw.html.create('table'):addClass('article-table') local row = wikitable:tag('tr') row:tag('th') :attr({ rowspan=2 }) :wikitext(centerText('Stat Bonus')) :done row:tag('th') :attr({ colspan=config.lenX, }) :wikitext(centerText('Rarity')) :done row:done local row = wikitable:tag('tr') for i=1, config.lenX, 1 do       row:tag('th'):wikitext(centerText(makeRarity(rTiers[i]))):done if i == config.lenX then row:done end end for i=1, config.lenY, 1 do       argsList = processStatArgs(i) local rowIsEmpty = (argsList[1] == string.blankCell            and argsList[2] == string.blankCell             and argsList[3] == string.blankCell             and argsList[4] == string.blankCell             and argsList[5] == string.blankCell             and argsList[6] == string.blankCell) local row = not rowIsEmpty and wikitable:tag('tr'):tag('td'):wikitext(centerText(makeStat(statsMap[i]))):done or '' if not rowIsEmpty then for j=1, config.lenX, 1 do                local arg, endNotice = extractEndNotice(argsList[j]) if isNormal(arg) and arg:match('([^%d%%%-%+]+)') then arg = arg end row:tag('td') :wikitext(centerText( (isNormal(arg) and not arg:match('([^%d%%%-%+]+)')) and makeColor(                           'green',                             arg:gsub('^([%+%-]*)(%d+)(%%?)$', function(prep, d)                                   local pattern = tostring(d) if statsListShowPerc[i] then pattern = pattern..'%' end return (prep == '-' and prep or '+')..pattern end )                       )..endNotice or processOtherArg(arg) )):done end row:done end end local row = wikitable:tag('tr') row:tag('th'):wikitext(table.concat{'Cost to apply (in ', tostring(coins), ')'}):done for i=1, config.lenX, 1 do       local arg, endNotice = extractEndNotice(processCostArgs(i)) row:tag('td'):wikitext(           ((arg and isNormal(arg)) and centerText(                tostring(coins(                    arg,                    nil,                    true                ))..endNotice            ) or processOtherArg(arg) ))   end local specialEffect = args["special effect"] or args["specialeff"] or args["special"] or args["eff"] or args["se"] if specialEffect then local name, desc = specialEffect:match('(.+)%s*[%-;,%:]%s*(.+)') local row = wikitable:tag('tr') row:tag('th'):wikitext('Reforge Special Effect'):done row:tag('td'):attr({ colspan=config.lenX }):wikitext(table.concat{               makeColor('Blue', name),                ': ',                desc,            }):done row:done end wikitable:done return frame:preprocess(tostring(wikitable)) end --Finish Module return p