Module:Dungeon

--Get Required Modules local loader = require('Module:Loader') local getArgs = require('Module:Arguments').getArgs

local string, table, yesno, lu, color, item = loader.require('String', 'Table', 'Yesno', 'LibraryUtil', 'Color', 'Item')

local addColor = color._color local checkType = lu.checkType

local p = {} local MAXSTARS = 5

local prefixes = { ['Helmet'] = 'h', ['Chestplate'] = 'c', ['Leggings'] = 'l', ['Boots'] = 'b', } local pieces = { 'Helmet', 'Chestplate', 'Leggings', 'Boots', }

local function getCumSum(tb, index) local sum = tb.conversion or 0 for i = index, 1, -1 do		sum = sum + (tb[i] or 0) end return sum end

local function getHoriSum(tb, row) local sum = 0 for i = 1, MAXSTARS, 1 do		local q = tb[i] and tb[i][row] sum = sum + (q or 0) end return sum end

local function needRow(tb, data) return table.some(tb, function(k, v)		return not not v[data]	end) end

--- -- Template:Star -- -- Produces the star icon --- function p.stars(frame) local args = getArgs(frame) local amount = tonumber(args[1]) or 1 local unfilled = args.unfilled return string.pcall(unfilled and p._unfilledstars or p._stars, amount) end

function p._stars(amount) return string._repeat('', amount, ' ') end

function p._unfilledstars(amount) return string.wrapHtml(p._stars(amount),		'span', { style = 'opacity: .2;' }	) end

--- -- Template:Dungeon Ranking -- -- Produces colored dungeon score with link to the Dungeons Score page and a hover info with the dungeon score needed to get that ranking. --- function p.dungeonRanking(frame) local args = getArgs(frame) local val = args[1] local nolink = yesno(args['nolink'] or args['no_link'] or args['nl'], false) return string.pcall(p._dungeonRanking, val, nolink) end

--Module Access Point function p._dungeonRanking(val, nolink) checkType('dungeonsRanking', 1, val, 'string') checkType('dungeonsRanking', 2, nolink, 'boolean', true) local data = { ['D'] = { color = '#fc2803', score = 'between 0 and 99' },		['C'] = { color = '#0373fc', score = 'between 100 and 159' },		['B'] = { color = '#40a40c', score = 'between 160 and 229' },		['A'] = { color = '#b700ff', score = 'between 230 and 269' },		['S'] = { color = '#ffc800', score = 'between 270 and 299' },		['S+'] = { color = '#ffc800', score = 'above 300' }	}	local col = data[val:upper].color or error('Invalid value "'..val..'"', 2) local score = data[val:upper].score val = val:upper val = addColor(col, val) val = string.wrapHtml(val, 'b') val = string.wrapHtml(val, 'abbr', {title = 'Score '..score..' points. Click to go to Dungeon Score page.'}) val = nolink and val or string.wrapLink('Dungeon Score', val) return val end

--- --Template:Essence Crafting -- --Produces a table with all tiers of essence crafting (star upgrading) --- function p.essenceCrafting(frame) local args = getArgs(frame) local type = args['type'] or args['t'] local essence = args['essence'] or args['e'] local allPiecesSameStats = yesno(args['all_pieces_same_stats'], false) -- phased out, consider removing return p._essenceCrafting(type, essence, args, allPiecesSameStats) end

function p._essenceCrafting(type, essence, args, allPiecesSameStats) checkType('essenceCrafting', 1, type, 'string') checkType('essenceCrafting', 2, essence, 'string') checkType('essenceCrafting', 3, args, 'table') essence = mw.text.trim(essence:gsub('[Ee][Ss][Ss][Ee]?[Nn]?[Cc]?[Ee]?', '')) type = type:lower local g_convert = tonumber(args[6]) or tonumber(args['convert']) or tonumber(args['c']) or nil local function forWeapon local costs = { conversion = g_convert, }		for i = 1, 5 do			costs[i] = tonumber(args[i]) end return { costs } end local function forArmor local ret = {} for i, piece in ipairs(pieces) do local costs = { label = piece, conversion = tonumber(args[prefixes[piece] .. '_convert']) or tonumber(args[prefixes[piece] .. '_c']) or g_convert, } for i = 1, 5 do costs[i] = tonumber(args[prefixes[piece] .. i]) or tonumber(args[i]) end table.push(ret, costs) end return ret end -- Make Data local tabledata, horizontalSum if type == 'weapon' or type == 'w' then tabledata, horizontalSum = forWeapon, false elseif type == 'armor' or type == 'a' then tabledata, horizontalSum = forArmor, true else error(string.format('Invalid item type "%s"', type), 2) end -- Add 'Combined' Column if needed if horizontalSum then local tb = { label = 'Combined', conversion = needRow(tabledata, 'conversion') and getHoriSum(tabledata, 'conversion') or nil } for i = 1, MAXSTARS, 1 do			tb[i] = getHoriSum(tabledata, i)		end tabledata[#tabledata + 1] = tb	end -- Construct Table local wikitable = mw.html.create('table'):addClass('wikitable'):addClass('centertext'):css('border', '3px solid gray') wikitable:tag('tr') :tag('th'):attr('colspan', 1 + 2 * #tabledata):wikitext(			('Essence Required \'\'\'%s Essence\'\'\''):format(essence, essence, essence)		):css('white-space: nowrap; padding: 1em 2em !important;'):done :done local row = wikitable:tag('tr') if needRow(tabledata, 'label') then -- Label row exists row:tag('th'):attr('rowspan', 2):wikitext('Action'):done for n = 1, #tabledata, 1 do			row:tag('th'):attr('colspan', 2):wikitext(tabledata[n].label or ''):done end row = wikitable:tag('tr') else row:tag('th'):wikitext('Action'):done end for n = 1, #tabledata, 1 do		row:tag('th'):css({ width = '60px', ['font-size'] = 'small' }):wikitext('Cost'):done row:tag('th'):css({ width = '60px', ['font-size'] = 'small' }):wikitext(string.makeTitle('Cumul.', 'Cumulative')):done end if needRow(tabledata, 'conversion') then -- Conversion row exists row = wikitable:tag('tr') row:tag('td'):wikitext(string.makeTitle('Conversion', 'The cost of the conversion to a dungeon item.')):done for n = 1, #tabledata, 1 do			row:tag('td'):attr('colspan', 2):wikitext(addColor('#ff55ff', tabledata[n].conversion or 0)):done end end for i = 1, MAXSTARS, 1 do		row = wikitable:tag('tr') row:tag('td'):wikitext(('%s %s'):format(p._stars(i), p._unfilledstars(5 - i))):css('white-space', 'nowrap'):done for n = 1, #tabledata, 1 do			local value, cumulativeSum = tabledata[n][i] or 0, getCumSum(tabledata[n], i)			mw.log(value) mw.log(cumulativeSum) mw.log(value == cumulativeSum) if value == cumulativeSum then row:tag('td'):attr('colspan', 2):wikitext(addColor('#ff55ff', string._formatNum(value))):done else row:tag('td'):wikitext(addColor('#ff55ff', string._formatNum(value))):done row:tag('td'):wikitext(addColor('#ff55ff', string._formatNum(cumulativeSum))):done end end end return tostring(wikitable) end

return p